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