KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > forum > ModerationHelper


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  * This file creation date: 10/03/2004 - 18:43:12
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.forum;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48
49 import net.jforum.ActionServletRequest;
50 import net.jforum.JForumExecutionContext;
51 import net.jforum.dao.DataAccessDriver;
52 import net.jforum.dao.ForumDAO;
53 import net.jforum.dao.TopicDAO;
54 import net.jforum.entities.Forum;
55 import net.jforum.entities.Topic;
56 import net.jforum.repository.ForumRepository;
57 import net.jforum.repository.PostRepository;
58 import net.jforum.repository.SecurityRepository;
59 import net.jforum.repository.TopicRepository;
60 import net.jforum.security.SecurityConstants;
61 import net.jforum.util.I18n;
62 import net.jforum.util.preferences.TemplateKeys;
63 import net.jforum.view.forum.common.ForumCommon;
64
65 import org.apache.log4j.Logger;
66
67 import freemarker.template.SimpleHash;
68
69 /**
70  * @author Rafael Steil
71  * @version $Id: ModerationHelper.java,v 1.25 2006/01/29 15:06:56 rafaelsteil Exp $
72  */

73 public class ModerationHelper
74 {
75     private static Logger logger = Logger.getLogger(ModerationHelper.class);
76     
77     public static final int SUCCESS = 1;
78     public static final int FAILURE = 2;
79     public static final int IGNORE = 3;
80     
81     public int doModeration(String JavaDoc returnUrl) throws Exception JavaDoc
82     {
83         int status = FAILURE;
84
85         if (SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION)) {
86             // Deleting topics
87
ActionServletRequest request = JForumExecutionContext.getRequest();
88             
89             if (request.getParameter("topicRemove") != null) {
90                 if (SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE)) {
91                     this.removeTopics();
92                     
93                     status = SUCCESS;
94                 }
95             }
96             else if (request.getParameter("topicMove") != null) {
97                 if (SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_MOVE)) {
98                     this.moveTopics();
99                     
100                     status = IGNORE;
101                 }
102             }
103             else if (request.getParameter("topicLock") != null) {
104                 if (SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_LOCK_UNLOCK)) {
105                     this.lockUnlockTopics(Topic.STATUS_LOCKED);
106                     
107                     status = SUCCESS;
108                 }
109             }
110             else if (request.getParameter("topicUnlock") != null) {
111                 if (SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_LOCK_UNLOCK)) {
112                     this.lockUnlockTopics(Topic.STATUS_UNLOCKED);
113                     
114                     status = SUCCESS;
115                 }
116             }
117         }
118
119         if (status == ModerationHelper.FAILURE) {
120             this.denied();
121         }
122         else if (status == ModerationHelper.SUCCESS && returnUrl != null) {
123             JForumExecutionContext.setRedirect(returnUrl);
124         }
125         
126         return status;
127     }
128     
129     public int doModeration() throws Exception JavaDoc
130     {
131         return this.doModeration(null);
132     }
133     
134     private void removeTopics() throws Exception JavaDoc
135     {
136         String JavaDoc[] topics = JForumExecutionContext.getRequest().getParameterValues("topic_id");
137         
138         List JavaDoc forumsList = new ArrayList JavaDoc();
139         TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
140         
141         List JavaDoc topicsToDelete = new ArrayList JavaDoc();
142         
143         if (topics != null && topics.length > 0) {
144             for (int i = 0; i < topics.length; i++) {
145                 Topic t = tm.selectById(Integer.parseInt(topics[i]));
146                 
147                 if (!forumsList.contains(new Integer JavaDoc(t.getForumId()))) {
148                     forumsList.add(new Integer JavaDoc(t.getForumId()));
149                 }
150                 
151                 topicsToDelete.add(t);
152                 PostRepository.clearCache(t.getId());
153             }
154             
155             tm.deleteTopics(topicsToDelete);
156             
157             ForumDAO fm = DataAccessDriver.getInstance().newForumDAO();
158             TopicRepository.loadMostRecentTopics();
159             
160             // Reload changed forums
161
for (Iterator JavaDoc iter = forumsList.iterator(); iter.hasNext(); ) {
162                 int forumId = ((Integer JavaDoc)iter.next()).intValue();
163
164                 TopicRepository.clearCache(forumId);
165                 
166                 int postId = fm.getMaxPostId(forumId);
167                 if (postId > -1) {
168                     fm.setLastPost(forumId, postId);
169                 }
170                 else {
171                     logger.warn("Could not find last post id for forum " + forumId);
172                 }
173                 
174                 ForumRepository.reloadForum(forumId);
175             }
176         }
177     }
178     
179     private void lockUnlockTopics(int status) throws Exception JavaDoc
180     {
181         String JavaDoc[] topics = JForumExecutionContext.getRequest().getParameterValues("topic_id");
182         
183         if (topics != null && topics.length > 0) {
184             int[] ids = new int[topics.length];
185
186             for (int i = 0; i < topics.length; i++) {
187                 ids[i] = Integer.parseInt(topics[i]);
188             }
189             
190             DataAccessDriver.getInstance().newTopicDAO().lockUnlock(ids, status);
191             
192             // Clear the cache
193
Topic t = DataAccessDriver.getInstance().newTopicDAO().selectById(ids[0]);
194             TopicRepository.clearCache(t.getForumId());
195         }
196     }
197     
198     private void moveTopics() throws Exception JavaDoc
199     {
200         SimpleHash context = JForumExecutionContext.getTemplateContext();
201         
202         context.put("persistData", JForumExecutionContext.getRequest().getParameter("persistData"));
203         context.put("allCategories", ForumCommon.getAllCategoriesAndForums(false));
204         
205         String JavaDoc[] topics = JForumExecutionContext.getRequest().getParameterValues("topic_id");
206         
207         if (topics.length > 0) {
208             // If forum_id is null, get from the database
209
String JavaDoc forumId = JForumExecutionContext.getRequest().getParameter("forum_id");
210             
211             if (forumId == null) {
212                 int topicId = Integer.parseInt(topics[0]);
213                 
214                 Topic topic = TopicRepository.getTopic(new Topic(topicId));
215                 
216                 if (topic == null) {
217                     topic = DataAccessDriver.getInstance().newTopicDAO().selectRaw(topicId);
218                 }
219                 
220                 forumId = Integer.toString(topic.getForumId());
221             }
222             
223             context.put("forum_id", forumId);
224             
225             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(128);
226             
227             for (int i = 0; i < topics.length - 1; i++) {
228                 sb.append(topics[i]).append(",");
229             }
230             
231             sb.append(topics[topics.length - 1]);
232             
233             context.put("topics", sb.toString());
234         }
235     }
236     
237     public int moveTopicsSave(String JavaDoc successUrl) throws Exception JavaDoc
238     {
239         int status = SUCCESS;
240         
241         if (!SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_MOVE)) {
242             status = FAILURE;
243         }
244         else {
245             ActionServletRequest request = JForumExecutionContext.getRequest();
246             String JavaDoc topics = request.getParameter("topics");
247             
248             if (topics != null) {
249                 int fromForumId = Integer.parseInt(request.getParameter("forum_id"));
250                 int toForumId = Integer.parseInt(request.getParameter("to_forum"));
251                 
252                 DataAccessDriver.getInstance().newForumDAO().moveTopics(topics.split(","), fromForumId, toForumId);
253                 
254                 Forum fromForum = ForumRepository.getForum(fromForumId);
255                 
256                 ForumRepository.reloadForum(fromForumId);
257                 ForumRepository.reloadForum(toForumId);
258                 
259                 TopicRepository.clearCache(fromForumId);
260                 TopicRepository.clearCache(toForumId);
261                 
262                 TopicRepository.loadMostRecentTopics();
263             }
264         }
265         
266         if (status == FAILURE) {
267             this.denied();
268         }
269         else {
270             this.moderationDone(successUrl);
271         }
272         
273         return status;
274     }
275     
276     public String JavaDoc moderationDone(String JavaDoc redirectUrl)
277     {
278         JForumExecutionContext.getRequest().setAttribute("template", TemplateKeys.MODERATION_DONE);
279         JForumExecutionContext.getTemplateContext().put("message", I18n.getMessage("Moderation.ModerationDone", new String JavaDoc[] { redirectUrl }));
280         
281         return TemplateKeys.MODERATION_DONE;
282     }
283     
284     public void denied()
285     {
286         this.denied(I18n.getMessage("Moderation.Denied"));
287     }
288     
289     public void denied(String JavaDoc message)
290     {
291         JForumExecutionContext.getRequest().setAttribute("template", TemplateKeys.MODERATION_DENIED);
292         JForumExecutionContext.getTemplateContext().put("message", message);
293     }
294 }
295
Popular Tags