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    public String getElementName() {
084        return ELEMENT_NAME;
085    }
086
087    public String getNamespace() {
088        return NAMESPACE;
089    }
090
091    public String getInviter() {
092        return inviter;
093    }
094
095    public String getRoom() {
096        return room;
097    }
098
099    public String getReason() {
100        return reason;
101    }
102
103    public String getSessionID() {
104        return sessionID;
105    }
106
107    @Override
108    public XmlStringBuilder toXML() {
109        XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this));
110        xml.closeElement(this);
111        return xml;
112    }
113
114    public IQ.IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
115        buf.append("\" type=\"").append(type.name()).append("\">");
116        buf.append("<session xmlns=\"http://jivesoftware.com/protocol/workgroup\" id=\"").append(sessionID).append("\"></session>");
117        if (invitee != null) {
118            buf.append("<invitee>").append(invitee).append("</invitee>");
119        }
120        if (inviter != null) {
121            buf.append("<inviter>").append(inviter).append("</inviter>");
122        }
123        if (reason != null) {
124            buf.append("<reason>").append(reason).append("</reason>");
125        }
126
127        return buf;
128    }
129
130    /**
131     * Type of entity being invited to a groupchat support session.
132     */
133    public static enum Type {
134        /**
135         * A user is being invited to a groupchat support session. The user could be another agent
136         * or just a regular XMPP user.
137         */
138        user,
139        /**
140         * Some agent of the specified queue will be invited to the groupchat support session.
141         */
142        queue,
143        /**
144         * Some agent of the specified workgroup will be invited to the groupchat support session.
145         */
146        workgroup
147    }
148
149    public static class RoomTransferIQ extends IQ {
150        private final RoomTransfer roomTransfer;
151        public RoomTransferIQ(RoomTransfer roomTransfer) {
152            super(ELEMENT_NAME, NAMESPACE);
153            this.roomTransfer = roomTransfer;
154        }
155        @Override
156        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
157            return roomTransfer.getIQChildElementBuilder(xml);
158        }
159    }
160
161    public static class Provider extends ExtensionElementProvider<RoomTransfer> {
162
163        @Override
164        public RoomTransfer parse(XmlPullParser parser,
165                        int initialDepth) throws XmlPullParserException,
166                        IOException {
167            final RoomTransfer invitation = new RoomTransfer();
168            invitation.type = RoomTransfer.Type.valueOf(parser.getAttributeValue("", "type"));
169
170            boolean done = false;
171            while (!done) {
172                parser.next();
173                String elementName = parser.getName();
174                if (parser.getEventType() == XmlPullParser.START_TAG) {
175                    if ("session".equals(elementName)) {
176                        invitation.sessionID = parser.getAttributeValue("", "id");
177                    }
178                    else if ("invitee".equals(elementName)) {
179                        invitation.invitee = parser.nextText();
180                    }
181                    else if ("inviter".equals(elementName)) {
182                        invitation.inviter = parser.nextText();
183                    }
184                    else if ("reason".equals(elementName)) {
185                        invitation.reason = parser.nextText();
186                    }
187                    else if ("room".equals(elementName)) {
188                        invitation.room = parser.nextText();
189                    }
190                }
191                else if (parser.getEventType() == XmlPullParser.END_TAG && ELEMENT_NAME.equals(elementName)) {
192                    done = true;
193                }
194            }
195            return invitation;
196        }
197    }
198}