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