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 
     20 package org.jivesoftware.smackx.workgroup.packet;
     21 
     22 import org.jivesoftware.smackx.workgroup.agent.WorkgroupQueue;
     23 import org.jivesoftware.smack.packet.PacketExtension;
     24 import org.jivesoftware.smack.provider.PacketExtensionProvider;
     25 import org.xmlpull.v1.XmlPullParser;
     26 
     27 import java.text.SimpleDateFormat;
     28 import java.util.Date;
     29 
     30 public class QueueOverview implements PacketExtension {
     31 
     32     /**
     33      * Element name of the packet extension.
     34      */
     35     public static String ELEMENT_NAME = "notify-queue";
     36 
     37     /**
     38      * Namespace of the packet extension.
     39      */
     40     public static String NAMESPACE = "http://jabber.org/protocol/workgroup";
     41 
     42     private static final String DATE_FORMAT = "yyyyMMdd'T'HH:mm:ss";
     43     private SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
     44 
     45     private int averageWaitTime;
     46     private Date oldestEntry;
     47     private int userCount;
     48     private WorkgroupQueue.Status status;
     49 
     50     QueueOverview() {
     51         this.averageWaitTime = -1;
     52         this.oldestEntry = null;
     53         this.userCount = -1;
     54         this.status = null;
     55     }
     56 
     57     void setAverageWaitTime(int averageWaitTime) {
     58         this.averageWaitTime = averageWaitTime;
     59     }
     60 
     61     public int getAverageWaitTime () {
     62         return averageWaitTime;
     63     }
     64 
     65     void setOldestEntry(Date oldestEntry) {
     66         this.oldestEntry = oldestEntry;
     67     }
     68 
     69     public Date getOldestEntry() {
     70         return oldestEntry;
     71     }
     72 
     73     void setUserCount(int userCount) {
     74         this.userCount = userCount;
     75     }
     76 
     77     public int getUserCount() {
     78         return userCount;
     79     }
     80 
     81     public WorkgroupQueue.Status getStatus() {
     82         return status;
     83     }
     84 
     85     void setStatus(WorkgroupQueue.Status status) {
     86         this.status = status;
     87     }
     88 
     89     public String getElementName () {
     90         return ELEMENT_NAME;
     91     }
     92 
     93     public String getNamespace () {
     94         return NAMESPACE;
     95     }
     96 
     97     public String toXML () {
     98         StringBuilder buf = new StringBuilder();
     99         buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">");
    100 
    101         if (userCount != -1) {
    102             buf.append("<count>").append(userCount).append("</count>");
    103         }
    104         if (oldestEntry != null) {
    105             buf.append("<oldest>").append(dateFormat.format(oldestEntry)).append("</oldest>");
    106         }
    107         if (averageWaitTime != -1) {
    108             buf.append("<time>").append(averageWaitTime).append("</time>");
    109         }
    110         if (status != null) {
    111             buf.append("<status>").append(status).append("</status>");
    112         }
    113         buf.append("</").append(ELEMENT_NAME).append(">");
    114 
    115         return buf.toString();
    116     }
    117 
    118     public static class Provider implements PacketExtensionProvider {
    119 
    120         public PacketExtension parseExtension (XmlPullParser parser) throws Exception {
    121             int eventType = parser.getEventType();
    122             QueueOverview queueOverview = new QueueOverview();
    123             SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
    124 
    125             if (eventType != XmlPullParser.START_TAG) {
    126                 // throw exception
    127             }
    128 
    129             eventType = parser.next();
    130             while ((eventType != XmlPullParser.END_TAG)
    131                          || (!ELEMENT_NAME.equals(parser.getName())))
    132             {
    133                 if ("count".equals(parser.getName())) {
    134                     queueOverview.setUserCount(Integer.parseInt(parser.nextText()));
    135                 }
    136                 else if ("time".equals(parser.getName())) {
    137                     queueOverview.setAverageWaitTime(Integer.parseInt(parser.nextText()));
    138                 }
    139                 else if ("oldest".equals(parser.getName())) {
    140                     queueOverview.setOldestEntry((dateFormat.parse(parser.nextText())));
    141                 }
    142                 else if ("status".equals(parser.getName())) {
    143                     queueOverview.setStatus(WorkgroupQueue.Status.fromString(parser.nextText()));
    144                 }
    145 
    146                 eventType = parser.next();
    147 
    148                 if (eventType != XmlPullParser.END_TAG) {
    149                     // throw exception
    150                 }
    151             }
    152 
    153             if (eventType != XmlPullParser.END_TAG) {
    154                 // throw exception
    155             }
    156 
    157             return queueOverview;
    158         }
    159     }
    160 }