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.ExtensionElement;
023import org.jivesoftware.smack.packet.IQ;
024import org.jivesoftware.smack.packet.IQ.IQChildElementXmlStringBuilder;
025import org.jivesoftware.smack.provider.ExtensionElementProvider;
026import org.jivesoftware.smack.util.XmlStringBuilder;
027
028import org.xmlpull.v1.XmlPullParser;
029import org.xmlpull.v1.XmlPullParserException;
030
031/**
032 * Stanza extension for {@link org.jivesoftware.smackx.workgroup.agent.TransferRequest}.
033 *
034 * @author Gaston Dombiak
035 */
036public class RoomTransfer implements ExtensionElement {
037
038    /**
039     * Element name of the stanza extension.
040     */
041    public static final String ELEMENT_NAME = "transfer";
042
043    /**
044     * Namespace of the stanza extension.
045     */
046    public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
047
048    /**
049     * Type of entity being invited to a groupchat support session.
050     */
051    private RoomTransfer.Type type;
052    /**
053     * JID of the entity being invited. The entity could be another agent, user , a queue or a workgroup. In
054     * the case of a queue or a workgroup the server will select the best agent to invite.
055     */
056    private String invitee;
057    /**
058     * Full JID of the user that sent the invitation.
059     */
060    private String inviter;
061    /**
062     * ID of the session that originated the initial user request.
063     */
064    private String sessionID;
065    /**
066     * JID of the room to join if offer is accepted.
067     */
068    private String room;
069    /**
070     * Text provided by the inviter explaining the reason why the invitee is invited.
071     */
072    private String reason;
073
074    public RoomTransfer(RoomTransfer.Type type, String invitee, String sessionID, String reason) {
075        this.type = type;
076        this.invitee = invitee;
077        this.sessionID = sessionID;
078        this.reason = reason;
079    }
080
081    private RoomTransfer() {
082    }
083
084    @Override
085    public String getElementName() {
086        return ELEMENT_NAME;
087    }
088
089    @Override
090    public String getNamespace() {
091        return NAMESPACE;
092    }
093
094    public String getInviter() {
095        return inviter;
096    }
097
098    public String getRoom() {
099        return room;
100    }
101
102    public String getReason() {
103        return reason;
104    }
105
106    public String getSessionID() {
107        return sessionID;
108    }
109
110    @Override
111    public XmlStringBuilder toXML(String enclosingNamespace) {
112        XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this));
113        xml.closeElement(this);
114        return xml;
115    }
116
117    public IQ.IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
118        buf.append(" type=\"").append(type.name()).append("\">");
119        buf.append("<session xmlns=\"http://jivesoftware.com/protocol/workgroup\" id=\"").append(sessionID).append("\"></session>");
120        if (invitee != null) {
121            buf.append("<invitee>").append(invitee).append("</invitee>");
122        }
123        if (inviter != null) {
124            buf.append("<inviter>").append(inviter).append("</inviter>");
125        }
126        if (reason != null) {
127            buf.append("<reason>").append(reason).append("</reason>");
128        }
129
130        return buf;
131    }
132
133    /**
134     * Type of entity being invited to a groupchat support session.
135     */
136    public enum Type {
137        /**
138         * A user is being invited to a groupchat support session. The user could be another agent
139         * or just a regular XMPP user.
140         */
141        user,
142        /**
143         * Some agent of the specified queue will be invited to the groupchat support session.
144         */
145        queue,
146        /**
147         * Some agent of the specified workgroup will be invited to the groupchat support session.
148         */
149        workgroup
150    }
151
152    public static class RoomTransferIQ extends IQ {
153        private final RoomTransfer roomTransfer;
154        public RoomTransferIQ(RoomTransfer roomTransfer) {
155            super(ELEMENT_NAME, NAMESPACE);
156            this.roomTransfer = roomTransfer;
157        }
158        @Override
159        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
160            return roomTransfer.getIQChildElementBuilder(xml);
161        }
162    }
163
164    public static class Provider extends ExtensionElementProvider<RoomTransfer> {
165
166        @Override
167        public RoomTransfer parse(XmlPullParser parser,
168                        int initialDepth) throws XmlPullParserException,
169                        IOException {
170            final RoomTransfer invitation = new RoomTransfer();
171            invitation.type = RoomTransfer.Type.valueOf(parser.getAttributeValue("", "type"));
172
173            boolean done = false;
174            while (!done) {
175                parser.next();
176                String elementName = parser.getName();
177                if (parser.getEventType() == XmlPullParser.START_TAG) {
178                    if ("session".equals(elementName)) {
179                        invitation.sessionID = parser.getAttributeValue("", "id");
180                    }
181                    else if ("invitee".equals(elementName)) {
182                        invitation.invitee = parser.nextText();
183                    }
184                    else if ("inviter".equals(elementName)) {
185                        invitation.inviter = parser.nextText();
186                    }
187                    else if ("reason".equals(elementName)) {
188                        invitation.reason = parser.nextText();
189                    }
190                    else if ("room".equals(elementName)) {
191                        invitation.room = parser.nextText();
192                    }
193                }
194                else if (parser.getEventType() == XmlPullParser.END_TAG && ELEMENT_NAME.equals(elementName)) {
195                    done = true;
196                }
197            }
198            return invitation;
199        }
200    }
201}