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                String elementName = parser.getName();
075                if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT && "user-jid".equals(elementName)) {
076                    confirmation.setUserJID(parser.nextText());
077                }
078                else if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT && "session-id".equals(elementName)) {
079                    confirmation.setSessionID(Long.valueOf(parser.nextText()));
080                }
081                else if (parser.getEventType() == XmlPullParser.Event.END_ELEMENT && "offer-confirmation".equals(elementName)) {
082                    done = true;
083                }
084            }
085
086
087            return confirmation;
088        }
089    }
090
091
092    /**
093     * Stanza for notifying server of RoomName
094     */
095    private static class NotifyServicePacket extends IQ {
096        String roomName;
097
098        NotifyServicePacket(Jid workgroup, String roomName) {
099            super("offer-confirmation", "http://jabber.org/protocol/workgroup");
100            this.setTo(workgroup);
101            this.setType(IQ.Type.result);
102
103            this.roomName = roomName;
104        }
105
106        @Override
107        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
108            xml.attribute("roomname", roomName);
109            xml.setEmptyElement();
110            return xml;
111        }
112    }
113
114
115}