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