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;
021
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.provider.ExtensionElementProvider;
025import org.jivesoftware.smack.xml.XmlPullParser;
026import org.jivesoftware.smack.xml.XmlPullParserException;
027
028/**
029 * An IQ stanza that encapsulates both types of workgroup queue
030 * status notifications -- position updates, and estimated time
031 * left in the queue updates.
032 */
033public class QueueUpdate implements ExtensionElement {
034
035    /**
036     * Element name of the stanza extension.
037     */
038    public static final String ELEMENT_NAME = "queue-status";
039
040    /**
041     * Namespace of the stanza extension.
042     */
043    public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
044
045    private final int position;
046    private final int remainingTime;
047
048    public QueueUpdate(int position, int remainingTime) {
049        this.position = position;
050        this.remainingTime = remainingTime;
051    }
052
053    /**
054     * Returns the user's position in the workgroup queue, or -1 if the
055     * value isn't set on this packet.
056     *
057     * @return the position in the workgroup queue.
058     */
059    public int getPosition() {
060        return this.position;
061    }
062
063    /**
064     * Returns the user's estimated time left in the workgroup queue, or
065     * -1 if the value isn't set on this packet.
066     *
067     * @return the estimated time left in the workgroup queue.
068     */
069    public int getRemaingTime() {
070        return remainingTime;
071    }
072
073    @Override
074    public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
075        StringBuilder buf = new StringBuilder();
076        buf.append("<queue-status xmlns=\"http://jabber.org/protocol/workgroup\">");
077        if (position != -1) {
078            buf.append("<position>").append(position).append("</position>");
079        }
080        if (remainingTime != -1) {
081            buf.append("<time>").append(remainingTime).append("</time>");
082        }
083        buf.append("</queue-status>");
084        return buf.toString();
085    }
086
087    @Override
088    public String getElementName() {
089        return ELEMENT_NAME;
090    }
091
092    @Override
093    public String getNamespace() {
094        return NAMESPACE;
095    }
096
097    public static class Provider extends ExtensionElementProvider<QueueUpdate> {
098
099        @Override
100        public QueueUpdate parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
101                        throws XmlPullParserException, IOException {
102            boolean done = false;
103            int position = -1;
104            int timeRemaining = -1;
105            while (!done) {
106                parser.next();
107                String elementName = parser.getName();
108                if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT && "position".equals(elementName)) {
109                    try {
110                        position = Integer.parseInt(parser.nextText());
111                    }
112                    catch (NumberFormatException nfe) {
113                    }
114                }
115                else if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT && "time".equals(elementName)) {
116                    try {
117                        timeRemaining = Integer.parseInt(parser.nextText());
118                    }
119                    catch (NumberFormatException nfe) {
120                    }
121                }
122                else if (parser.getEventType() == XmlPullParser.Event.END_ELEMENT && "queue-status".equals(elementName)) {
123                    done = true;
124                }
125            }
126            return new QueueUpdate(position, timeRemaining);
127        }
128    }
129}