Home | History | Annotate | Download | only in packet
      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 package org.jivesoftware.smackx.workgroup.packet;
     20 
     21 import org.jivesoftware.smack.packet.PacketExtension;
     22 import org.jivesoftware.smack.provider.PacketExtensionProvider;
     23 import org.xmlpull.v1.XmlPullParser;
     24 
     25 import java.text.ParseException;
     26 import java.text.SimpleDateFormat;
     27 import java.util.*;
     28 
     29 /**
     30  * Agent status packet.
     31  *
     32  * @author Matt Tucker
     33  */
     34 public class AgentStatus implements PacketExtension {
     35 
     36     private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
     37 
     38     static {
     39         UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
     40     }
     41 
     42     /**
     43      * Element name of the packet extension.
     44      */
     45     public static final String ELEMENT_NAME = "agent-status";
     46 
     47     /**
     48      * Namespace of the packet extension.
     49      */
     50     public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
     51 
     52     private String workgroupJID;
     53     private List<ChatInfo> currentChats = new ArrayList<ChatInfo>();
     54     private int maxChats = -1;
     55 
     56     AgentStatus() {
     57     }
     58 
     59     public String getWorkgroupJID() {
     60         return workgroupJID;
     61     }
     62 
     63     /**
     64      * Returns a collection of ChatInfo where each ChatInfo represents a Chat where this agent
     65      * is participating.
     66      *
     67      * @return a collection of ChatInfo where each ChatInfo represents a Chat where this agent
     68      *         is participating.
     69      */
     70     public List<ChatInfo> getCurrentChats() {
     71         return Collections.unmodifiableList(currentChats);
     72     }
     73 
     74     public int getMaxChats() {
     75         return maxChats;
     76     }
     77 
     78     public String getElementName() {
     79         return ELEMENT_NAME;
     80     }
     81 
     82     public String getNamespace() {
     83         return NAMESPACE;
     84     }
     85 
     86     public String toXML() {
     87         StringBuilder buf = new StringBuilder();
     88 
     89         buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\"");
     90         if (workgroupJID != null) {
     91             buf.append(" jid=\"").append(workgroupJID).append("\"");
     92         }
     93         buf.append(">");
     94         if (maxChats != -1) {
     95             buf.append("<max-chats>").append(maxChats).append("</max-chats>");
     96         }
     97         if (!currentChats.isEmpty()) {
     98             buf.append("<current-chats xmlns= \"http://jivesoftware.com/protocol/workgroup\">");
     99             for (Iterator<ChatInfo> it = currentChats.iterator(); it.hasNext();) {
    100                 buf.append(((ChatInfo)it.next()).toXML());
    101             }
    102             buf.append("</current-chats>");
    103         }
    104         buf.append("</").append(this.getElementName()).append("> ");
    105 
    106         return buf.toString();
    107     }
    108 
    109     /**
    110      * Represents information about a Chat where this Agent is participating.
    111      *
    112      * @author Gaston Dombiak
    113      */
    114     public static class ChatInfo {
    115 
    116         private String sessionID;
    117         private String userID;
    118         private Date date;
    119         private String email;
    120         private String username;
    121         private String question;
    122 
    123         public ChatInfo(String sessionID, String userID, Date date, String email, String username, String question) {
    124             this.sessionID = sessionID;
    125             this.userID = userID;
    126             this.date = date;
    127             this.email = email;
    128             this.username = username;
    129             this.question = question;
    130         }
    131 
    132         /**
    133          * Returns the sessionID associated to this chat. Each chat will have a unique sessionID
    134          * that could be used for retrieving the whole transcript of the conversation.
    135          *
    136          * @return the sessionID associated to this chat.
    137          */
    138         public String getSessionID() {
    139             return sessionID;
    140         }
    141 
    142         /**
    143          * Returns the user unique identification of the user that made the initial request and
    144          * for which this chat was generated. If the user joined using an anonymous connection
    145          * then the userID will be the value of the ID attribute of the USER element. Otherwise,
    146          * the userID will be the bare JID of the user that made the request.
    147          *
    148          * @return the user unique identification of the user that made the initial request.
    149          */
    150         public String getUserID() {
    151             return userID;
    152         }
    153 
    154         /**
    155          * Returns the date when this agent joined the chat.
    156          *
    157          * @return the date when this agent joined the chat.
    158          */
    159         public Date getDate() {
    160             return date;
    161         }
    162 
    163         /**
    164          * Returns the email address associated with the user.
    165          *
    166          * @return the email address associated with the user.
    167          */
    168         public String getEmail() {
    169             return email;
    170         }
    171 
    172         /**
    173          * Returns the username(nickname) associated with the user.
    174          *
    175          * @return the username associated with the user.
    176          */
    177         public String getUsername() {
    178             return username;
    179         }
    180 
    181         /**
    182          * Returns the question the user asked.
    183          *
    184          * @return the question the user asked, if any.
    185          */
    186         public String getQuestion() {
    187             return question;
    188         }
    189 
    190         public String toXML() {
    191             StringBuilder buf = new StringBuilder();
    192 
    193             buf.append("<chat ");
    194             if (sessionID != null) {
    195                 buf.append(" sessionID=\"").append(sessionID).append("\"");
    196             }
    197             if (userID != null) {
    198                 buf.append(" userID=\"").append(userID).append("\"");
    199             }
    200             if (date != null) {
    201                 buf.append(" startTime=\"").append(UTC_FORMAT.format(date)).append("\"");
    202             }
    203             if (email != null) {
    204                 buf.append(" email=\"").append(email).append("\"");
    205             }
    206             if (username != null) {
    207                 buf.append(" username=\"").append(username).append("\"");
    208             }
    209             if (question != null) {
    210                 buf.append(" question=\"").append(question).append("\"");
    211             }
    212             buf.append("/>");
    213 
    214             return buf.toString();
    215         }
    216     }
    217 
    218     /**
    219      * Packet extension provider for AgentStatus packets.
    220      */
    221     public static class Provider implements PacketExtensionProvider {
    222 
    223         public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
    224             AgentStatus agentStatus = new AgentStatus();
    225 
    226             agentStatus.workgroupJID = parser.getAttributeValue("", "jid");
    227 
    228             boolean done = false;
    229             while (!done) {
    230                 int eventType = parser.next();
    231 
    232                 if (eventType == XmlPullParser.START_TAG) {
    233                     if ("chat".equals(parser.getName())) {
    234                         agentStatus.currentChats.add(parseChatInfo(parser));
    235                     }
    236                     else if ("max-chats".equals(parser.getName())) {
    237                         agentStatus.maxChats = Integer.parseInt(parser.nextText());
    238                     }
    239                 }
    240                 else if (eventType == XmlPullParser.END_TAG &&
    241                     ELEMENT_NAME.equals(parser.getName())) {
    242                     done = true;
    243                 }
    244             }
    245             return agentStatus;
    246         }
    247 
    248         private ChatInfo parseChatInfo(XmlPullParser parser) {
    249 
    250             String sessionID = parser.getAttributeValue("", "sessionID");
    251             String userID = parser.getAttributeValue("", "userID");
    252             Date date = null;
    253             try {
    254                 date = UTC_FORMAT.parse(parser.getAttributeValue("", "startTime"));
    255             }
    256             catch (ParseException e) {
    257             }
    258 
    259             String email = parser.getAttributeValue("", "email");
    260             String username = parser.getAttributeValue("", "username");
    261             String question = parser.getAttributeValue("", "question");
    262 
    263             return new ChatInfo(sessionID, userID, date, email, username, question);
    264         }
    265     }
    266 }