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

📄 dbforumthread.java

📁 Jive Forums 1.0 src
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * DbForumThread.java
 * August 31, 2000
 *
 * 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.*;
//JDK1.1// import com.sun.java.util.collections.*;
import java.text.*;

import com.coolservlets.forum.*;
import com.coolservlets.util.*;
import com.coolservlets.forum.filter.*;

/**
 * Database implementation of the ForumThread interface.
 */
public class DbForumThread implements ForumThread {

    /** DATABASE QUERIES **/
    private static final String MESSAGE_COUNT =
        "SELECT count(*) FROM jiveMessage WHERE threadID=?";
    private static final String ADD_MESSAGE1 =
        "UPDATE jiveMessage SET threadID=? WHERE messageID=?";
    private static final String ADD_MESSAGE2 =
        "INSERT INTO jiveMessageTree(parentID,childID) VALUES(?,?)";
    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 * FROM jiveThread WHERE threadID=?";
    private static final String INSERT_THREAD1 =
        "INSERT INTO jiveThread(threadID,rootMessageID,creationDate,modifiedDate," +
        "name,approved) VALUES(?,?,?,?,?,?)";
    private static final String INSERT_THREAD2 =
        "UPDATE jiveMessage SET threadID=? WHERE messageID=?";
    private static final String SAVE_THREAD =
        "UPDATE jiveThread SET name=?, rootMessageID=?, creationDate=?, " +
        "modifiedDate=? WHERE threadID=?";

    private int id = -1;
    private String name;
    private int rootMessageID;
    private java.util.Date creationDate = new java.util.Date();
    private java.util.Date modifiedDate = new java.util.Date();
    private boolean approved;
    private int forumID;

    //The forum allows us access to the message cache.
    private DbForum forum;
    //The factory provides services such as db connections and logging.
    private DbForumFactory factory;

    /**
     * Creates a new DbForumThread.
     */
    protected DbForumThread(ForumMessage rootMessage, boolean approved,
            DbForum forum, DbForumFactory factory) throws UnauthorizedException
    {
        this.id = DbSequenceManager.nextID("ForumThread");
        this.forum = forum;
        this.factory = factory;
        //Make the name of the thread be the same as the subject
        //of the message by default. The skin can always change this
        //later if needed.
        this.name = rootMessage.getSubject();
        this.rootMessageID = rootMessage.getID();
        this.creationDate = rootMessage.getCreationDate();
        this.approved = approved;
        insertIntoDb();
    }

    /**
     * Loads a DbForumThread from the database based on its id.
     */
    protected DbForumThread(int id, DbForum forum, DbForumFactory factory)
            throws ForumThreadNotFoundException
    {
        this.id = id;
        this.forum = forum;
        this.factory = factory;
        loadFromDb();
    }

    /**
     * Returns the id of the thread.
     */
    public int getID() {
        return id;
    }

    /**
     * Returns the name of the thread. The name is set by default to the
     * subject of the root message.
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the thread.
     *
     * @throws UnauthorizedException if does not have ADMIN permissions.
     */
    public void setName(String name) throws UnauthorizedException {
        this.name = name;
        //We're updating the thread, so set modifiedDate to the current time.
        modifiedDate.setTime(System.currentTimeMillis());
        saveToDb();
    }

    /**
     * Returns the Date that the thread was created.
     */
    public java.util.Date getCreationDate() {
        return creationDate;
    }

    /**
     * Sets the creation date of the thread. In most cases, the creation date
     * will default to when the thread was entered into the system. However,
     * the creation date needs to be set manually when importing data.
     * In other words, skin authors should ignore this method since it only
     * intended for system maintenance.
     *
     * @param creationDate the date the thread was created.
     *
     * @throws UnauthorizedException if does not have ADMIN permissions.
     */
    public void setCreationDate(java.util.Date creationDate)
            throws UnauthorizedException
    {
        this.creationDate = creationDate;
        saveToDb();
    }

    /**
     * Returns the Date that the thread was last modified. In other words, the
     * date of the most recent message in the thread.
     */
    public java.util.Date getModifiedDate() {
        return modifiedDate;
    }

    /**
     * Sets the date the thread was last modified. In most cases, last modifed
     * will default to when the thread data was last changed. However,
     * the last modified date needs to be set manually when importing data.
     * In other words, skin authors should ignore this method since it only
     * intended for system maintenance.
     *
     * @param modifiedDate the date the thread was modified.
     *
     * @throws UnauthorizedException if does not have ADMIN permissions.
     */
    public void setModifiedDate(java.util.Date modifiedDate)
            throws UnauthorizedException
    {
        this.modifiedDate = modifiedDate;
        saveToDb();
    }

    /**
     * Returns the parent Forum of the thread.     */    public Forum getForum() {
        Forum forum = null;
        try {
            forum = factory.getForum(forumID);
        }
        //Since the thread in the forum exists, there is basically
        //no chance that the forum won't. Therefore, we need no special
        //handling of the error condition.
        catch (ForumNotFoundException fnfe) {
            fnfe.printStackTrace();
        }
        //We already have a handle on the thread within the forum, so there
        //shouldn't be a security exception.
        catch (UnauthorizedException ue) {
            ue.printStackTrace();
        }
        return forum;
    }

    /**
     * Returns a message from the thread based on its id.
     *
     * @param messageID the ID of the message to get from the thread.
     */
    public ForumMessage getMessage(int messageID)
            throws ForumMessageNotFoundException
    {
        ForumMessage message = factory.getMessage(messageID);
        
        //Apply filters to message.
        message = forum.applyFilters(message);
        return message;
    }

    /**
     * Returns the root message of a thread. The root message is a special
     * first message that is intimately tied to the thread for most forumViews.
     * All other messages in the thread are children of the root message.
     */
    public ForumMessage getRootMessage() throws ForumMessageNotFoundException {
        return getMessage(rootMessageID);
    }

    /**
     * Returns the number of messages in the thread. This includes the root
     * message. So, to find the number of replies to the root message,
     * subtract one from the answer of this method.
     */
    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 /*"mCount"*/);
        }

⌨️ 快捷键说明

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