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 */
017package org.jivesoftware.smackx.workgroup.packet;
018
019import java.io.IOException;
020import java.text.ParseException;
021import java.text.SimpleDateFormat;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.Date;
025import java.util.Iterator;
026import java.util.List;
027import java.util.TimeZone;
028
029import org.jivesoftware.smack.packet.ExtensionElement;
030import org.jivesoftware.smack.packet.XmlEnvironment;
031import org.jivesoftware.smack.provider.ExtensionElementProvider;
032import org.jivesoftware.smack.util.ParserUtils;
033import org.jivesoftware.smack.xml.XmlPullParser;
034import org.jivesoftware.smack.xml.XmlPullParserException;
035
036import org.jxmpp.jid.EntityBareJid;
037
038/**
039 * Agent status packet.
040 *
041 * @author Matt Tucker
042 */
043public class AgentStatus implements ExtensionElement {
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 = "agent-status";
056
057    /**
058     * Namespace of the stanza extension.
059     */
060    public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
061
062    private EntityBareJid workgroupJID;
063    private final List<ChatInfo> currentChats = new ArrayList<>();
064    private int maxChats = -1;
065
066    AgentStatus() {
067    }
068
069    public EntityBareJid getWorkgroupJID() {
070        return workgroupJID;
071    }
072
073    /**
074     * Returns a collection of ChatInfo where each ChatInfo represents a Chat where this agent
075     * is participating.
076     *
077     * @return a collection of ChatInfo where each ChatInfo represents a Chat where this agent
078     *         is participating.
079     */
080    public List<ChatInfo> getCurrentChats() {
081        return Collections.unmodifiableList(currentChats);
082    }
083
084    public int getMaxChats() {
085        return maxChats;
086    }
087
088    @Override
089    public String getElementName() {
090        return ELEMENT_NAME;
091    }
092
093    @Override
094    public String getNamespace() {
095        return NAMESPACE;
096    }
097
098    @Override
099    public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
100        StringBuilder buf = new StringBuilder();
101
102        buf.append('<').append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append('"');
103        if (workgroupJID != null) {
104            buf.append(" jid=\"").append(workgroupJID).append('"');
105        }
106        buf.append('>');
107        if (maxChats != -1) {
108            buf.append("<max-chats>").append(maxChats).append("</max-chats>");
109        }
110        if (!currentChats.isEmpty()) {
111            buf.append("<current-chats xmlns= \"http://jivesoftware.com/protocol/workgroup\">");
112            for (Iterator<ChatInfo> it = currentChats.iterator(); it.hasNext();) {
113                buf.append(it.next().toXML());
114            }
115            buf.append("</current-chats>");
116        }
117        buf.append("</").append(this.getElementName()).append("> ");
118
119        return buf.toString();
120    }
121
122    /**
123     * Represents information about a Chat where this Agent is participating.
124     *
125     * @author Gaston Dombiak
126     */
127    public static class ChatInfo {
128
129        private final String sessionID;
130        private final String userID;
131        private final Date date;
132        private final String email;
133        private final String username;
134        private final String question;
135
136        public ChatInfo(String sessionID, String userID, Date date, String email, String username, String question) {
137            this.sessionID = sessionID;
138            this.userID = userID;
139            this.date = date;
140            this.email = email;
141            this.username = username;
142            this.question = question;
143        }
144
145        /**
146         * Returns the sessionID associated to this chat. Each chat will have a unique sessionID
147         * that could be used for retrieving the whole transcript of the conversation.
148         *
149         * @return the sessionID associated to this chat.
150         */
151        public String getSessionID() {
152            return sessionID;
153        }
154
155        /**
156         * Returns the user unique identification of the user that made the initial request and
157         * for which this chat was generated. If the user joined using an anonymous connection
158         * then the userID will be the value of the ID attribute of the USER element. Otherwise,
159         * the userID will be the bare JID of the user that made the request.
160         *
161         * @return the user unique identification of the user that made the initial request.
162         */
163        public String getUserID() {
164            return userID;
165        }
166
167        /**
168         * Returns the date when this agent joined the chat.
169         *
170         * @return the date when this agent joined the chat.
171         */
172        public Date getDate() {
173            return date;
174        }
175
176        /**
177         * Returns the email address associated with the user.
178         *
179         * @return the email address associated with the user.
180         */
181        public String getEmail() {
182            return email;
183        }
184
185        /**
186         * Returns the username(nickname) associated with the user.
187         *
188         * @return the username associated with the user.
189         */
190        public String getUsername() {
191            return username;
192        }
193
194        /**
195         * Returns the question the user asked.
196         *
197         * @return the question the user asked, if any.
198         */
199        public String getQuestion() {
200            return question;
201        }
202
203        public String toXML() {
204            StringBuilder buf = new StringBuilder();
205
206            buf.append("<chat ");
207            if (sessionID != null) {
208                buf.append(" sessionID=\"").append(sessionID).append('"');
209            }
210            if (userID != null) {
211                buf.append(" userID=\"").append(userID).append('"');
212            }
213            if (date != null) {
214                buf.append(" startTime=\"");
215                synchronized (UTC_FORMAT) {
216                    buf.append(UTC_FORMAT.format(date));
217                }
218                buf.append('"');
219            }
220            if (email != null) {
221                buf.append(" email=\"").append(email).append('"');
222            }
223            if (username != null) {
224                buf.append(" username=\"").append(username).append('"');
225            }
226            if (question != null) {
227                buf.append(" question=\"").append(question).append('"');
228            }
229            buf.append("/>");
230
231            return buf.toString();
232        }
233    }
234
235    /**
236     * Stanza extension provider for AgentStatus packets.
237     */
238    public static class Provider extends ExtensionElementProvider<AgentStatus> {
239
240        @Override
241        public AgentStatus parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
242            AgentStatus agentStatus = new AgentStatus();
243
244            agentStatus.workgroupJID = ParserUtils.getBareJidAttribute(parser);
245
246            boolean done = false;
247            while (!done) {
248                XmlPullParser.Event eventType = parser.next();
249
250                if (eventType == XmlPullParser.Event.START_ELEMENT) {
251                    if ("chat".equals(parser.getName())) {
252                        agentStatus.currentChats.add(parseChatInfo(parser));
253                    }
254                    else if ("max-chats".equals(parser.getName())) {
255                        agentStatus.maxChats = Integer.parseInt(parser.nextText());
256                    }
257                }
258                else if (eventType == XmlPullParser.Event.END_ELEMENT &&
259                    ELEMENT_NAME.equals(parser.getName())) {
260                    done = true;
261                }
262            }
263            return agentStatus;
264        }
265
266        private static ChatInfo parseChatInfo(XmlPullParser parser) {
267
268            String sessionID = parser.getAttributeValue("", "sessionID");
269            String userID = parser.getAttributeValue("", "userID");
270            Date date = null;
271            try {
272                synchronized (UTC_FORMAT) {
273                    date = UTC_FORMAT.parse(parser.getAttributeValue("", "startTime"));
274                }
275            }
276            catch (ParseException e) {
277            }
278
279            String email = parser.getAttributeValue("", "email");
280            String username = parser.getAttributeValue("", "username");
281            String question = parser.getAttributeValue("", "question");
282
283            return new ChatInfo(sessionID, userID, date, email, username, question);
284        }
285    }
286}