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.IqData;
026import org.jivesoftware.smack.packet.SimpleIQ;
027import org.jivesoftware.smack.packet.XmlEnvironment;
028import org.jivesoftware.smack.provider.IqProvider;
029import org.jivesoftware.smack.xml.XmlPullParser;
030import org.jivesoftware.smack.xml.XmlPullParserException;
031
032import org.jxmpp.jid.Jid;
033
034
035public class OfferConfirmation extends SimpleIQ {
036    private String userJID;
037    private long sessionID;
038
039    public OfferConfirmation() {
040        super("offer-confirmation", "http://jabber.org/protocol/workgroup");
041    }
042
043    public String getUserJID() {
044        return userJID;
045    }
046
047    public void setUserJID(String userJID) {
048        this.userJID = userJID;
049    }
050
051    public long getSessionID() {
052        return sessionID;
053    }
054
055    public void setSessionID(long sessionID) {
056        this.sessionID = sessionID;
057    }
058
059
060    public void notifyService(XMPPConnection con, Jid workgroup, String createdRoomName) throws NotConnectedException, InterruptedException {
061        NotifyServicePacket packet = new NotifyServicePacket(workgroup, createdRoomName);
062        con.sendStanza(packet);
063    }
064
065    public static class Provider extends IqProvider<OfferConfirmation> {
066
067        @Override
068        public OfferConfirmation parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
069                        throws XmlPullParserException, IOException {
070            final OfferConfirmation confirmation = new OfferConfirmation();
071
072            boolean done = false;
073            while (!done) {
074                parser.next();
075                if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT && "user-jid".equals(parser.getName())) {
076                    confirmation.setUserJID(parser.nextText());
077                }
078                else if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT && "session-id".equals(parser.getName())) {
079                    confirmation.setSessionID(Long.valueOf(parser.nextText()));
080                }
081                else if (parser.getEventType() == XmlPullParser.Event.END_ELEMENT && "offer-confirmation".equals(parser.getName())) {
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}