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