001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/SingleWatchMail.java,v 1.11 2007/11/06 12:14:56 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.11 $
005: * $Date: 2007/11/06 12:14:56 $
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.util.ArrayList;
048: import java.util.Collection;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.Map;
052:
053: import net.myvietnam.mvncore.exception.DatabaseException;
054: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
055: import net.myvietnam.mvncore.util.DateUtil;
056: import net.myvietnam.mvncore.util.MailMessageStruct;
057:
058: import org.apache.commons.logging.Log;
059: import org.apache.commons.logging.LogFactory;
060:
061: import com.mvnforum.auth.MVNForumPermission;
062: import com.mvnforum.db.*;
063:
064: import freemarker.template.SimpleHash;
065: import freemarker.template.Template;
066: import freemarker.template.TemplateException;
067:
068: public class SingleWatchMail extends WatchMail {
069:
070: private static Log log = LogFactory.getLog(SingleWatchMail.class);
071:
072: private Map m_threadWatchs = new HashMap();
073:
074: public SingleWatchMail(MemberBean member, MVNForumPermission perm,
075: String forumBase, Timestamp lastSent, Timestamp now)
076: throws IllegalArgumentException {
077:
078: super (member, perm, forumBase, lastSent, now);
079:
080: }
081:
082: void appendWatch(WatchBean watchBean)
083: throws ObjectNotFoundException, DatabaseException {
084:
085: super .appendWatch(watchBean);
086: for (Iterator iterator = m_threadList.iterator(); iterator
087: .hasNext();) {
088: int threadID = Integer.parseInt(iterator.next().toString());
089: createWatchMessageBean(threadID);
090: }
091: }
092:
093: private void createWatchMessageBean(int threadID)
094: throws ObjectNotFoundException, DatabaseException {
095:
096: Map threadWatch = new HashMap();
097:
098: // TODO : why lastSent is set 2 times here ?
099: threadWatch.put("lastSent", m_lastSent);
100: threadWatch.put("lastSent", m_root.get("lastSent"));
101: threadWatch.put("now", m_root.get("now"));
102: threadWatch.put("forumBase", m_forumBase);
103:
104: ThreadBean thread = DAOFactory.getThreadDAO().getThread(
105: threadID);
106:
107: ForumBean forumBean = ForumCache.getInstance().getBean(
108: thread.getForumID());
109: CategoryBean categoryBean = CategoryCache.getInstance()
110: .getBean(forumBean.getCategoryID());
111:
112: if ((m_permission.canReadPost(thread.getForumID()) == false)
113: || (forumBean.getForumStatus() == ForumBean.FORUM_STATUS_DISABLED)) {
114: return;
115: }
116:
117: int lastForumID = -1;
118: SimpleHash beanWatchMail = new SimpleHash();
119: threadWatch.put("threadWatch", beanWatchMail);
120: m_threadWatchs.put(new Integer(threadID), threadWatch);
121:
122: // if move to a new forum, then we print the summary of category and forum
123: if (thread.getForumID() != lastForumID) {
124: lastForumID = thread.getForumID();
125: beanWatchMail.put("leader", true);
126: } else {
127: beanWatchMail.put("leader", false);
128: }
129:
130: String forumName = forumBean.getForumName();
131: String categoryName = categoryBean.getCategoryName();
132:
133: String memberName = m_receiver.getMemberName();
134:
135: beanWatchMail.put("categoryName", categoryName);
136: beanWatchMail.put("forumName", forumName);
137: beanWatchMail.put("threadTopic", thread.getThreadTopic());
138: beanWatchMail.put("memberName", memberName);
139: beanWatchMail.put("lastPostMemberName", thread
140: .getLastPostMemberName());
141: beanWatchMail.put("threadLastPostDate", m_dateFormat
142: .format(DateUtil.convertGMTDate(thread
143: .getThreadLastPostDate(), m_receiverTimeZone)));
144: beanWatchMail.put("threadUrl", m_forumBase
145: + "/viewthread?thread=" + thread.getThreadID());
146:
147: m_watchMailService.loadCustomizedVariablesForBeanWatchMail(
148: thread, memberName, beanWatchMail);
149: }
150:
151: private String getWatchMailSubject(int threadID)
152: throws IOException, TemplateException {
153:
154: StringWriter bodyWriter = new StringWriter(4096);
155: Template subjectTemplate = m_watchMailService
156: .getSingleSubjectTemplate();
157: subjectTemplate.process(m_threadWatchs
158: .get(new Integer(threadID)), bodyWriter);
159:
160: return bodyWriter.toString();
161: }
162:
163: private String getWatchMailContent(int threadID)
164: throws IOException, TemplateException {
165:
166: StringWriter subjectWriter = new StringWriter(256);
167: Template bodyTemplate = m_watchMailService
168: .getSingleBodyTemplate();
169: bodyTemplate.process(m_threadWatchs.get(new Integer(threadID)),
170: subjectWriter);
171:
172: return subjectWriter.toString();
173: }
174:
175: public Collection getMailMessageStructs(String from, String to)
176: throws IOException, TemplateException {
177:
178: ArrayList mailMessageStructs = new ArrayList();
179: for (int i = 0; i < m_threadList.size(); i++) {
180: int threadID = Integer.parseInt(m_threadList.get(i)
181: .toString());
182: MailMessageStruct mailMessageStruct = new MailMessageStruct();
183: mailMessageStruct.setSubject(getWatchMailSubject(threadID));
184: mailMessageStruct.setMessage(getWatchMailContent(threadID));
185: mailMessageStruct.setFrom(from);
186: mailMessageStruct.setTo(to);
187: mailMessageStructs.add(mailMessageStruct);
188: }
189: return mailMessageStructs;
190: }
191: }
|