AgentWorkgroups.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.packet;

  18. import org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.provider.IQProvider;
  20. import org.jivesoftware.smack.util.ParserUtils;
  21. import org.jxmpp.jid.Jid;
  22. import org.xmlpull.v1.XmlPullParser;
  23. import org.xmlpull.v1.XmlPullParserException;

  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.Iterator;
  28. import java.util.List;

  29. /**
  30.  * Represents a request for getting the jid of the workgroups where an agent can work or could
  31.  * represent the result of such request which will contain the list of workgroups JIDs where the
  32.  * agent can work.
  33.  *
  34.  * @author Gaston Dombiak
  35.  */
  36. public class AgentWorkgroups extends IQ {

  37.     private Jid agentJID;
  38.     private List<String> workgroups;

  39.     private AgentWorkgroups() {
  40.         super("workgroups", "http://jabber.org/protocol/workgroup");
  41.     }

  42.     /**
  43.      * Creates an AgentWorkgroups request for the given agent. This IQ will be sent and an answer
  44.      * will be received with the jid of the workgroups where the agent can work.
  45.      *
  46.      * @param agentJID the id of the agent to get his workgroups.
  47.      */
  48.     public AgentWorkgroups(Jid agentJID) {
  49.         this();
  50.         this.agentJID = agentJID;
  51.         this.workgroups = new ArrayList<String>();
  52.     }

  53.     /**
  54.      * Creates an AgentWorkgroups which will contain the JIDs of the workgroups where an agent can
  55.      * work.
  56.      *
  57.      * @param agentJID the id of the agent that can work in the list of workgroups.
  58.      * @param workgroups the list of workgroup JIDs where the agent can work.
  59.      */
  60.     public AgentWorkgroups(Jid agentJID, List<String> workgroups) {
  61.         this();
  62.         this.agentJID = agentJID;
  63.         this.workgroups = workgroups;
  64.     }

  65.     public Jid getAgentJID() {
  66.         return agentJID;
  67.     }

  68.     /**
  69.      * Returns a list of workgroup JIDs where the agent can work.
  70.      *
  71.      * @return a list of workgroup JIDs where the agent can work.
  72.      */
  73.     public List<String> getWorkgroups() {
  74.         return Collections.unmodifiableList(workgroups);
  75.     }

  76.     @Override
  77.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  78.         buf.attribute("jid", agentJID).rightAngleBracket();

  79.         for (Iterator<String> it=workgroups.iterator(); it.hasNext();) {
  80.             String workgroupJID = it.next();
  81.             buf.append("<workgroup jid=\"" + workgroupJID + "\"/>");
  82.         }

  83.         return buf;
  84.     }

  85.     /**
  86.      * An IQProvider for AgentWorkgroups packets.
  87.      *
  88.      * @author Gaston Dombiak
  89.      */
  90.     public static class Provider extends IQProvider<AgentWorkgroups> {

  91.         @Override
  92.         public AgentWorkgroups parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  93.             final Jid agentJID = ParserUtils.getJidAttribute(parser);
  94.             List<String> workgroups = new ArrayList<String>();

  95.             boolean done = false;
  96.             while (!done) {
  97.                 int eventType = parser.next();
  98.                 if (eventType == XmlPullParser.START_TAG) {
  99.                     if (parser.getName().equals("workgroup")) {
  100.                         workgroups.add(parser.getAttributeValue("", "jid"));
  101.                     }
  102.                 }
  103.                 else if (eventType == XmlPullParser.END_TAG) {
  104.                     if (parser.getName().equals("workgroups")) {
  105.                         done = true;
  106.                     }
  107.                 }
  108.             }

  109.             return new AgentWorkgroups(agentJID, workgroups);
  110.         }
  111.     }
  112. }