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: * This file creation date: 10/03/2004 - 18:43:12
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum;
044:
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: import net.jforum.JForumExecutionContext;
050: import net.jforum.SessionFacade;
051: import net.jforum.context.RequestContext;
052: import net.jforum.dao.DataAccessDriver;
053: import net.jforum.dao.ForumDAO;
054: import net.jforum.dao.ModerationLogDAO;
055: import net.jforum.dao.TopicDAO;
056: import net.jforum.entities.ModerationLog;
057: import net.jforum.entities.Topic;
058: import net.jforum.entities.User;
059: import net.jforum.repository.ForumRepository;
060: import net.jforum.repository.PostRepository;
061: import net.jforum.repository.SecurityRepository;
062: import net.jforum.repository.TopicRepository;
063: import net.jforum.security.SecurityConstants;
064: import net.jforum.util.I18n;
065: import net.jforum.util.preferences.TemplateKeys;
066: import net.jforum.view.forum.common.ForumCommon;
067:
068: import org.apache.log4j.Logger;
069:
070: import freemarker.template.SimpleHash;
071:
072: /**
073: * @author Rafael Steil
074: * @version $Id: ModerationHelper.java,v 1.43 2007/09/10 14:56:38 rafaelsteil Exp $
075: */
076: public class ModerationHelper {
077: private static Logger logger = Logger
078: .getLogger(ModerationHelper.class);
079:
080: public static final int SUCCESS = 1;
081: public static final int FAILURE = 2;
082: public static final int IGNORE = 3;
083:
084: public int doModeration(String returnUrl) {
085: int status = FAILURE;
086:
087: if (SecurityRepository
088: .canAccess(SecurityConstants.PERM_MODERATION)) {
089: // Deleting topics
090: RequestContext request = JForumExecutionContext
091: .getRequest();
092:
093: if (request.getParameter("topicRemove") != null) {
094: if (SecurityRepository
095: .canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE)) {
096: this .removeTopics();
097:
098: status = SUCCESS;
099: }
100: } else if (request.getParameter("topicMove") != null) {
101: if (SecurityRepository
102: .canAccess(SecurityConstants.PERM_MODERATION_TOPIC_MOVE)) {
103: this .moveTopics();
104:
105: status = IGNORE;
106: }
107: } else if (request.getParameter("topicLock") != null) {
108: if (SecurityRepository
109: .canAccess(SecurityConstants.PERM_MODERATION_TOPIC_LOCK_UNLOCK)) {
110: this .lockUnlockTopics(Topic.STATUS_LOCKED);
111:
112: status = SUCCESS;
113: }
114: } else if (request.getParameter("topicUnlock") != null) {
115: if (SecurityRepository
116: .canAccess(SecurityConstants.PERM_MODERATION_TOPIC_LOCK_UNLOCK)) {
117: this .lockUnlockTopics(Topic.STATUS_UNLOCKED);
118:
119: status = SUCCESS;
120: }
121: }
122: }
123:
124: if (status == ModerationHelper.FAILURE) {
125: this .denied();
126: } else if (status == ModerationHelper.SUCCESS
127: && returnUrl != null) {
128: JForumExecutionContext.setRedirect(returnUrl);
129: }
130:
131: return status;
132: }
133:
134: public void saveModerationLog(ModerationLog log) {
135: ModerationLogDAO dao = DataAccessDriver.getInstance()
136: .newModerationLogDAO();
137: dao.add(log);
138: }
139:
140: public ModerationLog buildModerationLogFromRequest() {
141: RequestContext request = JForumExecutionContext.getRequest();
142:
143: ModerationLog log = new ModerationLog();
144:
145: User user = new User();
146: user.setId(SessionFacade.getUserSession().getUserId());
147: log.setUser(user);
148:
149: log.setDescription(request.getParameter("log_description"));
150: log.setOriginalMessage(request
151: .getParameter("log_original_message"));
152: log.setType(request.getIntParameter("log_type"));
153:
154: if (request.getParameter("post_id") != null) {
155: log.setPostId(request.getIntParameter("post_id"));
156: }
157:
158: String[] values = request.getParameterValues("topic_id");
159:
160: if (values != null && values.length == 1) {
161: log.setTopicId(request.getIntParameter("topic_id"));
162: }
163:
164: return log;
165: }
166:
167: public int doModeration() {
168: return this .doModeration(null);
169: }
170:
171: private void removeTopics() {
172: String[] topics = JForumExecutionContext.getRequest()
173: .getParameterValues("topic_id");
174:
175: List forumsList = new ArrayList();
176: TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
177:
178: List topicsToDelete = new ArrayList();
179:
180: if (topics != null && topics.length > 0) {
181: ModerationLog log = this .buildModerationLogFromRequest();
182:
183: for (int i = 0; i < topics.length; i++) {
184: Topic t = tm.selectRaw(Integer.parseInt(topics[i]));
185:
186: log.setTopicId(t.getId());
187: log.setPosterUser(t.getPostedBy());
188:
189: this .saveModerationLog(log);
190:
191: if (!forumsList.contains(new Integer(t.getForumId()))) {
192: forumsList.add(new Integer(t.getForumId()));
193: }
194:
195: topicsToDelete.add(t);
196: PostRepository.clearCache(t.getId());
197: }
198:
199: tm.deleteTopics(topicsToDelete, false);
200:
201: ForumDAO fm = DataAccessDriver.getInstance().newForumDAO();
202: TopicRepository.loadMostRecentTopics();
203:
204: // Reload changed forums
205: for (Iterator iter = forumsList.iterator(); iter.hasNext();) {
206: int forumId = ((Integer) iter.next()).intValue();
207:
208: TopicRepository.clearCache(forumId);
209:
210: int postId = fm.getMaxPostId(forumId);
211:
212: if (postId > -1) {
213: fm.setLastPost(forumId, postId);
214: } else {
215: logger
216: .warn("Could not find last post id for forum "
217: + forumId);
218: }
219:
220: ForumRepository.reloadForum(forumId);
221: }
222: }
223: }
224:
225: private void lockUnlockTopics(int status) {
226: String[] topics = JForumExecutionContext.getRequest()
227: .getParameterValues("topic_id");
228:
229: if (topics != null && topics.length > 0) {
230: int[] ids = new int[topics.length];
231:
232: ModerationLog log = this .buildModerationLogFromRequest();
233:
234: for (int i = 0; i < topics.length; i++) {
235: ids[i] = Integer.parseInt(topics[i]);
236: log.setTopicId(ids[i]);
237: this .saveModerationLog(log);
238: }
239:
240: DataAccessDriver.getInstance().newTopicDAO().lockUnlock(
241: ids, status);
242:
243: // Clear the cache
244: Topic t = DataAccessDriver.getInstance().newTopicDAO()
245: .selectById(ids[0]);
246: TopicRepository.clearCache(t.getForumId());
247: }
248: }
249:
250: private void moveTopics() {
251: SimpleHash context = JForumExecutionContext
252: .getTemplateContext();
253:
254: context.put("persistData", JForumExecutionContext.getRequest()
255: .getParameter("persistData"));
256: context.put("allCategories", ForumCommon
257: .getAllCategoriesAndForums(false));
258:
259: String[] topics = JForumExecutionContext.getRequest()
260: .getParameterValues("topic_id");
261:
262: if (topics.length > 0) {
263: // If forum_id is null, get from the database
264: String forumId = JForumExecutionContext.getRequest()
265: .getParameter("forum_id");
266:
267: if (forumId == null) {
268: int topicId = Integer.parseInt(topics[0]);
269:
270: Topic topic = TopicRepository.getTopic(new Topic(
271: topicId));
272:
273: if (topic == null) {
274: topic = DataAccessDriver.getInstance()
275: .newTopicDAO().selectRaw(topicId);
276: }
277:
278: forumId = Integer.toString(topic.getForumId());
279: }
280:
281: context.put("forum_id", forumId);
282:
283: StringBuffer sb = new StringBuffer(128);
284:
285: for (int i = 0; i < topics.length - 1; i++) {
286: sb.append(topics[i]).append(",");
287: }
288:
289: sb.append(topics[topics.length - 1]);
290:
291: context.put("topics", sb.toString());
292: }
293: }
294:
295: public int moveTopicsSave(String successUrl) {
296: int status = SUCCESS;
297:
298: if (!SecurityRepository
299: .canAccess(SecurityConstants.PERM_MODERATION_TOPIC_MOVE)) {
300: status = FAILURE;
301: } else {
302: RequestContext request = JForumExecutionContext
303: .getRequest();
304: String topics = request.getParameter("topics");
305:
306: if (topics != null) {
307: int fromForumId = Integer.parseInt(request
308: .getParameter("forum_id"));
309: int toForumId = Integer.parseInt(request
310: .getParameter("to_forum"));
311:
312: String[] topicList = topics.split(",");
313:
314: DataAccessDriver.getInstance().newForumDAO()
315: .moveTopics(topicList, fromForumId, toForumId);
316:
317: ModerationLog log = this
318: .buildModerationLogFromRequest();
319:
320: for (int i = 0; i < topicList.length; i++) {
321: int topicId = Integer.parseInt(topicList[i]);
322: log.setTopicId(topicId);
323: this .saveModerationLog(log);
324: }
325:
326: ForumRepository.reloadForum(fromForumId);
327: ForumRepository.reloadForum(toForumId);
328:
329: TopicRepository.clearCache(fromForumId);
330: TopicRepository.clearCache(toForumId);
331:
332: TopicRepository.loadMostRecentTopics();
333: TopicRepository.loadHottestTopics();
334: }
335: }
336:
337: if (status == FAILURE) {
338: this .denied();
339: } else {
340: this .moderationDone(successUrl);
341: }
342:
343: return status;
344: }
345:
346: public String moderationDone(String redirectUrl) {
347: JForumExecutionContext.getRequest().setAttribute("template",
348: TemplateKeys.MODERATION_DONE);
349: JForumExecutionContext.getTemplateContext().put(
350: "message",
351: I18n.getMessage("Moderation.ModerationDone",
352: new String[] { redirectUrl }));
353:
354: return TemplateKeys.MODERATION_DONE;
355: }
356:
357: public void denied() {
358: this .denied(I18n.getMessage("Moderation.Denied"));
359: }
360:
361: public void denied(String message) {
362: JForumExecutionContext.getRequest().setAttribute("template",
363: TemplateKeys.MODERATION_DENIED);
364: JForumExecutionContext.getTemplateContext().put("message",
365: message);
366: }
367: }
|