1/**
2 * $Revision$
3 * $Date$
4 *
5 * Copyright 2003-2007 Jive Software.
6 *
7 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20package org.jivesoftware.smackx.workgroup.packet;
21
22import org.jivesoftware.smack.packet.PacketExtension;
23import org.jivesoftware.smack.provider.PacketExtensionProvider;
24import org.xmlpull.v1.XmlPullParser;
25
26/**
27 * Packet extension for {@link org.jivesoftware.smackx.workgroup.agent.TransferRequest}.
28 *
29 * @author Gaston Dombiak
30 */
31public class RoomTransfer implements PacketExtension {
32
33    /**
34     * Element name of the packet extension.
35     */
36    public static final String ELEMENT_NAME = "transfer";
37
38    /**
39     * Namespace of the packet extension.
40     */
41    public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
42
43    /**
44     * Type of entity being invited to a groupchat support session.
45     */
46    private RoomTransfer.Type type;
47    /**
48     * JID of the entity being invited. The entity could be another agent, user , a queue or a workgroup. In
49     * the case of a queue or a workgroup the server will select the best agent to invite.
50     */
51    private String invitee;
52    /**
53     * Full JID of the user that sent the invitation.
54     */
55    private String inviter;
56    /**
57     * ID of the session that originated the initial user request.
58     */
59    private String sessionID;
60    /**
61     * JID of the room to join if offer is accepted.
62     */
63    private String room;
64    /**
65     * Text provided by the inviter explaining the reason why the invitee is invited.
66     */
67    private String reason;
68
69    public RoomTransfer(RoomTransfer.Type type, String invitee, String sessionID, String reason) {
70        this.type = type;
71        this.invitee = invitee;
72        this.sessionID = sessionID;
73        this.reason = reason;
74    }
75
76    private RoomTransfer() {
77    }
78
79    public String getElementName() {
80        return ELEMENT_NAME;
81    }
82
83    public String getNamespace() {
84        return NAMESPACE;
85    }
86
87    public String getInviter() {
88        return inviter;
89    }
90
91    public String getRoom() {
92        return room;
93    }
94
95    public String getReason() {
96        return reason;
97    }
98
99    public String getSessionID() {
100        return sessionID;
101    }
102
103    public String toXML() {
104        StringBuilder buf = new StringBuilder();
105
106        buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE);
107        buf.append("\" type=\"").append(type).append("\">");
108        buf.append("<session xmlns=\"http://jivesoftware.com/protocol/workgroup\" id=\"").append(sessionID).append("\"></session>");
109        if (invitee != null) {
110            buf.append("<invitee>").append(invitee).append("</invitee>");
111        }
112        if (inviter != null) {
113            buf.append("<inviter>").append(inviter).append("</inviter>");
114        }
115        if (reason != null) {
116            buf.append("<reason>").append(reason).append("</reason>");
117        }
118        // Add packet extensions, if any are defined.
119        buf.append("</").append(ELEMENT_NAME).append("> ");
120
121        return buf.toString();
122    }
123
124    /**
125     * Type of entity being invited to a groupchat support session.
126     */
127    public static enum Type {
128        /**
129         * A user is being invited to a groupchat support session. The user could be another agent
130         * or just a regular XMPP user.
131         */
132        user,
133        /**
134         * Some agent of the specified queue will be invited to the groupchat support session.
135         */
136        queue,
137        /**
138         * Some agent of the specified workgroup will be invited to the groupchat support session.
139         */
140        workgroup
141    }
142
143    public static class Provider implements PacketExtensionProvider {
144
145        public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
146            final RoomTransfer invitation = new RoomTransfer();
147            invitation.type = RoomTransfer.Type.valueOf(parser.getAttributeValue("", "type"));
148
149            boolean done = false;
150            while (!done) {
151                parser.next();
152                String elementName = parser.getName();
153                if (parser.getEventType() == XmlPullParser.START_TAG) {
154                    if ("session".equals(elementName)) {
155                        invitation.sessionID = parser.getAttributeValue("", "id");
156                    }
157                    else if ("invitee".equals(elementName)) {
158                        invitation.invitee = parser.nextText();
159                    }
160                    else if ("inviter".equals(elementName)) {
161                        invitation.inviter = parser.nextText();
162                    }
163                    else if ("reason".equals(elementName)) {
164                        invitation.reason = parser.nextText();
165                    }
166                    else if ("room".equals(elementName)) {
167                        invitation.room = parser.nextText();
168                    }
169                }
170                else if (parser.getEventType() == XmlPullParser.END_TAG && ELEMENT_NAME.equals(elementName)) {
171                    done = true;
172                }
173            }
174            return invitation;
175        }
176    }
177}
178