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