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