001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/PostUtil.java,v 1.22 2008/01/25 11:06:18 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.22 $
005: * $Date: 2008/01/25 11:06:18 $
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: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.user;
042:
043: import java.io.IOException;
044: import java.io.UnsupportedEncodingException;
045: import java.sql.Timestamp;
046: import java.util.*;
047:
048: import javax.mail.MessagingException;
049:
050: import net.myvietnam.mvncore.exception.*;
051: import net.myvietnam.mvncore.util.*;
052: import net.myvietnam.mvncore.web.GenericRequest;
053:
054: import org.apache.commons.logging.Log;
055: import org.apache.commons.logging.LogFactory;
056:
057: import com.mvnforum.*;
058: import com.mvnforum.auth.AuthenticationException;
059: import com.mvnforum.auth.MVNForumPermission;
060: import com.mvnforum.common.ActiveThread;
061: import com.mvnforum.common.StatisticsUtil;
062: import com.mvnforum.db.*;
063: import com.mvnforum.search.post.PostIndexer;
064:
065: import freemarker.template.TemplateException;
066:
067: public class PostUtil {
068:
069: private static Log log = LogFactory.getLog(PostUtil.class);
070:
071: public static int NUMBER_OF_MORE_THREADS_TO_GET = 5;
072:
073: public static void addPost(int parentPostID, int forumID,
074: int memberID, String currentIP, String memberName,
075: String postTopic, String postBody, String postIcon)
076: throws DatabaseException, ObjectNotFoundException,
077: BadInputException, AuthenticationException,
078: CreateException, ForeignKeyNotFoundException {
079:
080: //TODO will be set later
081: Locale locale = Locale.getDefault();
082: int threadID = 0;
083: Timestamp now = DateUtil.getCurrentGMTTimestamp();
084: boolean isForumModerator = false;
085:
086: if (parentPostID == 0) {// new thread
087: ForumBean forumBean = null;
088: try {
089: forumBean = ForumCache.getInstance().getBean(forumID);
090: } catch (ObjectNotFoundException e) {
091: String localizedMessage = MVNForumResourceBundle
092: .getString(
093: locale,
094: "mvncore.exception.ObjectNotFoundException.forumid_not_exists",
095: new Object[] { new Integer(forumID) });
096: throw new ObjectNotFoundException(localizedMessage);
097: }
098: forumBean.ensureNotDisabledForum();
099: forumBean.ensureNotClosedForum();
100: forumBean.ensureNotLockedForum();
101: } else {// reply to a post
102: // this is a parent post
103: PostBean postBean = null;
104: try {
105: postBean = DAOFactory.getPostDAO()
106: .getPost(parentPostID);// can throw DatabaseException
107: } catch (ObjectNotFoundException ex) {
108: String localizedMessage = MVNForumResourceBundle
109: .getString(
110: locale,
111: "mvncore.exception.ObjectNotFoundException.postid_not_exists",
112: new Object[] { new Integer(parentPostID) });
113: throw new ObjectNotFoundException(localizedMessage);
114: }
115:
116: ForumBean forumBean = null;
117: try {
118: forumBean = ForumCache.getInstance().getBean(forumID);
119: } catch (ObjectNotFoundException e) {
120: String localizedMessage = MVNForumResourceBundle
121: .getString(
122: locale,
123: "mvncore.exception.ObjectNotFoundException.forumid_not_exists",
124: new Object[] { new Integer(forumID) });
125: throw new ObjectNotFoundException(localizedMessage);
126: }
127: forumBean.ensureNotDisabledForum();
128: forumBean.ensureNotClosedForum();
129: forumBean.ensureNotLockedForum();
130:
131: // now we prepare to list latest post in the thread
132: threadID = postBean.getThreadID();
133:
134: // now check if thread is closed or locked, if it is, then cannot reply to a post
135: ThreadBean threadBean = null;
136: try {
137: threadBean = DAOFactory.getThreadDAO().getThread(
138: threadID);
139: } catch (ObjectNotFoundException ex) {
140: String localizedMessage = MVNForumResourceBundle
141: .getString(
142: locale,
143: "mvncore.exception.ObjectNotFoundException.threadid_not_exists",
144: new Object[] { new Integer(threadID) });
145: throw new ObjectNotFoundException(localizedMessage);
146: }
147:
148: threadBean.ensureStatusCanReply();
149: }
150:
151: // Timestamp postLastEditDate = now;
152: String postCreationIP = ((currentIP == null) || (currentIP
153: .equals(""))) ? "127.0.0.1" : currentIP;
154: String postLastEditIP = "";// should we init it to postCreationIP ???
155: int postFormatOption = 0;
156: int postOption = 0;
157: int postStatus = PostBean.POST_STATUS_DEFAULT;
158:
159: try {
160: // Ensure that moderator dont have to moderate the thread to enable it
161: if (ForumCache.getInstance().getBean(forumID)
162: .shouldModeratePost()
163: && !isForumModerator) {
164: // we will not disble post that is a thread (parentPostID == 0)
165: if (parentPostID != 0) {// replied post
166: postStatus = PostBean.POST_STATUS_DISABLED;
167: }
168: }
169: } catch (ObjectNotFoundException e) {
170: String localizedMessage = MVNForumResourceBundle
171: .getString(
172: locale,
173: "mvncore.exception.ObjectNotFoundException.forumid_not_exists",
174: new Object[] { new Integer(forumID) });
175: throw new ObjectNotFoundException(localizedMessage);
176: }
177:
178: int postAttachCount = 0;
179:
180: int postID = DAOFactory.getPostDAO().createPost(parentPostID,
181: forumID, threadID, memberID, memberName,
182: ""/*lastEditMemberName*/, postTopic, postBody,
183: now/*postCreationDate*/, now/*postLastEditDate*/,
184: postCreationIP, postLastEditIP, 0/*postEditCount*/,
185: postFormatOption, postOption, postStatus, postIcon,
186: postAttachCount);
187:
188: StatisticsUtil.updateMemberStatistics(memberID);
189: StatisticsUtil.updateForumStatistics(forumID);
190: StatisticsUtil.updateThreadStatistics(threadID);
191:
192: /** @todo Update PostEditLog table here */
193:
194: // Now clear the cache
195: PostCache.getInstance().clear();
196: ThreadCache.getInstance().clear();
197:
198: // now, update the Search Index
199: //@todo check the performance here
200: PostBean justAddedPostBean = null;
201: try {
202: justAddedPostBean = DAOFactory.getPostDAO().getPost(postID);
203: } catch (ObjectNotFoundException ex) {
204: String localizedMessage = MVNForumResourceBundle
205: .getString(
206: locale,
207: "mvncore.exception.ObjectNotFoundException.postid_not_exists",
208: new Object[] { new Integer(postID) });
209: throw new ObjectNotFoundException(localizedMessage);
210: }
211:
212: PostIndexer.scheduleAddPostTask(justAddedPostBean);
213: }
214:
215: public static void sendEmailToAdminBecauseCensoredPost(
216: GenericRequest request, int postID, int forumID,
217: int threadID, int memberID) throws IOException,
218: TemplateException, MessagingException, BadInputException {
219:
220: HashMap subjectInfo = new HashMap();
221: subjectInfo.put("postID", new Integer(postID));
222:
223: HashMap messageInfo = new HashMap();
224: messageInfo.put("forumID", new Integer(forumID));
225: messageInfo.put("threadID", new Integer(threadID));
226: messageInfo.put("postID", new Integer(postID));
227: messageInfo.put("memberID", new Integer(memberID));
228:
229: StringBuffer postURL = new StringBuffer(512);
230: postURL.append(ParamUtil.getServerPath());
231: postURL.append(request.getContextPath());
232: postURL.append(UserModuleConfig.getUrlPattern());
233: postURL.append("/viewthread?thread=");
234: postURL.append(threadID);
235: postURL.append("#");
236: postURL.append(postID);
237: messageInfo.put("postURL", postURL.toString());
238:
239: String emailAddr = MVNForumConfig.getWebMasterEmail();
240:
241: MailMessageStruct mailMessageStruct = new MailMessageStruct();
242: mailMessageStruct.setFrom(emailAddr);
243: mailMessageStruct.setTo(emailAddr);
244: mailMessageStruct
245: .setSubject(MyUtil
246: .getStringFromFreeMarkerTemplate(
247: subjectInfo,
248: MVNForumGlobal.TEMPLATE_SENDMAIL_BECAUSE_CENSORED_POST_SUBJECT));
249: mailMessageStruct
250: .setMessage(MyUtil
251: .getStringFromFreeMarkerTemplate(
252: messageInfo,
253: MVNForumGlobal.TEMPLATE_SENDMAIL_BECAUSE_CENSORED_POST_BODY));
254:
255: try {
256: MailUtil.sendMail(mailMessageStruct);
257: } catch (UnsupportedEncodingException e) {
258: log.error("Cannot support encoding", e);
259: }
260:
261: }
262:
263: public static Collection getMostActiveThreadsAfterCheckThreadType(
264: Timestamp since) throws DatabaseException {
265:
266: // get more than NUMBER_OF_MORE_THREADS_TO_GET threads in case user does not have permission to view previous threads
267: Collection mostActiveThreads = PostCache.getInstance()
268: .getMostActiveThreads(
269: since,
270: MVNForumConfig.getMaxActiveThreads()
271: + NUMBER_OF_MORE_THREADS_TO_GET);
272: for (Iterator iter = mostActiveThreads.iterator(); iter
273: .hasNext();) {
274: ActiveThread activeThread = (ActiveThread) iter.next();
275:
276: if (MVNForumConfig.getOnlyNormalThreadTypeInActiveThreads() == true) {
277: if (activeThread.getThreadType() == ThreadBean.THREAD_TYPE_GLOBAL_ANNOUNCEMENT) {
278: iter.remove();
279: } else if (activeThread.getThreadType() == ThreadBean.THREAD_TYPE_FORUM_ANNOUNCEMENT) {
280: iter.remove();
281: } else if (activeThread.getThreadType() == ThreadBean.THREAD_TYPE_STICKY) {
282: iter.remove();
283: }
284: }
285: }
286: return mostActiveThreads;
287: }
288:
289: public static Collection getMyMostActiveThreads(
290: MVNForumPermission permission, Timestamp since)
291: throws DatabaseException {
292:
293: // get more than 2 threads in case user does not have permission to view previous threads
294: Collection mostActiveThreads = getMostActiveThreadsAfterCheckThreadType(since);
295: int remainCount = 0;
296: for (Iterator iter = mostActiveThreads.iterator(); iter
297: .hasNext();) {
298: ActiveThread activeThread = (ActiveThread) iter.next();
299:
300: if (permission.canReadPost(activeThread.getForumID()) == false) {
301: iter.remove();
302: } else if (++remainCount > MVNForumConfig
303: .getMaxActiveThreads()) {
304: iter.remove();
305: }
306: }
307: return mostActiveThreads;
308: }
309:
310: public static Collection getChildrenPosts(int postID)
311: throws DatabaseException, ObjectNotFoundException {
312:
313: PostBean postBean = DAOFactory.getPostDAO().getPost(postID);
314:
315: Collection posts = new ArrayList();
316:
317: int threadID = postBean.getThreadID();
318: int numberOfPosts = DAOFactory.getPostDAO()
319: .getNumberOfEnablePosts_inThread(threadID);
320: Collection postBeans = PostCache.getInstance()
321: .getEnablePosts_inThread_limit(threadID, 0,
322: numberOfPosts);
323: for (Iterator iterator = postBeans.iterator(); iterator
324: .hasNext();) {
325: PostBean post = (PostBean) iterator.next();
326: if (post.getParentPostID() == postID) {
327: posts.add(post);
328: }
329: }
330:
331: return posts;
332: }
333: }
|