AgentChatHistory.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.ext.history;

  18. import org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.provider.IQProvider;
  20. import org.xmlpull.v1.XmlPullParser;
  21. import org.xmlpull.v1.XmlPullParserException;

  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Date;
  26. import java.util.List;

  27. /**
  28.  * IQ provider used to retrieve individual agent information. Each chat session can be mapped
  29.  * to one or more jids and therefore retrievable.
  30.  */
  31. public class AgentChatHistory extends IQ {

  32.     /**
  33.      * Element name of the packet extension.
  34.      */
  35.     public static final String ELEMENT_NAME = "chat-sessions";

  36.     /**
  37.      * Namespace of the packet extension.
  38.      */
  39.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  40.     private String agentJID;
  41.     private int maxSessions;
  42.     private long startDate;

  43.     private List<AgentChatSession> agentChatSessions = new ArrayList<AgentChatSession>();

  44.     public AgentChatHistory(String agentJID, int maxSessions, Date startDate) {
  45.         this();
  46.         this.agentJID = agentJID;
  47.         this.maxSessions = maxSessions;
  48.         this.startDate = startDate.getTime();
  49.     }

  50.     public AgentChatHistory(String agentJID, int maxSessions) {
  51.         this();
  52.         this.agentJID = agentJID;
  53.         this.maxSessions = maxSessions;
  54.         this.startDate = 0;
  55.     }

  56.     public AgentChatHistory() {
  57.         super(ELEMENT_NAME, NAMESPACE);
  58.     }

  59.     public void addChatSession(AgentChatSession chatSession) {
  60.         agentChatSessions.add(chatSession);
  61.     }

  62.     public Collection<AgentChatSession> getAgentChatSessions() {
  63.         return agentChatSessions;
  64.     }

  65.     @Override
  66.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  67.         buf.append(" agentJID=\"" + agentJID + "\"");
  68.         buf.append(" maxSessions=\"" + maxSessions + "\"");
  69.         buf.append(" startDate=\"" + startDate + "\"");
  70.         buf.setEmptyElement();
  71.         return buf;
  72.     }

  73.     /**
  74.      * Packet extension provider for AgentHistory packets.
  75.      */
  76.     public static class InternalProvider extends IQProvider<AgentChatHistory> {

  77.         @Override
  78.         public AgentChatHistory parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  79.             if (parser.getEventType() != XmlPullParser.START_TAG) {
  80.                 throw new IllegalStateException("Parser not in proper position, or bad XML.");
  81.             }

  82.             AgentChatHistory agentChatHistory = new AgentChatHistory();

  83.             boolean done = false;
  84.             while (!done) {
  85.                 int eventType = parser.next();
  86.                 if ((eventType == XmlPullParser.START_TAG) && ("chat-session".equals(parser.getName()))) {
  87.                     agentChatHistory.addChatSession(parseChatSetting(parser));

  88.                 }
  89.                 else if (eventType == XmlPullParser.END_TAG && ELEMENT_NAME.equals(parser.getName())) {
  90.                     done = true;
  91.                 }
  92.             }
  93.             return agentChatHistory;
  94.         }

  95.         private AgentChatSession parseChatSetting(XmlPullParser parser) throws XmlPullParserException, IOException {

  96.             boolean done = false;
  97.             Date date = null;
  98.             long duration = 0;
  99.             String visitorsName = null;
  100.             String visitorsEmail = null;
  101.             String sessionID = null;
  102.             String question = null;

  103.             while (!done) {
  104.                 int eventType = parser.next();
  105.                 if ((eventType == XmlPullParser.START_TAG) && ("date".equals(parser.getName()))) {
  106.                     String dateStr = parser.nextText();
  107.                     long l = Long.valueOf(dateStr).longValue();
  108.                     date = new Date(l);
  109.                 }
  110.                 else if ((eventType == XmlPullParser.START_TAG) && ("duration".equals(parser.getName()))) {
  111.                     duration = Long.valueOf(parser.nextText()).longValue();
  112.                 }
  113.                 else if ((eventType == XmlPullParser.START_TAG) && ("visitorsName".equals(parser.getName()))) {
  114.                     visitorsName = parser.nextText();
  115.                 }
  116.                 else if ((eventType == XmlPullParser.START_TAG) && ("visitorsEmail".equals(parser.getName()))) {
  117.                     visitorsEmail = parser.nextText();
  118.                 }
  119.                 else if ((eventType == XmlPullParser.START_TAG) && ("sessionID".equals(parser.getName()))) {
  120.                     sessionID = parser.nextText();
  121.                 }
  122.                 else if ((eventType == XmlPullParser.START_TAG) && ("question".equals(parser.getName()))) {
  123.                     question = parser.nextText();
  124.                 }
  125.                 else if (eventType == XmlPullParser.END_TAG && "chat-session".equals(parser.getName())) {
  126.                     done = true;
  127.                 }
  128.             }
  129.             return new AgentChatSession(date, duration, visitorsName, visitorsEmail, sessionID, question);
  130.         }
  131.     }
  132. }