Home | History | Annotate | Download | only in packet
      1 /**
      2  * $Revision$
      3  * $Date$
      4  *
      5  * Copyright 2003-2007 Jive Software.
      6  *
      7  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *     http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  */
     19 
     20 package org.jivesoftware.smackx.workgroup.packet;
     21 
     22 import org.jivesoftware.smack.packet.IQ;
     23 import org.jivesoftware.smack.provider.IQProvider;
     24 import org.xmlpull.v1.XmlPullParser;
     25 
     26 import java.util.ArrayList;
     27 import java.util.Collections;
     28 import java.util.Iterator;
     29 import java.util.List;
     30 
     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 
     40     private String agentJID;
     41     private List<String> workgroups;
     42 
     43     /**
     44      * Creates an AgentWorkgroups request for the given agent. This IQ will be sent and an answer
     45      * will be received with the jid of the workgroups where the agent can work.
     46      *
     47      * @param agentJID the id of the agent to get his workgroups.
     48      */
     49     public AgentWorkgroups(String agentJID) {
     50         this.agentJID = agentJID;
     51         this.workgroups = new ArrayList<String>();
     52     }
     53 
     54     /**
     55      * Creates an AgentWorkgroups which will contain the JIDs of the workgroups where an agent can
     56      * work.
     57      *
     58      * @param agentJID the id of the agent that can work in the list of workgroups.
     59      * @param workgroups the list of workgroup JIDs where the agent can work.
     60      */
     61     public AgentWorkgroups(String agentJID, List<String> workgroups) {
     62         this.agentJID = agentJID;
     63         this.workgroups = workgroups;
     64     }
     65 
     66     public String getAgentJID() {
     67         return agentJID;
     68     }
     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 
     79     public String getChildElementXML() {
     80         StringBuilder buf = new StringBuilder();
     81 
     82         buf.append("<workgroups xmlns=\"http://jabber.org/protocol/workgroup\" jid=\"")
     83                 .append(agentJID)
     84                 .append("\">");
     85 
     86         for (Iterator<String> it=workgroups.iterator(); it.hasNext();) {
     87             String workgroupJID = it.next();
     88             buf.append("<workgroup jid=\"" + workgroupJID + "\"/>");
     89         }
     90 
     91         buf.append("</workgroups>");
     92 
     93         return buf.toString();
     94     }
     95 
     96     /**
     97      * An IQProvider for AgentWorkgroups packets.
     98      *
     99      * @author Gaston Dombiak
    100      */
    101     public static class Provider implements IQProvider {
    102 
    103         public Provider() {
    104             super();
    105         }
    106 
    107         public IQ parseIQ(XmlPullParser parser) throws Exception {
    108             String agentJID = parser.getAttributeValue("", "jid");
    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 }
    130