/**
* $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
评论5