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 org.jivesoftware.smack.packet.IQ;
021
022import java.text.SimpleDateFormat;
023import java.util.*;
024
025/**
026 * Represents a list of conversation transcripts that a user had in all his history. Each
027 * transcript summary includes the sessionID which may be used for getting more detailed
028 * information about the conversation. {@link org.jivesoftware.smackx.workgroup.packet.Transcript}
029 *
030 * @author Gaston Dombiak
031 */
032public class Transcripts extends IQ {
033
034    private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
035    static {
036        UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
037    }
038
039    private String userID;
040    private List<Transcripts.TranscriptSummary> summaries;
041
042
043    /**
044     * Creates a transcripts request for the given userID.
045     *
046     * @param userID the id of the user to get his conversations transcripts.
047     */
048    public Transcripts(String userID) {
049        this.userID = userID;
050        this.summaries = new ArrayList<Transcripts.TranscriptSummary>();
051    }
052
053    /**
054     * Creates a Transcripts which will contain the transcript summaries of the given user.
055     *
056     * @param userID the id of the user. Could be a real JID or a unique String that identifies
057     *        anonymous users.
058     * @param summaries the list of TranscriptSummaries.
059     */
060    public Transcripts(String userID, List<Transcripts.TranscriptSummary> summaries) {
061        this.userID = userID;
062        this.summaries = summaries;
063    }
064
065    /**
066     * Returns the id of the user that was involved in the conversations. The userID could be a
067     * real JID if the connected user was not anonymous. Otherwise, the userID will be a String
068     * that was provided by the anonymous user as a way to idenitify the user across many user
069     * sessions.
070     *
071     * @return the id of the user that was involved in the conversations.
072     */
073    public String getUserID() {
074        return userID;
075    }
076
077    /**
078     * Returns a list of TranscriptSummary. A TranscriptSummary does not contain the conversation
079     * transcript but some summary information like the sessionID and the time when the
080     * conversation started and finished. Once you have the sessionID it is possible to get the
081     * full conversation transcript.
082     *
083     * @return a list of TranscriptSummary.
084     */
085    public List<Transcripts.TranscriptSummary> getSummaries() {
086        return Collections.unmodifiableList(summaries);
087    }
088
089    public String getChildElementXML() {
090        StringBuilder buf = new StringBuilder();
091
092        buf.append("<transcripts xmlns=\"http://jivesoftware.com/protocol/workgroup\" userID=\"")
093                .append(userID)
094                .append("\">");
095
096        for (TranscriptSummary transcriptSummary : summaries) {
097            buf.append(transcriptSummary.toXML());
098        }
099
100        buf.append("</transcripts>");
101
102        return buf.toString();
103    }
104
105    /**
106     * A TranscriptSummary contains some information about a conversation such as the ID of the
107     * session or the date when the conversation started and finished. You will need to use the
108     * sessionID to get the full conversation transcript.
109     */
110    public static class TranscriptSummary {
111        private String sessionID;
112        private Date joinTime;
113        private Date leftTime;
114        private List<AgentDetail> agentDetails;
115
116        public TranscriptSummary(String sessionID, Date joinTime, Date leftTime, List<AgentDetail> agentDetails) {
117            this.sessionID = sessionID;
118            this.joinTime = joinTime;
119            this.leftTime = leftTime;
120            this.agentDetails = agentDetails;
121        }
122
123        /**
124         * Returns the ID of the session that is related to this conversation transcript. The
125         * sessionID could be used for getting the full conversation transcript.
126         *
127         * @return the ID of the session that is related to this conversation transcript.
128         */
129        public String getSessionID() {
130            return sessionID;
131        }
132
133        /**
134         * Returns the Date when the conversation started.
135         *
136         * @return the Date when the conversation started.
137         */
138        public Date getJoinTime() {
139            return joinTime;
140        }
141
142        /**
143         * Returns the Date when the conversation finished.
144         *
145         * @return the Date when the conversation finished.
146         */
147        public Date getLeftTime() {
148            return leftTime;
149        }
150
151        /**
152         * Returns a list of AgentDetails. For each Agent that was involved in the conversation
153         * the list will include an AgentDetail. An AgentDetail contains the JID of the agent
154         * as well as the time when the Agent joined and left the conversation.
155         *
156         * @return a list of AgentDetails.
157         */
158        public List<AgentDetail> getAgentDetails() {
159            return agentDetails;
160        }
161
162        public String toXML() {
163            StringBuilder buf = new StringBuilder();
164
165            buf.append("<transcript sessionID=\"")
166                    .append(sessionID)
167                    .append("\">");
168
169            if (joinTime != null) {
170                buf.append("<joinTime>").append(UTC_FORMAT.format(joinTime)).append("</joinTime>");
171            }
172            if (leftTime != null) {
173                buf.append("<leftTime>").append(UTC_FORMAT.format(leftTime)).append("</leftTime>");
174            }
175            buf.append("<agents>");
176            for (AgentDetail agentDetail : agentDetails) {
177                buf.append(agentDetail.toXML());
178            }
179            buf.append("</agents></transcript>");
180
181            return buf.toString();
182        }
183    }
184
185    /**
186     * An AgentDetail contains information of an Agent that was involved in a conversation. 
187     */
188    public static class AgentDetail {
189        private String agentJID;
190        private Date joinTime;
191        private Date leftTime;
192
193        public AgentDetail(String agentJID, Date joinTime, Date leftTime) {
194            this.agentJID = agentJID;
195            this.joinTime = joinTime;
196            this.leftTime = leftTime;
197        }
198
199        /**
200         * Returns the bare JID of the Agent that was involved in the conversation.
201         *
202         * @return the bared JID of the Agent that was involved in the conversation.
203         */
204        public String getAgentJID() {
205            return agentJID;
206        }
207
208        /**
209         * Returns the Date when the Agent joined the conversation.
210         *
211         * @return the Date when the Agent joined the conversation.
212         */
213        public Date getJoinTime() {
214            return joinTime;
215        }
216
217        /**
218         * Returns the Date when the Agent left the conversation.
219         *
220         * @return the Date when the Agent left the conversation.
221         */
222        public Date getLeftTime() {
223            return leftTime;
224        }
225
226        public String toXML() {
227            StringBuilder buf = new StringBuilder();
228
229            buf.append("<agent>");
230
231            if (agentJID != null) {
232                buf.append("<agentJID>").append(agentJID).append("</agentJID>");
233            }
234            if (joinTime != null) {
235                buf.append("<joinTime>").append(UTC_FORMAT.format(joinTime)).append("</joinTime>");
236            }
237            if (leftTime != null) {
238                buf.append("<leftTime>").append(UTC_FORMAT.format(leftTime)).append("</leftTime>");
239            }
240            buf.append("</agent>");
241
242            return buf.toString();
243        }
244    }
245}