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