1 43 package net.jforum.view.forum.common; 44 45 import java.util.ArrayList ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.Map ; 49 50 import net.jforum.SessionFacade; 51 import net.jforum.dao.ForumDAO; 52 import net.jforum.entities.Category; 53 import net.jforum.entities.Forum; 54 import net.jforum.entities.LastPostInfo; 55 import net.jforum.entities.Topic; 56 import net.jforum.entities.UserSession; 57 import net.jforum.repository.ForumRepository; 58 import net.jforum.util.concurrent.executor.QueuedExecutor; 59 import net.jforum.util.mail.EmailSenderTask; 60 import net.jforum.util.mail.ForumSpammer; 61 import net.jforum.util.preferences.ConfigKeys; 62 import net.jforum.util.preferences.SystemGlobals; 63 64 import org.apache.log4j.Logger; 65 66 70 public class ForumCommon 71 { 72 private static Logger logger = Logger.getLogger(ForumCommon.class); 73 79 public static void checkUnreadPosts(Forum forum, Map tracking, long lastVisit) throws Exception 80 { 81 LastPostInfo lpi = forum.getLastPostInfo(); 82 83 if (lpi == null) { 84 return; 85 } 86 87 Integer topicId = new Integer (lpi.getTopicId()); 88 89 if (tracking != null && tracking.containsKey(topicId)) { 90 long readTime = ((Long )tracking.get(topicId)).longValue(); 91 92 forum.setUnread(readTime > 0 && lpi.getPostTimeMillis() > readTime); 93 } 94 else { 95 forum.setUnread(lpi.getPostTimeMillis() > lastVisit); 96 } 97 } 98 99 112 public static List getAllCategoriesAndForums(UserSession us, int anonymousUserId, 113 Map tracking, boolean checkUnreadPosts) throws Exception 114 { 115 long lastVisit = 0; 116 int userId = anonymousUserId; 117 118 if (us != null) { 119 lastVisit = us.getLastVisit().getTime(); 120 userId = us.getUserId(); 121 } 122 123 checkUnreadPosts = checkUnreadPosts && (us.getUserId() != anonymousUserId); 125 126 List categories = ForumRepository.getAllCategories(userId); 127 128 if (!checkUnreadPosts) { 129 return categories; 130 } 131 132 List returnCategories = new ArrayList (); 133 for (Iterator iter = categories.iterator(); iter.hasNext(); ) { 134 Category c = new Category((Category)iter.next()); 135 136 for (Iterator tmpIterator = c.getForums().iterator(); tmpIterator.hasNext(); ) { 137 Forum f = (Forum)tmpIterator.next(); 138 ForumCommon.checkUnreadPosts(f, tracking, us.getLastVisit().getTime()); 139 } 140 141 returnCategories.add(c); 142 } 143 144 return returnCategories; 145 } 146 147 150 public static List getAllCategoriesAndForums(boolean checkUnreadPosts) throws Exception 151 { 152 return getAllCategoriesAndForums(SessionFacade.getUserSession(), 153 SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID), 154 (Map )SessionFacade.getAttribute(ConfigKeys.TOPICS_TRACKING), 155 checkUnreadPosts); 156 } 157 158 161 public static List getAllCategoriesAndForums() throws Exception 162 { 163 UserSession us = SessionFacade.getUserSession(); 164 boolean checkUnread = (us != null && us.getUserId() 165 != SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)); 166 return getAllCategoriesAndForums(checkUnread); 167 } 168 169 177 public static void notifyUsers(Forum f, Topic t, ForumDAO tm) throws Exception 178 { 179 if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) { 180 try { 181 List usersToNotify = tm.notifyUsers(f); 182 183 if (usersToNotify != null && usersToNotify.size() > 0) { 186 QueuedExecutor.getInstance().execute( 187 new EmailSenderTask(new ForumSpammer(f, t, usersToNotify))); 188 } 189 } 190 catch (Exception e) { 191 logger.warn("Error while sending notification emails: " + e); 192 } 193 } 194 } 195 } 196 | Popular Tags |