1 43 package net.jforum.view.forum.common; 44 45 import java.util.ArrayList ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import java.util.Map ; 50 51 import net.jforum.JForumExecutionContext; 52 import net.jforum.SessionFacade; 53 import net.jforum.dao.DataAccessDriver; 54 import net.jforum.dao.ForumDAO; 55 import net.jforum.dao.TopicDAO; 56 import net.jforum.entities.Forum; 57 import net.jforum.entities.Topic; 58 import net.jforum.entities.UserSession; 59 import net.jforum.repository.ForumRepository; 60 import net.jforum.repository.SecurityRepository; 61 import net.jforum.repository.TopicRepository; 62 import net.jforum.security.PermissionControl; 63 import net.jforum.security.SecurityConstants; 64 import net.jforum.util.I18n; 65 import net.jforum.util.concurrent.executor.QueuedExecutor; 66 import net.jforum.util.mail.EmailSenderTask; 67 import net.jforum.util.mail.TopicSpammer; 68 import net.jforum.util.preferences.ConfigKeys; 69 import net.jforum.util.preferences.SystemGlobals; 70 import net.jforum.view.forum.ModerationHelper; 71 72 import org.apache.log4j.Logger; 73 74 import freemarker.template.SimpleHash; 75 76 82 public class TopicsCommon 83 { 84 private static Logger logger = Logger.getLogger(TopicsCommon.class); 85 86 96 public static List topicsByForum(int forumId, int start) throws Exception 97 { 98 TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO(); 99 int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE); 100 List topics = null; 101 102 if (start == 0 && SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) { 104 topics = TopicRepository.getTopics(forumId); 105 106 if (topics.size() == 0 || !TopicRepository.isLoaded(forumId)) { 107 topics = tm.selectAllByForumByLimit(forumId, start, topicsPerPage); 108 TopicRepository.addAll(forumId, topics); 109 } 110 } 111 else { 112 topics = tm.selectAllByForumByLimit(forumId, start, topicsPerPage); 113 } 114 115 return topics; 116 } 117 118 127 public static List prepareTopics(List topics) 128 { 129 UserSession userSession = SessionFacade.getUserSession(); 130 131 long lastVisit = userSession.getLastVisit().getTime(); 132 int hotBegin = SystemGlobals.getIntValue(ConfigKeys.HOT_TOPIC_BEGIN); 133 134 int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE); 135 Map topicsTracking = (HashMap )SessionFacade.getAttribute(ConfigKeys.TOPICS_TRACKING); 136 List newTopics = new ArrayList (topics.size()); 137 138 boolean checkUnread = (userSession.getUserId() 139 != SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)); 140 141 for (Iterator iter = topics.iterator(); iter.hasNext(); ) { 142 boolean read = false; 143 Topic t = (Topic)iter.next(); 144 145 if (checkUnread && t.getLastPostDate().getTime() > lastVisit) { 146 if (topicsTracking.containsKey(new Integer (t.getId()))) { 147 read = (((Long )topicsTracking.get(new Integer (t.getId()))).longValue() > t.getLastPostDate().getTime()); 148 } 149 } 150 else { 151 read = true; 152 } 153 154 if (t.getTotalReplies() + 1 > postsPerPage) { 155 t.setPaginate(true); 156 t.setTotalPages(new Double (Math.floor(t.getTotalReplies() / postsPerPage))); 157 } 158 else { 159 t.setPaginate(false); 160 t.setTotalPages(new Double (0)); 161 } 162 163 t.setHot(t.getTotalReplies() >= hotBegin); 165 166 t.setRead(read); 167 newTopics.add(t); 168 } 169 170 return newTopics; 171 } 172 173 176 public static void topicListingBase() throws Exception 177 { 178 SimpleHash context = JForumExecutionContext.getTemplateContext(); 179 180 context.put("TOPIC_ANNOUNCE", new Integer (Topic.TYPE_ANNOUNCE)); 182 context.put("TOPIC_STICKY", new Integer (Topic.TYPE_STICKY)); 183 context.put("TOPIC_NORMAL", new Integer (Topic.TYPE_NORMAL)); 184 185 context.put("STATUS_LOCKED", new Integer (Topic.STATUS_LOCKED)); 187 context.put("STATUS_UNLOCKED", new Integer (Topic.STATUS_UNLOCKED)); 188 189 PermissionControl pc = SecurityRepository.get(SessionFacade.getUserSession().getUserId()); 191 192 context.put("moderator", pc.canAccess(SecurityConstants.PERM_MODERATION)); 193 context.put("can_remove_posts", pc.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE)); 194 context.put("can_move_topics", pc.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_MOVE)); 195 context.put("can_lockUnlock_topics", pc.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_LOCK_UNLOCK)); 196 context.put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED)); 197 } 198 199 209 public static boolean isTopicAccessible(int forumId) throws Exception 210 { 211 Forum f = ForumRepository.getForum(forumId); 212 213 if (f == null || !ForumRepository.isCategoryAccessible(f.getCategoryId())) { 214 new ModerationHelper().denied(I18n.getMessage("PostShow.denied")); 215 return false; 216 } 217 218 return true; 219 } 220 221 228 public static void notifyUsers(Topic t, TopicDAO tm) throws Exception 229 { 230 if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) { 231 try { 232 List usersToNotify = tm.notifyUsers(t); 233 234 if (usersToNotify != null && usersToNotify.size() > 0) { 237 QueuedExecutor.getInstance().execute( 238 new EmailSenderTask(new TopicSpammer(t, usersToNotify))); 239 } 240 } 241 catch (Exception e) { 242 logger.warn("Error while sending notification emails: " + e); 243 } 244 } 245 } 246 247 259 public static void updateBoardStatus(Topic t, int lastPostId, boolean firstPost, TopicDAO tm, ForumDAO fm) throws Exception 260 { 261 t.setLastPostId(lastPostId); 262 tm.update(t); 263 264 fm.setLastPost(t.getForumId(), lastPostId); 265 266 if (!firstPost) { 267 tm.incrementTotalReplies(t.getId()); 268 } 269 else { 270 fm.incrementTotalTopics(t.getForumId(), 1); 271 } 272 273 tm.incrementTotalViews(t.getId()); 274 275 TopicRepository.addTopic(t); 276 TopicRepository.pushTopic(t); 277 ForumRepository.incrementTotalMessages(); 278 } 279 280 290 public static void deleteTopic(int topicId, int forumId, boolean fromModeration) throws Exception 291 { 292 TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO(); 293 ForumDAO fm = DataAccessDriver.getInstance().newForumDAO(); 294 295 Topic topic = new Topic(); 296 topic.setId(topicId); 297 topic.setForumId(forumId); 298 299 tm.delete(topic); 300 301 if (!fromModeration) { 302 TopicRepository.popTopic(topic); 304 TopicRepository.loadMostRecentTopics(); 305 306 TopicRepository.remove(topic); 307 308 tm.removeSubscriptionByTopic(topicId); 309 fm.decrementTotalTopics(forumId, 1); 310 } 311 } 312 } 313 | Popular Tags |