dbforum.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,659 行 · 第 1/5 页

JAVA
1,659
字号
/** * $RCSfile: DbForum.java,v $ * $Revision: 1.40 $ * $Date: 2002/08/02 00:11:21 $ * * Copyright (C) 1999-2002 CoolServlets, Inc. All rights reserved. * * 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.LinkedList;import java.util.Date;import java.sql.*;import java.io.*;import com.jivesoftware.forum.*;import com.jivesoftware.util.*;import com.jivesoftware.forum.gateway.*;/** * Database implementation of the Forum interface. It loads and stores forum * information from a a database. * * @author Matt Tucker */public class DbForum implements Forum, Cacheable {    /** DATABASE QUERIES **/    private static final String ALL_THREADS =        "SELECT threadID FROM jiveThread WHERE forumID=?";    private static final String MOVE_THREAD =        "UPDATE jiveThread SET forumID=? WHERE threadID=?";    private static final String MOVE_MESSAGES =        "UPDATE jiveMessage SET forumID=? WHERE threadID=?";    protected static final String ALL_MESSAGES_ATTACHMENTS =        "SELECT attachmentID FROM jiveAttachment, jiveMessage WHERE " +        "jiveAttachment.messageID=jiveMessage.messageID AND threadID=?";    protected static final String MESSAGES_BY_NEWEST =        "SELECT messageID FROM jiveMessage WHERE threadID=? ORDER BY creationDate DESC";    protected static final String DELETE_MESSAGES =        "DELETE FROM jiveMessage WHERE threadID=?";    protected static final String DELETE_MESSAGES_PROPERTIES =        "DELETE from jiveMessageProp WHERE messageID IN" +        "(SELECT messageID FROM jiveMessage WHERE threadID=? AND forumID=?)";    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 UPDATE_PROPERTY =        "UPDATE jiveForumProp SET propValue=? WHERE name=? AND 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, creationDate, " +        "modifiedDate, categoryID FROM jiveForum WHERE forumID=?";    private static final String ADD_FORUM =        "INSERT INTO jiveForum(forumID, categoryID, categoryIndex, name, " +        "description, modDefaultThreadVal, modDefaultMsgVal, modMinThreadVal, " +        "modMinMsgVal, creationDate, modifiedDate) VALUES (?,?,?,?,?,?,?,?,?,?,?)";    private static final String SAVE_FORUM =        "UPDATE jiveForum SET name=?, description=?, modDefaultThreadVal=?, " +        "modDefaultMsgVal=?, modMinThreadVal=?, modMinMsgVal=?, " +        "creationDate=?, modifiedDate=? 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, jiveForum WHERE " +        "jiveMessage.modifiedDate > ? AND jiveMessage.forumID=? AND " +        "jiveMessage.forumID = jiveForum.forumID AND jiveMessage.modValue >= " +        "jiveForum.modMinMsgVal 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, jiveForum WHERE jiveMessage.modifiedDate > ? " +        "AND jiveMessage.forumID=? AND jiveMessage.forumID=jiveForum.forumID AND " +        "jiveMessage.modValue >= jiveForum.modMinMsgVal 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 ID's per cache block.     */    protected static final int BLOCK_SIZE = 400;    // 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();    private static final long serialVersionUID = 01L;    private long id = -1;    private long categoryID;    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 transient DbForumFactory factory;    private transient DbFilterManager filterManager;    private long [] popularThreads = null;    // List to keep track of the keys that we insert into global caches.    // Keeping track of the keys allows us to delete them from the caches    // as necessary.    private LinkedList queryKeys = new LinkedList();    /**     * 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 category the ForumCategory the forum is a part of.     */    protected DbForum(String name, String description, ForumCategory category) {        this.id = SequenceManager.nextID(JiveGlobals.FORUM);        this.categoryID = category.getID();        this.name = name;        this.description = description;        long now = System.currentTimeMillis();        creationDate = new java.util.Date(now);        modifiedDate = new java.util.Date(now);        insertIntoDb(category.getForumCount());        properties = new Hashtable();        init();    }    /**     * Loads a forum with the specified id.     */    protected DbForum(long id) throws ForumNotFoundException {        if (id < 0) {            throw new ForumNotFoundException("ID " + id + " is not valid.");        }        this.id = id;        loadFromDb();        init();    }    private void init() {        factory = DbForumFactory.getInstance();        filterManager = new DbFilterManager(this.id, factory);    }    private void readObject(java.io.ObjectInputStream in)            throws IOException, ClassNotFoundException    {        in.defaultReadObject();        init();    }    // 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())) {                factory.getForum(name);                // If we get here, the forum must already exist, so throw exception.                throw new ForumAlreadyExistsException();            }        }        catch (ForumNotFoundException e) { }        // Before changing the name of the forum to the new name, we need to        // remove the old name-to-id mapping.        factory.cacheManager.forumIDCache.remove(new Long(this.id));        this.name = name;        saveToDb();    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;        saveToDb();    }    public java.util.Date getCreationDate() {        return creationDate;    }    public void setCreationDate(java.util.Date creationDate) {       this.creationDate = creationDate;       saveToDb();    }    public java.util.Date getModifiedDate() {        return modifiedDate;    }    public void setModifiedDate(java.util.Date modifiedDate) {        this.modifiedDate = modifiedDate;        saveToDb();    }    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();    }    public int getModerationMinThreadValue() {        return modMinThreadValue;    }    public void setModerationMinThreadValue(int value) {        this.modMinThreadValue = value;        saveToDb();    }    public int getModerationMinMessageValue() {        return modMinMessageValue;    }    public void setModerationMinMessageValue(int value) {        this.modMinMessageValue = value;        saveToDb();    }    public String getProperty(String name) {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        return (String)properties.get(name);    }    public void setProperty(String name, String value) {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        // Make sure the property name and value aren't null.        if (name == null || value == null || "".equals(name) || "".equals(value)) {            throw new NullPointerException("Cannot set property with empty or null value.");        }        // See if we need to update a property value or insert a new one.        if (properties.containsKey(name)) {            // Only update the value in the database if the property value            // has changed.            if (!(value.equals(properties.get(name)))) {                properties.put(name, value);                updatePropertyInDb(name, value);                // Re-add forum to cache.                factory.cacheManager.forumCache.put(new Long(this.id), this);            }        }        else {            properties.put(name, value);            insertPropertyIntoDb(name, value);            // Re-add forum to cache.            factory.cacheManager.forumCache.put(new Long(this.id), this);        }    }    public void deleteProperty(String name) {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        // Only delete the property if it exists.        if (properties.containsKey(name)) {            properties.remove(name);            deletePropertyFromDb(name);            // Re-add forum to cache.            factory.cacheManager.forumCache.put(new Long(this.id), this);        }    }

⌨️ 快捷键说明

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