KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > user > WatchMail


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/WatchMail.java,v 1.20 2006/04/14 17:05:27 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.20 $
5  * $Date: 2006/04/14 17:05:27 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  * @author: Cord cord_sw@lupinex.com
41  */

42 package com.mvnforum.user;
43
44 import java.io.IOException JavaDoc;
45 import java.io.StringWriter JavaDoc;
46 import java.sql.Timestamp JavaDoc;
47 import java.text.DateFormat JavaDoc;
48 import java.util.*;
49
50 import com.mvnforum.*;
51 import com.mvnforum.auth.MVNForumPermission;
52 import com.mvnforum.db.*;
53 import freemarker.template.*;
54 import net.myvietnam.mvncore.exception.*;
55 import net.myvietnam.mvncore.util.DateUtil;
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58
59 class WatchMail {
60
61     // static variable
62
private static Log log = LogFactory.getLog(WatchMail.class);
63
64     private ArrayList m_threadList = new ArrayList();
65     private DateFormat JavaDoc m_dateFormat;
66
67     private String JavaDoc m_forumBase = null;
68     private Timestamp JavaDoc m_lastSent = null;
69     private Timestamp JavaDoc m_now = null;
70
71     private MemberBean m_receiver;
72     private double m_receiverTimeZone = 0;
73
74     private MVNForumPermission m_permission;
75
76     private Map m_root = new HashMap();
77     private ArrayList m_threadWatchList = new ArrayList();
78
79     WatchMail(MemberBean member, MVNForumPermission perm, String JavaDoc forumBase, Timestamp JavaDoc lastSent, Timestamp JavaDoc now)
80         throws IllegalArgumentException JavaDoc {
81
82         if (member == null) {
83             throw new IllegalArgumentException JavaDoc("MemberBean in WatchMail must not be null.");
84         }
85         if (perm == null) {
86             throw new IllegalArgumentException JavaDoc("Permission in WatchMail must not be null.");
87         }
88
89         m_receiver = member;
90         m_permission = perm;
91         m_forumBase = forumBase;
92         m_lastSent = lastSent;
93         m_now = now;
94
95         init();
96     }
97
98     private void init() {
99         m_receiverTimeZone = m_receiver.getMemberTimeZone();
100         if (Math.abs(m_receiverTimeZone) > 12) {
101             // timeZone > 12 or timeZone < -12
102
m_receiverTimeZone = 0;
103         }
104
105         String JavaDoc localeName = m_receiver.getMemberLanguage();
106         Locale locale = null;
107         if (localeName.length() == 0) {
108             locale = MVNForumConfig.getDefaultLocale();
109         } else {
110             locale = MyUtil.getLocale(localeName);
111         }
112         m_dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, locale);
113
114         String JavaDoc lastSentDesc = m_dateFormat.format(DateUtil.convertGMTDate(m_lastSent, m_receiverTimeZone));
115         String JavaDoc nowDesc = m_dateFormat.format(DateUtil.convertGMTDate(m_now, m_receiverTimeZone));
116
117         m_root.put("lastSent", lastSentDesc);
118         m_root.put("now", nowDesc);
119         m_root.put("forumBase", m_forumBase);
120         m_root.put("threadWatchList", m_threadWatchList);
121     }
122
123     boolean haveAtLeastOneNewThread() {
124         return (m_threadList.size() > 0);
125     }
126
127     void appendWatch(WatchBean watchBean)
128         throws ObjectNotFoundException, DatabaseException, AssertionException {
129
130         if (m_receiver.getMemberID() != watchBean.getMemberID()) {
131             throw new AssertionException("Assertion: receiver.getMemberID() == watchBean.getMemberID()");
132         }
133
134         Timestamp JavaDoc lastSent = watchBean.getWatchLastSentDate();
135
136         int categoryID = watchBean.getCategoryID();
137         int forumID = watchBean.getForumID();
138         int threadID = watchBean.getThreadID();
139
140         //log.debug("appendWatch called!!! c = " + categoryID + " f = " + forumID + " t = " + threadID);
141

142         Collection threadBeans = null;
143         if (categoryID != 0) {
144             threadBeans = DAOFactory.getThreadDAO().getEnableThreads_inCategory(categoryID, lastSent);
145         } else if (forumID != 0) {
146             threadBeans = DAOFactory.getThreadDAO().getEnableThreads_inForum(forumID, lastSent);
147         } else if (threadID != 0) {
148             if (shouldProcessThread(threadID)) {
149                 threadBeans = DAOFactory.getThreadDAO().getEnableThreads_inThread(threadID, lastSent);
150             } else {
151                 return;//ignore the reduntdant thread
152
}
153         } else {
154             threadBeans = DAOFactory.getThreadDAO().getEnableThreads_inGlobal(lastSent);
155         }
156         if (threadBeans.size() == 0) {
157             return;// no new thread
158
}
159
160         // remember that this WatchMail has process these thread
161
rememberThread(threadBeans);
162
163         createWatchMessageBean(threadBeans);
164     }
165
166     private void createWatchMessageBean(Collection threadBeans)
167         throws DatabaseException, ObjectNotFoundException {
168
169         // now, has at least one new thread, then we get the mail content
170
int lastForumID = -1;//init it to a not existed forumID
171

172         for (Iterator iterator = threadBeans.iterator(); iterator.hasNext(); ) {
173             ThreadBean thread = (ThreadBean)iterator.next();
174             SimpleHash beanWatchMail = new SimpleHash();
175
176             ForumBean forumBean = ForumCache.getInstance().getBean(thread.getForumID());
177             CategoryBean categoryBean = CategoryCache.getInstance().getBean(forumBean.getCategoryID());
178
179             if ((m_permission.canReadPost(thread.getForumID()) == false) ||
180                 (forumBean.getForumStatus() == ForumBean.FORUM_STATUS_DISABLED)) {
181                 continue;
182             }
183
184             m_threadWatchList.add(beanWatchMail);
185
186             // if move to a new forum, then we print the summary of category and forum
187
if (thread.getForumID() != lastForumID) {
188                 lastForumID = thread.getForumID();
189                 beanWatchMail.put("leader", true);
190             } else {
191                 beanWatchMail.put("leader", false);
192             }
193
194             String JavaDoc forumName = forumBean.getForumName();
195             String JavaDoc categoryName = categoryBean.getCategoryName();
196
197             beanWatchMail.put("categoryName", categoryName);
198             beanWatchMail.put("forumName", forumName);
199             beanWatchMail.put("threadTopic", thread.getThreadTopic());
200             beanWatchMail.put("memberName", thread.getMemberName());
201             beanWatchMail.put("lastPostMemberName", thread.getLastPostMemberName());
202             beanWatchMail.put("threadLastPostDate", m_dateFormat.format(DateUtil.convertGMTDate(thread.getThreadLastPostDate(),m_receiverTimeZone)));
203             beanWatchMail.put("threadUrl", m_forumBase + "/viewthread?thread=" + thread.getThreadID());
204         }
205     }
206
207     private boolean shouldProcessThread(int threadID) {
208         int size = m_threadList.size();
209         for (int i = 0; i < size; i++) {
210             int currentThreadID = ((Integer JavaDoc)m_threadList.get(i)).intValue();
211             if (currentThreadID == threadID) {
212                 return false;
213             }
214         }
215         return true;
216     }
217
218     /**
219      * Also remove the redundant thread in this threadBeans
220      * @param threadBeans : the threads to remember
221      */

222     private void rememberThread(Collection threadBeans) {
223         for (Iterator iter = threadBeans.iterator(); iter.hasNext(); ) {
224             int currentThreadID = ((ThreadBean)iter.next()).getThreadID();
225             if (shouldProcessThread(currentThreadID)) {
226                 m_threadList.add(new Integer JavaDoc(currentThreadID));
227             } else {
228                 iter.remove();
229             }
230         }
231     }
232
233     String JavaDoc getWatchSubject() throws IOException JavaDoc, TemplateException {
234         // Prepare the FreeMarker configuration;
235
Configuration cfg = MVNForumConfig.getFreeMarkerConfiguration();
236
237         StringWriter JavaDoc subjectWriter = new StringWriter JavaDoc(256);
238         Template subjectTemplate = cfg.getTemplate(MVNForumGlobal.TEMPLATE_WATCHMAIL_SUBJECT, "UTF-8");
239         subjectTemplate.process(m_root, subjectWriter);
240
241         return subjectWriter.toString();
242     }
243
244     String JavaDoc getMailContent() throws IOException JavaDoc, TemplateException {
245         // Prepare the FreeMarker configuration;
246
Configuration cfg = MVNForumConfig.getFreeMarkerConfiguration();
247
248         StringWriter JavaDoc bodyWriter = new StringWriter JavaDoc(4096);
249         Template bodyTemplate = cfg.getTemplate(MVNForumGlobal.TEMPLATE_WATCHMAIL_BODY, "UTF-8");
250         bodyTemplate.process(m_root, bodyWriter);
251
252         return bodyWriter.toString();
253     }
254
255 }
256
Popular Tags