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.     @SuppressWarnings("DateFormatConstant")
  35.     private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
  36.     static {
  37.         UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
  38.     }

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


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

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

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

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

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

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

  91.         return buf;
  92.     }

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

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

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

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

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

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

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

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

  149.             if (joinTime != null) {
  150.                 buf.append("<joinTime>");
  151.                 synchronized (UTC_FORMAT) {
  152.                     buf.append(UTC_FORMAT.format(joinTime));
  153.                 }
  154.                 buf.append("</joinTime>");
  155.             }
  156.             if (leftTime != null) {
  157.                 buf.append("<leftTime>");
  158.                 synchronized (UTC_FORMAT) {
  159.                     buf.append(UTC_FORMAT.format(leftTime));
  160.                 }
  161.                 buf.append("</leftTime>");
  162.             }
  163.             buf.append("<agents>");
  164.             for (AgentDetail agentDetail : agentDetails) {
  165.                 buf.append(agentDetail.toXML());
  166.             }
  167.             buf.append("</agents></transcript>");

  168.             return buf.toString();
  169.         }
  170.     }

  171.     /**
  172.      * An AgentDetail contains information of an Agent that was involved in a conversation.
  173.      */
  174.     public static class AgentDetail {
  175.         private final String agentJID;
  176.         private final Date joinTime;
  177.         private final Date leftTime;

  178.         public AgentDetail(String agentJID, Date joinTime, Date leftTime) {
  179.             this.agentJID = agentJID;
  180.             this.joinTime = joinTime;
  181.             this.leftTime = leftTime;
  182.         }

  183.         /**
  184.          * Returns the bare JID of the Agent that was involved in the conversation.
  185.          *
  186.          * @return the bared JID of the Agent that was involved in the conversation.
  187.          */
  188.         public String getAgentJID() {
  189.             return agentJID;
  190.         }

  191.         /**
  192.          * Returns the Date when the Agent joined the conversation.
  193.          *
  194.          * @return the Date when the Agent joined the conversation.
  195.          */
  196.         public Date getJoinTime() {
  197.             return joinTime;
  198.         }

  199.         /**
  200.          * Returns the Date when the Agent left the conversation.
  201.          *
  202.          * @return the Date when the Agent left the conversation.
  203.          */
  204.         public Date getLeftTime() {
  205.             return leftTime;
  206.         }

  207.         public String toXML() {
  208.             StringBuilder buf = new StringBuilder();

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

  210.             if (agentJID != null) {
  211.                 buf.append("<agentJID>").append(agentJID).append("</agentJID>");
  212.             }
  213.             if (joinTime != null) {
  214.                 buf.append("<joinTime>");
  215.                 synchronized (UTC_FORMAT) {
  216.                     buf.append(UTC_FORMAT.format(joinTime));
  217.                 }
  218.                 buf.append("</joinTime>");
  219.             }
  220.             if (leftTime != null) {
  221.                 buf.append("<leftTime>");
  222.                 synchronized (UTC_FORMAT) {
  223.                     buf.append(UTC_FORMAT.format(leftTime));
  224.                 }
  225.                 buf.append("</leftTime>");
  226.             }
  227.             buf.append("</agent>");

  228.             return buf.toString();
  229.         }
  230.     }
  231. }