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