KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003, 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 12/11/2004 18:04:12
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.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Map JavaDoc;
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 /**
67  * @author Rafael Steil
68  * @version $Id: ForumCommon.java,v 1.13 2006/02/21 13:59:48 rafaelsteil Exp $
69  */

70 public class ForumCommon
71 {
72     private static Logger logger = Logger.getLogger(ForumCommon.class);
73     /**
74      * Check if some forum has unread messages.
75      * @param forum The forum to search for unread messages
76      * @param tracking Tracking of the topics read by the user
77      * @param lastVisit The last visit time of the current user
78      */

79     public static void checkUnreadPosts(Forum forum, Map JavaDoc tracking, long lastVisit) throws Exception JavaDoc
80     {
81         LastPostInfo lpi = forum.getLastPostInfo();
82         
83         if (lpi == null) {
84             return;
85         }
86
87         Integer JavaDoc topicId = new Integer JavaDoc(lpi.getTopicId());
88         
89         if (tracking != null && tracking.containsKey(topicId)) {
90             long readTime = ((Long JavaDoc)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     /**
100      * Gets all forums available to the user.
101      *
102      * @param us An <code>UserSession</code> instance with user information
103      * @param anonymousUserId The id which represents the anonymous user
104      * @param tracking <code>Map</code> instance with information
105      * about the topics read by the user
106      * @param checkUnreadPosts <code>true</code> if is to search for unread topics inside the forums,
107      * or <code>false</code> if this action is not needed.
108      * @return A <code>List</code> instance where each record is an instance of a <code>Category</code>
109      * object
110      * @throws Exception
111      */

112     public static List JavaDoc getAllCategoriesAndForums(UserSession us, int anonymousUserId,
113             Map JavaDoc tracking, boolean checkUnreadPosts) throws Exception JavaDoc
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         // Do not check for unread posts if the user is not logged in
124
checkUnreadPosts = checkUnreadPosts && (us.getUserId() != anonymousUserId);
125
126         List JavaDoc categories = ForumRepository.getAllCategories(userId);
127         
128         if (!checkUnreadPosts) {
129             return categories;
130         }
131
132         List JavaDoc returnCategories = new ArrayList JavaDoc();
133         for (Iterator JavaDoc iter = categories.iterator(); iter.hasNext(); ) {
134             Category c = new Category((Category)iter.next());
135             
136             for (Iterator JavaDoc 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     /**
148      * @see #getAllCategoriesAndForums(UserSession, int, Map, boolean)
149      */

150     public static List JavaDoc getAllCategoriesAndForums(boolean checkUnreadPosts) throws Exception JavaDoc
151     {
152         return getAllCategoriesAndForums(SessionFacade.getUserSession(),
153                 SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID),
154                 (Map JavaDoc)SessionFacade.getAttribute(ConfigKeys.TOPICS_TRACKING),
155                 checkUnreadPosts);
156     }
157     
158     /**
159      * @see #getAllCategoriesAndForums(boolean)
160      */

161     public static List JavaDoc getAllCategoriesAndForums() throws Exception JavaDoc
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     /**
170      * Sends a "new topic" notification message to all users watching the forum.
171      *
172      * @param f The Forum changed
173      * @param t The new topic
174      * @param tm A ForumModel instance
175      * @throws Exception
176      */

177     public static void notifyUsers(Forum f, Topic t, ForumDAO tm) throws Exception JavaDoc
178     {
179         if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) {
180             try {
181                 List JavaDoc usersToNotify = tm.notifyUsers(f);
182
183                 // we only have to send an email if there are users
184
// subscribed to the topic
185
if (usersToNotify != null && usersToNotify.size() > 0) {
186                     QueuedExecutor.getInstance().execute(
187                             new EmailSenderTask(new ForumSpammer(f, t, usersToNotify)));
188                 }
189             }
190             catch (Exception JavaDoc e) {
191                 logger.warn("Error while sending notification emails: " + e);
192             }
193         }
194     }
195 }
196
Popular Tags