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