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 java.io.IOException;
021import java.text.ParseException;
022import java.text.SimpleDateFormat;
023import java.util.Date;
024
025import javax.xml.namespace.QName;
026
027import org.jivesoftware.smack.packet.ExtensionElement;
028import org.jivesoftware.smack.packet.XmlEnvironment;
029import org.jivesoftware.smack.provider.ExtensionElementProvider;
030import org.jivesoftware.smack.xml.XmlPullParser;
031import org.jivesoftware.smack.xml.XmlPullParserException;
032
033import org.jivesoftware.smackx.workgroup.agent.WorkgroupQueue;
034
035import org.jxmpp.JxmppContext;
036
037public class QueueOverview implements ExtensionElement {
038
039    /**
040     * Element name of the stanza extension.
041     */
042    public static String ELEMENT_NAME = "notify-queue";
043
044    /**
045     * Namespace of the stanza extension.
046     */
047    public static String NAMESPACE = "http://jabber.org/protocol/workgroup";
048    public static final QName QNAME = new QName(NAMESPACE, ELEMENT_NAME);
049
050    private static final String DATE_FORMAT = "yyyyMMdd'T'HH:mm:ss";
051    private final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
052
053    private int averageWaitTime;
054    private Date oldestEntry;
055    private int userCount;
056    private WorkgroupQueue.Status status;
057
058    QueueOverview() {
059        this.averageWaitTime = -1;
060        this.oldestEntry = null;
061        this.userCount = -1;
062        this.status = null;
063    }
064
065    void setAverageWaitTime(int averageWaitTime) {
066        this.averageWaitTime = averageWaitTime;
067    }
068
069    public int getAverageWaitTime () {
070        return averageWaitTime;
071    }
072
073    void setOldestEntry(Date oldestEntry) {
074        this.oldestEntry = oldestEntry;
075    }
076
077    public Date getOldestEntry() {
078        return oldestEntry;
079    }
080
081    void setUserCount(int userCount) {
082        this.userCount = userCount;
083    }
084
085    public int getUserCount() {
086        return userCount;
087    }
088
089    public WorkgroupQueue.Status getStatus() {
090        return status;
091    }
092
093    void setStatus(WorkgroupQueue.Status status) {
094        this.status = status;
095    }
096
097    @Override
098    public String getElementName () {
099        return ELEMENT_NAME;
100    }
101
102    @Override
103    public String getNamespace () {
104        return NAMESPACE;
105    }
106
107    @Override
108    public String toXML(XmlEnvironment enclosingEnvironment) {
109        StringBuilder buf = new StringBuilder();
110        buf.append('<').append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">");
111
112        if (userCount != -1) {
113            buf.append("<count>").append(userCount).append("</count>");
114        }
115        if (oldestEntry != null) {
116            buf.append("<oldest>").append(dateFormat.format(oldestEntry)).append("</oldest>");
117        }
118        if (averageWaitTime != -1) {
119            buf.append("<time>").append(averageWaitTime).append("</time>");
120        }
121        if (status != null) {
122            buf.append("<status>").append(status).append("</status>");
123        }
124        buf.append("</").append(ELEMENT_NAME).append('>');
125
126        return buf.toString();
127    }
128
129    public static class Provider extends ExtensionElementProvider<QueueOverview> {
130
131        @Override
132        public QueueOverview parse(XmlPullParser parser,
133                        int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException,
134                        IOException, TextParseException, ParseException {
135            QueueOverview queueOverview = new QueueOverview();
136            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
137
138            XmlPullParser.Event eventType = parser.next();
139            while (eventType != XmlPullParser.Event.END_ELEMENT
140                         || !ELEMENT_NAME.equals(parser.getName())) {
141                if ("count".equals(parser.getName())) {
142                    queueOverview.setUserCount(Integer.parseInt(parser.nextText()));
143                }
144                else if ("time".equals(parser.getName())) {
145                    queueOverview.setAverageWaitTime(Integer.parseInt(parser.nextText()));
146                }
147                else if ("oldest".equals(parser.getName())) {
148                    queueOverview.setOldestEntry(dateFormat.parse(parser.nextText()));
149                }
150                else if ("status".equals(parser.getName())) {
151                    queueOverview.setStatus(WorkgroupQueue.Status.fromString(parser.nextText()));
152                }
153
154                eventType = parser.next();
155
156                if (eventType != XmlPullParser.Event.END_ELEMENT) {
157                    // throw exception
158                }
159            }
160
161            if (eventType != XmlPullParser.Event.END_ELEMENT) {
162                // throw exception
163            }
164
165            return queueOverview;
166        }
167    }
168}