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