QueueDetails.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 org.jivesoftware.smackx.workgroup.QueueUser;
  19. import org.jivesoftware.smack.SmackException;
  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  22. import org.xmlpull.v1.XmlPullParser;
  23. import org.xmlpull.v1.XmlPullParserException;

  24. import java.io.IOException;
  25. import java.text.ParseException;
  26. import java.text.SimpleDateFormat;
  27. import java.util.Date;
  28. import java.util.HashSet;
  29. import java.util.Iterator;
  30. import java.util.Set;
  31. import java.util.logging.Logger;

  32. /**
  33.  * Queue details packet extension, which contains details about the users
  34.  * currently in a queue.
  35.  */
  36. public class QueueDetails implements ExtensionElement {
  37.     private static final Logger LOGGER = Logger.getLogger(QueueDetails.class.getName());
  38.    
  39.     /**
  40.      * Element name of the packet extension.
  41.      */
  42.     public static final String ELEMENT_NAME = "notify-queue-details";

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

  47.     private static final String DATE_FORMAT = "yyyyMMdd'T'HH:mm:ss";

  48.     private SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
  49.     /**
  50.      * The list of users in the queue.
  51.      */
  52.     private Set<QueueUser> users;

  53.     /**
  54.      * Creates a new QueueDetails packet
  55.      */
  56.     private QueueDetails() {
  57.         users = new HashSet<QueueUser>();
  58.     }

  59.     /**
  60.      * Returns the number of users currently in the queue that are waiting to
  61.      * be routed to an agent.
  62.      *
  63.      * @return the number of users in the queue.
  64.      */
  65.     public int getUserCount() {
  66.         return users.size();
  67.     }

  68.     /**
  69.      * Returns the set of users in the queue that are waiting to
  70.      * be routed to an agent (as QueueUser objects).
  71.      *
  72.      * @return a Set for the users waiting in a queue.
  73.      */
  74.     public Set<QueueUser> getUsers() {
  75.         synchronized (users) {
  76.             return users;
  77.         }
  78.     }

  79.     /**
  80.      * Adds a user to the packet.
  81.      *
  82.      * @param user the user.
  83.      */
  84.     private void addUser(QueueUser user) {
  85.         synchronized (users) {
  86.             users.add(user);
  87.         }
  88.     }

  89.     public String getElementName() {
  90.         return ELEMENT_NAME;
  91.     }

  92.     public String getNamespace() {
  93.         return NAMESPACE;
  94.     }

  95.     public String toXML() {
  96.         StringBuilder buf = new StringBuilder();
  97.         buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">");

  98.         synchronized (users) {
  99.             for (Iterator<QueueUser> i=users.iterator(); i.hasNext(); ) {
  100.                 QueueUser user = (QueueUser)i.next();
  101.                 int position = user.getQueuePosition();
  102.                 int timeRemaining = user.getEstimatedRemainingTime();
  103.                 Date timestamp = user.getQueueJoinTimestamp();

  104.                 buf.append("<user jid=\"").append(user.getUserID()).append("\">");

  105.                 if (position != -1) {
  106.                     buf.append("<position>").append(position).append("</position>");
  107.                 }

  108.                 if (timeRemaining != -1) {
  109.                     buf.append("<time>").append(timeRemaining).append("</time>");
  110.                 }

  111.                 if (timestamp != null) {
  112.                     buf.append("<join-time>");
  113.                     buf.append(dateFormat.format(timestamp));
  114.                     buf.append("</join-time>");
  115.                 }

  116.                 buf.append("</user>");
  117.             }
  118.         }
  119.         buf.append("</").append(ELEMENT_NAME).append(">");
  120.         return buf.toString();
  121.     }

  122.     /**
  123.      * Provider class for QueueDetails packet extensions.
  124.      */
  125.     public static class Provider extends ExtensionElementProvider<QueueDetails> {

  126.         @Override
  127.         public QueueDetails parse(XmlPullParser parser,
  128.                         int initialDepth) throws XmlPullParserException,
  129.                         IOException, SmackException {
  130.            
  131.             SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
  132.             QueueDetails queueDetails = new QueueDetails();

  133.             int eventType = parser.getEventType();
  134.             while (eventType != XmlPullParser.END_TAG &&
  135.                     "notify-queue-details".equals(parser.getName()))
  136.             {
  137.                 eventType = parser.next();
  138.                 while ((eventType == XmlPullParser.START_TAG) && "user".equals(parser.getName())) {
  139.                     String uid = null;
  140.                     int position = -1;
  141.                     int time = -1;
  142.                     Date joinTime = null;

  143.                     uid = parser.getAttributeValue("", "jid");
  144.                
  145.                     if (uid == null) {
  146.                         // throw exception
  147.                     }

  148.                     eventType = parser.next();
  149.                     while ((eventType != XmlPullParser.END_TAG)
  150.                                 || (! "user".equals(parser.getName())))
  151.                     {                        
  152.                         if ("position".equals(parser.getName())) {
  153.                             position = Integer.parseInt(parser.nextText());
  154.                         }
  155.                         else if ("time".equals(parser.getName())) {
  156.                             time = Integer.parseInt(parser.nextText());
  157.                         }
  158.                         else if ("join-time".equals(parser.getName())) {
  159.                             try {
  160.                                 joinTime = dateFormat.parse(parser.nextText());
  161.                             } catch (ParseException e) {
  162.                                 throw new SmackException(e);
  163.                             }
  164.                         }
  165.                         else if( parser.getName().equals( "waitTime" ) ) {
  166.                             Date wait;
  167.                             try {
  168.                                 wait = dateFormat.parse(parser.nextText());
  169.                             } catch (ParseException e) {
  170.                                 throw new SmackException(e);
  171.                             }
  172.                             LOGGER.fine(wait.toString());
  173.                         }

  174.                         eventType = parser.next();

  175.                         if (eventType != XmlPullParser.END_TAG) {
  176.                             // throw exception
  177.                         }
  178.                     }

  179.                     queueDetails.addUser(new QueueUser(uid, position, time, joinTime));

  180.                     eventType = parser.next();
  181.                 }
  182.             }
  183.             return queueDetails;
  184.         }
  185.     }
  186. }