001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/common/PostChecker.java,v 1.5 2007/10/09 11:09:16 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.5 $
005: * $Date: 2007/10/09 11:09:16 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Phong Ta Quoc
039: */
040: package com.mvnforum.common;
041:
042: import java.sql.Timestamp;
043: import java.util.Collection;
044: import java.util.Iterator;
045:
046: import net.myvietnam.mvncore.exception.BadInputException;
047: import net.myvietnam.mvncore.exception.DatabaseException;
048: import net.myvietnam.mvncore.util.DateUtil;
049:
050: import com.mvnforum.MVNForumConfig;
051: import com.mvnforum.MVNForumResourceBundle;
052: import com.mvnforum.auth.*;
053: import com.mvnforum.db.DAOFactory;
054: import com.mvnforum.db.PostBean;
055:
056: public class PostChecker {
057:
058: public static final int ROOT_POST = 0;
059:
060: public static void checkEditPost(OnlineUser user, PostBean post)
061: throws AuthenticationException, BadInputException {
062:
063: MVNForumPermission permission = user.getPermission();
064: int forumId = post.getForumID();
065:
066: // check constraint
067: if (permission.canEditPost(forumId)) {
068: // have permission, just do nothing, that is dont check the max day contraint
069: } else if (isAuthorOfPost(user, post)) {
070: // make sure user have permission to edit his own post
071: permission.ensureCanEditOwnPost(forumId);
072:
073: // post should not be too old, gt 7 days by default
074: checkTooOld(user, post);
075:
076: // post should not be disabled
077: checkDisable(user, post);
078: } else {//not an author, so this user must have Edit Permission
079: permission.ensureCanEditPost(forumId);// this method ALWAYS throws AuthenticationException
080: }
081:
082: }
083:
084: public static boolean isAuthorOfPost(OnlineUser user, PostBean post) {
085: return (user.getMemberID() == post.getMemberID() && user
086: .isMember());
087: }
088:
089: public static boolean isDisabled(PostBean post) {
090: return (post.getPostStatus() == PostBean.POST_STATUS_DISABLED);
091: }
092:
093: public static void checkTooOld(OnlineUser user, PostBean post)
094: throws BadInputException {
095: Timestamp now = DateUtil.getCurrentGMTTimestamp();
096: int maxDays = MVNForumConfig.getMaxEditDays();
097: if ((now.getTime() - post.getPostCreationDate().getTime()) > (DateUtil.DAY * maxDays)) {
098: /** @todo choose a better Exception here */
099: String localizedMessage = MVNForumResourceBundle
100: .getString(
101: user.getLocale(),
102: "mvncore.exception.BadInputException.cannot_edit.post_is_too_old",
103: new Object[] { new Integer(maxDays) });
104: throw new BadInputException(localizedMessage);
105: //throw new BadInputException("You cannot edit a post which is older than " + maxDays + " days.");
106: }
107: }
108:
109: public static void checkDisable(OnlineUser user, PostBean post)
110: throws BadInputException {
111: if (isDisabled(post)) {
112: String localizedMessage = MVNForumResourceBundle
113: .getString(user.getLocale(),
114: "mvncore.exception.BadInputException.cannot_edit_your_post.which_is_disabled");
115: throw new BadInputException(localizedMessage);
116: }
117: }
118:
119: public static void checkNoReply(OnlineUser user, PostBean post)
120: throws IllegalArgumentException, DatabaseException,
121: BadInputException {
122: Collection posts = DAOFactory.getPostDAO()
123: .getEnablePosts_inThread_limit(post.getThreadID(), 0,
124: 10000);
125: boolean foundReply = false;
126: for (Iterator ite = posts.iterator(); ite.hasNext();) {
127: PostBean tPost = (PostBean) ite.next();
128: if (tPost.getParentPostID() == post.getPostID()) {
129: foundReply = true;
130: break;
131: }
132: }
133:
134: if (foundReply) {
135: String localizedMessage = MVNForumResourceBundle
136: .getString(user.getLocale(),
137: "mvncore.exception.BadInputException.cannot_delete_post.post_has_reply");
138: throw new BadInputException(localizedMessage);
139: //throw new BadInputException("Cannot delete a post that has reply!");
140: }
141: }
142:
143: public static void checkRootOfThread(OnlineUser user, PostBean post)
144: throws BadInputException {
145: if (post.getParentPostID() == ROOT_POST) {
146: String localizedMessage = MVNForumResourceBundle
147: .getString(user.getLocale(),
148: "mvncore.exception.BadInputException.cannot_delete_root_post");
149: throw new BadInputException(localizedMessage);
150: //throw new BadInputException("Cannot delete a root post. Use delete thread instead.");
151: }
152:
153: }
154:
155: }
|