📄 dbforumfactory.java
字号:
/**
* $RCSfile: DbForumFactory.java,v $
* $Revision: 1.1.1.1 $
* $Date: 2002/09/09 13:50:53 $
*
* 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 com.jivesoftware.util.*;
import com.jivesoftware.forum.*;
import com.jivesoftware.forum.util.*;
import com.jivesoftware.forum.filter.*;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.lang.reflect.Constructor;
/**
* Database implementation of the ForumFactory interface.
*/
public class DbForumFactory
extends ForumFactory {
/** DATABASE QUERIES **/
private static final String GET_FORUMS = "SELECT forumID FROM jiveForum";
private static final String FORUM_COUNT = "SELECT count(1) FROM jiveForum";
private static final String DELETE_FORUM =
"DELETE FROM jiveForum WHERE forumID=?";
private static final String GET_FORUM_ID =
"SELECT forumID FROM jiveForum WHERE name=?";
private static final String DELETE_FORUM_PROPERTIES =
"DELETE FROM jiveForumProp WHERE forumID=?";
private static final String POPULAR_FORUMS =
"SELECT forumID, count(1) AS msgCount FROM jiveMessage " +
"WHERE modifiedDate > ? GROUP BY forumID ORDER BY msgCount DESC";
private static final String POPULAR_THREADS =
"SELECT threadID, count(1) AS msgCount FROM jiveMessage " +
"WHERE modifiedDate > ? GROUP BY threadID ORDER BY msgCount DESC";
private static final String MOVE_THREADS_TO_FORUM =
"UPDATE jiveThread SET forumID=? WHERE forumID=?";
private static final String MOVE_MESSAGES_TO_FORUM =
"UPDATE jiveMessage SET forumID=? WHERE forumID=?";
/**
* The cache manager is the global entry point for all the various object
* caches in Jive.
*/
public DatabaseCacheManager cacheManager;
protected UserManager userManager;
protected DbGroupManager groupManager;
private DbSearchManager searchManager;
private DbFilterManager filterManager;
protected DbPermissionsManager permissionsManager;
protected DbWatchManager watchManager;
protected DbRewardManager rewardManager;
/**
* Store GatewayManager objects for each forum. We can't attach the objects
* to instances of DbForum since those objects go in and out of cache and
* GatewayManager objects must remain active over time. The only added
* complexity with storing the managers globally is that we must keep the
* map up to date as forums are created and deleted.
*/
protected LongHashMap gatewayManagers;
/**
* Keep a cache of user message counts. This greatly helps for skins that
* show the number of messages a user has posted next to each of their
* messages. Each userManager implementation is responsible for actually
* using this cache at its option.
*/
public LongCache userMessageCountCache = new LongCache(64 * 1024,
6 * JiveGlobals.HOUR);
// Values for the popular forums/threads feature that can be overidden
// with Jive property settings. Defaults are specified.
private int popularForumsNumber = 4; // 10 forums
private int popularForumsWindow = 1; // 1 day
private int popularThreadsNumber = 4; // 10 threads
private int popularThreadsWindow = 1; // 1 day
protected int forumCount = -1;
protected long[] forums = null;
protected long[] popularForums = null;
protected long[] popularThreads = null;
/**
* Creates a new DbForumFactory.
*/
public DbForumFactory() {
cacheManager = new DatabaseCacheManager(this);
groupManager = new DbGroupManager(this);
searchManager = new DbSearchManager();
filterManager = new DbFilterManager( -1, this);
permissionsManager = new DbPermissionsManager( -1, this);
watchManager = new DbWatchManager(this);
rewardManager = new DbRewardManager(this);
// Load up a UserManager implementation. If the correct Jive property
// is set, it will be a custom UserManager class.
String uManagerProp = JiveGlobals.getJiveProperty("UserManager.className");
String className = "com.jivesoftware.forum.database.DbUserManager";
if (uManagerProp != null) {
className = uManagerProp;
}
try {
Class c = Class.forName(className);
// Attempt to instantiate the UserManager implementation with a
// DbForumFactory as a paramater.
Class[] params = new Class[] {
this.getClass()};
Constructor constructor = c.getConstructor(params);
// Intantiate the gateway object. We assume that
userManager = (UserManager) constructor.newInstance(
new Object[] {this});
}
catch (Exception e) {
System.err.println("Exception creating UserManager!");
e.printStackTrace();
}
// Load up gateway managers.
// gatewayManagers = new LongHashMap();
//for (Iterator iter = forums(); iter.hasNext(); ) {
//Forum forum = (Forum)iter.next();
//GatewayManager gManager = new GatewayManager(this, forum);
//gatewayManagers.put(forum.getID(), gManager);
//}
// Load custom popular threads/forums values if they exist.
try {
String number = JiveGlobals.getJiveProperty("popularForums.number");
if (number != null) {
popularForumsNumber = Integer.parseInt(number);
}
}
catch (Exception e) {}
try {
String window = JiveGlobals.getJiveProperty("popularForums.timeWindow");
if (window != null) {
popularForumsWindow = Integer.parseInt(window);
}
}
catch (Exception e) {}
try {
String number = JiveGlobals.getJiveProperty("popularThreads.number");
if (number != null) {
popularThreadsNumber = Integer.parseInt(number);
}
}
catch (Exception e) {}
try {
String window = JiveGlobals.getJiveProperty("popularThreads.timeWindow");
if (window != null) {
popularThreadsWindow = Integer.parseInt(window);
}
}
catch (Exception e) {}
}
//FROM THE FORUMFACTORY INTERFACE//
public Forum createForum(String name, String description) throws
UnauthorizedException, ForumAlreadyExistsException {
Forum newForum = null;
try {
Forum existingForum = getForum(name);
// The forum already exists since now exception, so:
throw new ForumAlreadyExistsException();
}
catch (ForumNotFoundException fnfe) {
// The forum doesn't already exist so we can create a new one
newForum = new DbForum(name, description, this);
}
// Create a new GatewayManager for the forum.
// gatewayManagers.put(newForum.getID(), new GatewayManager(this, newForum));
// Now, delete the forum list since we created a new one.
this.forums = null;
this.forumCount = -1;
return newForum;
}
public ForumThread createThread(ForumMessage rootMessage) throws
UnauthorizedException {
return new DbForumThread(rootMessage, this);
}
public ForumMessage createMessage() {
return new DbForumMessage(null, this);
}
public ForumMessage createMessage(User user) throws UnauthorizedException {
return new DbForumMessage(user, this);
}
public void deleteForum(Forum forum) throws UnauthorizedException {
// Delete all messages and threads in the forum. Disable the search
// component if it's on so that speed is reasonable. This means that
// the index should be rebuilt after deleting a forum, but this
// probably isn't unresonable.
boolean searchEnabled = searchManager.isSearchEnabled();
searchManager.setSearchEnabled(false);
ResultFilter ignoreModerationFilter = ResultFilter.
createDefaultThreadFilter();
ignoreModerationFilter.setModerationRangeMin(Integer.MIN_VALUE);
Iterator threads = forum.threads(ignoreModerationFilter);
while (threads.hasNext()) {
ForumThread thread = (ForumThread) threads.next();
forum.deleteThread(thread);
}
// Restore searching to its previous state.
searchManager.setSearchEnabled(searchEnabled);
// Now, delete all filters associated with the forum. We delete in
// reverse order since filter indexes will change if we don't delete
// the last filter entry.
FilterManager filterManager = forum.getFilterManager();
int filterCount = filterManager.getFilterCount();
for (int i = filterCount - 1; i >= 0; i--) {
filterManager.removeFilter(i);
}
// Delete all gateways.
// forum.getGatewayManager().deleteContext();
//gatewayManagers.removeKey(forum.getID());
// Delete all permissions
PermissionsManager permManager = forum.getPermissionsManager();
permManager.removeAllUserPermissions();
permManager.removeAllGroupPermissions();
// Finally, delete the forum itself and all its properties.
boolean abortTransaction = false;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getTransactionConnection();
// Properties
pstmt = con.prepareStatement(DELETE_FORUM_PROPERTIES);
pstmt.setLong(1, forum.getID());
pstmt.execute();
pstmt.close();
pstmt = con.prepareStatement(DELETE_FORUM);
pstmt.setLong(1, forum.getID());
pstmt.execute();
}
catch (Exception sqle) {
sqle.printStackTrace();
abortTransaction = true;
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
ConnectionManager.closeTransactionConnection(con, abortTransaction);
}
// Remove from cache
cacheManager.forumCache.remove(forum.getID());
// Finally, delete the list of forums
this.forumCount = -1;
this.forums = null;
}
public void mergeForums(Forum forum1, Forum forum2) throws
UnauthorizedException {
// First, remove all permissions to forum2 so that people no longer
// post in it or see it.
PermissionsManager permManager = forum2.getPermissionsManager();
permManager.removeAllUserPermissions();
permManager.removeAllGroupPermissions();
// Read all messageIDs of the thread into an array so that we can expire
// each of them later. This may be quite large for huge forums, but there
// isn't really a better way around this.
ResultFilter ignoreModerationFilter = ResultFilter.
createDefaultMessageFilter();
ignoreModerationFilter.setModerationRangeMin(Integer.MIN_VALUE);
LongList messageIDList = new LongList();
Iterator iter = forum2.messages(ignoreModerationFilter);
while (iter.hasNext()) {
long messageID = ( (ForumMessage) iter.next()).getID();
messageIDList.add(messageID);
}
// Perform db operations.
boolean abortTransaction = false;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getTransactionConnection();
// Move all threads to new forum
pstmt = con.prepareStatement(MOVE_THREADS_TO_FORUM);
pstmt.setLong(1, forum1.getID());
pstmt.setLong(2, forum2.getID());
pstmt.executeUpdate();
pstmt.close();
// Move all messages to new forum.
pstmt = con.prepareStatement(MOVE_MESSAGES_TO_FORUM);
pstmt.setLong(1, forum1.getID());
pstmt.setLong(2, forum2.getID());
pstmt.executeUpdate();
}
catch (SQLException sqle) {
sqle.printStackTrace();
abortTransaction = true;
return;
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
ConnectionManager.closeTransactionConnection(con, abortTransaction);
}
// Delete forum2
deleteForum(forum2);
// Erase both forums from cache.
cacheManager.forumCache.remove(forum1.getID());
cacheManager.forumCache.remove(forum2.getID());
// Clear out entire message and thread caches. This is drastic, but is
// pretty necessary to make everything work correctly. Merging forums
// shouldn't be something that is done all the time, so we should be ok.
cacheManager.messageCache.clear();
cacheManager.threadCache.clear();
// Update the last modified date of forum1 to the most recently
// updated thread (this may have changed during forum merge).
ResultFilter newestThreadFilter = ResultFilter.createDefaultThreadFilter();
newestThreadFilter.setNumResults(1);
Iterator threadIter = forum1.threads(newestThreadFilter);
if (threadIter.hasNext()) {
ForumThread newestThread = (ForumThread) threadIter.next();
if (newestThread != null) {
forum1.setModifiedDate(newestThread.getModifiedDate());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -