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