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 java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.List;

  23. import org.jivesoftware.smack.packet.IQ;
  24. import org.jivesoftware.smack.packet.IqData;
  25. import org.jivesoftware.smack.packet.XmlEnvironment;
  26. import org.jivesoftware.smack.provider.IqProvider;
  27. import org.jivesoftware.smack.util.ParserUtils;
  28. import org.jivesoftware.smack.xml.XmlPullParser;
  29. import org.jivesoftware.smack.xml.XmlPullParserException;

  30. import org.jxmpp.jid.Jid;

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

  39.     private Jid agentJID;
  40.     private List<String> workgroups;

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

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

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

  67.     public Jid getAgentJID() {
  68.         return agentJID;
  69.     }

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

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

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

  85.         return buf;
  86.     }

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

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

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

  111.             return new AgentWorkgroups(agentJID, workgroups);
  112.         }
  113.     }
  114. }