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;
032
033import org.xmlpull.v1.XmlPullParser;
034import org.xmlpull.v1.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) throws XmlPullParserException, IOException, SmackException {
140            OccupantsInfo occupantsInfo = new OccupantsInfo(parser.getAttributeValue("", "roomID"));
141
142            boolean done = false;
143            while (!done) {
144                int eventType = parser.next();
145                if ((eventType == XmlPullParser.START_TAG) &&
146                        ("occupant".equals(parser.getName()))) {
147                    occupantsInfo.occupants.add(parseOccupantInfo(parser));
148                } else if (eventType == XmlPullParser.END_TAG &&
149                        ELEMENT_NAME.equals(parser.getName())) {
150                    done = true;
151                }
152            }
153            return occupantsInfo;
154        }
155
156        private OccupantInfo parseOccupantInfo(XmlPullParser parser) throws XmlPullParserException, IOException, SmackException {
157
158            boolean done = false;
159            String jid = null;
160            String nickname = null;
161            Date joined = null;
162            while (!done) {
163                int eventType = parser.next();
164                if ((eventType == XmlPullParser.START_TAG) && ("jid".equals(parser.getName()))) {
165                    jid = parser.nextText();
166                } else if ((eventType == XmlPullParser.START_TAG) &&
167                        ("nickname".equals(parser.getName()))) {
168                    nickname = parser.nextText();
169                } else if ((eventType == XmlPullParser.START_TAG) &&
170                        ("joined".equals(parser.getName()))) {
171                    try {
172                        synchronized (UTC_FORMAT) {
173                            joined = UTC_FORMAT.parse(parser.nextText());
174                        }
175                    } catch (ParseException e) {
176                        throw new SmackException(e);
177                    }
178                } else if (eventType == XmlPullParser.END_TAG &&
179                        "occupant".equals(parser.getName())) {
180                    done = true;
181                }
182            }
183            return new OccupantInfo(jid, nickname, joined);
184        }
185    }
186}