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