📄 dbforumthread.java
字号:
/**
* Copyright (C) 2001 Yasna.com. All rights reserved.
*
* ===================================================================
* The Apache Software License, Version 1.1
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* Yasna.com (http://www.yasna.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Yazd" and "Yasna.com" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact yazd@yasna.com.
*
* 5. Products derived from this software may not be called "Yazd",
* nor may "Yazd" appear in their name, without prior written
* permission of Yasna.com.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Yasna.com. For more information
* on Yasna.com, please see <http://www.yasna.com>.
*/
/**
* Copyright (C) 2000 CoolServlets.com. All rights reserved.
*
* ===================================================================
* The Apache Software License, Version 1.1
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* CoolServlets.com (http://www.coolservlets.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Jive" and "CoolServlets.com" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@coolservlets.com.
*
* 5. Products derived from this software may not be called "Jive",
* nor may "Jive" appear in their name, without prior written
* permission of CoolServlets.com.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of CoolServlets.com. For more information
* on CoolServlets.com, please see <http://www.coolservlets.com>.
*/
package com.Yasna.forum.database;
import java.sql.*;
import java.util.Date;
import java.util.Iterator;
import com.Yasna.forum.*;
import com.Yasna.util.Cacheable;
import com.Yasna.util.CacheSizes;
/**
* Database implementation of the ForumThread interface.
*
* @see ForumThread
*/
public class DbForumThread implements ForumThread, Cacheable {
/** DATABASE QUERIES **/
private static final String MESSAGE_COUNT =
"SELECT count(*) FROM yazdMessage WHERE threadID=?";
private static final String ADD_MESSAGE =
"INSERT INTO yazdMessageTree(parentID,childID) VALUES(?,?)";
private static final String MOVE_MESSAGE =
"UPDATE yazdMessageTree SET parentID=? WHERE childID=?";
private static final String CHANGE_MESSAGE_THREAD =
"UPDATE yazdMessage SET threadID=? WHERE messageID=?";
private static final String UPDATE_THREAD_MODIFIED_DATE =
"UPDATE yazdThread SET modifiedDate=? WHERE threadID=?";
private static final String DELETE_MESSAGE1 =
"DELETE FROM yazdMessageTree WHERE childID=?";
private static final String DELETE_MESSAGE2 =
"DELETE FROM yazdMessage WHERE messageID=?";
private static final String DELETE_MESSAGE_PROPERTIES =
"DELETE FROM yazdMessageProp WHERE messageID=?";
private static final String LOAD_THREAD =
"SELECT rootMessageID, creationDate, modifiedDate FROM yazdThread WHERE threadID=?";
private static final String INSERT_THREAD =
"INSERT INTO yazdThread(threadID,forumID, rootMessageID,creationDate," +
"modifiedDate,approved) VALUES(?,?,?,?,?,?)";
private static final String SAVE_THREAD =
"UPDATE yazdThread SET rootMessageID=?, creationDate=?, " +
"modifiedDate=? WHERE threadID=?";
private int id = -1;
//Temporary object reference for when inserting new record.
private ForumMessage rootMessage;
private int rootMessageID;
private java.util.Date creationDate;
private java.util.Date modifiedDate;
private boolean approved;
/**
* 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;
/**
* The forum allows us access to the message filters.
*/
private DbForum forum;
/**
* The factory provides services such as db connections and logging.
*/
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, boolean approved,
DbForum forum, DbForumFactory factory) throws UnauthorizedException
{
this.id = DbSequenceManager.nextID("ForumThread");
this.forum = forum;
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);
this.approved = approved;
}
/**
* Loads a DbForumThread from the database based on its id.
*
* @param id in unique id of the ForumThread to load.
* @param forum the Forum that the thread belongs to.
* @param factory a ForumFactory to use for loading.
*/
protected DbForumThread(int id, DbForum forum, DbForumFactory factory)
throws ForumThreadNotFoundException
{
this.id = id;
this.forum = forum;
this.factory = factory;
loadFromDb();
isReadyToSave = true;
}
//FROM THE FORUMMESSAGE INTERFACE//
public int getID() {
return id;
}
public String getName() {
return getRootMessage().getSubject();
}
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();
}
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();
}
public Forum getForum() {
return forum;
}
public ForumMessage getMessage(int messageID)
throws ForumMessageNotFoundException
{
ForumMessage message = factory.getMessage(messageID);
//Apply filters to message.
message = forum.applyFilters(message);
return message;
}
public ForumMessage getRootMessage() {
try {
return getMessage(rootMessageID);
}
catch (ForumMessageNotFoundException e) {
System.err.println("Could not load root message with id " + rootMessageID);
e.printStackTrace();
return null;
}
/*DbForumMessage message = (DbForumMessage)factory.cacheManager.get(
DbCacheManager.MESSAGE_CACHE,
new Integer(rootMessageID)
);
if (message == null) {
//Load and add to cache
try {
message = new DbForumMessage(rootMessageID, factory);
factory.cacheManager.add(DbCacheManager.MESSAGE_CACHE, new Integer(rootMessageID), message);
}
catch (ForumMessageNotFoundException e) {
System.err.println("Could not load root message with id " + rootMessageID);
e.printStackTrace();
}
}
return message;
*/
}
public int getMessageCount() {
int messageCount = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(MESSAGE_COUNT);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
rs.next();
messageCount = rs.getInt(1);
}
catch( SQLException sqle ) {
System.err.println("DbForumThread:getMessageCount() failed: "+sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
return messageCount;
}
public void addMessage(ForumMessage parentMessage, ForumMessage newMessage) {
boolean abortTransaction = false;
boolean supportsTransactions = false;
//Add message to db
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
supportsTransactions = con.getMetaData().supportsTransactions();
if (supportsTransactions) {
con.setAutoCommit(false);
}
//Now, insert the message into the database.
//Now, insert the message into the database.
((ForumMessageProxy)newMessage).insertIntoDb(con, this);
pstmt = con.prepareStatement(ADD_MESSAGE);
pstmt.setInt(1, parentMessage.getID());
pstmt.setInt(2, newMessage.getID());
pstmt.executeUpdate();
pstmt.close();
}
catch( Exception e ) {
e.printStackTrace();
abortTransaction = true;
return;
}
finally {
try {
if (supportsTransactions) {
if (abortTransaction == true) {
con.rollback();
}
else {
con.commit();
}
}
}
catch (Exception e) { e.printStackTrace(); }
try {
if (supportsTransactions) {
con.setAutoCommit(true);
}
con.close();
}
catch (Exception e) { e.printStackTrace(); }
}
//Added new message, so update the modified date of this thread
updateModifiedDate(newMessage.getModifiedDate());
//Also, update the modified date of the forum
DbForum dbForum = (DbForum)factory.cacheManager.get(
DbCacheManager.FORUM_CACHE,
new Integer(forum.getID())
);
if (dbForum != null) {
dbForum.updateModifiedDate(modifiedDate);
}
else {
forum.updateModifiedDate(modifiedDate);
}
}
public void deleteMessage(ForumMessage message)
throws UnauthorizedException
{
//Skip null messages or the case that we're already deleting the thread.
if (message == null) {
return;
}
//If the message does not belong to this thread, don't perform delete.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -