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