openfire服务器端增加聊过天的记录(openfire源码修改)

最近在openfire源码上做修改,以完成公司的聊天工具的服务器端的功能

今天实现了在服务器端增加聊天记录,这个记录不是所有的对话的记录,而只是简单的把聊过天的两个用户的id插入到一个数据库表中,这样如果用户换了设备登录仍可以看到和自己曾经聊过天的用户

首先建立一个chatHistory.java用来做数据库操作

package org.jivesoftware.openfire;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.openfire.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ChatHistory {

private static final Logger Log = LoggerFactory.getLogger(User.class);

private static final String INSERT_PROPERTY = "INSERT INTO chatHistory (user_id, target_id) VALUES (?, ?)";

private static final String LOAD_PROPERTY = "SELECT target_id FROM chatHistory WHERE user_id=?";

private static final String LOAD = "SELECT user_id FROM chatHistory"+ " WHERE target_id=?";

private String user_id;
private String target_id;

public List<String> getPropertyValue(String user, String domain) {
List<String> list = new ArrayList();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTY);
pstmt.setString(1, user);
rs = pstmt.executeQuery();
while (rs.next()) {
list.add(rs.getString(1));
}
pstmt = con.prepareStatement(LOAD);
pstmt.setString(1, user + "@" + domain);
rs = pstmt.executeQuery();
while (rs.next()) {
list.add(rs.getString(1));
}
} catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle);
sqle.printStackTrace();
} finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
}
return list;
}

public void createChatHistory(String user_id, String target_id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DbConnectionManager.getConnection();
pstmt = conn.prepareStatement(INSERT_PROPERTY);

pstmt.setString(1, user_id);
pstmt.setString(2, target_id);
pstmt.executeUpdate();
} catch (SQLException sql) {
sql.printStackTrace();
Log.error(sql.getMessage(), sql);
System.out.println("error");
} finally {
DbConnectionManager.closeConnection(pstmt, conn);
}
}

public ChatHistory() {
}

public ChatHistory(String user_id, String target_id) {
if (user_id == null) {
throw new NullPointerException("user_id cannot be null");
}
this.user_id = user_id;
if (target_id == null) {
throw new NullPointerException("target_id cannot be null");
}
this.target_id = target_id;
}

}

注意,执行rs.getString();之前必须先执行rs.next();

List只有定义为ArrayList形式才可以用add方法来增加其内容而不必指出下标值

接下来只要在MessageRouter类中增加代码来判断数据库中是否有这两个用户会话的相应记录即可,有的话则不作操作,没有的话则在数据库中加入两个用户的id对即可

                ClientSession session = sessionManager.getSession(packet.getFrom());

String messageFrom = packet.getFrom().getNode();
String domain = packet.getFrom().getDomain();
String messageTo = packet.getTo().toString();
String messageToNode = packet.getTo().getNode();

List<String> list = null;
chatHistory = new ChatHistory(messageFrom, messageTo);
list = chatHistory.getPropertyValue(messageFrom, domain);

if (list == null) {
chatHistory.createChatHistory(messageFrom, messageTo);
} else {
boolean i = list.contains(messageTo);
boolean k = list.contains(messageToNode);
if (!i && !k) {
chatHistory.createChatHistory(messageFrom, messageTo);
}
}

你可能感兴趣的:(String,list,user,服务器,domain,insert)