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