📄 forumdao.java
字号:
/*
* $$Id: ForumDAO.java,v 1.65 2004/06/18 10:56:40 bel70 Exp $$
*
* ***** BEGIN LICENSE BLOCK *****
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License
* at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and
* limitations under the License.
*
* The Original Code is JGossip forum code.
*
* The Initial Developer of the Original Code is the JResearch, Org.
* Portions created by the Initial Developer are Copyright (C) 2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Dmitry Belov <bel@jresearch.org>
*
* ***** END LICENSE BLOCK ***** */
/*
* Created on 08.05.2003
*
*/
package org.jresearch.gossip.dao;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import org.apache.struts.util.MessageResources;
import org.jresearch.gossip.IConst;
import org.jresearch.gossip.beans.forum.Forum;
import org.jresearch.gossip.beans.forum.Group;
import org.jresearch.gossip.beans.forum.LastTopic;
import org.jresearch.gossip.beans.forum.Message;
import org.jresearch.gossip.beans.forum.NewTopic;
import org.jresearch.gossip.beans.forum.SearchResult;
import org.jresearch.gossip.beans.forum.Topic;
import org.jresearch.gossip.beans.subscription.Subscriber;
import org.jresearch.gossip.beans.user.Entry;
import org.jresearch.gossip.beans.user.EntryList;
import org.jresearch.gossip.beans.user.User;
import org.jresearch.gossip.configuration.Configurator;
import org.jresearch.gossip.dao.drivers.DbDriver;
import org.jresearch.gossip.dao.drivers.keygen.IKeyGenConst;
import org.jresearch.gossip.dao.drivers.keygen.KeyGenerator;
import org.jresearch.gossip.dao.drivers.keygen.KeyGeneratorFactory;
import org.jresearch.gossip.exception.ConfiguratorException;
import org.jresearch.gossip.forms.ForumForm;
import org.jresearch.gossip.forms.GroupForm;
import org.jresearch.gossip.forms.MessageForm;
import org.jresearch.gossip.forms.ProcessForumForm;
import org.jresearch.gossip.forms.ProcessMessageForm;
import org.jresearch.gossip.forms.ProcessTopicForm;
import org.jresearch.gossip.forms.SearchForm;
import org.jresearch.gossip.forms.StylesForm;
import org.jresearch.gossip.list.RecordsData;
import org.jresearch.gossip.util.MySQLCodec;
/**
* DOCUMENT ME!
*
* @author Bel
*/
public class ForumDAO extends DAO {
private static ForumDAO instance = new ForumDAO();
private final DbDriver dbDriver;
private final KeyGenerator keyGen;
private ForumDAO() {
try {
this.dbDriver = DbDriver.getInstance();
this.keyGen = KeyGeneratorFactory
.getKeyGenerator(IKeyGenConst.DEFAULT_KEYGEN);
} catch (SQLException ex) {
throw new RuntimeException("ForumDAO not initialized", ex);
}
}
/**
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public static ForumDAO getInstance() {
return instance;
}
/**
* DOCUMENT ME!
*
* @param form
*
* @throws SQLException
* DOCUMENT ME!
*/
public void addForum(ForumForm form) throws SQLException {
Connection connection = this.dataSource.getConnection();
PreparedStatement st = connection.prepareStatement(dbDriver
.getQueries().getForumQueries().getSql_ADD_FORUM());
try {
int fid = ((Integer) keyGen.generateKey(
IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_FORUM], connection))
.intValue();
;
st.setString(1, form.getForum_name());
st.setString(2, form.getForum_desc());
st.setInt(3, Integer.parseInt(form.getGroupid()));
st.setString(4, form.getForum_sort());
st.setInt(5, fid);
st.execute();
} finally {
st.close();
connection.close();
}
}
/**
* DOCUMENT ME!
*
* @param form
*
* @throws SQLException
* DOCUMENT ME!
*/
public void addGroup(GroupForm form) throws SQLException {
Connection connection = this.dataSource.getConnection();
PreparedStatement st = connection.prepareStatement(dbDriver
.getQueries().getForumQueries().getSql_ADD_GROUP());
int gid = ((Integer) keyGen.generateKey(
IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_GROUP], connection))
.intValue();
try {
st.setString(1, form.getGroup_name());
st.setString(2, form.getGroup_sort());
st.setInt(3, gid);
st.execute();
} finally {
st.close();
connection.close();
}
}
/**
* DOCUMENT ME!
*
* @param form
* @param ip
* DOCUMENT ME!
* @param announce
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*
* @throws SQLException
* DOCUMENT ME!
*/
public int addMessage(MessageForm form, String ip, boolean announce)
throws SQLException {
Connection connection = this.dataSource.getConnection();
PreparedStatement st = connection.prepareStatement(dbDriver
.getQueries().getForumQueries().getSql_ADD_MESSAGE());
int mid = ((Integer) keyGen.generateKey(
IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_MESSAGE], connection))
.intValue();
try {
Timestamp now = new Timestamp(now().getTime());
st.setString(1, form.getName());
st.setString(2, form.getText());
st.setTimestamp(3, now);
st.setString(4, form.getTitle());
st.setInt(5, Integer.parseInt(form.getTid()));
st.setString(6, ip);
st.setInt(7, mid);
st.execute();
if (announce) {
setThreadSortBy(form.getTid(), 5);
}
updateThreadIntime(form.getTid(), now);
} finally {
st.close();
connection.close();
}
return mid;
}
/**
* DOCUMENT ME!
*
* @param fid
* @param uid
*
* @throws SQLException
* DOCUMENT ME!
*/
public void addMod(String fid, String uid) throws SQLException {
Connection connection = this.dataSource.getConnection();
PreparedStatement st = connection.prepareStatement(dbDriver
.getQueries().getForumQueries().getSql_CHECK_USER_MOD());
ResultSet rs = null;
try {
st.setString(1, uid);
st.setInt(2, Integer.parseInt(fid));
rs = st.executeQuery();
if (!rs.next()) {
st = connection.prepareStatement(dbDriver.getQueries()
.getForumQueries().getSql_ADD_MOD());
st.setString(1, uid);
st.setInt(2, Integer.parseInt(fid));
st.execute();
}
} finally {
if (rs != null) {
rs.close();
}
st.close();
connection.close();
}
}
/**
* DOCUMENT ME!
*
* @param forumId
* DOCUMENT ME!
* @param user
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*
* @throws SQLException
* DOCUMENT ME!
*/
public boolean checkMod(int forumId, User user) throws SQLException {
boolean mod = false;
Connection connection = this.dataSource.getConnection();
PreparedStatement st = connection.prepareStatement(dbDriver
.getQueries().getForumQueries().getSql_CHECK_USER_MOD());
ResultSet rs = null;
try {
st.setInt(1, forumId);
st.setString(2, user.getName());
rs = (ResultSet) st.executeQuery();
mod = (rs.next() || (user.getStatus() > 7));
} finally {
if (rs != null) {
rs.close();
}
st.close();
connection.close();
}
return mod;
}
/**
* DOCUMENT ME!
*
* @param forumid
* @param st
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*
* @throws SQLException
* DOCUMENT ME!
*/
private int countForumTopics(int forumid, PreparedStatement st)
throws SQLException {
ResultSet rs = null;
try {
st.setInt(1, forumid);
rs = (ResultSet) st.executeQuery();
rs.next();
return rs.getInt(1);
} finally {
if (rs != null) {
rs.close();
}
}
}
/**
* DOCUMENT ME!
*
* @param tid
*
* @return
* @throws SQLException
* DOCUMENT ME!
*/
public int countThreadMessages(int tid) throws SQLException {
Connection connection = this.dataSource.getConnection();
PreparedStatement st = connection.prepareStatement(dbDriver
.getQueries().getForumQueries().getSql_COUNT_THREAD_MESSAGES());
ResultSet rs = null;
try {
st.setInt(1, tid);
rs = st.executeQuery();
rs.next();
return rs.getInt(1);
} finally {
if (rs != null) {
rs.close();
}
st.close();
connection.close();
}
}
/**
* DOCUMENT ME!
*
* @param fid
*
* @throws SQLException
* DOCUMENT ME!
*/
public void deleteForum(String fid) throws SQLException {
Connection connection = this.dataSource.getConnection();
PreparedStatement st = connection.prepareStatement(dbDriver
.getQueries().getForumQueries().getSql_DELETE_FORUM());
ResultSet rs = null;
try {
st.setInt(1, Integer.parseInt(fid));
st.execute();
st = connection.prepareStatement(dbDriver.getQueries()
.getForumQueries().getSql_GET_THREAD_ID_LIST());
st.setInt(1, Integer.parseInt(fid));
rs = st.executeQuery();
while (rs.next()) {
deleteThread(rs.getString(1), true);
}
} finally {
if (rs != null) {
rs.close();
}
st.close();
connection.close();
}
}
/**
* DOCUMENT ME!
*
* @param gid
*
* @throws SQLException
* DOCUMENT ME!
*/
public void deleteGroup(String gid) throws SQLException {
Connection connection = this.dataSource.getConnection();
PreparedStatement st = connection.prepareStatement(dbDriver
.getQueries().getForumQueries().getSql_DELETE_GROUP());
ResultSet rs = null;
try {
st.setInt(1, Integer.parseInt(gid));
st.execute();
st = connection.prepareStatement(dbDriver.getQueries()
.getForumQueries().getSql_GET_FORUMS_ID_LIST());
st.setInt(1, Integer.parseInt(gid));
rs = st.executeQuery();
while (rs.next()) {
deleteForum(rs.getString(1));
}
} finally {
if (rs != null) {
rs.close();
}
st.close();
connection.close();
}
}
/**
* DOCUMENT ME!
*
* @param form
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -