📄 chattranscriptmanager.java
字号:
}
return session;
}
/**
* Adds all metadata associated with a <code>ChatSession</code>
*
* @param session the ChatSession.
*/
public static void populateSessionWithMetadata(ChatSession session) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_SESSION_META_DATA);
pstmt.setString(1, session.getSessionID());
rs = pstmt.executeQuery();
Map<String, List<String>> metadata = new HashMap<String, List<String>>();
while (rs.next()) {
String name = rs.getString(1);
String value = rs.getString(2);
metadata.put(name, Request.decodeMetadataValue(value));
}
session.setMetadata(metadata);
}
catch (Exception ex) {
ComponentManagerFactory.getComponentManager().getLog().error(ex);
}
finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
}
}
/**
* Adds all participating agents to a <code>ChatSession</code>
*
* @param session the ChatSession.
*/
public static void populateSessionWithAgents(ChatSession session) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_SESSION_AGENTS);
pstmt.setString(1, session.getSessionID());
rs = pstmt.executeQuery();
while (rs.next()) {
String agentJID = rs.getString(1);
String joinTime = rs.getString(2);
String endTime = rs.getString(3);
long start = -1;
if (joinTime != null && joinTime.trim().length() > 0) {
start = Long.parseLong(joinTime);
}
long end = -1;
if (endTime != null && endTime.trim().length() > 0) {
end = Long.parseLong(endTime);
}
AgentChatSession agentSession = new AgentChatSession(agentJID, start, end);
session.addAgent(agentSession);
}
}
catch (Exception ex) {
ComponentManagerFactory.getComponentManager().getLog().error(ex);
}
finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
}
}
/**
* Formats a given XML Chat Transcript.
*
* @param transcript the XMP ChatTranscript.
* @return the pretty-version of a transcript.
*/
public static String formatTranscript(String transcript) {
if (transcript == null || "".equals(transcript)) {
return "";
}
final 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();
String conv1 = null;
// 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);
if(conv1 == null){
conv1 = from;
}
}
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(
"<tr valign=\"top\"><td class=\"notification-label\" colspan=2 nowrap>[")
.append(formattedDate).append("] ").append(message)
.append("</td></tr>");
}
else {
String cssClass = conv1.equals(from) ? "conversation-label1" : "conversation-label2";
buf.append(
"<tr valign=\"top\"><td width=1% class=\""+cssClass+"\" nowrap>[")
.append(formattedDate).append("] ").append(from)
.append(":</td><td class=\"conversation-body\">").append(message).append("</td></tr>");
}
}
catch (ParseException e) {
ComponentManagerFactory.getComponentManager().getLog().error(e);
}
}
}
}
return buf.toString();
}
/**
* Sends a transcript mapped by it's sessionID.
*
* @param sessionID the sessionID of the Chat.
* @param from specify who the email is from.
* @param to specify the email address of the person to receive the email.
* @param body specify header text to use in the email.
* @param subject specify the subject of this email.
*/
public static void sendTranscriptByMail(String sessionID, String from, String to, String body, String subject) {
final ChatSession chatSession = getChatSession(sessionID);
if (chatSession != null) {
final StringBuilder builder = new StringBuilder();
String transcript = chatSession.getTranscript();
if (ModelUtil.hasLength(body)) {
builder.append(body);
}
builder.append("<br/>");
builder.append("<table>").append(transcript).append("</table>");
EmailService emailService = EmailService.getInstance();
emailService.sendMessage(null, to, null, from, subject, null, builder.toString());
}
}
/**
* Sends a transcript mapped by it's sessionID using the transcript settings.
*
* @param sessionID the sessionID of the Chat.
* @param to the email address to send the transcript to.
*/
public static void sendTranscriptByMail(String sessionID, String to) {
final ChatSession chatSession = getChatSession(sessionID);
final WorkgroupManager workgroupManager = WorkgroupManager.getInstance();
Workgroup workgroup = null;
for (Workgroup wgroup : workgroupManager.getWorkgroups()) {
if (wgroup.getID() == chatSession.getWorkgroupID()) {
workgroup = wgroup;
break;
}
}
// If for some reason, the workgroup could not be found.
if (workgroup == null) {
return;
}
DbProperties props = workgroup.getProperties();
String context = "jive.transcript";
String from = props.getProperty(context + ".from");
if (from == null) {
from = workgroup.getJID().toBareJID();
}
String fromEmail = props.getProperty(context + ".fromEmail");
if (fromEmail == null) {
fromEmail = workgroup.getJID().toBareJID();
}
String subject = props.getProperty(context + ".subject");
if (subject == null) {
subject = "Chat transcript";
}
String message = props.getProperty(context + ".message");
if (message == null) {
message = "";
}
if (chatSession != null) {
final StringBuilder builder = new StringBuilder();
String transcript = chatSession.getTranscript();
if (ModelUtil.hasLength(message)) {
builder.append(message);
}
builder.append("<br/>");
builder.append("<table>").append(transcript).append("</table>");
EmailService emailService = EmailService.getInstance();
emailService.sendMessage(null, to, from, fromEmail, subject, null, builder.toString());
}
}
/**
* Sorts ChatSessions by date.
*/
static final Comparator<ChatSession> dateComparator = new Comparator<ChatSession>() {
public int compare(ChatSession item1, ChatSession item2) {
float int1 = item1.getStartTime();
float int2 = item2.getStartTime();
if (int1 == int2) {
return 0;
}
if (int1 > int2) {
return -1;
}
if (int1 < int2) {
return 1;
}
return 0;
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -