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