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 org.jivesoftware.smack.SmackException.NotConnectedException;
021import org.jivesoftware.smack.XMPPConnection;
022import org.jivesoftware.smack.packet.IQ;
023import org.jivesoftware.smack.provider.IQProvider;
024import org.xmlpull.v1.XmlPullParser;
025
026
027public class OfferConfirmation extends IQ {
028    private String userJID;
029    private long sessionID;
030
031    public String getUserJID() {
032        return userJID;
033    }
034
035    public void setUserJID(String userJID) {
036        this.userJID = userJID;
037    }
038
039    public long getSessionID() {
040        return sessionID;
041    }
042
043    public void setSessionID(long sessionID) {
044        this.sessionID = sessionID;
045    }
046
047
048    public void notifyService(XMPPConnection con, String workgroup, String createdRoomName) throws NotConnectedException {
049        NotifyServicePacket packet = new NotifyServicePacket(workgroup, createdRoomName);
050        con.sendPacket(packet);
051    }
052
053    public String getChildElementXML() {
054        StringBuilder buf = new StringBuilder();
055        buf.append("<offer-confirmation xmlns=\"http://jabber.org/protocol/workgroup\">");
056        buf.append("</offer-confirmation>");
057        return buf.toString();
058    }
059
060    public static class Provider implements IQProvider {
061
062        public IQ parseIQ(XmlPullParser parser) throws Exception {
063            final OfferConfirmation confirmation = new OfferConfirmation();
064
065            boolean done = false;
066            while (!done) {
067                parser.next();
068                String elementName = parser.getName();
069                if (parser.getEventType() == XmlPullParser.START_TAG && "user-jid".equals(elementName)) {
070                    try {
071                        confirmation.setUserJID(parser.nextText());
072                    }
073                    catch (NumberFormatException nfe) {
074                    }
075                }
076                else if (parser.getEventType() == XmlPullParser.START_TAG && "session-id".equals(elementName)) {
077                    try {
078                        confirmation.setSessionID(Long.valueOf(parser.nextText()));
079                    }
080                    catch (NumberFormatException nfe) {
081                    }
082                }
083                else if (parser.getEventType() == XmlPullParser.END_TAG && "offer-confirmation".equals(elementName)) {
084                    done = true;
085                }
086            }
087
088
089            return confirmation;
090        }
091    }
092
093
094    /**
095     * Packet for notifying server of RoomName
096     */
097    private class NotifyServicePacket extends IQ {
098        String roomName;
099
100        NotifyServicePacket(String workgroup, String roomName) {
101            this.setTo(workgroup);
102            this.setType(IQ.Type.RESULT);
103
104            this.roomName = roomName;
105        }
106
107        public String getChildElementXML() {
108            return "<offer-confirmation  roomname=\"" + roomName + "\" xmlns=\"http://jabber.org/protocol/workgroup" + "\"/>";
109        }
110    }
111
112
113}