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