| 1 41 package com.mvnforum.user; 42 43 import java.io.IOException ; 44 import java.io.UnsupportedEncodingException ; 45 import java.sql.Timestamp ; 46 import java.util.*; 47 48 import javax.mail.MessagingException ; 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 69 public WatchWebHandler() { 70 } 71 72 void sendMail() 73 throws AssertionException, DatabaseException, MessagingException , BadInputException, ObjectNotFoundException, TemplateException, IOException { 74 75 if (MVNForumConfig.getEnableWatch() == false) { 76 log.warn("Ingore Watch sendMail because this feature is disabled by administrator."); 77 return; 78 } 79 String forumBase = ParamUtil.getServerPath() + ParamUtil.getContextPath() + UserModuleConfig.getUrlPattern(); 80 82 Collection beans = DAOFactory.getWatchDAO().getMemberBeans(); 84 Iterator iterator = beans.iterator(); 86 while (iterator.hasNext()) { 87 WatchBean watchBean = (WatchBean)iterator.next(); 88 int memberID = watchBean.getMemberID(); 89 90 if (DAOFactory.getMemberDAO().getActivateCode(memberID).equals(MemberBean.MEMBER_ACTIVATECODE_ACTIVATED) == false) { 92 continue; 94 } 95 96 Timestamp lastSent = watchBean.getWatchLastSentDate(); 98 Timestamp now = DateUtil.getCurrentGMTTimestamp(); 99 100 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: 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 forumBase, Timestamp lastSent) 134 throws AssertionException, DatabaseException, MessagingException , BadInputException, ObjectNotFoundException, TemplateException, IOException { 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; } 143 144 if (permission.isActivated() == false) { 145 return; 147 } 148 149 Collection watchBeans = DAOFactory.getWatchDAO().getWatches_forMember(memberID); 150 Timestamp 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; } 160 161 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 String from = MVNForumConfig.getWebMasterEmail(); String to = receiver.getMemberEmail(); 175 String subject = watchMail.getWatchSubject(); 176 String content = watchMail.getMailContent(); 177 try { 179 MailUtil.sendMail(from, to, "" , "" , subject, content); 180 } catch (UnsupportedEncodingException e) { 181 log.error("Cannot support encoding", e); 182 } 183 184 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 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 localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.threadid_not_exists", new Object [] {new Integer (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 localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_add_watch.watch_is_disabled"); 237 throw new AssertionException(localizedMessage); 238 } 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 localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_add_watch.watch_is_disabled"); 257 throw new AssertionException(localizedMessage); 258 } 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 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; int watchOption = WatchBean.WATCH_OPTION_DEFAULT; int watchStatus = 0; Timestamp watchCreationDate = now; 278 Timestamp watchLastSentDate = now; 279 Timestamp watchEndDate = now; 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 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 } 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 int watchID = GenericParamUtil.getParameterInt(request, "watch"); 325 326 WatchBean watchBean = DAOFactory.getWatchDAO().getWatch(watchID); 327 328 if (watchBean.getMemberID() != memberID) { 330 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete.watch_is_not_owned_by_current_member"); 331 throw new BadInputException(localizedMessage); 332 } 334 335 DAOFactory.getWatchDAO().delete(watchID); 337 } 338 } 339 | Popular Tags |