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