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 org.jivesoftware.smackx.workgroup.QueueUser; 021import org.jivesoftware.smack.packet.PacketExtension; 022import org.jivesoftware.smack.provider.PacketExtensionProvider; 023import org.xmlpull.v1.XmlPullParser; 024 025import java.text.SimpleDateFormat; 026import java.util.Date; 027import java.util.HashSet; 028import java.util.Iterator; 029import java.util.Set; 030import java.util.logging.Logger; 031 032/** 033 * Queue details packet extension, which contains details about the users 034 * currently in a queue. 035 */ 036public class QueueDetails implements PacketExtension { 037 private static final Logger LOGGER = Logger.getLogger(QueueDetails.class.getName()); 038 039 /** 040 * Element name of the packet extension. 041 */ 042 public static final String ELEMENT_NAME = "notify-queue-details"; 043 044 /** 045 * Namespace of the packet extension. 046 */ 047 public static final String NAMESPACE = "http://jabber.org/protocol/workgroup"; 048 049 private static final String DATE_FORMAT = "yyyyMMdd'T'HH:mm:ss"; 050 051 private SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 052 /** 053 * The list of users in the queue. 054 */ 055 private Set<QueueUser> users; 056 057 /** 058 * Creates a new QueueDetails packet 059 */ 060 private QueueDetails() { 061 users = new HashSet<QueueUser>(); 062 } 063 064 /** 065 * Returns the number of users currently in the queue that are waiting to 066 * be routed to an agent. 067 * 068 * @return the number of users in the queue. 069 */ 070 public int getUserCount() { 071 return users.size(); 072 } 073 074 /** 075 * Returns the set of users in the queue that are waiting to 076 * be routed to an agent (as QueueUser objects). 077 * 078 * @return a Set for the users waiting in a queue. 079 */ 080 public Set<QueueUser> getUsers() { 081 synchronized (users) { 082 return users; 083 } 084 } 085 086 /** 087 * Adds a user to the packet. 088 * 089 * @param user the user. 090 */ 091 private void addUser(QueueUser user) { 092 synchronized (users) { 093 users.add(user); 094 } 095 } 096 097 public String getElementName() { 098 return ELEMENT_NAME; 099 } 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 LOGGER.fine(wait.toString()); 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}