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

📄 dbforum.java

📁 java开发的一套非常好用的oa系统
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/**
 * $RCSfile: DbForum.java,v $
 * $Revision: 1.1.1.1 $
 * $Date: 2002/09/09 13:50:52 $
 *
 * 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.util.*;
import java.util.Date;
import java.sql.*;
import java.io.*;

import com.jivesoftware.forum.*;
import com.jivesoftware.util.*;


/**
 * Database implementation of the Forum interface. It loads and stores forum
 * information from a a database.
 *
 * @see Forum
 */
public class DbForum implements Forum, Cacheable {

    /** DATABASE QUERIES **/
    private static final String ALL_THREADS =
        "SELECT threadID from jiveThread WHERE forumID=?";
    private static final String ADD_THREAD =
        "UPDATE jiveThread set forumID=? WHERE threadID=?";
    private static final String MOVE_MESSAGES =
        "UPDATE jiveMessage set forumID=? WHERE threadID=?";
    protected static final String DELETE_THREAD =
        "DELETE FROM jiveThread WHERE threadID=?";
    private static final String DELETE_THREAD_PROPERTIES =
        "DELETE FROM jiveThreadProp WHERE threadID=?";
    private static final String LOAD_PROPERTIES =
        "SELECT name, propValue FROM jiveForumProp WHERE forumID=?";
    private static final String DELETE_PROPERTY =
        "DELETE FROM jiveForumProp WHERE forumID=? AND name=?";
    private static final String DELETE_PROPERTIES =
        "DELETE FROM jiveForumProp WHERE forumID=?";
    private static final String INSERT_PROPERTY =
        "INSERT INTO jiveForumProp(forumID,name,propValue) VALUES(?,?,?)";
    private static final String LOAD_FORUM =
        "SELECT forumID, name, description, modDefaultThreadVal, " +
        "modDefaultMsgVal, modMinThreadVal, modMinMsgVal, modifiedDate, " +
        "creationDate FROM jiveForum WHERE forumID=?";
    private static final String ADD_FORUM =
        "INSERT INTO jiveForum(forumID, name, description, modDefaultThreadVal, " +
        "modDefaultMsgVal, modMinThreadVal, modMinMsgVal, modifiedDate, creationDate)" +
        " VALUES (?,?,?,?,?,?,?,?,?)";
    private static final String SAVE_FORUM =
        "UPDATE jiveForum SET name=?, description=?, modDefaultThreadVal=?, " +
        "modDefaultMsgVal=?, modMinThreadVal=?, modMinMsgVal=?, " +
        "modifiedDate=?, creationDate=? WHERE forumID=?";
    private static final String UPDATE_FORUM_MODIFIED_DATE =
        "UPDATE jiveForum SET modifiedDate=? WHERE forumID=?";
    private static final String POPULAR_THREADS =
        "SELECT threadID, count(1) AS msgCount FROM jiveMessage WHERE " +
        "modifiedDate > ? AND forumID=? GROUP BY threadID ORDER BY msgCount DESC";
    private static final String POPULAR_THREADS_ORACLE =
        "SELECT /*+ INDEX (jiveMessage jiveMessage_mDate_idx) */ threadID, " +
        "count(1) AS msgCount FROM jiveMessage WHERE modifiedDate > ? " +
        "AND forumID=? GROUP BY threadID ORDER BY msgCount DESC";
     /*
        // Note, the above query includes hints for Oracle, which are necessary
        // so that modified date index will always be used. This is a great
        // tradeoff when the time window you're looking at is not excessively
        // large. MySQL also handles the query very quickly. If your own db
        // needs a hint, you may want to edit the sql logic to add it.
    */

    /**
     * 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;

    /**
     * Number of threadID's per cache block.
     */
    public static final int THREAD_BLOCK_SIZE = 200;
    /**
     * Number of messageID's per cache block.
     */
    public static final int MESSAGE_BLOCK_SIZE = 100;

    // Constant for an empty bock. This is returned in the case that there are
    // no results when trying to load a thread or message block.
    private static final long[] EMPTY_BLOCK = new long[0];

    // A ResultFilter is used to filter and sort the values that are returned
    // from the threads() and messages() methods. We use a default
    private static final ResultFilter DEFAULT_THREAD_FILTER =
            ResultFilter.createDefaultThreadFilter();
    private static final ResultFilter DEFAULT_MESSAGE_FILTER =
            ResultFilter.createDefaultMessageFilter();

    /**
     * Cache for lists of thread id's. The default size is 16K, which should
     * let us hold about 2000 thread 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 considerably larger.
     */
    protected Cache threadListCache = new Cache(16*1024, JiveGlobals.HOUR * 6);

    /**
     * Cache for lists of message id's. The default size is 8K, which should
     * let us hold about 100 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 considerably larger.
     */
    protected Cache messageListCache = new Cache(8*1024, JiveGlobals.HOUR * 6);

    /**
     * Cache for thread counts. Default size is 256 bytes.
     */
    protected Cache threadCountCache = new Cache(256, JiveGlobals.HOUR * 6);

    /**
     * Cache for message counts. Default size is 256 bytes.
     */
    protected Cache messageCountCache = new Cache(256, JiveGlobals.HOUR * 6);

    private long id = -1;
    private String name;
    private String description;
    private java.util.Date creationDate;
    private java.util.Date modifiedDate;
    private int modDefaultThreadValue = 1;
    private int modDefaultMessageValue = 1;
    private int modMinThreadValue = 1 ;
    private int modMinMessageValue = 1;
    private Map properties;

    private DbForumFactory factory;
    private long[] popularThreads = null;
    private DbFilterManager filterManager;

    /**
     * Creates a new forum with the specified name and description.
     *
     * @param name the name of the forum.
     * @param description the description of the forum.
     * @param factory the DbForumFactory the forum is a part of.
     */
    protected DbForum(String name, String description, DbForumFactory factory) {
        this.id = SequenceManager.nextID(JiveGlobals.FORUM);
        this.name = name;
        this.description = description;
        long now = System.currentTimeMillis();
        creationDate = new java.util.Date(now);
        modifiedDate = new java.util.Date(now);
        this.factory = factory;
        insertIntoDb();
        properties = new Hashtable();
        init();
    }

    /**
     * Loads a forum with the specified id.
     */
    protected DbForum(long id, DbForumFactory factory)
            throws ForumNotFoundException
    {
        this.id = id;
        this.factory = factory;
        loadFromDb();
        init();
    }

    private void init() {
        filterManager = new DbFilterManager(this.id, factory);
    }

    //FROM THE FORUM INTERFACE//

    public long getID() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) throws ForumAlreadyExistsException {
        // If the new name is the same as the current name, do nothing.
        if (this.name.equals(name)) {
            return;
        }
        // If a forum with the new name already exists, throw an exception.
        try {
            // Check to make sure that's it's not just a case change. If we
            // don't do this check, the db lookup may give us a false-positive
            // that the name is already in use.
            if (!this.name.toLowerCase().equals(name.toLowerCase())) {
                Forum forum = factory.getForum(name);
                // If we get here, the forum must already exist, so throw exception.
                throw new ForumAlreadyExistsException();
            }
        }
        catch (ForumNotFoundException e) { }

        // This is a special case since names of forums and id's are linked
        // and cached. Before changing the name of the forum to the new name,
        // we need to clear from cache to remove the old name to id mapping.
        factory.cacheManager.forumCache.remove(id);

        this.name = name;
        saveToDb();

        // Finally, expire from cache again to revflect name change.
        factory.cacheManager.forumCache.remove(id);
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
        saveToDb();
        factory.cacheManager.forumCache.remove(id);
    }

    public java.util.Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(java.util.Date creationDate) {
       this.creationDate = creationDate;
       saveToDb();
       factory.cacheManager.forumCache.remove(id);
    }

    public java.util.Date getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(java.util.Date modifiedDate)
            throws UnauthorizedException
    {
        this.modifiedDate = modifiedDate;
        saveToDb();
        factory.cacheManager.forumCache.remove(id);
    }

    public int getModerationDefaultThreadValue() {
        return modDefaultThreadValue;
    }

    public void setModerationDefaultThreadValue(int value) {
        this.modDefaultThreadValue = value;
        saveToDb();
    }

    public int getModerationDefaultMessageValue() {
        return modDefaultMessageValue;
    }

    public void setModerationDefaultMessageValue(int value) {
        this.modDefaultMessageValue = value;
        saveToDb();
        factory.cacheManager.forumCache.remove(id);
    }

    public int getModerationMinThreadValue() {
        return modMinThreadValue;
    }

    public void setModerationMinThreadValue(int value) {
        this.modMinThreadValue = value;
        saveToDb();
        factory.cacheManager.forumCache.remove(id);
    }

    public int getModerationMinMessageValue() {
        return modMinMessageValue;
    }

    public void setModerationMinMessageValue(int value) {
        this.modMinMessageValue = value;
        saveToDb();
        factory.cacheManager.forumCache.remove(id);
    }

    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) {
        if (LAZY_PROP_LOADING) {
            if (properties == null) {
                loadPropertiesFromDb();
            }
        }
        properties.put(name, value);
        savePropertiesToDb();
        factory.cacheManager.forumCache.remove(id);
    }

    public void deleteProperty(String name) {
        if (LAZY_PROP_LOADING) {
            if (properties == null) {
                loadPropertiesFromDb();
            }
        }
        properties.remove(name);
        deletePropertyFromDb(name);
        factory.cacheManager.forumCache.remove(id);
    }

⌨️ 快捷键说明

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