📄 dbforum.java
字号:
/**
* DbForum.java
* August 31, 2000
*
* Copyright (C) 2000 CoolServlets.com. All rights reserved.
*
* ===================================================================
* The Apache Software License, Version 1.1
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* CoolServlets.com (http://www.coolservlets.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Jive" and "CoolServlets.com" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@coolservlets.com.
*
* 5. Products derived from this software may not be called "Jive",
* nor may "Jive" appear in their name, without prior written
* permission of CoolServlets.com.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of CoolServlets.com. For more information
* on CoolServlets.com, please see <http://www.coolservlets.com>.
*/
package com.coolservlets.forum.database;
import java.util.*;
//JDK1.1// import com.sun.java.util.collections.*;
import java.sql.*;
import java.io.*;
import com.coolservlets.forum.*;
import com.coolservlets.util.*;
import com.coolservlets.forum.filter.*;
/**
* Database implementation of the Forum interface.
*/
public class DbForum implements Forum {
/** DATABASE QUERIES **/
private static final String ADD_THREAD =
"UPDATE jiveThread set forumID=? WHERE threadID=?";
private static final String DELETE_THREAD = "DELETE FROM jiveThread WHERE threadID=?";
private static final String THREAD_COUNT =
"SELECT count(*) FROM jiveThread WHERE forumID=?";
private static final String MESSAGE_COUNT =
"SELECT count(*) FROM jiveThread, jiveMessage WHERE " +
"jiveThread.forumID=? AND jiveThread.threadID=jiveMessage.threadID";
private static final String ADD_USER_PERM =
"INSERT INTO jiveUserPerm(forumID,userID,permission) VALUES(?,?,?)";
private static final String REMOVE_USER_PERM =
"DELETE FROM jiveUserPerm WHERE forumID=? AND userID=? AND permission=?";
private static final String USERS_WITH_PERM =
"SELECT DISTINCT userID FROM jiveUserPerm WHERE forumID=? AND permission=?";
private static final String ADD_GROUP_PERM =
"INSERT INTO jiveGroupPerm(forumID,groupID,permission) VALUES(?,?,?)";
private static final String REMOVE_GROUP_PERM =
"DELETE FROM jiveGroupPerm WHERE forumID=? AND groupID=? AND permission=?";
private static final String GROUPS_WITH_PERM =
"SELECT DISTINCT groupID FROM jiveGroupPerm WHERE forumID=? AND permission=?";
private static final String LOAD_FILTERS =
"SELECT filterObject, filterIndex FROM jiveFilter WHERE forumID=? ORDER BY filterIndex ASC";
private static final String DELETE_FILTERS = "DELETE FROM jiveFilter WHERE forumID=?";
private static final String ADD_FILTER =
"INSERT INTO jiveFilter(forumID,filterIndex,filterObject) VALUES(?,?,?)";
private static final String LOAD_PROPERTIES =
"SELECT name, propValue FROM jiveForumProp WHERE forumID=?";
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 name, description, creationDate, modifiedDate, moderated FROM jiveForum WHERE forumID=?";
private static final String ADD_FORUM =
"INSERT INTO jiveForum(forumID, name, description, creationDate, " +
"modifiedDate, moderated) VALUES (?,?,?,?,?,?)";
private static final String SAVE_FORUM =
"UPDATE jiveForum SET name=?, description=?, creationDate=?, " +
"modifiedDate=?, moderated=? WHERE forumID=?";
private static final String UPDATE_FORUM_MODIFIED_DATE =
"UPDATE jiveForum SET modifiedDate=? WHERE forumID=?";
private int id = -1;
private String name;
private String description;
private java.util.Date creationDate;
private java.util.Date modifiedDate;
private int moderated = 0;
private ForumMessageFilter[] filters;
private Properties properties;
private Object propertyLock = new Object();
//lock for synchronizing saving filters.
private Object filterLock = new Object();
private DbForumFactory factory;
private DbSequenceManager sequenceManager;
protected Cache userPermissionsCache = new Cache();
/**
* Creates a new forum with the specified name and description.
*/
protected DbForum(String name, String description, DbForumFactory factory) {
this.id = DbSequenceManager.nextID("Forum");
this.name = name;
this.description = description;
creationDate = new java.util.Date();
modifiedDate = new java.util.Date();
this.factory = factory;
insertIntoDb();
properties = new Properties();
//Forums should start with an html filter by default for
//security purposes.
filters = new ForumMessageFilter[0];
try {
addForumMessageFilter(new FilterHtml(), 0);
addForumMessageFilter(new FilterNewline(), 1);
}
catch (UnauthorizedException ue) {
ue.printStackTrace();
}
}
/**
* Loads a forum with the specified id.
*/
protected DbForum(int id, DbForumFactory factory)
throws ForumNotFoundException
{
this.id = id;
this.factory = factory;
if (id != -1)
{
loadFromDb();
loadFiltersFromDb();
loadProperties();
}
}
public int getID() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) throws UnauthorizedException {
this.name = name;
saveToDb();
}
public String getDescription() {
return description;
}
public void setDescription(String description) throws UnauthorizedException
{
this.description = description;
saveToDb();
}
public java.util.Date getCreationDate() {
return creationDate;
}
public void setCreationDate(java.util.Date creationDate)
throws UnauthorizedException
{
this.creationDate = creationDate;
saveToDb();
}
public java.util.Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(java.util.Date modifiedDate)
throws UnauthorizedException
{
this.modifiedDate = modifiedDate;
saveToDb();
}
public String getProperty(String name) {
return (String)properties.get(name);
}
public void setProperty(String name, String value)
throws UnauthorizedException
{
properties.put(name, value);
saveProperties();
}
public Enumeration propertyNames() {
return properties.keys();
}
public boolean isModerated(int type) {
return false;
//if (type == Forum.THREAD) {
// return threadsModerated;
//}
//else {
// return messagesModerated;
//}
}
public void setModerated(int type, boolean moderated)
throws UnauthorizedException
{
//if (type == Forum.THREAD) {
// threadsModerated = moderated;
//}
//else {
// messagesModerated = moderated;
//}
}
public ForumThread createThread(ForumMessage rootMessage)
throws UnauthorizedException
{
//If the forum is moderated, the thread is not automatically
//approved.
boolean approved = true;
return new DbForumThread(rootMessage, approved, this, factory);
}
public ForumMessage createMessage(User user)
throws UnauthorizedException
{
//If the forum is moderated, the message is not automatically
//approved.
boolean approved = !isModerated(JiveConstants.MESSAGE);
return new DbForumMessage(user, approved, factory);
}
public void addThread(ForumThread thread) throws UnauthorizedException {
//Add message to db
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ADD_THREAD);
pstmt.setInt(1,id);
pstmt.setInt(2,thread.getID());
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForum:addThread()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//Since we added a thread, update the modified date of this thread.
updateModifiedDate(thread.getModifiedDate());
}
public ForumThread getThread(int threadID) throws
ForumThreadNotFoundException
{
return factory.getThread(threadID, this);
}
public void deleteThread(ForumThread thread) throws UnauthorizedException
{
//First, delete from cache
factory.threadCache.remove(thread.getID());
//Now delete the actual thread
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_THREAD);
pstmt.setInt(1,thread.getID());
pstmt.execute();
}
catch( Exception sqle ) {
System.err.println("Error in DbForum:deleteThread()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//Finally, delete all messages from the thread. We could do this
//through a direct sql statement. However, doing so would increase
//coupling and mean that we'd have to manage sql statments and cache
//management in multiple places. Since deletion is such an infrequent
//operation for typical use, we'll go with the easier, less-optimized
//method. If optimizing Jive for deletion is something you need to do,
//feel free to modify this method to make a direct sql call. :)
try {
ForumMessage message = thread.getRootMessage();
thread.deleteMessage(message);
}
catch (ForumMessageNotFoundException fmnfe) {
fmnfe.printStackTrace();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -