001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 12/11/2004 18:04:12
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum.common;
044:
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048: import java.util.Map;
049:
050: import net.jforum.SessionFacade;
051: import net.jforum.dao.DataAccessDriver;
052: import net.jforum.dao.ForumDAO;
053: import net.jforum.entities.Category;
054: import net.jforum.entities.Forum;
055: import net.jforum.entities.LastPostInfo;
056: import net.jforum.entities.Post;
057: import net.jforum.entities.Topic;
058: import net.jforum.entities.UserSession;
059: import net.jforum.repository.ForumRepository;
060: import net.jforum.util.concurrent.Executor;
061: import net.jforum.util.mail.EmailSenderTask;
062: import net.jforum.util.mail.ForumNewTopicSpammer;
063: import net.jforum.util.preferences.ConfigKeys;
064: import net.jforum.util.preferences.SystemGlobals;
065:
066: import org.apache.log4j.Logger;
067:
068: /**
069: * @author Rafael Steil
070: * @version $Id: ForumCommon.java,v 1.24 2007/08/01 22:30:05 rafaelsteil Exp $
071: */
072: public class ForumCommon {
073: private static Logger logger = Logger.getLogger(ForumCommon.class);
074:
075: /**
076: * Check if some forum has unread messages.
077: * @param forum The forum to search for unread messages
078: * @param tracking Tracking of the topics read by the user
079: * @param lastVisit The last visit time of the current user
080: */
081: public static void checkUnreadPosts(Forum forum, Map tracking,
082: long lastVisit) {
083: LastPostInfo lpi = forum.getLastPostInfo();
084:
085: if (lpi == null) {
086: return;
087: }
088:
089: Integer topicId = new Integer(lpi.getTopicId());
090:
091: if (tracking != null && tracking.containsKey(topicId)) {
092: long readTime = ((Long) tracking.get(topicId)).longValue();
093:
094: forum.setUnread(readTime > 0
095: && lpi.getPostTimeMillis() > readTime);
096: } else {
097: forum.setUnread(lpi.getPostTimeMillis() > lastVisit);
098: }
099: }
100:
101: /**
102: * Gets all forums available to the user.
103: *
104: * @param us An <code>UserSession</code> instance with user information
105: * @param anonymousUserId The id which represents the anonymous user
106: * @param tracking <code>Map</code> instance with information
107: * about the topics read by the user
108: * @param checkUnreadPosts <code>true</code> if is to search for unread topics inside the forums,
109: * or <code>false</code> if this action is not needed.
110: * @return A <code>List</code> instance where each record is an instance of a <code>Category</code>
111: * object
112: */
113: public static List getAllCategoriesAndForums(UserSession us,
114: int anonymousUserId, Map tracking, boolean checkUnreadPosts) {
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
125: && (userId != anonymousUserId);
126:
127: List categories = ForumRepository.getAllCategories(userId);
128:
129: if (!checkUnreadPosts) {
130: return categories;
131: }
132:
133: List returnCategories = new ArrayList();
134: for (Iterator iter = categories.iterator(); iter.hasNext();) {
135: Category c = new Category((Category) iter.next());
136:
137: for (Iterator tmpIterator = c.getForums().iterator(); tmpIterator
138: .hasNext();) {
139: Forum f = (Forum) tmpIterator.next();
140: ForumCommon.checkUnreadPosts(f, tracking, lastVisit);
141: }
142:
143: returnCategories.add(c);
144: }
145:
146: return returnCategories;
147: }
148:
149: /**
150: * @see #getAllCategoriesAndForums(UserSession, int, Map, boolean)
151: * @return List
152: * @param checkUnreadPosts boolean
153: */
154: public static List getAllCategoriesAndForums(
155: boolean checkUnreadPosts) {
156: return getAllCategoriesAndForums(
157: SessionFacade.getUserSession(), SystemGlobals
158: .getIntValue(ConfigKeys.ANONYMOUS_USER_ID),
159: SessionFacade.getTopicsReadTime(), checkUnreadPosts);
160: }
161:
162: /**
163: * @see #getAllCategoriesAndForums(boolean)
164: * @return List
165: */
166: public static List getAllCategoriesAndForums() {
167: UserSession us = SessionFacade.getUserSession();
168: boolean checkUnread = (us != null && us.getUserId() != SystemGlobals
169: .getIntValue(ConfigKeys.ANONYMOUS_USER_ID));
170: return getAllCategoriesAndForums(checkUnread);
171: }
172:
173: /**
174: * Sends a "new topic" notification message to all users watching the forum.
175: *
176: * @param f The Forum changed
177: * @param t The new topic
178: * @param post the newly created message
179: */
180: public static void notifyUsers(Forum f, Topic t, Post post) {
181: if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) {
182: try {
183: ForumDAO dao = DataAccessDriver.getInstance()
184: .newForumDAO();
185: List usersToNotify = dao.notifyUsers(f);
186:
187: // we only have to send an email if there are users
188: // subscribed to the topic
189: if (usersToNotify != null && usersToNotify.size() > 0) {
190: Executor.execute(new EmailSenderTask(
191: new ForumNewTopicSpammer(f, t, post,
192: usersToNotify)));
193: }
194: } catch (Exception e) {
195: logger.warn("Error while sending notification emails: "
196: + e);
197: }
198: }
199: }
200: }
|