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.agent;
019
020import java.io.IOException;
021
022import org.jivesoftware.smack.SmackException.NotConnectedException;
023import org.jivesoftware.smack.XMPPConnection;
024import org.jivesoftware.smack.packet.IQ;
025import org.jivesoftware.smack.packet.SimpleIQ;
026import org.jivesoftware.smack.packet.XmlEnvironment;
027import org.jivesoftware.smack.provider.IQProvider;
028import org.jivesoftware.smack.xml.XmlPullParser;
029import org.jivesoftware.smack.xml.XmlPullParserException;
030
031import org.jxmpp.jid.Jid;
032
033
034public class OfferConfirmation extends SimpleIQ {
035    private String userJID;
036    private long sessionID;
037
038    public OfferConfirmation() {
039        super("offer-confirmation", "http://jabber.org/protocol/workgroup");
040    }
041
042    public String getUserJID() {
043        return userJID;
044    }
045
046    public void setUserJID(String userJID) {
047        this.userJID = userJID;
048    }
049
050    public long getSessionID() {
051        return sessionID;
052    }
053
054    public void setSessionID(long sessionID) {
055        this.sessionID = sessionID;
056    }
057
058
059    public void notifyService(XMPPConnection con, Jid workgroup, String createdRoomName) throws NotConnectedException, InterruptedException {
060        NotifyServicePacket packet = new NotifyServicePacket(workgroup, createdRoomName);
061        con.sendStanza(packet);
062    }
063
064    public static class Provider extends IQProvider<OfferConfirmation> {
065
066        @Override
067        public OfferConfirmation parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
068                        throws XmlPullParserException, IOException {
069            final OfferConfirmation confirmation = new OfferConfirmation();
070
071            boolean done = false;
072            while (!done) {
073                parser.next();
074                if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT && "user-jid".equals(parser.getName())) {
075                    confirmation.setUserJID(parser.nextText());
076                }
077                else if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT && "session-id".equals(parser.getName())) {
078                    confirmation.setSessionID(Long.valueOf(parser.nextText()));
079                }
080                else if (parser.getEventType() == XmlPullParser.Event.END_ELEMENT && "offer-confirmation".equals(parser.getName())) {
081                    done = true;
082                }
083            }
084
085
086            return confirmation;
087        }
088    }
089
090
091    /**
092     * Stanza for notifying server of RoomName
093     */
094    private static class NotifyServicePacket extends IQ {
095        String roomName;
096
097        NotifyServicePacket(Jid workgroup, String roomName) {
098            super("offer-confirmation", "http://jabber.org/protocol/workgroup");
099            this.setTo(workgroup);
100            this.setType(IQ.Type.result);
101
102            this.roomName = roomName;
103        }
104
105        @Override
106        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
107            xml.attribute("roomname", roomName);
108            xml.setEmptyElement();
109            return xml;
110        }
111    }
112
113
114}