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