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