📄 dbforumthread.java
字号:
/**
* $RCSfile: DbForumThread.java,v $
* $Revision: 1.1.1.1 $
* $Date: 2002/09/09 13:50:55 $
*
* New Jive from Jdon.com.
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/
package com.jivesoftware.forum.database;
import java.sql.*;
import java.util.*;
import java.util.Date;
import com.jivesoftware.forum.*;
import com.jivesoftware.util.*;
/**
* Database implementation of the ForumThread interface. It stores thread
* data in the jiveThread table, and thread properties in jiveThreadProp.
*
* @see ForumThread
*/
public class DbForumThread implements ForumThread, Cacheable {
/** DATABASE QUERIES **/
private static final String SET_MESSAGE_PARENT =
"UPDATE jiveMessage SET parentID=? WHERE messageID=?";
private static final String CHANGE_MESSAGE_THREAD =
"UPDATE jiveMessage SET threadID=? WHERE messageID=?";
private static final String UPDATE_THREAD_MODIFIED_DATE =
"UPDATE jiveThread SET modifiedDate=? WHERE threadID=?";
private static final String DELETE_MESSAGE =
"DELETE FROM jiveMessage WHERE messageID=?";
private static final String DELETE_MESSAGE_PROPERTIES =
"DELETE FROM jiveMessageProp WHERE messageID=?";
private static final String LOAD_THREAD =
"SELECT forumID, rootMessageID, modValue, rewardPoints, creationDate, " +
"modifiedDate FROM jiveThread WHERE threadID=?";
private static final String INSERT_THREAD =
"INSERT INTO jiveThread(threadID,forumID,rootMessageID,modValue, " +
"rewardPoints,creationDate,modifiedDate) VALUES(?,?,?,?,?,?,?)";
private static final String SAVE_THREAD =
"UPDATE jiveThread SET rootMessageID=?, modValue=?, rewardPoints=?, " +
"creationDate=?, modifiedDate=? WHERE threadID=?";
private static final String LOAD_PROPERTIES =
"SELECT name, propValue FROM jiveThreadProp WHERE threadID=?";
private static final String INSERT_PROPERTY =
"INSERT INTO jiveThreadProp(threadID,name,propValue) VALUES(?,?,?)";
private static final String DELETE_PROPERTY =
"DELETE FROM jiveThreadProp WHERE threadID=? AND name=?";
private static final String DELETE_PROPERTIES =
"DELETE FROM jiveThreadProp WHERE threadID=?";
private static final String INSERT_MODERATION_ENTRY =
"INSERT INTO jiveModeration(objectID, objectType, userID, modDate, " +
"modValue) VALUES(?,?,?,?,?)";
/**
* Controls whether extended properties should be lazily loaded (not loaded
* until requested). If the properties are infrequently used, this provides
* a great speedup in initial object loading time. However, if your
* application does use extended properties all the time, you may wish to
* turn lazy loading off, as it's actually faster in total db lookup time
* to load everything at once.
*/
private static final boolean LAZY_PROP_LOADING = true;
// Constant for an empty bock. This is returned in the case that there are
// no results when trying to load a message block.
private static final long[] EMPTY_BLOCK = new long[0];
private long id = -1;
private long rootMessageID;
private java.util.Date creationDate;
private java.util.Date modifiedDate;
private DbTreeWalker treeWalker = null;
//Default the moderation point value to Integer.MIN_VALUE. Then, if the
//moderation value gets changed before being inserted into the db, we'll know.
private int moderationValue = Integer.MIN_VALUE;
private int rewardPoints = 0;
private Map properties;
protected long forumID = -1;
/**
* Cache for lists of message id's. The default size is 2K, which should
* let us hold about 250 message id's in memory at once. If you have a lot
* of memory and very large forums, you may wish to make the size of this
* cache a bit larger.
*/
protected Cache messageListCache = new Cache(2048, JiveGlobals.HOUR * 6);
/**
* Cache for message counts. Default size is 256 bytes.
*/
protected Cache messageCountCache = new Cache(256, JiveGlobals.HOUR * 6);
// Default filter for messages, called with messages() method.
private static final ResultFilter DEFAULT_MESSAGE_FILTER =
ResultFilter.createDefaultMessageFilter();
/**
* Temporarily store a rootMessage object pointer when the thread is being
* initially created. After creation, the object will remain null.
*/
private ForumMessage rootMessage = null;
/**
* Indicates if the object is ready to be saved or not. An object is not
* ready to be saved if it has just been created and has not yet been added
* to its container. For example, a message added to a thread, etc.
*/
private boolean isReadyToSave = false;
private DbForumFactory factory;
/**
* Creates a new DbForumThread. The supplied message object is used to
* derive the name of the thread (subject of message), as well as the
* creation date and modified date of thread.
*
* @param rootMessage the root message of the thread.
*/
protected DbForumThread(ForumMessage rootMessage,
DbForumFactory factory) throws UnauthorizedException
{
this.id = SequenceManager.nextID(JiveGlobals.THREAD);
this.factory = factory;
this.rootMessage = rootMessage;
this.rootMessageID = rootMessage.getID();
// Set the creation and modified dates to be the same as those of
// root message.
long rootMessageTime = rootMessage.getCreationDate().getTime();
this.creationDate = new java.util.Date(rootMessageTime);
this.modifiedDate = new java.util.Date(rootMessageTime);
properties = new Hashtable();
}
/**
* Loads a DbForumThread from the database based on its id.
*
* @param id in unique id of the ForumThread to load.
* @param factory a ForumFactory to use for loading.
*/
protected DbForumThread(long id, DbForumFactory factory)
throws ForumThreadNotFoundException
{
this.id = id;
this.factory = factory;
loadFromDb();
isReadyToSave = true;
}
//FROM THE FORUMMESSAGE INTERFACE//
public long getID() {
return id;
}
public String getName() {
String name = null;
try {
name = getMessage(rootMessageID).getSubject();
}
catch (Exception e) { }
return name;
}
public java.util.Date getCreationDate() {
return creationDate;
}
public void setCreationDate(java.util.Date creationDate)
throws UnauthorizedException
{
this.creationDate = creationDate;
// Only save to the db if the object is ready
if (!isReadyToSave) {
return;
}
saveToDb();
// Remove thread from cache.
factory.cacheManager.threadCache.remove(this.id);
}
public java.util.Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(java.util.Date modifiedDate)
throws UnauthorizedException
{
this.modifiedDate = modifiedDate;
// Only save to the db if the object is ready
if (!isReadyToSave) {
return;
}
saveToDb();
// Remove thread from cache.
factory.cacheManager.threadCache.remove(this.id);
}
public int getModerationValue() {
return moderationValue;
}
public void setModerationValue(int value, Authorization auth) {
// If the new value is the same as the old value, return.
if (moderationValue == value) {
return;
}
long now = System.currentTimeMillis();
// We modified the moderation value of the thread, so update the
// modified date.
this.modifiedDate.setTime(now);
// If the thread is switching from hidden to visible, we should update
// the modified date of forum it belongs to.
try {
Forum forum = factory.getForum(this.forumID);
if (this.moderationValue < forum.getModerationMinThreadValue()
&& value >= forum.getModerationMinThreadValue())
{
forum.setModifiedDate(modifiedDate);
}
}
catch (ForumNotFoundException fnfe) { /* ignore */ }
catch (UnauthorizedException ue) { /* ignore */ }
// Now, set the moderation value.
this.moderationValue = value;
// Only save to the db if the object is ready
if (!isReadyToSave) {
return;
}
saveToDb();
// Remove thread from cache
factory.cacheManager.threadCache.remove(this.id);
// Remove forum from cache
factory.cacheManager.forumCache.remove(this.forumID);
// Finally, make an entry into the jiveModeration table for moderation
// auditing purposes.
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_MODERATION_ENTRY);
pstmt.setLong(1, id);
pstmt.setInt(2, JiveGlobals.THREAD);
if (auth.isAnonymous()) {
pstmt.setNull(3, Types.NUMERIC);
}
else {
pstmt.setLong(3, auth.getUserID());
}
pstmt.setString(4, StringUtils.dateToMillis(modifiedDate));
pstmt.setInt(5, moderationValue);
pstmt.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
public String getProperty(String name) {
if (LAZY_PROP_LOADING) {
if (properties == null) {
loadPropertiesFromDb();
}
}
return (String)properties.get(name);
}
public void setProperty(String name, String value)
throws UnauthorizedException
{
if (LAZY_PROP_LOADING) {
if (properties == null) {
loadPropertiesFromDb();
}
}
properties.put(name, value);
// Only save to the db if the object is ready
if (!isReadyToSave) {
return;
}
// Open a database connection and start a transaction to save properties.
boolean abortTransaction = false;
Connection con = null;
try {
con = ConnectionManager.getTransactionConnection();
savePropertiesToDb(con, true);
}
catch( Exception e ) {
e.printStackTrace();
abortTransaction = true;
}
finally {
ConnectionManager.closeTransactionConnection(con, abortTransaction);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -