⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbforumthread.java

📁 Jive 是一个系统工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** * $RCSfile: DbForumThread.java,v $ * $Revision: 1.10 $ * $Date: 2000/12/20 02:14:04 $ * * 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.coolservlets.forum.database;import java.sql.*;import java.util.Date;import java.util.Iterator;import com.coolservlets.forum.*;import com.coolservlets.util.Cacheable;import com.coolservlets.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 jiveMessage WHERE threadID=?";    private static final String ADD_MESSAGE =        "INSERT INTO jiveMessageTree(parentID,childID) VALUES(?,?)";    private static final String MOVE_MESSAGE =        "UPDATE jiveMessageTree SET parentID=? WHERE childID=?";    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_MESSAGE1 =        "DELETE FROM jiveMessageTree WHERE childID=?";    private static final String DELETE_MESSAGE2 =        "DELETE FROM jiveMessage WHERE messageID=?";    private static final String DELETE_MESSAGE_PROPERTIES =        "DELETE FROM jiveMessageProp WHERE messageID=?";    private static final String LOAD_THREAD =        "SELECT rootMessageID, creationDate, modifiedDate FROM jiveThread WHERE threadID=?";    private static final String INSERT_THREAD =        "INSERT INTO jiveThread(threadID,forumID, rootMessageID,creationDate," +        "modifiedDate,approved) VALUES(?,?,?,?,?,?)";    private static final String SAVE_THREAD =        "UPDATE jiveThread SET rootMessageID=?, creationDate=?, " +        "modifiedDate=? WHERE threadID=?";    private int id = -1;    private ForumMessage rootMessage;    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;    /**     * Indicates if the thread is in the process of deleting itself.     */    protected boolean isDeleting = 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;        //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 rootMessage.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()  {        return rootMessage;    }    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 supportsTransactions = false;        //Add message to db        Connection con = null;        PreparedStatement pstmt = null;        try {            con = DbConnectionManager.getConnection();            supportsTransactions = con.getMetaData().supportsTransactions();            //Make the insert transactional if possible.            if (supportsTransactions) {                con.setAutoCommit(false);            }            //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();            //Commit the operation.            if (supportsTransactions) {                con.commit();            }        }        catch( SQLException sqle ) {            System.err.println("Error in DbForumThread:addMessage()-" + sqle);            return;        }        finally {            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        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 || isDeleting) {            return;        }        //If the message does not belong to this thread, don't perform delete.        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 + ".");        }        //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.rootMessage.getID() && !isDeleting) {            isDeleting = true;            forum.deleteThread(this);        }        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=0; i<childCount; 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();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -