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    public String getElementName () {
092        return ELEMENT_NAME;
093    }
094
095    public String getNamespace () {
096        return NAMESPACE;
097    }
098
099    public String toXML () {
100        StringBuilder buf = new StringBuilder();
101        buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">");
102
103        if (userCount != -1) {
104            buf.append("<count>").append(userCount).append("</count>");
105        }
106        if (oldestEntry != null) {
107            buf.append("<oldest>").append(dateFormat.format(oldestEntry)).append("</oldest>");
108        }
109        if (averageWaitTime != -1) {
110            buf.append("<time>").append(averageWaitTime).append("</time>");
111        }
112        if (status != null) {
113            buf.append("<status>").append(status).append("</status>");
114        }
115        buf.append("</").append(ELEMENT_NAME).append(">");
116
117        return buf.toString();
118    }
119
120    public static class Provider extends ExtensionElementProvider<QueueOverview> {
121
122        @Override
123        public QueueOverview parse(XmlPullParser parser,
124                        int initialDepth) throws XmlPullParserException,
125                        IOException, SmackException {
126            int eventType = parser.getEventType();
127            QueueOverview queueOverview = new QueueOverview();            
128            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
129
130            eventType = parser.next();
131            while ((eventType != XmlPullParser.END_TAG)
132                         || (!ELEMENT_NAME.equals(parser.getName())))
133            {
134                if ("count".equals(parser.getName())) {
135                    queueOverview.setUserCount(Integer.parseInt(parser.nextText()));
136                }
137                else if ("time".equals(parser.getName())) {
138                    queueOverview.setAverageWaitTime(Integer.parseInt(parser.nextText()));
139                }
140                else if ("oldest".equals(parser.getName())) {
141                    try {
142                        queueOverview.setOldestEntry((dateFormat.parse(parser.nextText())));
143                    } catch (ParseException e) {
144                        throw new SmackException(e);
145                    }
146                }
147                else if ("status".equals(parser.getName())) {
148                    queueOverview.setStatus(WorkgroupQueue.Status.fromString(parser.nextText()));
149                }
150
151                eventType = parser.next();
152
153                if (eventType != XmlPullParser.END_TAG) {
154                    // throw exception
155                }
156            }
157
158            if (eventType != XmlPullParser.END_TAG) {
159                // throw exception
160            }
161
162            return queueOverview;
163        }
164    }
165}