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;
023import org.xmlpull.v1.XmlPullParserException;
024
025import java.io.IOException;
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.Iterator;
029import java.util.List;
030
031/**
032 * Represents a request for getting the jid of the workgroups where an agent can work or could
033 * represent the result of such request which will contain the list of workgroups JIDs where the
034 * agent can work.
035 *
036 * @author Gaston Dombiak
037 */
038public class AgentWorkgroups extends IQ {
039
040    private String agentJID;
041    private List<String> workgroups;
042
043    private AgentWorkgroups() {
044        super("workgroups", "http://jabber.org/protocol/workgroup");
045    }
046
047    /**
048     * Creates an AgentWorkgroups request for the given agent. This IQ will be sent and an answer
049     * will be received with the jid of the workgroups where the agent can work.
050     *
051     * @param agentJID the id of the agent to get his workgroups.
052     */
053    public AgentWorkgroups(String agentJID) {
054        this();
055        this.agentJID = agentJID;
056        this.workgroups = new ArrayList<String>();
057    }
058
059    /**
060     * Creates an AgentWorkgroups which will contain the JIDs of the workgroups where an agent can
061     * work.
062     *
063     * @param agentJID the id of the agent that can work in the list of workgroups.
064     * @param workgroups the list of workgroup JIDs where the agent can work.
065     */
066    public AgentWorkgroups(String agentJID, List<String> workgroups) {
067        this();
068        this.agentJID = agentJID;
069        this.workgroups = workgroups;
070    }
071
072    public String getAgentJID() {
073        return agentJID;
074    }
075
076    /**
077     * Returns a list of workgroup JIDs where the agent can work.
078     *
079     * @return a list of workgroup JIDs where the agent can work.
080     */
081    public List<String> getWorkgroups() {
082        return Collections.unmodifiableList(workgroups);
083    }
084
085    @Override
086    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
087        buf.attribute("jid", agentJID).rightAngleBracket();
088
089        for (Iterator<String> it=workgroups.iterator(); it.hasNext();) {
090            String workgroupJID = it.next();
091            buf.append("<workgroup jid=\"" + workgroupJID + "\"/>");
092        }
093
094        return buf;
095    }
096
097    /**
098     * An IQProvider for AgentWorkgroups packets.
099     *
100     * @author Gaston Dombiak
101     */
102    public static class Provider extends IQProvider<AgentWorkgroups> {
103
104        @Override
105        public AgentWorkgroups parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
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}