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