001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.workgroup.packet;
019
020import org.jivesoftware.smackx.workgroup.agent.WorkgroupQueue;
021import org.jivesoftware.smack.packet.PacketExtension;
022import org.jivesoftware.smack.provider.PacketExtensionProvider;
023import org.xmlpull.v1.XmlPullParser;
024
025import java.text.SimpleDateFormat;
026import java.util.Date;
027
028public class QueueOverview implements PacketExtension {
029
030    /**
031     * Element name of the packet extension.
032     */
033    public static String ELEMENT_NAME = "notify-queue";
034
035    /**
036     * Namespace of the packet extension.
037     */
038    public static String NAMESPACE = "http://jabber.org/protocol/workgroup";
039
040    private static final String DATE_FORMAT = "yyyyMMdd'T'HH:mm:ss";
041    private SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
042
043    private int averageWaitTime;
044    private Date oldestEntry;
045    private int userCount;
046    private WorkgroupQueue.Status status;
047
048    QueueOverview() {
049        this.averageWaitTime = -1;
050        this.oldestEntry = null;
051        this.userCount = -1;
052        this.status = null;
053    }
054
055    void setAverageWaitTime(int averageWaitTime) {
056        this.averageWaitTime = averageWaitTime;
057    }
058
059    public int getAverageWaitTime () {
060        return averageWaitTime;
061    }
062
063    void setOldestEntry(Date oldestEntry) {
064        this.oldestEntry = oldestEntry;
065    }
066
067    public Date getOldestEntry() {
068        return oldestEntry;
069    }
070
071    void setUserCount(int userCount) {
072        this.userCount = userCount;
073    }
074
075    public int getUserCount() {
076        return userCount;
077    }
078
079    public WorkgroupQueue.Status getStatus() {
080        return status;
081    }
082
083    void setStatus(WorkgroupQueue.Status status) {
084        this.status = status;
085    }
086
087    public String getElementName () {
088        return ELEMENT_NAME;
089    }
090
091    public String getNamespace () {
092        return NAMESPACE;
093    }
094
095    public String toXML () {
096        StringBuilder buf = new StringBuilder();
097        buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">");
098
099        if (userCount != -1) {
100            buf.append("<count>").append(userCount).append("</count>");
101        }
102        if (oldestEntry != null) {
103            buf.append("<oldest>").append(dateFormat.format(oldestEntry)).append("</oldest>");
104        }
105        if (averageWaitTime != -1) {
106            buf.append("<time>").append(averageWaitTime).append("</time>");
107        }
108        if (status != null) {
109            buf.append("<status>").append(status).append("</status>");
110        }
111        buf.append("</").append(ELEMENT_NAME).append(">");
112
113        return buf.toString();
114    }
115
116    public static class Provider implements PacketExtensionProvider {
117
118        public PacketExtension parseExtension (XmlPullParser parser) throws Exception {
119            int eventType = parser.getEventType();
120            QueueOverview queueOverview = new QueueOverview();            
121            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
122
123            if (eventType != XmlPullParser.START_TAG) {
124                // throw exception
125            }
126
127            eventType = parser.next();
128            while ((eventType != XmlPullParser.END_TAG)
129                         || (!ELEMENT_NAME.equals(parser.getName())))
130            {
131                if ("count".equals(parser.getName())) {
132                    queueOverview.setUserCount(Integer.parseInt(parser.nextText()));
133                }
134                else if ("time".equals(parser.getName())) {
135                    queueOverview.setAverageWaitTime(Integer.parseInt(parser.nextText()));
136                }
137                else if ("oldest".equals(parser.getName())) {
138                    queueOverview.setOldestEntry((dateFormat.parse(parser.nextText())));                    
139                }
140                else if ("status".equals(parser.getName())) {
141                    queueOverview.setStatus(WorkgroupQueue.Status.fromString(parser.nextText()));
142                }
143
144                eventType = parser.next();
145
146                if (eventType != XmlPullParser.END_TAG) {
147                    // throw exception
148                }
149            }
150
151            if (eventType != XmlPullParser.END_TAG) {
152                // throw exception
153            }
154
155            return queueOverview;
156        }
157    }
158}