📄 dbforumthread.java
字号:
if (message.getForumThread().getID() != this.id) {
throw new IllegalArgumentException("Message " + message.getID() +
" could not be deleted. It belongs to thread " +
message.getForumThread().getID() + ", and not thread " +
this.id + ".");
}
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
//Delete the message from the parent/child table
pstmt = con.prepareStatement(DELETE_MESSAGE1);
pstmt.setInt(1, message.getID());
pstmt.execute();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForumThread:deleteMessage()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//Recursively delete all children
TreeWalker walker = treeWalker();
int childCount = walker.getChildCount(message);
for (int i=childCount-1; i>=0; i--) {
ForumMessage childMessage = walker.getChild(message, i);
if (childMessage == null) {
System.err.println("child message was null -- index " + i);
}
deleteMessage(childMessage);
}
try {
//Delete the actual message.
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_MESSAGE2);
pstmt.setInt(1, message.getID());
pstmt.execute();
pstmt.close();
//Delete any message properties.
pstmt = con.prepareStatement(DELETE_MESSAGE_PROPERTIES);
pstmt.setInt(1, message.getID());
pstmt.execute();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForumThread:deleteMessage()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//Now, delete from the cache.
factory.getCacheManager().remove(
DbCacheManager.MESSAGE_CACHE,
new Integer(message.getID())
);
//Finally, delete it from the search index
factory.getSearchIndexer().removeFromIndex(message);
//Now, make sure that the message being deleted isn't the root message
//of this thread. If it is, the whole thread should just be deleted.
if (message.getID() == this.rootMessageID) {
forum.deleteThreadRecord(this.id);
}
}
public void moveMessage(ForumMessage message, ForumThread newThread,
ForumMessage parentMessage)
throws UnauthorizedException, IllegalArgumentException
{
if (message.getForumThread().getID() != this.id ||
parentMessage.getForumThread().getID() != newThread.getID())
{
throw new IllegalArgumentException(
"The messages and threads did not match."
);
}
// Save the mesageID of message to move
int messageID = message.getID();
// Original message thread rootMessageID
int oldRootMessageID = getRootMessage().getID();
// Move the children of this message to the new thread
TreeWalker walker = treeWalker();
int childCount = walker.getChildCount(message);
for (int i=0; i<childCount; i++) {
ForumMessage childMessage = walker.getChild(message, i);
changeMessageThread(childMessage, newThread);
}
//Move the message to the new thread.
changeMessageThread(message, newThread);
//Make message a child of parentMessage
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
if( oldRootMessageID != messageID ) {
pstmt = con.prepareStatement(MOVE_MESSAGE);
pstmt.setInt(1, parentMessage.getID());
pstmt.setInt(2, messageID);
}
else {
pstmt = con.prepareStatement(ADD_MESSAGE);
pstmt.setInt(1, parentMessage.getID());
pstmt.setInt(2, messageID);
}
pstmt.executeUpdate();
pstmt.close();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForumThread:moveMessage()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//Update the modified date of newThread
Date now = new Date();
newThread.setModifiedDate(now);
//Update the modified date of newThread forum
newThread.getForum().setModifiedDate(now);
//Thread has been modified, invalidate the cache
DbCacheManager cacheManager = factory.getCacheManager();
Integer key = new Integer(this.id);
cacheManager.remove(DbCacheManager.THREAD_CACHE, key);
//If we moved the root message of this thread, the thread should be
//deleted. Normally, deleting a thread will delete all of it's messages.
//However, we've already adjusted the thread/message relationship at the
//SQL level and removed the thread from cache. That should mean we're safe.
if (getRootMessage().getID() == messageID) {
//rootMessage = null;
this.getForum().deleteThread(this);
}
}
public TreeWalker treeWalker() {
return new DbTreeWalker(this, factory);
}
public Iterator messages() {
return new DbThreadIterator(this);
}
public Iterator messages(int startIndex, int numResults) {
return new DbThreadIterator(this, startIndex, numResults);
}
public boolean hasPermission(int type) {
return true;
}
//FROM THE CACHEABLE INTERFACE//
public int getSize() {
//Approximate the size of the object in bytes by calculating the size
//of each field.
int size = 0;
size += CacheSizes.sizeOfObject(); //overhead of object
size += CacheSizes.sizeOfInt(); //id
size += CacheSizes.sizeOfDate(); //creation date
size += CacheSizes.sizeOfDate(); //modified date
size += CacheSizes.sizeOfBoolean(); //approved
size += CacheSizes.sizeOfObject(); //ref to rootMessage
size += CacheSizes.sizeOfObject(); //ref to forum
size += CacheSizes.sizeOfObject(); //ref to factory
size += CacheSizes.sizeOfBoolean(); //ready save var
size += CacheSizes.sizeOfBoolean(); //deleting var
return size;
}
//OTHER METHODS//
/**
* Converts the object to a String by returning the name of the thread.
* This functionality is primarily for Java applications that might be
* accessing Jive objects through a GUI.
*/
public String toString() {
return getName();
}
public int hashCode() {
return id;
}
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object != null && object instanceof DbForumThread) {
return id == ((DbForumThread)object).getID();
}
else {
return false;
}
}
/**
* Updates the modified date but doesn't require a security check since
* it is a protected method.
*/
protected void updateModifiedDate(java.util.Date modifiedDate) {
this.modifiedDate = modifiedDate;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_THREAD_MODIFIED_DATE);
pstmt.setString(1, ""+modifiedDate.getTime());
pstmt.setInt(2, id);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForumThread:updateModifiedDate()-" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Moves a message to a new thread by modifying the message table threadID
* column.
*
* @param message the message to move.
* @param newThread the thread to move the message to.
*/
private void changeMessageThread(ForumMessage message, ForumThread newThread)
throws UnauthorizedException
{
//Remove message from the search index
factory.getSearchIndexer().removeFromIndex(message);
//Remove message from cache.
DbCacheManager cacheManager = factory.getCacheManager();
Integer key = new Integer(message.getID());
cacheManager.remove(DbCacheManager.MESSAGE_CACHE, key);
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(CHANGE_MESSAGE_THREAD);
pstmt.setInt(1, newThread.getID());
pstmt.setInt(2, key.intValue());
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
// Add message back to search index and update modified date
try {
ForumMessage movedMessage = newThread.getMessage(key.intValue());
factory.getSearchIndexer().addToIndex(movedMessage);
movedMessage.setModifiedDate(new Date());
}
catch(ForumMessageNotFoundException e) {
System.err.println("Error in DbForumThread:changeMessageThread()-" +
"messageID=" + key.intValue() + "newThreadID=" + newThread.getID());
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Loads a ForumThread from the database.
*/
private void loadFromDb() throws ForumThreadNotFoundException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_THREAD);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if( !rs.next() ) {
throw new ForumThreadNotFoundException("Thread " + id +
" could not be loaded from the database.");
}
//try {
rootMessageID = rs.getInt("rootMessageID");
//}
//catch (ForumMessageNotFoundException fmnfe) {
// System.err.println("Error: could not load root message of thread "
// + id + ". The database record could be corrupt.");
// fmnfe.printStackTrace();
//}
creationDate = new java.util.Date(Long.parseLong(rs.getString("creationDate").trim()));
modifiedDate = new java.util.Date(Long.parseLong(rs.getString("modifiedDate").trim()));
pstmt.close();
}
catch( SQLException sqle ) {
throw new ForumThreadNotFoundException("Thread " + id +
" could not be loaded from the database.");
}
catch (NumberFormatException nfe) {
System.err.println("WARNING: In DbForumThread.loadFromDb() -- there " +
"was an error parsing the dates returned from the database. Ensure " +
"that they're being stored correctly.");
}
finally {
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Inserts a new forum thread into the database. A connection object must
* be passed in. The connection must be open when passed in, and will
* remain open when passed back. This method allows us to make insertions
* be transactional.
*
* @param con an open Connection used to insert the thread to the db.
*/
public void insertIntoDb(Connection con) throws SQLException {
PreparedStatement pstmt = con.prepareStatement(INSERT_THREAD);
pstmt.setInt(1, id);
pstmt.setInt(2, forum.getID());
pstmt.setInt(3, rootMessageID);
pstmt.setString(4, Long.toString(creationDate.getTime()));
pstmt.setString(5, Long.toString(modifiedDate.getTime()));
pstmt.setInt(6, approved?1:0);
pstmt.executeUpdate();
pstmt.close();
//Now, insert the message into the database.
((ForumMessageProxy)rootMessage).insertIntoDb(con, this);
//since we're done inserting the object to the database, it is ready
//for future insertions.
isReadyToSave = true;
}
/**
* Saves the ForumThread to the database.
*/
private synchronized void saveToDb() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_THREAD);
pstmt.setInt(1, rootMessageID);
pstmt.setString(2, Long.toString(creationDate.getTime()));
pstmt.setString(3, Long.toString(modifiedDate.getTime()));
pstmt.setInt(4, id);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForumThread:saveToDb()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -