📄 shortmsgdao.java
字号:
package com.hongshee.ejforum.data;
/**
* <p>Title: ShortMsgDAO.java</p>
* <p>Description: Short message data access object</p>
* <p>Copyright: Hongshee Software (c) 2007</p>
* @author jackie du
* @version 1.0
*/
import java.sql.Connection;
//import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import com.hongshee.ejforum.util.AppUtils;
import com.hongshee.ejforum.util.PageUtils;
import com.hongshee.ejforum.data.UserDAO.UserInfo;
import com.hongshee.ejforum.common.ForumSetting;
public class ShortMsgDAO extends EntityDAO
{
private static ShortMsgDAO _dao = null;
protected ShortMsgDAO()
{}
public static ShortMsgDAO getInstance()
{
if (_dao == null)
{
_dao = new ShortMsgDAO();
}
return _dao;
}
/**
* Add a short message
* @param
* request - HttpServletRequest
* @return none
* @throws SQLException
* @since 1.0
*/
public String addShortMsg(HttpServletRequest request, String fromUser) throws SQLException
{
String users = PageUtils.getParam(request,"users");
if (users.length() == 0) return "缺乏有效的目标用户名";
String[] userIDs = users.split(",");
String[] emails = null;
String subject = PageUtils.getParam(request,"subject");
subject = subject.replace("<", "<");
subject = subject.replace(">", ">");
String message = PageUtils.getParam(request,"message");
message = message.replace("<", "<");
message = message.replace(">", ">");
PreparedStatement pstmtUpdate1 = null;
PreparedStatement pstmtUpdate2 = null;
Connection conn = dbManager.getConnection();
try
{
conn.setAutoCommit(false);
// Check if user IDs are valid first
emails = UserDAO.getInstance().getEmailsFromIDs(userIDs, conn);
StringBuilder sbuf = new StringBuilder();
for (int i=0; i<emails.length; i++)
{
if (emails[i] == null)
{
if (sbuf.length() > 0)
sbuf.append(",");
sbuf.append(userIDs[i]);
}
}
if (sbuf.length() > 0)
{
conn.commit();
return "发送短消息失败! 如下目标用户名无效: " + sbuf.toString();
}
pstmtUpdate1 = conn.prepareStatement(adapter.ShortMsg_Insert);
pstmtUpdate2 = conn.prepareStatement(adapter.User_IncUnreadSMs);
for (int i=0; i<userIDs.length; i++)
{
pstmtUpdate1.setString(1, subject);
pstmtUpdate1.setString(2, message);
pstmtUpdate1.setString(3, userIDs[i]);
pstmtUpdate1.setString(4, fromUser);
pstmtUpdate1.addBatch();
pstmtUpdate2.setString(1, userIDs[i]);
pstmtUpdate2.addBatch();
}
pstmtUpdate1.executeBatch();
pstmtUpdate2.executeBatch();
conn.commit();
}
catch(SQLException se)
{
conn.rollback();
throw se;
}
finally
{
dbManager.closePStatement(pstmtUpdate1);
dbManager.closePStatement(pstmtUpdate2);
dbManager.closeConnection(conn);
}
UserInfo userinfo = null;
for (int i=0; i<userIDs.length; i++)
{
userinfo = PageUtils.getSessionUser(userIDs[i]);
if (userinfo != null)
{
userinfo.unreadSMs = userinfo.unreadSMs + 1;
}
}
// Send email at the same time
String sendmail = request.getParameter("sendmail");
if (sendmail != null && sendmail.equals("yes") && emails != null)
{
String forumName = ForumSetting.getInstance().getForumName();
String title = forumName + ":用户\"" + fromUser + "\"给您发送来一条短消息";
StringBuilder content = new StringBuilder("<b>标题: </b>");
content.append(subject).append("<br>\n");
content.append("<b>内容: </b>").append(message);
content.append(PageUtils.getSysMailFooter(request));
AppUtils.sendMail(emails, title, content.toString());
}
return "OK";
}
/**
* Add a short message
* @param
* request - HttpServletRequest
* @return none
* @throws SQLException
* @since 1.0
*/
public void addShortMsgs(String fromUser, String[] users,
String subject, String message) throws SQLException
{
Connection conn = dbManager.getConnection();
PreparedStatement pstmtInsert1 = null;
PreparedStatement pstmtInsert2 = null;
try
{
conn.setAutoCommit(false);
pstmtInsert1 = conn.prepareStatement(adapter.ShortMsg_Insert);
pstmtInsert2 = conn.prepareStatement(adapter.User_IncUnreadSMs);
for (int i=0; i<users.length; i++)
{
if (users[i] == null || users[i].trim().length() == 0)
continue;
pstmtInsert1.setString(1, subject);
pstmtInsert1.setString(2, message);
pstmtInsert1.setString(3, users[i]);
pstmtInsert1.setString(4, fromUser);
pstmtInsert1.addBatch();
pstmtInsert2.setString(1, users[i]);
pstmtInsert2.addBatch();
}
pstmtInsert1.executeBatch();
pstmtInsert2.executeBatch();
conn.commit();
}
catch(SQLException se)
{
conn.rollback();
throw se;
}
finally
{
dbManager.closePStatement(pstmtInsert1);
dbManager.closePStatement(pstmtInsert2);
dbManager.closeConnection(conn);
}
}
/**
* Add a short message
* @param
* request - HttpServletRequest
* @return none
* @throws SQLException
* @since 1.0
*/
public void addShortMsgs(String fromUser, String[] users, String subject,
String message[], Connection conn) throws SQLException
{
PreparedStatement pstmtInsert = null;
try
{
pstmtInsert = conn.prepareStatement(adapter.ShortMsg_Insert);
for (int i=0; i<users.length; i++)
{
if (users[i] == null || users[i].trim().length() == 0)
continue;
pstmtInsert.setString(1, subject);
pstmtInsert.setString(2, message[i]);
pstmtInsert.setString(3, users[i]);
pstmtInsert.setString(4, fromUser);
pstmtInsert.addBatch();
}
pstmtInsert.executeBatch();
dbManager.closePStatement(pstmtInsert);
pstmtInsert = conn.prepareStatement(adapter.User_IncUnreadSMs);
for (int i=0; i<users.length; i++)
{
if (users[i] == null || users[i].trim().length() == 0)
continue;
pstmtInsert.setString(1, users[i]);
pstmtInsert.addBatch();
}
pstmtInsert.executeBatch();
}
finally
{
dbManager.closePStatement(pstmtInsert);
}
}
/**
* Set a short message to read state
* @param
* request - HttpServletRequest
* @return none
* @throws SQLException
* @since 1.0
*/
private void setReadState(HttpServletRequest request, String msgID,
Connection conn) throws Exception
{
UserInfo userinfo = PageUtils.getSessionUser(request);
if (userinfo == null) return;
// Decrease user SMS count at first
ArrayList<Object> paramValues = new ArrayList<Object>();
paramValues.add(userinfo.userID);
this.execUpdateSql(adapter.User_DecUnreadSMs, paramValues, conn);
paramValues.clear();
paramValues.add(msgID);
this.execUpdateSql(adapter.ShortMsg_SetReadState, paramValues, conn);
userinfo.unreadSMs = userinfo.unreadSMs - 1;
if (userinfo.unreadSMs < 0)
userinfo.unreadSMs = 0;
}
/**
* Delete some short messages of user
* @param
* msgIDs - Message ID array
* userID - User ID
* @return none
* @throws SQLException
* @since 1.0
*/
public void deleteShortMsgs(HttpServletRequest request, UserInfo userinfo,
String action) throws SQLException
{
Connection conn = null;
PreparedStatement pstmtUpdate = null;
try
{
String[] msgIDs = request.getParameterValues("msgID");
conn = dbManager.getConnection();
conn.setAutoCommit(false);
if (action != null && action.equals("outbox"))
pstmtUpdate = conn.prepareStatement(adapter.ShortMsg_RemoveFromOutbox);
else
pstmtUpdate = conn.prepareStatement(adapter.ShortMsg_RemoveFromInbox);
for (int i=0; i<msgIDs.length; i++)
{
pstmtUpdate.setString(1, msgIDs[i]);
pstmtUpdate.addBatch();
}
pstmtUpdate.executeBatch();
// Delete records which removed from inbox and outbox
this.execUpdateSql(adapter.ShortMsg_Delete, null, conn);
ArrayList<Object> paramList = new ArrayList<Object>();
paramList.add(userinfo.userID);
int count =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -