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