Home | History | Annotate | Download | only in history
      1 /**
      2  * $Revision$
      3  * $Date$
      4  *
      5  * Copyright 2003-2007 Jive Software.
      6  *
      7  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *     http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  */
     19 
     20 package org.jivesoftware.smackx.workgroup.ext.history;
     21 
     22 import org.jivesoftware.smack.packet.IQ;
     23 import org.jivesoftware.smack.provider.IQProvider;
     24 import org.xmlpull.v1.XmlPullParser;
     25 
     26 import java.util.ArrayList;
     27 import java.util.Collection;
     28 import java.util.Date;
     29 import java.util.List;
     30 
     31 /**
     32  * IQ provider used to retrieve individual agent information. Each chat session can be mapped
     33  * to one or more jids and therefore retrievable.
     34  */
     35 public class AgentChatHistory extends IQ {
     36     private String agentJID;
     37     private int maxSessions;
     38     private long startDate;
     39 
     40     private List<AgentChatSession> agentChatSessions = new ArrayList<AgentChatSession>();
     41 
     42     public AgentChatHistory(String agentJID, int maxSessions, Date startDate) {
     43         this.agentJID = agentJID;
     44         this.maxSessions = maxSessions;
     45         this.startDate = startDate.getTime();
     46     }
     47 
     48     public AgentChatHistory(String agentJID, int maxSessions) {
     49         this.agentJID = agentJID;
     50         this.maxSessions = maxSessions;
     51         this.startDate = 0;
     52     }
     53 
     54     public AgentChatHistory() {
     55     }
     56 
     57     public void addChatSession(AgentChatSession chatSession) {
     58         agentChatSessions.add(chatSession);
     59     }
     60 
     61     public Collection<AgentChatSession> getAgentChatSessions() {
     62         return agentChatSessions;
     63     }
     64 
     65     /**
     66      * Element name of the packet extension.
     67      */
     68     public static final String ELEMENT_NAME = "chat-sessions";
     69 
     70     /**
     71      * Namespace of the packet extension.
     72      */
     73     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
     74 
     75     public String getChildElementXML() {
     76         StringBuilder buf = new StringBuilder();
     77 
     78         buf.append("<").append(ELEMENT_NAME).append(" xmlns=");
     79         buf.append('"');
     80         buf.append(NAMESPACE);
     81         buf.append('"');
     82         buf.append(" agentJID=\"" + agentJID + "\"");
     83         buf.append(" maxSessions=\"" + maxSessions + "\"");
     84         buf.append(" startDate=\"" + startDate + "\"");
     85 
     86         buf.append("></").append(ELEMENT_NAME).append("> ");
     87         return buf.toString();
     88     }
     89 
     90     /**
     91      * Packet extension provider for AgentHistory packets.
     92      */
     93     public static class InternalProvider implements IQProvider {
     94 
     95         public IQ parseIQ(XmlPullParser parser) throws Exception {
     96             if (parser.getEventType() != XmlPullParser.START_TAG) {
     97                 throw new IllegalStateException("Parser not in proper position, or bad XML.");
     98             }
     99 
    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 Exception {
    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 }
    156