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