blob: ef11a789d474c986df555e2622f62aed02c0f5c3 [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
2 * $Revision$
3 * $Date$
4 *
5 * Copyright 2003-2007 Jive Software.
6 *
7 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20package org.jivesoftware.smackx.workgroup.packet;
21
22import org.jivesoftware.smackx.workgroup.QueueUser;
23import org.jivesoftware.smack.packet.PacketExtension;
24import org.jivesoftware.smack.provider.PacketExtensionProvider;
25import org.xmlpull.v1.XmlPullParser;
26
27import java.text.SimpleDateFormat;
28import java.util.Date;
29import java.util.HashSet;
30import java.util.Iterator;
31import java.util.Set;
32
33/**
34 * Queue details packet extension, which contains details about the users
35 * currently in a queue.
36 */
37public class QueueDetails implements PacketExtension {
38
39 /**
40 * Element name of the packet extension.
41 */
42 public static final String ELEMENT_NAME = "notify-queue-details";
43
44 /**
45 * Namespace of the packet extension.
46 */
47 public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
48
49 private static final String DATE_FORMAT = "yyyyMMdd'T'HH:mm:ss";
50
51 private SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
52 /**
53 * The list of users in the queue.
54 */
55 private Set<QueueUser> users;
56
57 /**
58 * Creates a new QueueDetails packet
59 */
60 private QueueDetails() {
61 users = new HashSet<QueueUser>();
62 }
63
64 /**
65 * Returns the number of users currently in the queue that are waiting to
66 * be routed to an agent.
67 *
68 * @return the number of users in the queue.
69 */
70 public int getUserCount() {
71 return users.size();
72 }
73
74 /**
75 * Returns the set of users in the queue that are waiting to
76 * be routed to an agent (as QueueUser objects).
77 *
78 * @return a Set for the users waiting in a queue.
79 */
80 public Set<QueueUser> getUsers() {
81 synchronized (users) {
82 return users;
83 }
84 }
85
86 /**
87 * Adds a user to the packet.
88 *
89 * @param user the user.
90 */
91 private void addUser(QueueUser user) {
92 synchronized (users) {
93 users.add(user);
94 }
95 }
96
97 public String getElementName() {
98 return ELEMENT_NAME;
99 }
100
101 public String getNamespace() {
102 return NAMESPACE;
103 }
104
105 public String toXML() {
106 StringBuilder buf = new StringBuilder();
107 buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">");
108
109 synchronized (users) {
110 for (Iterator<QueueUser> i=users.iterator(); i.hasNext(); ) {
111 QueueUser user = (QueueUser)i.next();
112 int position = user.getQueuePosition();
113 int timeRemaining = user.getEstimatedRemainingTime();
114 Date timestamp = user.getQueueJoinTimestamp();
115
116 buf.append("<user jid=\"").append(user.getUserID()).append("\">");
117
118 if (position != -1) {
119 buf.append("<position>").append(position).append("</position>");
120 }
121
122 if (timeRemaining != -1) {
123 buf.append("<time>").append(timeRemaining).append("</time>");
124 }
125
126 if (timestamp != null) {
127 buf.append("<join-time>");
128 buf.append(dateFormat.format(timestamp));
129 buf.append("</join-time>");
130 }
131
132 buf.append("</user>");
133 }
134 }
135 buf.append("</").append(ELEMENT_NAME).append(">");
136 return buf.toString();
137 }
138
139 /**
140 * Provider class for QueueDetails packet extensions.
141 */
142 public static class Provider implements PacketExtensionProvider {
143
144 public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
145
146 SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
147 QueueDetails queueDetails = new QueueDetails();
148
149 int eventType = parser.getEventType();
150 while (eventType != XmlPullParser.END_TAG &&
151 "notify-queue-details".equals(parser.getName()))
152 {
153 eventType = parser.next();
154 while ((eventType == XmlPullParser.START_TAG) && "user".equals(parser.getName())) {
155 String uid = null;
156 int position = -1;
157 int time = -1;
158 Date joinTime = null;
159
160 uid = parser.getAttributeValue("", "jid");
161
162 if (uid == null) {
163 // throw exception
164 }
165
166 eventType = parser.next();
167 while ((eventType != XmlPullParser.END_TAG)
168 || (! "user".equals(parser.getName())))
169 {
170 if ("position".equals(parser.getName())) {
171 position = Integer.parseInt(parser.nextText());
172 }
173 else if ("time".equals(parser.getName())) {
174 time = Integer.parseInt(parser.nextText());
175 }
176 else if ("join-time".equals(parser.getName())) {
177 joinTime = dateFormat.parse(parser.nextText());
178 }
179 else if( parser.getName().equals( "waitTime" ) ) {
180 Date wait = dateFormat.parse(parser.nextText());
181 System.out.println( wait );
182 }
183
184 eventType = parser.next();
185
186 if (eventType != XmlPullParser.END_TAG) {
187 // throw exception
188 }
189 }
190
191 queueDetails.addUser(new QueueUser(uid, position, time, joinTime));
192
193 eventType = parser.next();
194 }
195 }
196 return queueDetails;
197 }
198 }
199}