Home | History | Annotate | Download | only in packet
      1 /**
      2  * $Revision$
      3  * $Date$
      4  *
      5  * Copyright 2003-2007 Jive Software.
      6  *
      7  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *     http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  */
     19 
     20 package org.jivesoftware.smackx.workgroup.packet;
     21 
     22 import org.jivesoftware.smack.packet.IQ;
     23 
     24 import java.text.SimpleDateFormat;
     25 import java.util.*;
     26 
     27 /**
     28  * Represents a list of conversation transcripts that a user had in all his history. Each
     29  * transcript summary includes the sessionID which may be used for getting more detailed
     30  * information about the conversation. {@link org.jivesoftware.smackx.workgroup.packet.Transcript}
     31  *
     32  * @author Gaston Dombiak
     33  */
     34 public class Transcripts extends IQ {
     35 
     36     private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
     37     static {
     38         UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
     39     }
     40 
     41     private String userID;
     42     private List<Transcripts.TranscriptSummary> summaries;
     43 
     44 
     45     /**
     46      * Creates a transcripts request for the given userID.
     47      *
     48      * @param userID the id of the user to get his conversations transcripts.
     49      */
     50     public Transcripts(String userID) {
     51         this.userID = userID;
     52         this.summaries = new ArrayList<Transcripts.TranscriptSummary>();
     53     }
     54 
     55     /**
     56      * Creates a Transcripts which will contain the transcript summaries of the given user.
     57      *
     58      * @param userID the id of the user. Could be a real JID or a unique String that identifies
     59      *        anonymous users.
     60      * @param summaries the list of TranscriptSummaries.
     61      */
     62     public Transcripts(String userID, List<Transcripts.TranscriptSummary> summaries) {
     63         this.userID = userID;
     64         this.summaries = summaries;
     65     }
     66 
     67     /**
     68      * Returns the id of the user that was involved in the conversations. The userID could be a
     69      * real JID if the connected user was not anonymous. Otherwise, the userID will be a String
     70      * that was provided by the anonymous user as a way to idenitify the user across many user
     71      * sessions.
     72      *
     73      * @return the id of the user that was involved in the conversations.
     74      */
     75     public String getUserID() {
     76         return userID;
     77     }
     78 
     79     /**
     80      * Returns a list of TranscriptSummary. A TranscriptSummary does not contain the conversation
     81      * transcript but some summary information like the sessionID and the time when the
     82      * conversation started and finished. Once you have the sessionID it is possible to get the
     83      * full conversation transcript.
     84      *
     85      * @return a list of TranscriptSummary.
     86      */
     87     public List<Transcripts.TranscriptSummary> getSummaries() {
     88         return Collections.unmodifiableList(summaries);
     89     }
     90 
     91     public String getChildElementXML() {
     92         StringBuilder buf = new StringBuilder();
     93 
     94         buf.append("<transcripts xmlns=\"http://jivesoftware.com/protocol/workgroup\" userID=\"")
     95                 .append(userID)
     96                 .append("\">");
     97 
     98         for (TranscriptSummary transcriptSummary : summaries) {
     99             buf.append(transcriptSummary.toXML());
    100         }
    101 
    102         buf.append("</transcripts>");
    103 
    104         return buf.toString();
    105     }
    106 
    107     /**
    108      * A TranscriptSummary contains some information about a conversation such as the ID of the
    109      * session or the date when the conversation started and finished. You will need to use the
    110      * sessionID to get the full conversation transcript.
    111      */
    112     public static class TranscriptSummary {
    113         private String sessionID;
    114         private Date joinTime;
    115         private Date leftTime;
    116         private List<AgentDetail> agentDetails;
    117 
    118         public TranscriptSummary(String sessionID, Date joinTime, Date leftTime, List<AgentDetail> agentDetails) {
    119             this.sessionID = sessionID;
    120             this.joinTime = joinTime;
    121             this.leftTime = leftTime;
    122             this.agentDetails = agentDetails;
    123         }
    124 
    125         /**
    126          * Returns the ID of the session that is related to this conversation transcript. The
    127          * sessionID could be used for getting the full conversation transcript.
    128          *
    129          * @return the ID of the session that is related to this conversation transcript.
    130          */
    131         public String getSessionID() {
    132             return sessionID;
    133         }
    134 
    135         /**
    136          * Returns the Date when the conversation started.
    137          *
    138          * @return the Date when the conversation started.
    139          */
    140         public Date getJoinTime() {
    141             return joinTime;
    142         }
    143 
    144         /**
    145          * Returns the Date when the conversation finished.
    146          *
    147          * @return the Date when the conversation finished.
    148          */
    149         public Date getLeftTime() {
    150             return leftTime;
    151         }
    152 
    153         /**
    154          * Returns a list of AgentDetails. For each Agent that was involved in the conversation
    155          * the list will include an AgentDetail. An AgentDetail contains the JID of the agent
    156          * as well as the time when the Agent joined and left the conversation.
    157          *
    158          * @return a list of AgentDetails.
    159          */
    160         public List<AgentDetail> getAgentDetails() {
    161             return agentDetails;
    162         }
    163 
    164         public String toXML() {
    165             StringBuilder buf = new StringBuilder();
    166 
    167             buf.append("<transcript sessionID=\"")
    168                     .append(sessionID)
    169                     .append("\">");
    170 
    171             if (joinTime != null) {
    172                 buf.append("<joinTime>").append(UTC_FORMAT.format(joinTime)).append("</joinTime>");
    173             }
    174             if (leftTime != null) {
    175                 buf.append("<leftTime>").append(UTC_FORMAT.format(leftTime)).append("</leftTime>");
    176             }
    177             buf.append("<agents>");
    178             for (AgentDetail agentDetail : agentDetails) {
    179                 buf.append(agentDetail.toXML());
    180             }
    181             buf.append("</agents></transcript>");
    182 
    183             return buf.toString();
    184         }
    185     }
    186 
    187     /**
    188      * An AgentDetail contains information of an Agent that was involved in a conversation.
    189      */
    190     public static class AgentDetail {
    191         private String agentJID;
    192         private Date joinTime;
    193         private Date leftTime;
    194 
    195         public AgentDetail(String agentJID, Date joinTime, Date leftTime) {
    196             this.agentJID = agentJID;
    197             this.joinTime = joinTime;
    198             this.leftTime = leftTime;
    199         }
    200 
    201         /**
    202          * Returns the bare JID of the Agent that was involved in the conversation.
    203          *
    204          * @return the bared JID of the Agent that was involved in the conversation.
    205          */
    206         public String getAgentJID() {
    207             return agentJID;
    208         }
    209 
    210         /**
    211          * Returns the Date when the Agent joined the conversation.
    212          *
    213          * @return the Date when the Agent joined the conversation.
    214          */
    215         public Date getJoinTime() {
    216             return joinTime;
    217         }
    218 
    219         /**
    220          * Returns the Date when the Agent left the conversation.
    221          *
    222          * @return the Date when the Agent left the conversation.
    223          */
    224         public Date getLeftTime() {
    225             return leftTime;
    226         }
    227 
    228         public String toXML() {
    229             StringBuilder buf = new StringBuilder();
    230 
    231             buf.append("<agent>");
    232 
    233             if (agentJID != null) {
    234                 buf.append("<agentJID>").append(agentJID).append("</agentJID>");
    235             }
    236             if (joinTime != null) {
    237                 buf.append("<joinTime>").append(UTC_FORMAT.format(joinTime)).append("</joinTime>");
    238             }
    239             if (leftTime != null) {
    240                 buf.append("<leftTime>").append(UTC_FORMAT.format(leftTime)).append("</leftTime>");
    241             }
    242             buf.append("</agent>");
    243 
    244             return buf.toString();
    245         }
    246     }
    247 }
    248