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