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