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