dbforumthread.java

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

JAVA
1,412
字号
/** * $RCSfile: DbForumThread.java,v $ * $Revision: 1.36 $ * $Date: 2002/08/06 22:03:42 $ * * Copyright (C) 1999-2001 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.sql.*;import java.util.*;import java.util.Date;import java.util.LinkedList;import java.io.IOException;import com.jivesoftware.forum.*;import com.jivesoftware.util.*;/** * Database implementation of the ForumThread interface. It stores thread * data in the jiveThread table, and thread properties in jiveThreadProp. * * @see ForumThread */public class DbForumThread implements ForumThread, Cacheable {    /** DATABASE QUERIES **/    private static final String UPDATE_THREAD_MODIFIED_DATE =        "UPDATE jiveThread SET modifiedDate=? WHERE threadID=?";    private static final String DELETE_MESSAGE =        "DELETE FROM jiveMessage WHERE messageID=?";    private static final String DELETE_MESSAGE_PROPERTIES =        "DELETE FROM jiveMessageProp WHERE messageID=?";    private static final String LOAD_THREAD =        "SELECT forumID, rootMessageID, modValue, rewardPoints, creationDate, " +        "modifiedDate FROM jiveThread WHERE threadID=?";    private static final String INSERT_THREAD =        "INSERT INTO jiveThread(threadID,forumID,rootMessageID,modValue, " +        "rewardPoints,creationDate,modifiedDate) VALUES(?,?,?,?,?,?,?)";    private static final String SAVE_THREAD =        "UPDATE jiveThread SET rootMessageID=?, modValue=?, rewardPoints=?, " +        "creationDate=?, modifiedDate=? WHERE threadID=?";    private static final String LOAD_PROPERTIES =        "SELECT name, propValue FROM jiveThreadProp WHERE threadID=?";    private static final String INSERT_PROPERTY =        "INSERT INTO jiveThreadProp(threadID,name,propValue) VALUES(?,?,?)";    private static final String UPDATE_PROPERTY =        "UPDATE jiveThreadProp SET propValue=? WHERE name=? AND threadID=?";    private static final String DELETE_PROPERTY =        "DELETE FROM jiveThreadProp WHERE threadID=? AND name=?";    private static final String INSERT_MODERATION_ENTRY =        "INSERT INTO jiveModeration(objectID, objectType, userID, modDate, " +        "modValue) VALUES(?,?,?,?,?)";    /**     * 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;    // Constant for an empty bock. This is returned in the case that there are    // no results when trying to load a message block.    private static final long [] EMPTY_BLOCK = new long[0];    // Default filter for messages, called with messages() method.    private static final ResultFilter DEFAULT_MESSAGE_FILTER =            ResultFilter.createDefaultMessageFilter();    private static final long serialVersionUID = 01L;    private long id = -1;    private long rootMessageID;    private java.util.Date creationDate;    private java.util.Date modifiedDate;    private DbTreeWalker treeWalker = null;    // Default the moderation point value to Integer.MIN_VALUE. Then, if the    // moderation value gets changed before being inserted into the db, we'll know.    private int moderationValue = Integer.MIN_VALUE;    private int rewardPoints = 0;    private Map properties;    protected long forumID = -1;    private LinkedList queryKeys = new LinkedList();    /**     * Temporarily store a rootMessage object pointer when the thread is being     * initially created. After creation, the object will remain null.     */    private transient ForumMessage rootMessage = null;    /**     * 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 transient boolean readyToSave = false;    private transient 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) {        factory = DbForumFactory.getInstance();        this.id = SequenceManager.nextID(JiveGlobals.THREAD);        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);        properties = new Hashtable();    }    /**     * Loads a DbForumThread from the database based on its id.     *     * @param id in unique id of the ForumThread to load.     */    protected DbForumThread(long id) throws ForumThreadNotFoundException {        if (id < 0) {            throw new ForumThreadNotFoundException("ID " + id + " is not valid.");        }        factory = DbForumFactory.getInstance();        this.id = id;        loadFromDb();        readyToSave = true;    }    private void readObject(java.io.ObjectInputStream in)            throws IOException, ClassNotFoundException    {        in.defaultReadObject();        readyToSave = true;        factory = DbForumFactory.getInstance();    }    public long getID() {        return id;    }    public String getName() {        String name = null;        try {            name = getMessage(rootMessageID).getSubject();        }        catch (Exception e) { }        return name;    }    public java.util.Date getCreationDate() {        return creationDate;    }    public void setCreationDate(java.util.Date creationDate) {        this.creationDate = creationDate;        // Only save to the db if the object is ready        if (!readyToSave) {            return;        }        saveToDb();        // Re-add thread to cache.        factory.cacheManager.threadCache.put(new Long(this.id), this);    }    public java.util.Date getModifiedDate() {        return modifiedDate;    }    public void setModifiedDate(java.util.Date modifiedDate) {        this.modifiedDate = modifiedDate;        // Only save to the db if the object is ready        if (!readyToSave) {            return;        }        saveToDb();        // Re-add thread to cache.        factory.cacheManager.threadCache.put(new Long(this.id), this);    }    public int getModerationValue() {        return moderationValue;    }    public void setModerationValue(int value, Authorization auth) {        // If the new value is the same as the old value, return.        if (moderationValue == value) {            return;        }        setModValue(value, auth);        // We must also set the moderation value of the root message since        // the two values must be linked. If the thread is not already added to        // the database, then we must use the rootMessage object, otherwise        // we use the getRootMessage() method.        DbForumMessage message = null;        if (readyToSave) {            message = (DbForumMessage)getRootMessage();        }        else {            // The root message object may be a proxy.            if (rootMessage instanceof ForumMessageProxy) {                ForumMessageProxy proxyMsg = (ForumMessageProxy)rootMessage;                try {                    message = (DbForumMessage)proxyMsg.getProxiedForumMessage();                }                catch (UnauthorizedException ue) {                    // The thread hasn't been added to the db if we're in                    // this branch of logic, so exception shouldn't happen.                    ue.printStackTrace();                }            }            // Otherwise, we'll assume it's a DbForumMessage already.            else {                message = (DbForumMessage)rootMessage;            }        }        message.setModValue(value, auth);    }    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);                if (readyToSave) {                    updatePropertyInDb(name, value);                    // Re-add thread to cache.                    factory.cacheManager.threadCache.put(new Long(this.id), this);                    try {                        factory.cacheManager.getForum(forumID).clearCache();                    } catch (Exception e) { }                }            }        }        else {            properties.put(name, value);            if (readyToSave) {                Connection con = null;                try {                    con = ConnectionManager.getConnection();                    insertPropertyIntoDb(name, value, con);                }                catch( SQLException sqle ) {                    sqle.printStackTrace();                }                finally {                    try {  con.close();   }                    catch (Exception e) { e.printStackTrace(); }                }                // Re-add thread to cache.                factory.cacheManager.threadCache.put(new Long(this.id), this);                try {                    factory.cacheManager.getForum(forumID).clearCache();                } catch (Exception e) { }            }        }    }    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);            // Only save to the db if the object is ready            if (readyToSave) {                deletePropertyFromDb(name);                // Update objects in cache.                factory.cacheManager.threadCache.put(new Long(this.id), this);                try {                    factory.cacheManager.getForum(forumID).clearCache();                } catch (Exception e) { }            }        }    }    public Iterator propertyNames() {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        return Collections.unmodifiableSet(properties.keySet()).iterator();    }    public Forum getForum() {        // If the thread hasn't been added to a forum yet, return null.        if (forumID == -1) {            return null;        }        try {            return factory.getForum(forumID);        }        catch (Exception e) {            e.printStackTrace();            return null;        }    }    public ForumMessage getMessage(long messageID) throws ForumMessageNotFoundException {        return factory.getMessage(messageID, this.id, forumID);    }    public ForumMessage getRootMessage()  {        ForumMessage rootMessage = null;        try {            rootMessage = getMessage(rootMessageID);        }        catch (Exception e) {            e.printStackTrace();        }        return rootMessage;    }    public int getMessageCount() {        return getMessageCount(DEFAULT_MESSAGE_FILTER);    }    public int getMessageCount(ResultFilter resultFilter) {        String sql = getMessageListSQL(resultFilter, true);        QueryCacheKey key = new QueryCacheKey(JiveGlobals.THREAD, id, sql, -1);        return getCount(key);    }

⌨️ 快捷键说明

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