KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/WatchWebHandler.java,v 1.27 2006/04/14 17:05:27 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.27 $
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  */

41 package com.mvnforum.user;
42
43 import java.io.IOException JavaDoc;
44 import java.io.UnsupportedEncodingException JavaDoc;
45 import java.sql.Timestamp JavaDoc;
46 import java.util.*;
47
48 import javax.mail.MessagingException JavaDoc;
49
50 import com.mvnforum.MVNForumConfig;
51 import com.mvnforum.MVNForumResourceBundle;
52 import com.mvnforum.auth.*;
53 import com.mvnforum.db.*;
54 import freemarker.template.TemplateException;
55 import net.myvietnam.mvncore.exception.*;
56 import net.myvietnam.mvncore.util.*;
57 import net.myvietnam.mvncore.web.GenericRequest;
58 import org.apache.commons.logging.Log;
59 import org.apache.commons.logging.LogFactory;
60
61 public class WatchWebHandler {
62
63     private static Log log = LogFactory.getLog(WatchWebHandler.class);
64
65     private OnlineUserManager userManager = OnlineUserManager.getInstance();
66
67     //private DateFormat dateFormat = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.DEFAULT, SimpleDateFormat.DEFAULT);
68

69     public WatchWebHandler() {
70     }
71
72     void sendMail()
73         throws AssertionException, DatabaseException, MessagingException JavaDoc, BadInputException, ObjectNotFoundException, TemplateException, IOException JavaDoc {
74
75         if (MVNForumConfig.getEnableWatch() == false) {
76             log.warn("Ingore Watch sendMail because this feature is disabled by administrator.");
77             return;
78         }
79         String JavaDoc forumBase = ParamUtil.getServerPath() + ParamUtil.getContextPath() + UserModuleConfig.getUrlPattern();
80         //log.debug("Forum base = " + forumBase);
81

82         //get the list of watch for each member, the watch is choosen based on oldest lastsent time
83
Collection beans = DAOFactory.getWatchDAO().getMemberBeans();
84         //log.debug("Watch: total member = " + beans.size());
85
Iterator iterator = beans.iterator();
86         while (iterator.hasNext()) {
87             WatchBean watchBean = (WatchBean)iterator.next();
88             int memberID = watchBean.getMemberID();
89
90             // check if member is enable here
91
if (DAOFactory.getMemberDAO().getActivateCode(memberID).equals(MemberBean.MEMBER_ACTIVATECODE_ACTIVATED) == false) {
92                 // Not activated, then we continue with the next user
93
continue;
94             }
95
96             // Check the frequency of the update
97
Timestamp JavaDoc lastSent = watchBean.getWatchLastSentDate();
98             Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
99
100             // We will now support a single watch standard, but should be
101
// bitmasked in future to support multiple watch schedule for the same
102
// thread or forum
103
long minimumWaitTime = 0;
104             int watchOption = watchBean.getWatchOption();
105             if (watchOption == WatchBean.WATCH_OPTION_DEFAULT) {
106                 watchOption = MVNForumConfig.getDefaultWatchOption();
107             }
108
109             switch (watchOption) {
110                 case WatchBean.WATCH_OPTION_LIVE:
111                     minimumWaitTime = 0;
112                     break;
113                 case WatchBean.WATCH_OPTION_HOURLY:
114                     minimumWaitTime = DateUtil.HOUR;
115                     break;
116                 case WatchBean.WATCH_OPTION_DAILY:
117                     minimumWaitTime = DateUtil.DAY;
118                     break;
119                 case WatchBean.WATCH_OPTION_WEEKLY:
120                     minimumWaitTime = DateUtil.WEEK;
121                     break;
122                 default:// currently only default is processed (WatchOption = 0)
123
// note that watch option might have any value so we have to have default fallback
124
minimumWaitTime = DateUtil.DAY;
125                     break;
126             }
127             if ( (now.getTime() - lastSent.getTime()) > minimumWaitTime ) {
128                 sendMail_forMember(memberID, forumBase, lastSent);
129             }
130         }
131     }
132
133     void sendMail_forMember(int memberID, String JavaDoc forumBase, Timestamp JavaDoc lastSent)
134         throws AssertionException, DatabaseException, MessagingException JavaDoc, BadInputException, ObjectNotFoundException, TemplateException, IOException JavaDoc {
135
136         MVNForumPermission permission = null;
137         try {
138             permission = MVNForumPermissionFactory.getAuthenticatedPermission(memberID);
139         } catch (AssertionException e) {
140             log.error("Cannot create watch mail for Guest with id = " + memberID, e);
141             return;// do nothing, just return if member is guest.
142
}
143
144         if (permission.isActivated() == false) {
145             // if member is not activated, then we ignore this member
146
return;
147         }
148
149         Collection watchBeans = DAOFactory.getWatchDAO().getWatches_forMember(memberID);
150         //log.debug("Watch size = " + watchBeans.size() + " for memberid = " + memberID);
151
Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
152
153         MemberBean receiver = null;
154         try {
155             receiver = DAOFactory.getMemberDAO().getMember_forViewCurrentMember(memberID);
156         } catch (ObjectNotFoundException e) {
157             log.error("Cannot get member with id = " + memberID, e);
158             return;// do nothing just return if member does not exist
159
}
160
161         //then optimize the watchBeans
162
watchBeans = WatchUtil.optimize(watchBeans);
163
164         WatchMail watchMail = new WatchMail(receiver, permission, forumBase, lastSent, now);
165
166         for (Iterator watchIterator = watchBeans.iterator(); watchIterator.hasNext(); ) {
167             WatchBean watchBean = (WatchBean)watchIterator.next();
168             watchMail.appendWatch(watchBean);
169         }
170
171         if (watchMail.haveAtLeastOneNewThread()) {
172             // send mail now
173
String JavaDoc from = MVNForumConfig.getWebMasterEmail(); //use the default MailFrom value
174
String JavaDoc to = receiver.getMemberEmail();
175             String JavaDoc subject = watchMail.getWatchSubject();
176             String JavaDoc content = watchMail.getMailContent();
177             //log.debug("Send message from websmater to ~ " + to + "~");
178
try {
179                 MailUtil.sendMail(from, to, "" /*cc*/, "" /*bcc*/, subject, content);
180             } catch (UnsupportedEncodingException JavaDoc e) {
181                 log.error("Cannot support encoding", e);
182             }
183
184             // finally, update the lastsent
185
DAOFactory.getWatchDAO().updateLastSentDate_forMember(memberID, now);
186         } else {
187             log.debug("No new thread for MemberID = " + memberID);
188         }
189     }
190
191     public void prepareList(GenericRequest request)
192         throws DatabaseException, AuthenticationException, AssertionException, ObjectNotFoundException {
193
194         OnlineUser onlineUser = userManager.getOnlineUser(request);
195         MVNForumPermission permission = onlineUser.getPermission();
196         permission.ensureIsAuthenticated();
197
198         int memberID = onlineUser.getMemberID();
199         Locale locale = I18nUtil.getLocaleInRequest(request);
200
201         Collection watchBeans = DAOFactory.getWatchDAO().getWatches_forMember(memberID);
202
203         Collection globalWatchBeans = WatchUtil.getGlobalWatchs(watchBeans);
204         Collection categoryWatchBeans = WatchUtil.getCategoryWatchs(watchBeans);
205         Collection forumWatchBeans = WatchUtil.getForumWatchs(watchBeans);
206         Collection threadWatchBeans = WatchUtil.getThreadWatchs(watchBeans);
207
208         // @todo Improve the performance of the below code
209
for (Iterator iter = threadWatchBeans.iterator(); iter.hasNext(); ) {
210             WatchBean threadWatchBean = (WatchBean)iter.next();
211             int threadID = threadWatchBean.getThreadID();
212
213             ThreadBean threadBean = null;
214             try {
215                 threadBean = DAOFactory.getThreadDAO().getThread(threadID);
216             } catch (ObjectNotFoundException e) {
217                 String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.threadid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(threadID)});
218                 throw new ObjectNotFoundException(localizedMessage);
219             }
220             threadWatchBean.setThreadBean(threadBean);
221         }
222
223         request.setAttribute("WatchBeans", watchBeans);
224         request.setAttribute("GlobalWatchBeans", globalWatchBeans);
225         request.setAttribute("CategoryWatchBeans", categoryWatchBeans);
226         request.setAttribute("ForumWatchBeans", forumWatchBeans);
227         request.setAttribute("ThreadWatchBeans", threadWatchBeans);
228     }
229
230     public void prepareAdd(GenericRequest request)
231         throws DatabaseException, AuthenticationException, AssertionException {
232
233         Locale locale = I18nUtil.getLocaleInRequest(request);
234
235         if (MVNForumConfig.getEnableWatch() == false) {
236             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_add_watch.watch_is_disabled");
237             throw new AssertionException(localizedMessage);
238             //throw new AssertionException("Cannot add Watch because Watch feature is disabled by administrator.");
239
}
240
241         OnlineUser onlineUser = userManager.getOnlineUser(request);
242         MVNForumPermission permission = onlineUser.getPermission();
243         permission.ensureIsAuthenticated();
244         if (MVNForumConfig.getEnableCompany() == false) {
245             permission.ensureIsActivated();
246         }
247     }
248
249     public void processAdd(GenericRequest request)
250         throws BadInputException, CreateException, DatabaseException, ObjectNotFoundException,
251         ForeignKeyNotFoundException, AuthenticationException, AssertionException {
252
253         Locale locale = I18nUtil.getLocaleInRequest(request);
254
255         if (MVNForumConfig.getEnableWatch() == false) {
256             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_add_watch.watch_is_disabled");
257             throw new AssertionException(localizedMessage);
258             //throw new AssertionException("Cannot add Watch because Watch feature is disabled by administrator.");
259
}
260
261         OnlineUser onlineUser = userManager.getOnlineUser(request);
262         MVNForumPermission permission = onlineUser.getPermission();
263         permission.ensureIsAuthenticated();
264         if (MVNForumConfig.getEnableCompany() == false) {
265             permission.ensureIsActivated();
266         }
267
268         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
269
270         int memberID = onlineUser.getMemberID();
271         int categoryID = 0;
272         int forumID = 0;
273         int threadID = 0;
274         int watchType = 0;//GenericParamUtil.getParameterInt(request, "WatchType");
275
int watchOption = WatchBean.WATCH_OPTION_DEFAULT;//GenericParamUtil.getParameterInt(request, "WatchOption");
276
int watchStatus = 0;//GenericParamUtil.getParameterInt(request, "WatchStatus");
277
Timestamp JavaDoc watchCreationDate = now;
278         Timestamp JavaDoc watchLastSentDate = now;
279         Timestamp JavaDoc watchEndDate = now;// @todo: check it !!!
280

281         int watchSelector = GenericParamUtil.getParameterInt(request, "WatchSelector");
282         switch (watchSelector) {
283             case 0:
284                 break;
285             case 1:
286                 categoryID = GenericParamUtil.getParameterInt(request, "category");
287                 break;
288             case 2:
289                 forumID = GenericParamUtil.getParameterInt(request, "forum");
290                 ForumCache.getInstance().getBean(forumID).ensureNotDisabledForum();
291                 break;
292             case 3:
293                 threadID = GenericParamUtil.getParameterInt(request, "thread");
294                 DAOFactory.getThreadDAO().findByPrimaryKey(threadID);
295                 break;
296             default:
297                 // please do not localize this
298
throw new AssertionException("Cannot process WatchSelector = " + watchSelector);
299         }
300
301         try {
302             DAOFactory.getWatchDAO().create(memberID, categoryID, forumID,
303                                        threadID, watchType, watchOption,
304                                        watchStatus, watchCreationDate, watchLastSentDate,
305                                        watchEndDate);
306         } catch (DuplicateKeyException ex) {
307             // User try to create a duplicate watch, just ignore
308
}
309     }
310
311     public void processDelete(GenericRequest request)
312         throws BadInputException, DatabaseException, AuthenticationException,
313         AssertionException, ObjectNotFoundException {
314
315         OnlineUser onlineUser = userManager.getOnlineUser(request);
316         MVNForumPermission permission = onlineUser.getPermission();
317         permission.ensureIsAuthenticated();
318
319         int memberID = onlineUser.getMemberID();
320
321         Locale locale = I18nUtil.getLocaleInRequest(request);
322
323         // primary key column(s)
324
int watchID = GenericParamUtil.getParameterInt(request, "watch");
325
326         WatchBean watchBean = DAOFactory.getWatchDAO().getWatch(watchID);
327
328         // check if the watch is owned by the current member
329
if (watchBean.getMemberID() != memberID) {
330             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete.watch_is_not_owned_by_current_member");
331             throw new BadInputException(localizedMessage);
332             //throw new BadInputException("Cannot delete watch: this watch is not owned by the current member.");
333
}
334
335         //now delete the watch
336
DAOFactory.getWatchDAO().delete(watchID);
337     }
338 }
339
Popular Tags