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