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