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