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.agent;
019
020import org.jivesoftware.smackx.workgroup.packet.AgentInfo;
021import org.jivesoftware.smackx.workgroup.packet.AgentWorkgroups;
022import org.jivesoftware.smack.SmackException.NoResponseException;
023import org.jivesoftware.smack.SmackException.NotConnectedException;
024import org.jivesoftware.smack.XMPPConnection;
025import org.jivesoftware.smack.XMPPException.XMPPErrorException;
026import org.jivesoftware.smack.packet.IQ;
027
028import java.util.Collection;
029
030/**
031 * The <code>Agent</code> class is used to represent one agent in a Workgroup Queue.
032 *
033 * @author Derek DeMoro
034 */
035public class Agent {
036    private XMPPConnection connection;
037    private String workgroupJID;
038
039    public static Collection<String> getWorkgroups(String serviceJID, String agentJID, XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException {
040        AgentWorkgroups request = new AgentWorkgroups(agentJID);
041        request.setTo(serviceJID);
042        AgentWorkgroups response = (AgentWorkgroups) connection.createPacketCollectorAndSend(request).nextResultOrThrow();
043        return response.getWorkgroups();
044    }
045
046    /**
047     * Constructs an Agent.
048     */
049    Agent(XMPPConnection connection, String workgroupJID) {
050        this.connection = connection;
051        this.workgroupJID = workgroupJID;
052    }
053
054    /**
055     * Return the agents JID
056     *
057     * @return - the agents JID.
058     */
059    public String getUser() {
060        return connection.getUser();
061    }
062
063    /**
064     * Return the agents name.
065     *
066     * @return - the agents name.
067     * @throws XMPPErrorException 
068     * @throws NoResponseException 
069     * @throws NotConnectedException 
070     */
071    public String getName() throws NoResponseException, XMPPErrorException, NotConnectedException {
072        AgentInfo agentInfo = new AgentInfo();
073        agentInfo.setType(IQ.Type.GET);
074        agentInfo.setTo(workgroupJID);
075        agentInfo.setFrom(getUser());
076        AgentInfo response = (AgentInfo) connection.createPacketCollectorAndSend(agentInfo).nextResultOrThrow();
077        return response.getName();
078    }
079
080    /**
081     * Changes the name of the agent in the server. The server may have this functionality
082     * disabled for all the agents or for this agent in particular. If the agent is not
083     * allowed to change his name then an exception will be thrown with a service_unavailable
084     * error code.
085     *
086     * @param newName the new name of the agent.
087     * @throws XMPPErrorException 
088     * @throws NoResponseException 
089     * @throws NotConnectedException 
090     */
091    public void setName(String newName) throws NoResponseException, XMPPErrorException, NotConnectedException {
092        AgentInfo agentInfo = new AgentInfo();
093        agentInfo.setType(IQ.Type.SET);
094        agentInfo.setTo(workgroupJID);
095        agentInfo.setFrom(getUser());
096        agentInfo.setName(newName);
097        connection.createPacketCollectorAndSend(agentInfo).nextResultOrThrow();
098    }
099}