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.packet;
019
020import java.io.IOException;
021import java.text.ParseException;
022import java.text.SimpleDateFormat;
023import java.util.Collections;
024import java.util.Date;
025import java.util.HashSet;
026import java.util.Set;
027import java.util.TimeZone;
028
029import org.jivesoftware.smack.SmackException;
030import org.jivesoftware.smack.packet.IQ;
031import org.jivesoftware.smack.provider.IQProvider;
032import org.xmlpull.v1.XmlPullParser;
033import org.xmlpull.v1.XmlPullParserException;
034
035/**
036 * Stanza(/Packet) used for requesting information about occupants of a room or for retrieving information
037 * such information.
038 *
039 * @author Gaston Dombiak
040 */
041public class OccupantsInfo extends IQ {
042
043    private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
044
045    static {
046        UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
047    }
048
049    /**
050     * Element name of the stanza(/packet) extension.
051     */
052    public static final String ELEMENT_NAME = "occupants-info";
053
054    /**
055     * Namespace of the stanza(/packet) extension.
056     */
057    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
058
059    private String roomID;
060    private final Set<OccupantInfo> occupants;
061
062    public OccupantsInfo(String roomID) {
063        super(ELEMENT_NAME, NAMESPACE);
064        this.roomID = roomID;
065        this.occupants = new HashSet<OccupantInfo>();
066    }
067
068    public String getRoomID() {
069        return roomID;
070    }
071
072    public int getOccupantsCount() {
073        return occupants.size();
074    }
075
076    public Set<OccupantInfo> getOccupants() {
077        return Collections.unmodifiableSet(occupants);
078    }
079
080    @Override
081    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
082        buf.append("\" roomID=\"").append(roomID).append("\">");
083        synchronized (occupants) {
084            for (OccupantInfo occupant : occupants) {
085                buf.append("<occupant>");
086                // Add the occupant jid
087                buf.append("<jid>");
088                buf.append(occupant.getJID());
089                buf.append("</jid>");
090                // Add the occupant nickname
091                buf.append("<name>");
092                buf.append(occupant.getNickname());
093                buf.append("</name>");
094                // Add the date when the occupant joined the room
095                buf.append("<joined>");
096                buf.append(UTC_FORMAT.format(occupant.getJoined()));
097                buf.append("</joined>");
098                buf.append("</occupant>");
099            }
100        }
101        return buf;
102    }
103
104    public static class OccupantInfo {
105
106        private String jid;
107        private String nickname;
108        private Date joined;
109
110        public OccupantInfo(String jid, String nickname, Date joined) {
111            this.jid = jid;
112            this.nickname = nickname;
113            this.joined = joined;
114        }
115
116        public String getJID() {
117            return jid;
118        }
119
120        public String getNickname() {
121            return nickname;
122        }
123
124        public Date getJoined() {
125            return joined;
126        }
127    }
128
129    /**
130     * Stanza(/Packet) extension provider for AgentStatusRequest packets.
131     */
132    public static class Provider extends IQProvider<OccupantsInfo> {
133
134        @Override
135        public OccupantsInfo parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
136            OccupantsInfo occupantsInfo = new OccupantsInfo(parser.getAttributeValue("", "roomID"));
137
138            boolean done = false;
139            while (!done) {
140                int eventType = parser.next();
141                if ((eventType == XmlPullParser.START_TAG) &&
142                        ("occupant".equals(parser.getName()))) {
143                    occupantsInfo.occupants.add(parseOccupantInfo(parser));
144                } else if (eventType == XmlPullParser.END_TAG &&
145                        ELEMENT_NAME.equals(parser.getName())) {
146                    done = true;
147                }
148            }
149            return occupantsInfo;
150        }
151
152        private OccupantInfo parseOccupantInfo(XmlPullParser parser) throws XmlPullParserException, IOException {
153
154            boolean done = false;
155            String jid = null;
156            String nickname = null;
157            Date joined = null;
158            while (!done) {
159                int eventType = parser.next();
160                if ((eventType == XmlPullParser.START_TAG) && ("jid".equals(parser.getName()))) {
161                    jid = parser.nextText();
162                } else if ((eventType == XmlPullParser.START_TAG) &&
163                        ("nickname".equals(parser.getName()))) {
164                    nickname = parser.nextText();
165                } else if ((eventType == XmlPullParser.START_TAG) &&
166                        ("joined".equals(parser.getName()))) {
167                    try {
168                        joined = UTC_FORMAT.parse(parser.nextText());
169                    } catch (ParseException e) {
170                        new SmackException(e);
171                    }
172                } else if (eventType == XmlPullParser.END_TAG &&
173                        "occupant".equals(parser.getName())) {
174                    done = true;
175                }
176            }
177            return new OccupantInfo(jid, nickname, joined);
178        }
179    }
180}