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