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.smack.packet.IQ; 021import org.jivesoftware.smack.provider.IQProvider; 022import org.jivesoftware.smack.util.ParserUtils; 023import org.jxmpp.jid.Jid; 024import org.xmlpull.v1.XmlPullParser; 025import org.xmlpull.v1.XmlPullParserException; 026 027import java.io.IOException; 028import java.util.ArrayList; 029import java.util.Collections; 030import java.util.Iterator; 031import java.util.List; 032 033/** 034 * Represents a request for getting the jid of the workgroups where an agent can work or could 035 * represent the result of such request which will contain the list of workgroups JIDs where the 036 * agent can work. 037 * 038 * @author Gaston Dombiak 039 */ 040public class AgentWorkgroups extends IQ { 041 042 private Jid agentJID; 043 private List<String> workgroups; 044 045 private AgentWorkgroups() { 046 super("workgroups", "http://jabber.org/protocol/workgroup"); 047 } 048 049 /** 050 * Creates an AgentWorkgroups request for the given agent. This IQ will be sent and an answer 051 * will be received with the jid of the workgroups where the agent can work. 052 * 053 * @param agentJID the id of the agent to get his workgroups. 054 */ 055 public AgentWorkgroups(Jid agentJID) { 056 this(); 057 this.agentJID = agentJID; 058 this.workgroups = new ArrayList<String>(); 059 } 060 061 /** 062 * Creates an AgentWorkgroups which will contain the JIDs of the workgroups where an agent can 063 * work. 064 * 065 * @param agentJID the id of the agent that can work in the list of workgroups. 066 * @param workgroups the list of workgroup JIDs where the agent can work. 067 */ 068 public AgentWorkgroups(Jid agentJID, List<String> workgroups) { 069 this(); 070 this.agentJID = agentJID; 071 this.workgroups = workgroups; 072 } 073 074 public Jid getAgentJID() { 075 return agentJID; 076 } 077 078 /** 079 * Returns a list of workgroup JIDs where the agent can work. 080 * 081 * @return a list of workgroup JIDs where the agent can work. 082 */ 083 public List<String> getWorkgroups() { 084 return Collections.unmodifiableList(workgroups); 085 } 086 087 @Override 088 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 089 buf.attribute("jid", agentJID).rightAngleBracket(); 090 091 for (Iterator<String> it=workgroups.iterator(); it.hasNext();) { 092 String workgroupJID = it.next(); 093 buf.append("<workgroup jid=\"" + workgroupJID + "\"/>"); 094 } 095 096 return buf; 097 } 098 099 /** 100 * An IQProvider for AgentWorkgroups packets. 101 * 102 * @author Gaston Dombiak 103 */ 104 public static class Provider extends IQProvider<AgentWorkgroups> { 105 106 @Override 107 public AgentWorkgroups parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { 108 final Jid agentJID = ParserUtils.getJidAttribute(parser); 109 List<String> workgroups = new ArrayList<String>(); 110 111 boolean done = false; 112 while (!done) { 113 int eventType = parser.next(); 114 if (eventType == XmlPullParser.START_TAG) { 115 if (parser.getName().equals("workgroup")) { 116 workgroups.add(parser.getAttributeValue("", "jid")); 117 } 118 } 119 else if (eventType == XmlPullParser.END_TAG) { 120 if (parser.getName().equals("workgroups")) { 121 done = true; 122 } 123 } 124 } 125 126 return new AgentWorkgroups(agentJID, workgroups); 127 } 128 } 129}