KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > forum > common > TopicsCommon


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * Created on 17/10/2004 23:54:47
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.forum.common;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Map JavaDoc;
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 /**
77  * General utilities methods for topic manipulation.
78  *
79  * @author Rafael Steil
80  * @version $Id: TopicsCommon.java,v 1.28 2006/02/28 01:10:51 rafaelsteil Exp $
81  */

82 public class TopicsCommon
83 {
84     private static Logger logger = Logger.getLogger(TopicsCommon.class);
85     
86     /**
87      * List all first 'n' topics of a given forum.
88      * This method returns no more than <code>ConfigKeys.TOPICS_PER_PAGE</code>
89      * topics for the forum.
90      *
91      * @param forumId The forum id to which the topics belongs to
92      * @param start The start fetching index
93      * @return <code>java.util.List</code> containing the topics found.
94      * @throws Exception
95      */

96     public static List JavaDoc topicsByForum(int forumId, int start) throws Exception JavaDoc
97     {
98         TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
99         int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
100         List JavaDoc topics = null;
101         
102         // Try to get the first's page of topics from the cache
103
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     /**
119      * Prepare the topics for listing.
120      * This method does some preparation for a set ot <code>net.jforum.entities.Topic</code>
121      * instances for the current user, like verification if the user already
122      * read the topic, if pagination is a need and so on.
123      *
124      * @param topics The topics to process
125      * @return The post-processed topics.
126      */

127     public static List JavaDoc prepareTopics(List JavaDoc 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 JavaDoc topicsTracking = (HashMap JavaDoc)SessionFacade.getAttribute(ConfigKeys.TOPICS_TRACKING);
136         List JavaDoc newTopics = new ArrayList JavaDoc(topics.size());
137         
138         boolean checkUnread = (userSession.getUserId()
139             != SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID));
140         
141         for (Iterator JavaDoc 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 JavaDoc(t.getId()))) {
147                     read = (((Long JavaDoc)topicsTracking.get(new Integer JavaDoc(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 JavaDoc(Math.floor(t.getTotalReplies() / postsPerPage)));
157             }
158             else {
159                 t.setPaginate(false);
160                 t.setTotalPages(new Double JavaDoc(0));
161             }
162             
163             // Check if this is a hot topic
164
t.setHot(t.getTotalReplies() >= hotBegin);
165             
166             t.setRead(read);
167             newTopics.add(t);
168         }
169         
170         return newTopics;
171     }
172
173     /**
174      * Common properties to be used when showing topic data
175      */

176     public static void topicListingBase() throws Exception JavaDoc
177     {
178         SimpleHash context = JForumExecutionContext.getTemplateContext();
179         
180         // Topic Types
181
context.put("TOPIC_ANNOUNCE", new Integer JavaDoc(Topic.TYPE_ANNOUNCE));
182         context.put("TOPIC_STICKY", new Integer JavaDoc(Topic.TYPE_STICKY));
183         context.put("TOPIC_NORMAL", new Integer JavaDoc(Topic.TYPE_NORMAL));
184     
185         // Topic Status
186
context.put("STATUS_LOCKED", new Integer JavaDoc(Topic.STATUS_LOCKED));
187         context.put("STATUS_UNLOCKED", new Integer JavaDoc(Topic.STATUS_UNLOCKED));
188         
189         // Moderation
190
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     /**
200      * Checks if the user is allowed to view the topic.
201      * If there currently logged user does not have access
202      * to the forum, the template context will be set to show
203      * an error message to the user, by calling
204      * <blockquote>new ModerationHelper().denied(I18n.getMessage("PostShow.denied"))</blockquote>
205      * @param forumId The forum id to which the topics belongs to
206      * @return <code>true</code> if the topic is accessible, <code>false</code> otherwise
207      * @throws Exception a
208      */

209     public static boolean isTopicAccessible(int forumId) throws Exception JavaDoc
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     /**
222      * Sends a "new post" notification message to all users watching the topic.
223      *
224      * @param t The changed topic
225      * @param tm A TopicModel instance
226      * @throws Exception
227      */

228     public static void notifyUsers(Topic t, TopicDAO tm) throws Exception JavaDoc
229     {
230         if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) {
231             try {
232                 List JavaDoc usersToNotify = tm.notifyUsers(t);
233
234                 // we only have to send an email if there are users
235
// subscribed to the topic
236
if (usersToNotify != null && usersToNotify.size() > 0) {
237                     QueuedExecutor.getInstance().execute(
238                             new EmailSenderTask(new TopicSpammer(t, usersToNotify)));
239                 }
240             }
241             catch (Exception JavaDoc e) {
242                 logger.warn("Error while sending notification emails: " + e);
243             }
244         }
245     }
246     
247     /**
248      * Updates the board status after a new post is inserted.
249      * This method is used in conjunct with moderation manipulation.
250      * It will increase by 1 the number of replies of the tpoic, set the
251      * last post id for the topic and the forum and refresh the cache.
252      *
253      * @param t The topic to update
254      * @param lastPostId The id of the last post
255      * @param tm A TopicModel instance
256      * @param fm A ForumModel instance
257      * @throws Exception
258      */

259     public static void updateBoardStatus(Topic t, int lastPostId, boolean firstPost, TopicDAO tm, ForumDAO fm) throws Exception JavaDoc
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     /**
281      * Deletes a topic.
282      * This method will remove the topic from the database,
283      * clear the entry frm the cache and update the last
284      * post info for the associated forum.
285      * @param topicId The topic id to remove
286      * @param fromModeration TODO
287      *
288      * @throws Exception
289      */

290     public static void deleteTopic(int topicId, int forumId, boolean fromModeration) throws Exception JavaDoc
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             // Updates the Recent Topics if it contains this topic
303
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