📄 dbforum.java
字号:
/**
* Copyright (C) 2001 Yasna.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
* Yasna.com (http://www.yasna.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Yazd" and "Yasna.com" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact yazd@yasna.com.
*
* 5. Products derived from this software may not be called "Yazd",
* nor may "Yazd" appear in their name, without prior written
* permission of Yasna.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 YASNA.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 Yasna.com. For more information
* on Yasna.com, please see <http://www.yasna.com>.
*/
/**
* 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.Yasna.forum.database;
import java.util.Iterator;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ArrayList;
import java.util.Date;
import java.sql.*;
import java.io.*;
import com.Yasna.forum.*;
import com.Yasna.forum.filter.*;
import com.Yasna.util.Cache;
import com.Yasna.util.Cacheable;
import com.Yasna.util.CacheSizes;
/**
* 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 ADD_THREAD =
"UPDATE yazdThread set forumID=? WHERE threadID=?";
protected static final String DELETE_THREAD = "DELETE FROM yazdThread WHERE threadID=?";
private static final String THREAD_COUNT =
"SELECT count(*) FROM yazdThread WHERE forumID=?";
private static final String MESSAGE_COUNT =
"SELECT count(*) FROM yazdThread, yazdMessage WHERE " +
"yazdThread.forumID=? AND yazdThread.threadID=yazdMessage.threadID";
private static final String ADD_USER_PERM =
"INSERT INTO yazdUserPerm(forumID,userID,permission) VALUES(?,?,?)";
private static final String REMOVE_USER_PERM =
"DELETE FROM yazdUserPerm WHERE forumID=? AND userID=? AND permission=?";
private static final String USERS_WITH_PERM =
"SELECT DISTINCT userID FROM yazdUserPerm WHERE forumID=? AND permission=?";
private static final String ADD_GROUP_PERM =
"INSERT INTO yazdGroupPerm(forumID,groupID,permission) VALUES(?,?,?)";
private static final String REMOVE_GROUP_PERM =
"DELETE FROM yazdGroupPerm WHERE forumID=? AND groupID=? AND permission=?";
private static final String GROUPS_WITH_PERM =
"SELECT DISTINCT groupID FROM yazdGroupPerm WHERE forumID=? AND permission=?";
private static final String LOAD_FILTERS =
"SELECT filterObject, filterIndex FROM yazdFilter WHERE forumID=? ORDER BY filterIndex ASC";
private static final String DELETE_FILTERS = "DELETE FROM yazdFilter WHERE forumID=?";
private static final String ADD_FILTER =
"INSERT INTO yazdFilter(forumID,filterIndex,filterObject) VALUES(?,?,?)";
private static final String LOAD_PROPERTIES =
"SELECT name, propValue FROM yazdForumProp WHERE forumID=?";
private static final String DELETE_PROPERTIES =
"DELETE FROM yazdForumProp WHERE forumID=?";
private static final String INSERT_PROPERTY =
"INSERT INTO yazdForumProp(forumID,name,propValue) VALUES(?,?,?)";
private static final String LOAD_FORUM_BY_ID =
"SELECT forumID, name, description, creationDate, modifiedDate, moderated FROM yazdForum WHERE forumID=?";
private static final String LOAD_FORUM_BY_NAME =
"SELECT forumID, name, description, creationDate, modifiedDate, moderated FROM yazdForum WHERE name=?";
private static final String ADD_FORUM =
"INSERT INTO yazdForum(forumID, name, description, creationDate, " +
"modifiedDate, moderated) VALUES (?,?,?,?,?,?)";
private static final String SAVE_FORUM =
"UPDATE yazdForum SET name=?, description=?, creationDate=?, " +
"modifiedDate=?, moderated=? WHERE forumID=?";
private static final String UPDATE_FORUM_MODIFIED_DATE =
"UPDATE yazdForum 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;
//Lock for saving state to database.
private Object saveLock = new Object();
private DbForumFactory factory;
/**
* 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 = DbSequenceManager.nextID("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 Properties();
//Forums should start with an html filter by default for
//security purposes.
filters = new ForumMessageFilter[2];
filters[0] = new FilterHtml();
filters[1] = new FilterNewline();
saveFiltersToDb();
//**Commenting out below since it doesn't seem to work for some reason.
//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;
loadFromDb();
loadFiltersFromDb();
loadProperties();
}
/**
* Loads a forum with the specified name.
*/
protected DbForum(String name, DbForumFactory factory)
throws ForumNotFoundException
{
this.name = name;
this.factory = factory;
loadFromDb();
loadFiltersFromDb();
loadProperties();
}
//FROM THE FORUM INTERFACE//
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -