QueueOverview.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.agent.WorkgroupQueue;
  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. public class QueueOverview implements ExtensionElement {

  29.     /**
  30.      * Element name of the packet extension.
  31.      */
  32.     public static String ELEMENT_NAME = "notify-queue";

  33.     /**
  34.      * Namespace of the packet extension.
  35.      */
  36.     public static String NAMESPACE = "http://jabber.org/protocol/workgroup";

  37.     private static final String DATE_FORMAT = "yyyyMMdd'T'HH:mm:ss";
  38.     private SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

  39.     private int averageWaitTime;
  40.     private Date oldestEntry;
  41.     private int userCount;
  42.     private WorkgroupQueue.Status status;

  43.     QueueOverview() {
  44.         this.averageWaitTime = -1;
  45.         this.oldestEntry = null;
  46.         this.userCount = -1;
  47.         this.status = null;
  48.     }

  49.     void setAverageWaitTime(int averageWaitTime) {
  50.         this.averageWaitTime = averageWaitTime;
  51.     }

  52.     public int getAverageWaitTime () {
  53.         return averageWaitTime;
  54.     }

  55.     void setOldestEntry(Date oldestEntry) {
  56.         this.oldestEntry = oldestEntry;
  57.     }

  58.     public Date getOldestEntry() {
  59.         return oldestEntry;
  60.     }

  61.     void setUserCount(int userCount) {
  62.         this.userCount = userCount;
  63.     }

  64.     public int getUserCount() {
  65.         return userCount;
  66.     }

  67.     public WorkgroupQueue.Status getStatus() {
  68.         return status;
  69.     }

  70.     void setStatus(WorkgroupQueue.Status status) {
  71.         this.status = status;
  72.     }

  73.     public String getElementName () {
  74.         return ELEMENT_NAME;
  75.     }

  76.     public String getNamespace () {
  77.         return NAMESPACE;
  78.     }

  79.     public String toXML () {
  80.         StringBuilder buf = new StringBuilder();
  81.         buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">");

  82.         if (userCount != -1) {
  83.             buf.append("<count>").append(userCount).append("</count>");
  84.         }
  85.         if (oldestEntry != null) {
  86.             buf.append("<oldest>").append(dateFormat.format(oldestEntry)).append("</oldest>");
  87.         }
  88.         if (averageWaitTime != -1) {
  89.             buf.append("<time>").append(averageWaitTime).append("</time>");
  90.         }
  91.         if (status != null) {
  92.             buf.append("<status>").append(status).append("</status>");
  93.         }
  94.         buf.append("</").append(ELEMENT_NAME).append(">");

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

  97.     public static class Provider extends ExtensionElementProvider<QueueOverview> {

  98.         @Override
  99.         public QueueOverview parse(XmlPullParser parser,
  100.                         int initialDepth) throws XmlPullParserException,
  101.                         IOException, SmackException {
  102.             int eventType = parser.getEventType();
  103.             QueueOverview queueOverview = new QueueOverview();            
  104.             SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

  105.             eventType = parser.next();
  106.             while ((eventType != XmlPullParser.END_TAG)
  107.                          || (!ELEMENT_NAME.equals(parser.getName())))
  108.             {
  109.                 if ("count".equals(parser.getName())) {
  110.                     queueOverview.setUserCount(Integer.parseInt(parser.nextText()));
  111.                 }
  112.                 else if ("time".equals(parser.getName())) {
  113.                     queueOverview.setAverageWaitTime(Integer.parseInt(parser.nextText()));
  114.                 }
  115.                 else if ("oldest".equals(parser.getName())) {
  116.                     try {
  117.                         queueOverview.setOldestEntry((dateFormat.parse(parser.nextText())));
  118.                     } catch (ParseException e) {
  119.                         throw new SmackException(e);
  120.                     }
  121.                 }
  122.                 else if ("status".equals(parser.getName())) {
  123.                     queueOverview.setStatus(WorkgroupQueue.Status.fromString(parser.nextText()));
  124.                 }

  125.                 eventType = parser.next();

  126.                 if (eventType != XmlPullParser.END_TAG) {
  127.                     // throw exception
  128.                 }
  129.             }

  130.             if (eventType != XmlPullParser.END_TAG) {
  131.                 // throw exception
  132.             }

  133.             return queueOverview;
  134.         }
  135.     }
  136. }