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; 021import java.text.ParseException; 022import java.text.SimpleDateFormat; 023import java.util.Date; 024 025import javax.xml.namespace.QName; 026 027import org.jivesoftware.smack.packet.ExtensionElement; 028import org.jivesoftware.smack.packet.XmlEnvironment; 029import org.jivesoftware.smack.provider.ExtensionElementProvider; 030import org.jivesoftware.smack.xml.XmlPullParser; 031import org.jivesoftware.smack.xml.XmlPullParserException; 032 033import org.jivesoftware.smackx.workgroup.agent.WorkgroupQueue; 034 035public class QueueOverview implements ExtensionElement { 036 037 /** 038 * Element name of the stanza extension. 039 */ 040 public static String ELEMENT_NAME = "notify-queue"; 041 042 /** 043 * Namespace of the stanza extension. 044 */ 045 public static String NAMESPACE = "http://jabber.org/protocol/workgroup"; 046 public static final QName QNAME = new QName(NAMESPACE, ELEMENT_NAME); 047 048 private static final String DATE_FORMAT = "yyyyMMdd'T'HH:mm:ss"; 049 private final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 050 051 private int averageWaitTime; 052 private Date oldestEntry; 053 private int userCount; 054 private WorkgroupQueue.Status status; 055 056 QueueOverview() { 057 this.averageWaitTime = -1; 058 this.oldestEntry = null; 059 this.userCount = -1; 060 this.status = null; 061 } 062 063 void setAverageWaitTime(int averageWaitTime) { 064 this.averageWaitTime = averageWaitTime; 065 } 066 067 public int getAverageWaitTime () { 068 return averageWaitTime; 069 } 070 071 void setOldestEntry(Date oldestEntry) { 072 this.oldestEntry = oldestEntry; 073 } 074 075 public Date getOldestEntry() { 076 return oldestEntry; 077 } 078 079 void setUserCount(int userCount) { 080 this.userCount = userCount; 081 } 082 083 public int getUserCount() { 084 return userCount; 085 } 086 087 public WorkgroupQueue.Status getStatus() { 088 return status; 089 } 090 091 void setStatus(WorkgroupQueue.Status status) { 092 this.status = status; 093 } 094 095 @Override 096 public String getElementName () { 097 return ELEMENT_NAME; 098 } 099 100 @Override 101 public String getNamespace () { 102 return NAMESPACE; 103 } 104 105 @Override 106 public String toXML(XmlEnvironment enclosingEnvironment) { 107 StringBuilder buf = new StringBuilder(); 108 buf.append('<').append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">"); 109 110 if (userCount != -1) { 111 buf.append("<count>").append(userCount).append("</count>"); 112 } 113 if (oldestEntry != null) { 114 buf.append("<oldest>").append(dateFormat.format(oldestEntry)).append("</oldest>"); 115 } 116 if (averageWaitTime != -1) { 117 buf.append("<time>").append(averageWaitTime).append("</time>"); 118 } 119 if (status != null) { 120 buf.append("<status>").append(status).append("</status>"); 121 } 122 buf.append("</").append(ELEMENT_NAME).append('>'); 123 124 return buf.toString(); 125 } 126 127 public static class Provider extends ExtensionElementProvider<QueueOverview> { 128 129 @Override 130 public QueueOverview parse(XmlPullParser parser, 131 int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, 132 IOException, TextParseException, ParseException { 133 QueueOverview queueOverview = new QueueOverview(); 134 SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 135 136 XmlPullParser.Event eventType = parser.next(); 137 while (eventType != XmlPullParser.Event.END_ELEMENT 138 || !ELEMENT_NAME.equals(parser.getName())) { 139 if ("count".equals(parser.getName())) { 140 queueOverview.setUserCount(Integer.parseInt(parser.nextText())); 141 } 142 else if ("time".equals(parser.getName())) { 143 queueOverview.setAverageWaitTime(Integer.parseInt(parser.nextText())); 144 } 145 else if ("oldest".equals(parser.getName())) { 146 queueOverview.setOldestEntry(dateFormat.parse(parser.nextText())); 147 } 148 else if ("status".equals(parser.getName())) { 149 queueOverview.setStatus(WorkgroupQueue.Status.fromString(parser.nextText())); 150 } 151 152 eventType = parser.next(); 153 154 if (eventType != XmlPullParser.Event.END_ELEMENT) { 155 // throw exception 156 } 157 } 158 159 if (eventType != XmlPullParser.Event.END_ELEMENT) { 160 // throw exception 161 } 162 163 return queueOverview; 164 } 165 } 166}