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    @Override
084    public String getElementName() {
085        return ELEMENT_NAME;
086    }
087
088    @Override
089    public String getNamespace() {
090        return NAMESPACE;
091    }
092
093    @Override
094    public String toXML() {
095        StringBuilder buf = new StringBuilder();
096
097        buf.append('<').append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append('"');
098        if (workgroupJID != null) {
099            buf.append(" jid=\"").append(workgroupJID).append('"');
100        }
101        buf.append('>');
102        if (maxChats != -1) {
103            buf.append("<max-chats>").append(maxChats).append("</max-chats>");
104        }
105        if (!currentChats.isEmpty()) {
106            buf.append("<current-chats xmlns= \"http://jivesoftware.com/protocol/workgroup\">");
107            for (Iterator<ChatInfo> it = currentChats.iterator(); it.hasNext();) {
108                buf.append(it.next().toXML());
109            }
110            buf.append("</current-chats>");
111        }
112        buf.append("</").append(this.getElementName()).append("> ");
113
114        return buf.toString();
115    }
116
117    /**
118     * Represents information about a Chat where this Agent is participating.
119     *
120     * @author Gaston Dombiak
121     */
122    public static class ChatInfo {
123
124        private String sessionID;
125        private String userID;
126        private Date date;
127        private String email;
128        private String username;
129        private String question;
130
131        public ChatInfo(String sessionID, String userID, Date date, String email, String username, String question) {
132            this.sessionID = sessionID;
133            this.userID = userID;
134            this.date = date;
135            this.email = email;
136            this.username = username;
137            this.question = question;
138        }
139
140        /**
141         * Returns the sessionID associated to this chat. Each chat will have a unique sessionID
142         * that could be used for retrieving the whole transcript of the conversation.
143         *
144         * @return the sessionID associated to this chat.
145         */
146        public String getSessionID() {
147            return sessionID;
148        }
149
150        /**
151         * Returns the user unique identification of the user that made the initial request and
152         * for which this chat was generated. If the user joined using an anonymous connection
153         * then the userID will be the value of the ID attribute of the USER element. Otherwise,
154         * the userID will be the bare JID of the user that made the request.
155         *
156         * @return the user unique identification of the user that made the initial request.
157         */
158        public String getUserID() {
159            return userID;
160        }
161
162        /**
163         * Returns the date when this agent joined the chat.
164         *
165         * @return the date when this agent joined the chat.
166         */
167        public Date getDate() {
168            return date;
169        }
170
171        /**
172         * Returns the email address associated with the user.
173         *
174         * @return the email address associated with the user.
175         */
176        public String getEmail() {
177            return email;
178        }
179
180        /**
181         * Returns the username(nickname) associated with the user.
182         *
183         * @return the username associated with the user.
184         */
185        public String getUsername() {
186            return username;
187        }
188
189        /**
190         * Returns the question the user asked.
191         *
192         * @return the question the user asked, if any.
193         */
194        public String getQuestion() {
195            return question;
196        }
197
198        public String toXML() {
199            StringBuilder buf = new StringBuilder();
200
201            buf.append("<chat ");
202            if (sessionID != null) {
203                buf.append(" sessionID=\"").append(sessionID).append('"');
204            }
205            if (userID != null) {
206                buf.append(" userID=\"").append(userID).append('"');
207            }
208            if (date != null) {
209                buf.append(" startTime=\"").append(UTC_FORMAT.format(date)).append('"');
210            }
211            if (email != null) {
212                buf.append(" email=\"").append(email).append('"');
213            }
214            if (username != null) {
215                buf.append(" username=\"").append(username).append('"');
216            }
217            if (question != null) {
218                buf.append(" question=\"").append(question).append('"');
219            }
220            buf.append("/>");
221
222            return buf.toString();
223        }
224    }
225
226    /**
227     * Stanza(/Packet) extension provider for AgentStatus packets.
228     */
229    public static class Provider extends ExtensionElementProvider<AgentStatus> {
230
231        @Override
232        public AgentStatus parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
233            AgentStatus agentStatus = new AgentStatus();
234
235            agentStatus.workgroupJID = parser.getAttributeValue("", "jid");
236
237            boolean done = false;
238            while (!done) {
239                int eventType = parser.next();
240
241                if (eventType == XmlPullParser.START_TAG) {
242                    if ("chat".equals(parser.getName())) {
243                        agentStatus.currentChats.add(parseChatInfo(parser));
244                    }
245                    else if ("max-chats".equals(parser.getName())) {
246                        agentStatus.maxChats = Integer.parseInt(parser.nextText());
247                    }
248                }
249                else if (eventType == XmlPullParser.END_TAG &&
250                    ELEMENT_NAME.equals(parser.getName())) {
251                    done = true;
252                }
253            }
254            return agentStatus;
255        }
256
257        private static ChatInfo parseChatInfo(XmlPullParser parser) {
258
259            String sessionID = parser.getAttributeValue("", "sessionID");
260            String userID = parser.getAttributeValue("", "userID");
261            Date date = null;
262            try {
263                date = UTC_FORMAT.parse(parser.getAttributeValue("", "startTime"));
264            }
265            catch (ParseException e) {
266            }
267
268            String email = parser.getAttributeValue("", "email");
269            String username = parser.getAttributeValue("", "username");
270            String question = parser.getAttributeValue("", "question");
271
272            return new ChatInfo(sessionID, userID, date, email, username, question);
273        }
274    }
275}