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