AgentStatus.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.packet;

  18. import java.io.IOException;
  19. import java.text.ParseException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.Date;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.TimeZone;

  27. import org.jivesoftware.smack.packet.ExtensionElement;
  28. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  29. import org.xmlpull.v1.XmlPullParser;
  30. import org.xmlpull.v1.XmlPullParserException;

  31. /**
  32.  * Agent status packet.
  33.  *
  34.  * @author Matt Tucker
  35.  */
  36. public class AgentStatus implements ExtensionElement {

  37.     private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");

  38.     static {
  39.         UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
  40.     }

  41.     /**
  42.      * Element name of the packet extension.
  43.      */
  44.     public static final String ELEMENT_NAME = "agent-status";

  45.     /**
  46.      * Namespace of the packet extension.
  47.      */
  48.     public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";

  49.     private String workgroupJID;
  50.     private List<ChatInfo> currentChats = new ArrayList<ChatInfo>();
  51.     private int maxChats = -1;

  52.     AgentStatus() {
  53.     }

  54.     public String getWorkgroupJID() {
  55.         return workgroupJID;
  56.     }

  57.     /**
  58.      * Returns a collection of ChatInfo where each ChatInfo represents a Chat where this agent
  59.      * is participating.
  60.      *
  61.      * @return a collection of ChatInfo where each ChatInfo represents a Chat where this agent
  62.      *         is participating.
  63.      */
  64.     public List<ChatInfo> getCurrentChats() {
  65.         return Collections.unmodifiableList(currentChats);
  66.     }

  67.     public int getMaxChats() {
  68.         return maxChats;
  69.     }

  70.     public String getElementName() {
  71.         return ELEMENT_NAME;
  72.     }

  73.     public String getNamespace() {
  74.         return NAMESPACE;
  75.     }

  76.     public String toXML() {
  77.         StringBuilder buf = new StringBuilder();

  78.         buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\"");
  79.         if (workgroupJID != null) {
  80.             buf.append(" jid=\"").append(workgroupJID).append("\"");
  81.         }
  82.         buf.append(">");
  83.         if (maxChats != -1) {
  84.             buf.append("<max-chats>").append(maxChats).append("</max-chats>");
  85.         }
  86.         if (!currentChats.isEmpty()) {
  87.             buf.append("<current-chats xmlns= \"http://jivesoftware.com/protocol/workgroup\">");
  88.             for (Iterator<ChatInfo> it = currentChats.iterator(); it.hasNext();) {
  89.                 buf.append(((ChatInfo)it.next()).toXML());
  90.             }
  91.             buf.append("</current-chats>");
  92.         }
  93.         buf.append("</").append(this.getElementName()).append("> ");

  94.         return buf.toString();
  95.     }

  96.     /**
  97.      * Represents information about a Chat where this Agent is participating.
  98.      *
  99.      * @author Gaston Dombiak
  100.      */
  101.     public static class ChatInfo {

  102.         private String sessionID;
  103.         private String userID;
  104.         private Date date;
  105.         private String email;
  106.         private String username;
  107.         private String question;

  108.         public ChatInfo(String sessionID, String userID, Date date, String email, String username, String question) {
  109.             this.sessionID = sessionID;
  110.             this.userID = userID;
  111.             this.date = date;
  112.             this.email = email;
  113.             this.username = username;
  114.             this.question = question;
  115.         }

  116.         /**
  117.          * Returns the sessionID associated to this chat. Each chat will have a unique sessionID
  118.          * that could be used for retrieving the whole transcript of the conversation.
  119.          *
  120.          * @return the sessionID associated to this chat.
  121.          */
  122.         public String getSessionID() {
  123.             return sessionID;
  124.         }

  125.         /**
  126.          * Returns the user unique identification of the user that made the initial request and
  127.          * for which this chat was generated. If the user joined using an anonymous connection
  128.          * then the userID will be the value of the ID attribute of the USER element. Otherwise,
  129.          * the userID will be the bare JID of the user that made the request.
  130.          *
  131.          * @return the user unique identification of the user that made the initial request.
  132.          */
  133.         public String getUserID() {
  134.             return userID;
  135.         }

  136.         /**
  137.          * Returns the date when this agent joined the chat.
  138.          *
  139.          * @return the date when this agent joined the chat.
  140.          */
  141.         public Date getDate() {
  142.             return date;
  143.         }

  144.         /**
  145.          * Returns the email address associated with the user.
  146.          *
  147.          * @return the email address associated with the user.
  148.          */
  149.         public String getEmail() {
  150.             return email;
  151.         }

  152.         /**
  153.          * Returns the username(nickname) associated with the user.
  154.          *
  155.          * @return the username associated with the user.
  156.          */
  157.         public String getUsername() {
  158.             return username;
  159.         }

  160.         /**
  161.          * Returns the question the user asked.
  162.          *
  163.          * @return the question the user asked, if any.
  164.          */
  165.         public String getQuestion() {
  166.             return question;
  167.         }

  168.         public String toXML() {
  169.             StringBuilder buf = new StringBuilder();

  170.             buf.append("<chat ");
  171.             if (sessionID != null) {
  172.                 buf.append(" sessionID=\"").append(sessionID).append("\"");
  173.             }
  174.             if (userID != null) {
  175.                 buf.append(" userID=\"").append(userID).append("\"");
  176.             }
  177.             if (date != null) {
  178.                 buf.append(" startTime=\"").append(UTC_FORMAT.format(date)).append("\"");
  179.             }
  180.             if (email != null) {
  181.                 buf.append(" email=\"").append(email).append("\"");
  182.             }
  183.             if (username != null) {
  184.                 buf.append(" username=\"").append(username).append("\"");
  185.             }
  186.             if (question != null) {
  187.                 buf.append(" question=\"").append(question).append("\"");
  188.             }
  189.             buf.append("/>");

  190.             return buf.toString();
  191.         }
  192.     }

  193.     /**
  194.      * Packet extension provider for AgentStatus packets.
  195.      */
  196.     public static class Provider extends ExtensionElementProvider<AgentStatus> {

  197.         @Override
  198.         public AgentStatus parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  199.             AgentStatus agentStatus = new AgentStatus();

  200.             agentStatus.workgroupJID = parser.getAttributeValue("", "jid");

  201.             boolean done = false;
  202.             while (!done) {
  203.                 int eventType = parser.next();

  204.                 if (eventType == XmlPullParser.START_TAG) {
  205.                     if ("chat".equals(parser.getName())) {
  206.                         agentStatus.currentChats.add(parseChatInfo(parser));
  207.                     }
  208.                     else if ("max-chats".equals(parser.getName())) {
  209.                         agentStatus.maxChats = Integer.parseInt(parser.nextText());
  210.                     }
  211.                 }
  212.                 else if (eventType == XmlPullParser.END_TAG &&
  213.                     ELEMENT_NAME.equals(parser.getName())) {
  214.                     done = true;
  215.                 }
  216.             }
  217.             return agentStatus;
  218.         }

  219.         private ChatInfo parseChatInfo(XmlPullParser parser) {

  220.             String sessionID = parser.getAttributeValue("", "sessionID");
  221.             String userID = parser.getAttributeValue("", "userID");
  222.             Date date = null;
  223.             try {
  224.                 date = UTC_FORMAT.parse(parser.getAttributeValue("", "startTime"));
  225.             }
  226.             catch (ParseException e) {
  227.             }

  228.             String email = parser.getAttributeValue("", "email");
  229.             String username = parser.getAttributeValue("", "username");
  230.             String question = parser.getAttributeValue("", "question");

  231.             return new ChatInfo(sessionID, userID, date, email, username, question);
  232.         }
  233.     }
  234. }