private int getChatDuration(Date start, ChatSession session) {
    long startTime = start.getTime();
    long end = startTime;
    List<AgentChatSession> agents = session.getAgentList();
    for (AgentChatSession chatSession : agents) {
      if (end < chatSession.getEndTime()) {
        end = chatSession.getEndTime();
      }
    }

    return (int) ((end - startTime) / 1000 / 60);
  }
  public void chatSupportFinished(Workgroup workgroup, String sessionID) {
    Log.debug("Chat Support Finished, sending transcripts");

    final EmailService emailService = EmailService.getInstance();

    String property = JiveGlobals.getProperty("mail.configured");
    if (!ModelUtil.hasLength(property)) {
      Log.debug("Mail settings are not configured, transcripts will not be sent.");
      return;
    }

    final ChatSession chatSession = ChatTranscriptManager.getChatSession(sessionID);
    if (chatSession == null || chatSession.getFirstSession() == null) {
      return;
    }

    final StringBuilder builder = new StringBuilder();

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyy hh:mm a");

    // Get duration of conversation
    Date date = new Date(chatSession.getFirstSession().getStartTime());
    int duration = getChatDuration(date, chatSession);

    TreeMap<String, List<String>> map =
        new TreeMap<String, List<String>>(chatSession.getMetadata());

    String body = JiveGlobals.getProperty("chat.transcript.body");

    if (ModelUtil.hasLength(body)) {
      builder.append(body).append("\n\n");
    }

    builder.append("formname=chat transcript\n");
    extractAndDisplay(builder, "question", map);
    display(builder, "fullname", chatSession.getCustomerName());
    extractAndDisplay(builder, "email", map);
    extractAndDisplay(builder, "Location", map);
    extractAndDisplay(builder, "userID", map);
    extractAndDisplay(builder, "username", map);
    extractAndDisplay(builder, "workgroup", map);
    display(builder, "chatduration", String.valueOf(duration));
    display(builder, "chatdate", formatter.format(date));
    if (chatSession.getFirstSession() != null
        && chatSession.getFirstSession().getAgentJID() != null) {
      try {
        display(builder, "agent", new JID(chatSession.getFirstSession().getAgentJID()).toBareJID());
      } catch (Exception e) {
        Log.debug("Could not display agent in transcript.", e);
      }
    }
    for (Iterator<Map.Entry<String, List<String>>> iterator = map.entrySet().iterator();
        iterator.hasNext(); ) {
      Map.Entry<String, List<String>> entry = iterator.next();
      display(builder, entry.getKey(), getListItem(entry.getValue()));
    }
    builder.append("ctranscript=\n");
    builder.append(ChatTranscriptManager.getTextTranscriptFromSessionID(sessionID));

    String subject = JiveGlobals.getProperty("chat.transcript.subject");
    String from = JiveGlobals.getProperty("chat.transcript.from");
    String to = JiveGlobals.getProperty("chat.transcript.to");

    if (!ModelUtil.hasLength(subject) || !ModelUtil.hasLength(from)) {
      Log.debug(
          "Transcript settings (chat.transcript.subject, chat.transcript.from) are not configured, "
              + "transcripts will not be sent.");
      return;
    }

    if (ModelUtil.hasLength(to)) {
      emailService.sendMessage(
          "Chat Transcript", to, "Chat Transcript", from, subject, builder.toString(), null);
      Log.debug("Transcript sent to " + to);
    }

    // NOTE: Do not sent to the customer. They will receive a prompt for a seperate chat transcript
    // that does not contain agent information.

    // Send to Agents
    UserManager um = UserManager.getInstance();
    for (Iterator<AgentChatSession> iterator = chatSession.getAgents(); iterator.hasNext(); ) {
      AgentChatSession agentSession = iterator.next();
      try {
        User user = um.getUser(new JID(agentSession.getAgentJID()).getNode());
        emailService.sendMessage(
            "Chat Transcript",
            user.getEmail(),
            "Chat Transcript",
            from,
            subject,
            builder.toString(),
            null);
        Log.debug("Transcript sent to agent " + agentSession.getAgentJID());
      } catch (UserNotFoundException e) {
        Log.error(
            "Email Transcript Not Sent:"
                + "Could not load agent user object for jid "
                + agentSession.getAgentJID());
      }
    }
  }