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;
021
022import org.jivesoftware.smack.packet.IQ;
023import org.jivesoftware.smack.packet.IqData;
024import org.jivesoftware.smack.packet.XmlEnvironment;
025import org.jivesoftware.smack.provider.IqProvider;
026import org.jivesoftware.smack.xml.XmlPullParser;
027import org.jivesoftware.smack.xml.XmlPullParserException;
028
029/**
030 * IQ stanza for retrieving and changing the Agent personal information.
031 */
032public class AgentInfo extends IQ {
033
034    /**
035    * Element name of the stanza extension.
036    */
037   public static final String ELEMENT_NAME = "agent-info";
038
039   /**
040    * Namespace of the stanza extension.
041    */
042   public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
043
044    private String jid;
045    private String name;
046
047    public AgentInfo() {
048        super(ELEMENT_NAME, NAMESPACE);
049    }
050
051    /**
052     * Returns the Agent's jid.
053     *
054     * @return the Agent's jid.
055     */
056    public String getJid() {
057        return jid;
058    }
059
060    /**
061     * Sets the Agent's jid.
062     *
063     * @param jid the jid of the agent.
064     */
065    public void setJid(String jid) {
066        this.jid = jid;
067    }
068
069    /**
070     * Returns the Agent's name. The name of the agent may be different than the user's name.
071     * This property may be shown in the webchat client.
072     *
073     * @return the Agent's name.
074     */
075    public String getName() {
076        return name;
077    }
078
079    /**
080     * Sets the Agent's name. The name of the agent may be different than the user's name.
081     * This property may be shown in the webchat client.
082     *
083     * @param name the new name of the agent.
084     */
085    public void setName(String name) {
086        this.name = name;
087    }
088
089    @Override
090    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
091        buf.rightAngleBracket();
092
093        if (jid != null) {
094            buf.append("<jid>").append(getJid()).append("</jid>");
095        }
096        if (name != null) {
097            buf.append("<name>").append(getName()).append("</name>");
098        }
099
100        return buf;
101    }
102
103    /**
104     * An IQProvider for AgentInfo packets.
105     *
106     * @author Gaston Dombiak
107     */
108    public static class Provider extends IqProvider<AgentInfo> {
109
110        @Override
111        public AgentInfo parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
112            AgentInfo answer = new AgentInfo();
113
114            boolean done = false;
115            while (!done) {
116                XmlPullParser.Event eventType = parser.next();
117                if (eventType == XmlPullParser.Event.START_ELEMENT) {
118                    if (parser.getName().equals("jid")) {
119                        answer.setJid(parser.nextText());
120                    }
121                    else if (parser.getName().equals("name")) {
122                        answer.setName(parser.nextText());
123                    }
124                }
125                else if (eventType == XmlPullParser.Event.END_ELEMENT) {
126                    if (parser.getName().equals(ELEMENT_NAME)) {
127                        done = true;
128                    }
129                }
130            }
131
132            return answer;
133        }
134    }
135}