📄 chattranscriptmanager.java
字号:
/**
* $RCSfile$
* $Revision$
* $Date: 2006-08-07 21:12:21 -0700 (Mon, 07 Aug 2006) $
*
* Copyright (C) 2004-2008 Jive Software. All rights reserved.
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/
package org.jivesoftware.openfire.fastpath.history;
import org.jivesoftware.xmpp.workgroup.DbProperties;
import org.jivesoftware.xmpp.workgroup.Workgroup;
import org.jivesoftware.xmpp.workgroup.WorkgroupManager;
import org.jivesoftware.xmpp.workgroup.request.Request;
import org.jivesoftware.xmpp.workgroup.utils.ModelUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.EmailService;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.StringUtils;
import org.xmpp.component.ComponentManagerFactory;
import org.xmpp.packet.JID;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Utility class to control the update and retrieval of Chat Transcripts within the
* Fastpath system.
*
* @author Derek DeMoro
*/
public class ChatTranscriptManager {
private static final String GET_WORKGROUP_SESSIONS =
"SELECT sessionID, userID, startTime, endTime, queueWaitTime, state " +
"FROM fpSession WHERE workgroupID=? AND startTime>=? AND endTime<=?";
private static final String GET_SESSION_TRANSCRIPT =
"SELECT transcript FROM fpSession WHERE sessionID=?";
private static final String GET_SESSION =
"SELECT userID, workgroupID, transcript, startTime, endTime, queueWaitTime, state " +
"FROM fpSession WHERE sessionID=?";
private static final String GET_SESSION_AGENTS =
"SELECT agentJID, joinTime, leftTime FROM fpAgentSession WHERE sessionID=?";
private static final String GET_SESSION_META_DATA =
"SELECT metadataName, metadataValue FROM fpSessionMetadata WHERE sessionID=?";
private ChatTranscriptManager() {
}
/**
* Returns a collection of ChatSessions for a particular workgroup.
*
* @param workgroup the workgroup.
* @param start retrieve all ChatSessions at or after this specified date.
* @param end retrieve all ChatSessions before or on this specified date.
* @return a collection of ChatSessions.
*/
public static Collection<ChatSession> getChatSessionsForWorkgroup(Workgroup workgroup,
Date start, Date end)
{
final List<ChatSession> resultList = new ArrayList<ChatSession>();
long wgID = workgroup.getID();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_WORKGROUP_SESSIONS);
pstmt.setLong(1, wgID);
pstmt.setString(2, StringUtils.dateToMillis(start));
pstmt.setString(3, StringUtils.dateToMillis(end));
rs = pstmt.executeQuery();
while (rs.next()) {
String sessionID = rs.getString(1);
String userID = rs.getString(2);
String startTime = rs.getString(3);
String endTime = rs.getString(4);
long queueWaitTime = rs.getLong(5);
int state = rs.getInt(6);
ChatSession session = new ChatSession();
session.setSessionID(sessionID);
session.setUserID(userID);
session.setWorkgroupID(wgID);
if (startTime.trim().length() > 0) {
session.setStartTime(Long.parseLong(startTime));
}
if (endTime.trim().length() > 0) {
session.setEndTime(Long.parseLong(endTime));
}
session.setQueueWaitTime(queueWaitTime);
session.setState(state);
populateSessionWithMetadata(session);
populateSessionWithAgents(session);
resultList.add(session);
}
}
catch (Exception ex) {
ComponentManagerFactory.getComponentManager().getLog().error(ex);
}
finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
}
// Sort by date
Collections.sort(resultList, dateComparator);
return resultList;
}
/**
* Return the plain text version of a chat transcript.
*
* @param sessionID the sessionID of the <code>ChatSession</code>
* @return the plain text version of a chat transcript.
*/
public static String getTextTranscriptFromSessionID(String sessionID) {
String transcript = null;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_SESSION_TRANSCRIPT);
pstmt.setString(1, sessionID);
rs = pstmt.executeQuery();
if (rs.next()) {
transcript = DbConnectionManager.getLargeTextField(rs, 1);
}
}
catch (Exception ex) {
ComponentManagerFactory.getComponentManager().getLog().error(ex);
}
finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
}
if (transcript == null || "".equals(transcript)) {
return "";
}
// Define time zone used in the transcript.
SimpleDateFormat UTC_FORMAT = new SimpleDateFormat(JiveConstants.XMPP_DELAY_DATETIME_FORMAT);
UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
final SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
Document element = null;
try {
element = DocumentHelper.parseText(transcript);
}
catch (DocumentException e) {
ComponentManagerFactory.getComponentManager().getLog().error(e);
}
StringBuilder buf = new StringBuilder();
// Add the Messages and Presences contained in the retrieved transcript element
for (Iterator it = element.getRootElement().elementIterator(); it.hasNext();) {
Element packet = (Element)it.next();
String name = packet.getName();
String message = "";
String from = "";
if ("presence".equals(name)) {
String type = packet.attributeValue("type");
from = new JID(packet.attributeValue("from")).getResource();
if (type == null) {
message = from + " has joined the room";
}
else {
message = from + " has left the room";
}
}
else if ("message".equals(name)) {
from = new JID(packet.attributeValue("from")).getResource();
message = packet.elementText("body");
message = StringUtils.escapeHTMLTags(message);
}
List el = packet.elements("x");
Iterator iter = el.iterator();
while (iter.hasNext()) {
Element ele = (Element)iter.next();
if ("jabber:x:delay".equals(ele.getNamespaceURI())) {
String stamp = ele.attributeValue("stamp");
try {
String formattedDate;
synchronized (UTC_FORMAT) {
Date d = UTC_FORMAT.parse(stamp);
formattedDate = formatter.format(d);
}
if ("presence".equals(name)) {
buf.append("[").append(formattedDate).append("] ").append(message)
.append("\n");
}
else {
buf.append("[").append(formattedDate).append("] ").append(from)
.append(": ").append(message).append("\n");
}
}
catch (ParseException e) {
ComponentManagerFactory.getComponentManager().getLog().error(e);
}
}
}
}
return buf.toString();
}
/**
* Retrieves a <code>ChatSession</code> based on it's session ID.
*
* @param sessionID the session ID.
* @return the ChatSession or null if no ChatSession is found.
*/
public static ChatSession getChatSession(String sessionID) {
final ChatSession session = new ChatSession();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_SESSION);
pstmt.setString(1, sessionID);
rs = pstmt.executeQuery();
while (rs.next()) {
String userID = rs.getString(1);
long workgroupID = rs.getLong(2);
String transcript = DbConnectionManager.getLargeTextField(rs, 3);
String startTime = rs.getString(4);
String endTime = rs.getString(5);
long queueWaitTime = rs.getLong(6);
int state = rs.getInt(7);
session.setSessionID(sessionID);
session.setWorkgroupID(workgroupID);
session.setUserID(userID);
session.setTranscript(formatTranscript(transcript));
if (startTime.trim().length() > 0) {
session.setStartTime(Long.parseLong(startTime));
}
if (endTime.trim().length() > 0) {
session.setEndTime(Long.parseLong(endTime));
}
session.setQueueWaitTime(queueWaitTime);
session.setState(state);
if (startTime.trim().length() > 0 && endTime.trim().length() > 0) {
populateSessionWithMetadata(session);
populateSessionWithAgents(session);
}
}
}
catch (Exception ex) {
ComponentManagerFactory.getComponentManager().getLog().error(ex);
}
finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -