001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/WatchMail.java,v 1.37 2007/10/09 11:09:15 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.37 $
005: * $Date: 2007/10/09 11:09:15 $
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: * @author: Cord cord_sw@lupinex.com
041: */
042: package com.mvnforum.user;
043:
044: import java.io.IOException;
045: import java.io.StringWriter;
046: import java.sql.Timestamp;
047: import java.text.DateFormat;
048: import java.util.ArrayList;
049: import java.util.Collection;
050: import java.util.HashMap;
051: import java.util.Iterator;
052: import java.util.Locale;
053: import java.util.Map;
054:
055: import net.myvietnam.mvncore.exception.DatabaseException;
056: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
057: import net.myvietnam.mvncore.util.AssertionUtil;
058: import net.myvietnam.mvncore.util.DateUtil;
059:
060: import org.apache.commons.logging.Log;
061: import org.apache.commons.logging.LogFactory;
062:
063: import com.mvnforum.MVNForumConfig;
064: import com.mvnforum.MyUtil;
065: import com.mvnforum.auth.MVNForumPermission;
066: import com.mvnforum.db.CategoryBean;
067: import com.mvnforum.db.CategoryCache;
068: import com.mvnforum.db.DAOFactory;
069: import com.mvnforum.db.ForumBean;
070: import com.mvnforum.db.ForumCache;
071: import com.mvnforum.db.MemberBean;
072: import com.mvnforum.db.ThreadBean;
073: import com.mvnforum.db.WatchBean;
074: import com.mvnforum.service.MvnForumServiceFactory;
075: import com.mvnforum.service.WatchMailService;
076:
077: import freemarker.template.SimpleHash;
078: import freemarker.template.Template;
079: import freemarker.template.TemplateException;
080:
081: class WatchMail {
082:
083: // static variable
084: private static Log log = LogFactory.getLog(WatchMail.class);
085:
086: //Lan Anh: I must to modify these fields to protected for SingleWatchMail
087: protected ArrayList m_threadList = new ArrayList();
088: protected DateFormat m_dateFormat;
089:
090: protected String m_forumBase = null;
091: protected Timestamp m_lastSent = null;
092: private Timestamp m_now = null;
093:
094: protected MemberBean m_receiver;
095: protected double m_receiverTimeZone = 0;
096:
097: protected MVNForumPermission m_permission;
098:
099: protected Map m_root = new HashMap();
100: protected WatchMailService m_watchMailService;
101:
102: private ArrayList m_threadWatchList = new ArrayList();
103:
104: WatchMail(MemberBean member, MVNForumPermission perm,
105: String forumBase, Timestamp lastSent, Timestamp now)
106: throws IllegalArgumentException {
107:
108: if (member == null) {
109: throw new IllegalArgumentException(
110: "MemberBean in WatchMail must not be null.");
111: }
112: if (perm == null) {
113: throw new IllegalArgumentException(
114: "Permission in WatchMail must not be null.");
115: }
116:
117: m_watchMailService = MvnForumServiceFactory
118: .getMvnForumService().getWatchMailService();
119:
120: m_receiver = member;
121: m_permission = perm;
122: m_forumBase = forumBase;
123: m_lastSent = lastSent;
124: m_now = now;
125:
126: init();
127: }
128:
129: private void init() {
130: m_receiverTimeZone = m_receiver.getMemberTimeZone();
131: if (Math.abs(m_receiverTimeZone) > 12) {
132: // timeZone < -12 or timeZone > 12
133: m_receiverTimeZone = 0;
134: }
135:
136: String localeName = m_receiver.getMemberLanguage();
137: Locale locale = null;
138: if (localeName.length() == 0) {
139: locale = MVNForumConfig.getDefaultLocale();
140: } else {
141: locale = MyUtil.getLocale(localeName);
142: }
143: m_dateFormat = DateFormat.getDateTimeInstance(
144: DateFormat.DEFAULT, DateFormat.DEFAULT, locale);
145:
146: String nowDesc = m_dateFormat.format(DateUtil.convertGMTDate(
147: m_now, m_receiverTimeZone));
148: String lastSentDesc = m_dateFormat.format(DateUtil
149: .convertGMTDate(m_lastSent, m_receiverTimeZone));
150:
151: m_root.put("now", nowDesc);
152: m_root.put("lastSent", lastSentDesc);
153: m_root.put("forumBase", m_forumBase);
154: m_root.put("threadWatchList", m_threadWatchList);
155: }
156:
157: boolean haveAtLeastOneNewThread() {
158: return (m_threadList.size() > 0);
159: }
160:
161: void appendWatch(WatchBean watchBean)
162: throws ObjectNotFoundException, DatabaseException {
163:
164: AssertionUtil
165: .doAssert(m_receiver.getMemberID() == watchBean
166: .getMemberID(),
167: "Assertion: receiver.getMemberID() must be equal to watchBean.getMemberID()!");
168:
169: Timestamp lastSent = watchBean.getWatchLastSentDate();
170:
171: int categoryID = watchBean.getCategoryID();
172: int forumID = watchBean.getForumID();
173: int threadID = watchBean.getThreadID();
174:
175: // log.debug("appendWatch called!!! c = " + categoryID + " f = " + forumID + " t = " + threadID);
176:
177: Collection threadBeans = null;
178: if (categoryID != 0) {
179: threadBeans = DAOFactory.getThreadDAO()
180: .getEnableThreads_inCategory(categoryID, lastSent);
181: } else if (forumID != 0) {
182: threadBeans = DAOFactory.getThreadDAO()
183: .getEnableThreads_inForum(forumID, lastSent);
184: } else if (threadID != 0) {
185: if (shouldProcessThread(threadID)) {
186: threadBeans = DAOFactory.getThreadDAO()
187: .getEnableThreads_inThread(threadID, lastSent);
188: } else {
189: return; // ignore the reduntdant thread
190: }
191: } else {
192: threadBeans = DAOFactory.getThreadDAO()
193: .getEnableThreads_inGlobal(lastSent);
194: }
195:
196: // check his owner post
197: if (MVNForumConfig.getEnableSendWatchMailOfMyOwnPost() == false) {
198: checkThreadOwner(threadBeans);
199: }
200:
201: if (threadBeans.size() == 0) {
202: return; // no new thread
203: }
204:
205: // remember that this WatchMail has process these thread
206: rememberThread(threadBeans);
207:
208: if (threadBeans.size() > 0) {
209: createWatchMessageBean(threadBeans);
210: }
211: }
212:
213: private void createWatchMessageBean(Collection threadBeans)
214: throws DatabaseException, ObjectNotFoundException {
215:
216: // now, has at least one new thread, then we get the mail content
217: int lastForumID = -1;//init it to a not existed forumID
218:
219: for (Iterator iterator = threadBeans.iterator(); iterator
220: .hasNext();) {
221: ThreadBean thread = (ThreadBean) iterator.next();
222: SimpleHash beanWatchMail = new SimpleHash();
223:
224: ForumBean forumBean = ForumCache.getInstance().getBean(
225: thread.getForumID());
226: CategoryBean categoryBean = CategoryCache.getInstance()
227: .getBean(forumBean.getCategoryID());
228:
229: if ((m_permission.canReadPost(thread.getForumID()) == false)
230: || (forumBean.getForumStatus() == ForumBean.FORUM_STATUS_DISABLED)) {
231: continue;
232: }
233:
234: m_threadWatchList.add(beanWatchMail);
235:
236: // if move to a new forum, then we print the summary of category and forum
237: if (thread.getForumID() != lastForumID) {
238: lastForumID = thread.getForumID();
239: beanWatchMail.put("leader", true);
240: } else {
241: beanWatchMail.put("leader", false);
242: }
243:
244: String forumName = forumBean.getForumName();
245: String categoryName = categoryBean.getCategoryName();
246: String memberName = m_receiver.getMemberName();
247:
248: beanWatchMail.put("categoryName", categoryName);
249: beanWatchMail.put("forumName", forumName);
250: beanWatchMail.put("threadTopic", thread.getThreadTopic());
251: beanWatchMail.put("memberName", thread.getMemberName());
252: beanWatchMail.put("lastPostMemberName", thread
253: .getLastPostMemberName());
254: beanWatchMail.put("threadLastPostDate", m_dateFormat
255: .format(DateUtil.convertGMTDate(thread
256: .getThreadLastPostDate(),
257: m_receiverTimeZone)));
258: beanWatchMail.put("threadUrl", m_forumBase
259: + "/viewthread?thread=" + thread.getThreadID());
260:
261: m_watchMailService.loadCustomizedVariablesForBeanWatchMail(
262: thread, memberName, beanWatchMail);
263: }
264: }
265:
266: private void checkThreadOwner(Collection threadBeans) {
267: for (Iterator iter = threadBeans.iterator(); iter.hasNext();) {
268: String postMemberName = ((ThreadBean) iter.next())
269: .getLastPostMemberName();
270: String memberName = m_receiver.getMemberName();
271: if (memberName.equals(postMemberName)) {
272: iter.remove();
273: }
274: }
275: }
276:
277: private boolean shouldProcessThread(int threadID) {
278:
279: for (Iterator iter = m_threadList.iterator(); iter.hasNext();) {
280: Integer element = (Integer) iter.next();
281: int currentThreadID = element.intValue();
282: if (currentThreadID == threadID) {
283: return false;
284: }
285: }
286: return true;
287: }
288:
289: /**
290: * Also remove the redundant thread in this threadBeans
291: * @param threadBeans : the threads to remember
292: */
293: private void rememberThread(Collection threadBeans) {
294:
295: for (Iterator iter = threadBeans.iterator(); iter.hasNext();) {
296: int currentThreadID = ((ThreadBean) iter.next())
297: .getThreadID();
298: if (shouldProcessThread(currentThreadID)) {
299: m_threadList.add(new Integer(currentThreadID));
300: } else {
301: iter.remove();
302: }
303: }
304: }
305:
306: String getWatchMailSubject() throws IOException, TemplateException {
307:
308: StringWriter subjectWriter = new StringWriter(256);
309: Template subjectTemplate = m_watchMailService
310: .getDigestSubjectTemplate();
311: subjectTemplate.process(m_root, subjectWriter);
312:
313: return subjectWriter.toString();
314: }
315:
316: String getWatchMailBody() throws IOException, TemplateException {
317:
318: StringWriter bodyWriter = new StringWriter(4096);
319: Template bodyTemplate = m_watchMailService
320: .getDigestBodyTemplate();
321: bodyTemplate.process(m_root, bodyWriter);
322:
323: return bodyWriter.toString();
324: }
325:
326: }
|