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