001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.workgroup.ext.history;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.Date;
024import java.util.List;
025
026import org.jivesoftware.smack.packet.IQ;
027import org.jivesoftware.smack.provider.IQProvider;
028
029import org.xmlpull.v1.XmlPullParser;
030import org.xmlpull.v1.XmlPullParserException;
031
032/**
033 * IQ provider used to retrieve individual agent information. Each chat session can be mapped
034 * to one or more jids and therefore retrievable.
035 */
036public class AgentChatHistory extends IQ {
037
038    /**
039     * Element name of the stanza(/packet) extension.
040     */
041    public static final String ELEMENT_NAME = "chat-sessions";
042
043    /**
044     * Namespace of the stanza(/packet) extension.
045     */
046    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
047
048    private String agentJID;
049    private int maxSessions;
050    private long startDate;
051
052    private final List<AgentChatSession> agentChatSessions = new ArrayList<>();
053
054    public AgentChatHistory(String agentJID, int maxSessions, Date startDate) {
055        this();
056        this.agentJID = agentJID;
057        this.maxSessions = maxSessions;
058        this.startDate = startDate.getTime();
059    }
060
061    public AgentChatHistory(String agentJID, int maxSessions) {
062        this();
063        this.agentJID = agentJID;
064        this.maxSessions = maxSessions;
065        this.startDate = 0;
066    }
067
068    public AgentChatHistory() {
069        super(ELEMENT_NAME, NAMESPACE);
070    }
071
072    public void addChatSession(AgentChatSession chatSession) {
073        agentChatSessions.add(chatSession);
074    }
075
076    public Collection<AgentChatSession> getAgentChatSessions() {
077        return agentChatSessions;
078    }
079
080    @Override
081    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
082        buf.append(" agentJID=\"" + agentJID + "\"");
083        buf.append(" maxSessions=\"" + maxSessions + "\"");
084        buf.append(" startDate=\"" + startDate + "\"");
085        buf.setEmptyElement();
086        return buf;
087    }
088
089    /**
090     * Stanza(/Packet) extension provider for AgentHistory packets.
091     */
092    public static class InternalProvider extends IQProvider<AgentChatHistory> {
093
094        @Override
095        public AgentChatHistory parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
096            if (parser.getEventType() != XmlPullParser.START_TAG) {
097                throw new IllegalStateException("Parser not in proper position, or bad XML.");
098            }
099
100            AgentChatHistory agentChatHistory = new AgentChatHistory();
101
102            boolean done = false;
103            while (!done) {
104                int eventType = parser.next();
105                if ((eventType == XmlPullParser.START_TAG) && ("chat-session".equals(parser.getName()))) {
106                    agentChatHistory.addChatSession(parseChatSetting(parser));
107
108                }
109                else if (eventType == XmlPullParser.END_TAG && ELEMENT_NAME.equals(parser.getName())) {
110                    done = true;
111                }
112            }
113            return agentChatHistory;
114        }
115
116        private AgentChatSession parseChatSetting(XmlPullParser parser) throws XmlPullParserException, IOException {
117
118            boolean done = false;
119            Date date = null;
120            long duration = 0;
121            String visitorsName = null;
122            String visitorsEmail = null;
123            String sessionID = null;
124            String question = null;
125
126            while (!done) {
127                int eventType = parser.next();
128                if ((eventType == XmlPullParser.START_TAG) && ("date".equals(parser.getName()))) {
129                    String dateStr = parser.nextText();
130                    long l = Long.valueOf(dateStr).longValue();
131                    date = new Date(l);
132                }
133                else if ((eventType == XmlPullParser.START_TAG) && ("duration".equals(parser.getName()))) {
134                    duration = Long.valueOf(parser.nextText()).longValue();
135                }
136                else if ((eventType == XmlPullParser.START_TAG) && ("visitorsName".equals(parser.getName()))) {
137                    visitorsName = parser.nextText();
138                }
139                else if ((eventType == XmlPullParser.START_TAG) && ("visitorsEmail".equals(parser.getName()))) {
140                    visitorsEmail = parser.nextText();
141                }
142                else if ((eventType == XmlPullParser.START_TAG) && ("sessionID".equals(parser.getName()))) {
143                    sessionID = parser.nextText();
144                }
145                else if ((eventType == XmlPullParser.START_TAG) && ("question".equals(parser.getName()))) {
146                    question = parser.nextText();
147                }
148                else if (eventType == XmlPullParser.END_TAG && "chat-session".equals(parser.getName())) {
149                    done = true;
150                }
151            }
152            return new AgentChatSession(date, duration, visitorsName, visitorsEmail, sessionID, question);
153        }
154    }
155}