Transcripts.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.packet;

  18. import java.text.SimpleDateFormat;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.TimeZone;

  24. import org.jivesoftware.smack.packet.IQ;
  25. import org.jxmpp.jid.Jid;

  26. /**
  27.  * Represents a list of conversation transcripts that a user had in all his history. Each
  28.  * transcript summary includes the sessionID which may be used for getting more detailed
  29.  * information about the conversation. {@link org.jivesoftware.smackx.workgroup.packet.Transcript}
  30.  *
  31.  * @author Gaston Dombiak
  32.  */
  33. public class Transcripts extends IQ {

  34.     private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
  35.     static {
  36.         UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
  37.     }

  38.     private Jid userID;
  39.     private List<Transcripts.TranscriptSummary> summaries;


  40.     /**
  41.      * Creates a transcripts request for the given userID.
  42.      *
  43.      * @param userID the id of the user to get his conversations transcripts.
  44.      */
  45.     public Transcripts(Jid userID) {
  46.         this(userID, new ArrayList<Transcripts.TranscriptSummary>());
  47.     }

  48.     /**
  49.      * Creates a Transcripts which will contain the transcript summaries of the given user.
  50.      *
  51.      * @param userID the id of the user. Could be a real JID or a unique String that identifies
  52.      *        anonymous users.
  53.      * @param summaries the list of TranscriptSummaries.
  54.      */
  55.     public Transcripts(Jid userID, List<Transcripts.TranscriptSummary> summaries) {
  56.         super("transcripts", "http://jabber.org/protocol/workgroup");
  57.         this.userID = userID;
  58.         this.summaries = summaries;
  59.     }

  60.     /**
  61.      * Returns the id of the user that was involved in the conversations. The userID could be a
  62.      * real JID if the connected user was not anonymous. Otherwise, the userID will be a String
  63.      * that was provided by the anonymous user as a way to idenitify the user across many user
  64.      * sessions.
  65.      *
  66.      * @return the id of the user that was involved in the conversations.
  67.      */
  68.     public Jid getUserID() {
  69.         return userID;
  70.     }

  71.     /**
  72.      * Returns a list of TranscriptSummary. A TranscriptSummary does not contain the conversation
  73.      * transcript but some summary information like the sessionID and the time when the
  74.      * conversation started and finished. Once you have the sessionID it is possible to get the
  75.      * full conversation transcript.
  76.      *
  77.      * @return a list of TranscriptSummary.
  78.      */
  79.     public List<Transcripts.TranscriptSummary> getSummaries() {
  80.         return Collections.unmodifiableList(summaries);
  81.     }

  82.     @Override
  83.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  84.         buf.append(" userID=\"")
  85.                 .append(userID)
  86.                 .append("\">");

  87.         for (TranscriptSummary transcriptSummary : summaries) {
  88.             buf.append(transcriptSummary.toXML());
  89.         }

  90.         return buf;
  91.     }

  92.     /**
  93.      * A TranscriptSummary contains some information about a conversation such as the ID of the
  94.      * session or the date when the conversation started and finished. You will need to use the
  95.      * sessionID to get the full conversation transcript.
  96.      */
  97.     public static class TranscriptSummary {
  98.         private String sessionID;
  99.         private Date joinTime;
  100.         private Date leftTime;
  101.         private List<AgentDetail> agentDetails;

  102.         public TranscriptSummary(String sessionID, Date joinTime, Date leftTime, List<AgentDetail> agentDetails) {
  103.             this.sessionID = sessionID;
  104.             this.joinTime = joinTime;
  105.             this.leftTime = leftTime;
  106.             this.agentDetails = agentDetails;
  107.         }

  108.         /**
  109.          * Returns the ID of the session that is related to this conversation transcript. The
  110.          * sessionID could be used for getting the full conversation transcript.
  111.          *
  112.          * @return the ID of the session that is related to this conversation transcript.
  113.          */
  114.         public String getSessionID() {
  115.             return sessionID;
  116.         }

  117.         /**
  118.          * Returns the Date when the conversation started.
  119.          *
  120.          * @return the Date when the conversation started.
  121.          */
  122.         public Date getJoinTime() {
  123.             return joinTime;
  124.         }

  125.         /**
  126.          * Returns the Date when the conversation finished.
  127.          *
  128.          * @return the Date when the conversation finished.
  129.          */
  130.         public Date getLeftTime() {
  131.             return leftTime;
  132.         }

  133.         /**
  134.          * Returns a list of AgentDetails. For each Agent that was involved in the conversation
  135.          * the list will include an AgentDetail. An AgentDetail contains the JID of the agent
  136.          * as well as the time when the Agent joined and left the conversation.
  137.          *
  138.          * @return a list of AgentDetails.
  139.          */
  140.         public List<AgentDetail> getAgentDetails() {
  141.             return agentDetails;
  142.         }

  143.         public String toXML() {
  144.             StringBuilder buf = new StringBuilder();

  145.             buf.append("<transcript sessionID=\"")
  146.                     .append(sessionID)
  147.                     .append("\">");

  148.             if (joinTime != null) {
  149.                 buf.append("<joinTime>").append(UTC_FORMAT.format(joinTime)).append("</joinTime>");
  150.             }
  151.             if (leftTime != null) {
  152.                 buf.append("<leftTime>").append(UTC_FORMAT.format(leftTime)).append("</leftTime>");
  153.             }
  154.             buf.append("<agents>");
  155.             for (AgentDetail agentDetail : agentDetails) {
  156.                 buf.append(agentDetail.toXML());
  157.             }
  158.             buf.append("</agents></transcript>");

  159.             return buf.toString();
  160.         }
  161.     }

  162.     /**
  163.      * An AgentDetail contains information of an Agent that was involved in a conversation.
  164.      */
  165.     public static class AgentDetail {
  166.         private String agentJID;
  167.         private Date joinTime;
  168.         private Date leftTime;

  169.         public AgentDetail(String agentJID, Date joinTime, Date leftTime) {
  170.             this.agentJID = agentJID;
  171.             this.joinTime = joinTime;
  172.             this.leftTime = leftTime;
  173.         }

  174.         /**
  175.          * Returns the bare JID of the Agent that was involved in the conversation.
  176.          *
  177.          * @return the bared JID of the Agent that was involved in the conversation.
  178.          */
  179.         public String getAgentJID() {
  180.             return agentJID;
  181.         }

  182.         /**
  183.          * Returns the Date when the Agent joined the conversation.
  184.          *
  185.          * @return the Date when the Agent joined the conversation.
  186.          */
  187.         public Date getJoinTime() {
  188.             return joinTime;
  189.         }

  190.         /**
  191.          * Returns the Date when the Agent left the conversation.
  192.          *
  193.          * @return the Date when the Agent left the conversation.
  194.          */
  195.         public Date getLeftTime() {
  196.             return leftTime;
  197.         }

  198.         public String toXML() {
  199.             StringBuilder buf = new StringBuilder();

  200.             buf.append("<agent>");

  201.             if (agentJID != null) {
  202.                 buf.append("<agentJID>").append(agentJID).append("</agentJID>");
  203.             }
  204.             if (joinTime != null) {
  205.                 buf.append("<joinTime>").append(UTC_FORMAT.format(joinTime)).append("</joinTime>");
  206.             }
  207.             if (leftTime != null) {
  208.                 buf.append("<leftTime>").append(UTC_FORMAT.format(leftTime)).append("</leftTime>");
  209.             }
  210.             buf.append("</agent>");

  211.             return buf.toString();
  212.         }
  213.     }
  214. }