📄 threadwebhandler.java
字号:
/*
* $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/ThreadWebHandler.java,v 1.55 2006/04/14 17:05:27 minhnn Exp $
* $Author: minhnn $
* $Revision: 1.55 $
* $Date: 2006/04/14 17:05:27 $
*
* ====================================================================
*
* Copyright (C) 2002-2006 by MyVietnam.net
*
* All copyright notices regarding mvnForum MUST remain
* intact in the scripts and in the outputted HTML.
* The "powered by" text/logo with a link back to
* http://www.mvnForum.com and http://www.MyVietnam.net in
* the footer of the pages MUST remain visible when the pages
* are viewed on the internet or intranet.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Support can be obtained from support forums at:
* http://www.mvnForum.com/mvnforum/index
*
* Correspondence and Marketing Questions can be sent to:
* info at MyVietnam net
*
* @author: Minh Nguyen
* @author: Mai Nguyen
*/
package com.mvnforum.user;
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import com.mvnforum.*;
import com.mvnforum.auth.*;
import com.mvnforum.common.StatisticsUtil;
import com.mvnforum.db.*;
import com.mvnforum.search.post.DeletePostIndexTask;
import com.mvnforum.search.post.PostIndexer;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.util.*;
import net.myvietnam.mvncore.web.GenericRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ThreadWebHandler {
private static Log log = LogFactory.getLog(ThreadWebHandler.class);
private OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();
public ThreadWebHandler() {
}
public void prepareDelete(GenericRequest request)
throws BadInputException, ObjectNotFoundException, DatabaseException,
AuthenticationException, AssertionException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
// primary key column(s)
int threadID = GenericParamUtil.getParameterInt(request, "thread");
Locale locale = I18nUtil.getLocaleInRequest(request);
ThreadBean threadBean = null;
try {
threadBean = DAOFactory.getThreadDAO().getThread(threadID);
} catch (ObjectNotFoundException e) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.threadid_not_exists", new Object[] {new Integer(threadID)});
throw new ObjectNotFoundException(localizedMessage);
}
int numberOfPosts = DAOFactory.getPostDAO().getNumberOfEnablePosts_inThread(threadID);
// check constraint
int forumID = threadBean.getForumID();
try {
ForumCache.getInstance().getBean(forumID).ensureNotDisabledForum();
} catch (ObjectNotFoundException e) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.forumid_not_exists", new Object[] {new Integer(forumID)});
throw new ObjectNotFoundException(localizedMessage);
}
int logonMemberID = onlineUser.getMemberID();
String authorName = threadBean.getMemberName();
int authorID = 0;
try {
authorID = DAOFactory.getMemberDAO().getMemberIDFromMemberName(authorName);
} catch (ObjectNotFoundException e) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.membername_not_exists", new Object[] {authorName});
throw new ObjectNotFoundException(localizedMessage);
}
if (permission.canDeletePost(forumID)) {
// have permission, just do nothing, that is dont check the max day contraint
} else if (logonMemberID == authorID) {// same author
// check date here, usually must not older than 7 days
Timestamp now = DateUtil.getCurrentGMTTimestamp();
Timestamp postDate = threadBean.getThreadCreationDate();
int maxDays = MVNForumConfig.getMaxDeleteDays();
if ( (now.getTime() - postDate.getTime()) > (DateUtil.DAY * maxDays) ) {
/** @todo choose a better Exception here */
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete.post_is_too_old", new Object[] {new Integer(maxDays)});
throw new BadInputException(localizedMessage);
//throw new BadInputException("You cannot delete a post which is older than " + maxDays + " days.");
}
//Check to make sure that "no reply" for this post
if (numberOfPosts > 1) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete.thread_has_reply");
throw new BadInputException(localizedMessage);
//throw new BadInputException("Cannot delete a thread that has reply!");
}
/** @todo check status of this thread */
/*
if (postBean.getPostStatus() == ?) {
throw new BadInputException("Cannot delete message which is disable.");
}*/
} else {//not an author, so this user must have Edit Permission
permission.ensureCanDeletePost(forumID);// this method ALWAYS throws AuthenticationException
}
int numberOfPendingPosts = DAOFactory.getPostDAO().getNumberOfDisablePosts_inThread(threadID);
request.setAttribute("ThreadBean", threadBean);
request.setAttribute("NumberOfPosts", new Integer(numberOfPosts));
request.setAttribute("NumberOfPendingPosts", new Integer(numberOfPendingPosts));
}
public void processDelete(GenericRequest request)
throws BadInputException, ObjectNotFoundException, DatabaseException,
AuthenticationException, AssertionException {
// primary key column(s)
Locale locale = I18nUtil.getLocaleInRequest(request);
int threadID = GenericParamUtil.getParameterInt(request, "thread");
ThreadBean threadBean = null;
try {
threadBean = DAOFactory.getThreadDAO().getThread(threadID);
} catch (ObjectNotFoundException e) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.threadid_not_exists", new Object[] {new Integer(threadID)});
throw new ObjectNotFoundException(localizedMessage);
}
ForumCache.getInstance().getBean(threadBean.getForumID()).ensureNotDisabledForum();
// now check the password
MyUtil.ensureCorrectCurrentPassword(request);
deleteThread(request, threadBean);
//now, update the statistics in the forum and member
int forumID = threadBean.getForumID();
StatisticsUtil.updateForumStatistics(forumID);
// Clear the cache
ThreadCache.getInstance().clear();
PostCache.getInstance().clear();// affect mostActiveThreads in Post
request.setAttribute("ForumID", String.valueOf(forumID));
}
// Note that this method does not update the forum statistics
// The caller must call method to update the forum statistics
public void deleteThread(GenericRequest request, ThreadBean threadBean)
throws BadInputException, ObjectNotFoundException, DatabaseException,
AuthenticationException, AssertionException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
// user must have been authenticated before he can delete
permission.ensureIsAuthenticated();
// primary key column(s)
int threadID = threadBean.getThreadID();
Locale locale = I18nUtil.getLocaleInRequest(request);
// now, check the permission
int forumID = threadBean.getForumID();
int logonMemberID = onlineUser.getMemberID();
String authorName = threadBean.getMemberName();
int authorID = 0;
try {
authorID = DAOFactory.getMemberDAO().getMemberIDFromMemberName(authorName);
} catch (ObjectNotFoundException e) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.membername_not_exists", new Object[] {authorName});
throw new ObjectNotFoundException(localizedMessage);
}
if (permission.canDeletePost(forumID)) {
// have permission, just do nothing, that is dont check the max day contraint
} else if (logonMemberID == authorID) {// same author
// check date here, usually must not older than 7 days
Timestamp now = DateUtil.getCurrentGMTTimestamp();
Timestamp postDate = threadBean.getThreadCreationDate();
int maxDays = MVNForumConfig.getMaxDeleteDays();
if ( (now.getTime() - postDate.getTime()) > (DateUtil.DAY * maxDays) ) {
/** @todo choose a better Exception here */
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete.post_is_too_old", new Object[] {new Integer(maxDays)});
throw new BadInputException(localizedMessage);
//throw new BadInputException("You cannot delete a post which is older than " + maxDays + " days.");
}
//Check to make sure that "no reply" for this post
int numberOfPosts = DAOFactory.getPostDAO().getNumberOfEnablePosts_inThread(threadID);
if (numberOfPosts > 1) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete.thread_has_reply");
throw new BadInputException(localizedMessage);
//throw new BadInputException("Cannot delete a thread that has reply!");
}
if (threadBean.getThreadStatus() == ThreadBean.THREAD_STATUS_DISABLED) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete_your_own_disabled_thread");
throw new BadInputException(localizedMessage);
//throw new BadInputException("Cannot delete your own disabled thread.");
}
} else {//not an author, so this user must have Edit Permission
permission.ensureCanDeletePost(forumID);// this method ALWAYS throws AuthenticationException
}
// Delete all attachments in this thread,
// we must call this before any attempt to delete the thread
// That is, the order when delete is VERY IMPORTANT
AttachmentWebHandler.deleteAttachments_inThread(threadID);
DAOFactory.getFavoriteThreadDAO().delete_inThread(threadID);
DAOFactory.getWatchDAO().delete_inThread(threadID);
DAOFactory.getPostDAO().delete_inThread(threadID);
// now delete the thread, note that we delete it after delete all child objects
DAOFactory.getThreadDAO().delete(threadID);
// now update the search index
// @todo : what if the above methods throw exception, then no thread is deleted from index ???
PostIndexer.scheduleDeletePostTask(threadID, DeletePostIndexTask.OBJECT_TYPE_THREAD);
//now, update the statistics in the member
StatisticsUtil.updateMemberStatistics(authorID);
}
public void prepareMoveThread(GenericRequest request)
throws BadInputException, ObjectNotFoundException, DatabaseException,
AuthenticationException, AssertionException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
Locale locale = I18nUtil.getLocaleInRequest(request);
// primary key column(s)
int threadID = GenericParamUtil.getParameterInt(request, "thread");
ThreadBean threadBean = null;
try {
threadBean = DAOFactory.getThreadDAO().getThread(threadID);
} catch (ObjectNotFoundException e) {
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.threadid_not_exists", new Object[] {new Integer(threadID)});
throw new ObjectNotFoundException(localizedMessage);
}
ForumCache.getInstance().getBean(threadBean.getForumID()).ensureNotDisabledForum();
// now, check the permission
// @todo: is it the correct permission ???
permission.ensureCanDeletePost(threadBean.getForumID());
int numberOfPosts = DAOFactory.getPostDAO().getNumberOfEnablePosts_inThread(threadID);
request.setAttribute("ThreadBean", threadBean);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -