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.jxmpp.jid.EntityBareJid;
030import org.xmlpull.v1.XmlPullParser;
031import org.xmlpull.v1.XmlPullParserException;
032
033/**
034 * IQ provider used to retrieve individual agent information. Each chat session can be mapped
035 * to one or more jids and therefore retrievable.
036 */
037public class AgentChatHistory extends IQ {
038
039    /**
040     * Element name of the stanza extension.
041     */
042    public static final String ELEMENT_NAME = "chat-sessions";
043
044    /**
045     * Namespace of the stanza extension.
046     */
047    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
048
049    private EntityBareJid agentJID;
050    private int maxSessions;
051    private long startDate;
052
053    private final List<AgentChatSession> agentChatSessions = new ArrayList<>();
054
055    public AgentChatHistory(EntityBareJid agentJID, int maxSessions, Date startDate) {
056        this();
057        this.agentJID = agentJID;
058        this.maxSessions = maxSessions;
059        this.startDate = startDate.getTime();
060    }
061
062    public AgentChatHistory(EntityBareJid agentJID, int maxSessions) {
063        this();
064        this.agentJID = agentJID;
065        this.maxSessions = maxSessions;
066        this.startDate = 0;
067    }
068
069    public AgentChatHistory() {
070        super(ELEMENT_NAME, NAMESPACE);
071    }
072
073    public void addChatSession(AgentChatSession chatSession) {
074        agentChatSessions.add(chatSession);
075    }
076
077    public Collection<AgentChatSession> getAgentChatSessions() {
078        return agentChatSessions;
079    }
080
081    @Override
082    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
083        buf.append(" agentJID=\"" + agentJID + "\"");
084        buf.append(" maxSessions=\"" + maxSessions + "\"");
085        buf.append(" startDate=\"" + startDate + "\"");
086        buf.setEmptyElement();
087        return buf;
088    }
089
090    /**
091     * Stanza extension provider for AgentHistory packets.
092     */
093    public static class InternalProvider extends IQProvider<AgentChatHistory> {
094
095        @Override
096        public AgentChatHistory parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
097            if (parser.getEventType() != XmlPullParser.START_TAG) {
098                throw new IllegalStateException("Parser not in proper position, or bad XML.");
099            }
100
101            AgentChatHistory agentChatHistory = new AgentChatHistory();
102
103            boolean done = false;
104            while (!done) {
105                int eventType = parser.next();
106                if ((eventType == XmlPullParser.START_TAG) && ("chat-session".equals(parser.getName()))) {
107                    agentChatHistory.addChatSession(parseChatSetting(parser));
108
109                }
110                else if (eventType == XmlPullParser.END_TAG && ELEMENT_NAME.equals(parser.getName())) {
111                    done = true;
112                }
113            }
114            return agentChatHistory;
115        }
116
117        private AgentChatSession parseChatSetting(XmlPullParser parser) throws XmlPullParserException, IOException {
118
119            boolean done = false;
120            Date date = null;
121            long duration = 0;
122            String visitorsName = null;
123            String visitorsEmail = null;
124            String sessionID = null;
125            String question = null;
126
127            while (!done) {
128                int eventType = parser.next();
129                if ((eventType == XmlPullParser.START_TAG) && ("date".equals(parser.getName()))) {
130                    String dateStr = parser.nextText();
131                    long l = Long.valueOf(dateStr).longValue();
132                    date = new Date(l);
133                }
134                else if ((eventType == XmlPullParser.START_TAG) && ("duration".equals(parser.getName()))) {
135                    duration = Long.valueOf(parser.nextText()).longValue();
136                }
137                else if ((eventType == XmlPullParser.START_TAG) && ("visitorsName".equals(parser.getName()))) {
138                    visitorsName = parser.nextText();
139                }
140                else if ((eventType == XmlPullParser.START_TAG) && ("visitorsEmail".equals(parser.getName()))) {
141                    visitorsEmail = parser.nextText();
142                }
143                else if ((eventType == XmlPullParser.START_TAG) && ("sessionID".equals(parser.getName()))) {
144                    sessionID = parser.nextText();
145                }
146                else if ((eventType == XmlPullParser.START_TAG) && ("question".equals(parser.getName()))) {
147                    question = parser.nextText();
148                }
149                else if (eventType == XmlPullParser.END_TAG && "chat-session".equals(parser.getName())) {
150                    done = true;
151                }
152            }
153            return new AgentChatSession(date, duration, visitorsName, visitorsEmail, sessionID, question);
154        }
155    }
156}