KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > admin > ForumAction


1 /*
2  * Copyright (c) 2003, 2004 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: Mar 28, 2003 / 8:21:56 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.admin;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.List JavaDoc;
47
48 import net.jforum.dao.CategoryDAO;
49 import net.jforum.dao.DataAccessDriver;
50 import net.jforum.dao.ForumDAO;
51 import net.jforum.dao.TopicDAO;
52 import net.jforum.dao.security.GroupSecurityDAO;
53 import net.jforum.entities.Category;
54 import net.jforum.entities.Forum;
55 import net.jforum.repository.ForumRepository;
56 import net.jforum.repository.SecurityRepository;
57 import net.jforum.repository.RolesRepository;
58 import net.jforum.security.PermissionControl;
59 import net.jforum.security.Role;
60 import net.jforum.security.RoleValue;
61 import net.jforum.security.RoleValueCollection;
62 import net.jforum.security.SecurityConstants;
63 import net.jforum.util.TreeGroup;
64 import net.jforum.util.preferences.TemplateKeys;
65 import net.jforum.view.admin.common.ModerationCommon;
66
67 /**
68  * @author Rafael Steil
69  * @version $Id: ForumAction.java,v 1.24 2005/09/15 22:47:32 vmal Exp $
70  */

71 public class ForumAction extends AdminCommand
72 {
73     // Listing
74
public void list() throws Exception JavaDoc
75     {
76         this.context.put("categories", DataAccessDriver.getInstance().newCategoryDAO().selectAll());
77         this.context.put("repository", new ForumRepository());
78         this.setTemplateName(TemplateKeys.FORUM_ADMIN_LIST);
79     }
80     
81     // One more, one more
82
public void insert() throws Exception JavaDoc
83     {
84         CategoryDAO cm = DataAccessDriver.getInstance().newCategoryDAO();
85         
86         this.context.put("groups", new TreeGroup().getNodes());
87         this.context.put("selectedList", new ArrayList JavaDoc());
88         this.setTemplateName(TemplateKeys.FORUM_ADMIN_INSERT);
89         this.context.put("categories",cm.selectAll());
90         this.context.put("action", "insertSave");
91     }
92     
93     // Edit
94
public void edit() throws Exception JavaDoc
95     {
96         CategoryDAO cm = DataAccessDriver.getInstance().newCategoryDAO();
97         
98         this.context.put("forum", DataAccessDriver.getInstance().newForumDAO().selectById(
99                 this.request.getIntParameter("forum_id")));
100         this.context.put("categories", cm.selectAll());
101         this.setTemplateName(TemplateKeys.FORUM_ADMIN_EDIT);
102         this.context.put("action", "editSave");
103     }
104     
105     // Save information
106
public void editSave() throws Exception JavaDoc
107     {
108         Forum f = new Forum(ForumRepository.getForum(this.request.getIntParameter("forum_id")));
109         boolean moderated = f.isModerated();
110         int categoryId = f.getCategoryId();
111         
112         f.setDescription(this.request.getParameter("description"));
113         f.setIdCategories(this.request.getIntParameter("categories_id"));
114         f.setName(this.request.getParameter("forum_name"));
115         f.setModerated("1".equals(this.request.getParameter("moderate")));
116
117         DataAccessDriver.getInstance().newForumDAO().update(f);
118
119         if (moderated != f.isModerated()) {
120             new ModerationCommon().setTopicModerationStatus(f.getId(), f.isModerated());
121         }
122         
123         if (categoryId != f.getCategoryId()) {
124             f.setIdCategories(categoryId);
125             ForumRepository.removeForum(f);
126             
127             f.setIdCategories(this.request.getIntParameter("categories_id"));
128             ForumRepository.addForum(f);
129         }
130         else {
131             ForumRepository.reloadForum(f.getId());
132         }
133         
134         this.list();
135     }
136     
137     public void up() throws Exception JavaDoc
138     {
139         this.processOrdering(true);
140     }
141     
142     public void down() throws Exception JavaDoc
143     {
144         this.processOrdering(false);
145     }
146     
147     private void processOrdering(boolean up) throws Exception JavaDoc
148     {
149         Forum toChange = new Forum(ForumRepository.getForum(Integer.parseInt(
150                 this.request.getParameter("forum_id"))));
151         
152         Category category = ForumRepository.getCategory(toChange.getCategoryId());
153         List JavaDoc forums = new ArrayList JavaDoc(category.getForums());
154         int index = forums.indexOf(toChange);
155         
156         if (index == -1 || (up && index == 0) || (!up && index + 1 == forums.size())) {
157             this.list();
158             return;
159         }
160         
161         ForumDAO fm = DataAccessDriver.getInstance().newForumDAO();
162         
163         if (up) {
164             // Get the forum which comes *before* the forum we're changing
165
Forum otherForum = new Forum((Forum)forums.get(index - 1));
166             fm.setOrderUp(toChange, otherForum);
167         }
168         else {
169             // Get the forum which comes *after* the forum we're changing
170
Forum otherForum = new Forum((Forum)forums.get(index + 1));
171             fm.setOrderDown(toChange, otherForum);
172         }
173         
174         category.changeForumOrder(toChange);
175         ForumRepository.refreshCategory(category);
176         
177         this.list();
178     }
179     
180     // Delete
181
public void delete() throws Exception JavaDoc
182     {
183         String JavaDoc ids[] = this.request.getParameterValues("forum_id");
184         
185         ForumDAO fm = DataAccessDriver.getInstance().newForumDAO();
186         TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
187         
188         if (ids != null) {
189             for (int i = 0; i < ids.length; i++) {
190                 int forumId = Integer.parseInt(ids[i]);
191
192                 tm.deleteByForum(forumId);
193                 fm.delete(forumId);
194                 
195                 Forum f = new Forum(ForumRepository.getForum(forumId));
196                 ForumRepository.removeForum(f);
197             }
198         }
199         
200         this.list();
201     }
202     
203     // A new one
204
public void insertSave() throws Exception JavaDoc
205     {
206         Forum f = new Forum();
207         f.setDescription(this.request.getParameter("description"));
208         f.setIdCategories(this.request.getIntParameter("categories_id"));
209         f.setName(this.request.getParameter("forum_name"));
210         f.setModerated("1".equals(this.request.getParameter("moderate")));
211             
212         int forumId = DataAccessDriver.getInstance().newForumDAO().addNew(f);
213         f.setId(forumId);
214         
215         ForumRepository.addForum(f);
216         
217         GroupSecurityDAO gmodel = DataAccessDriver.getInstance().newGroupSecurityDAO();
218         PermissionControl pc = new PermissionControl();
219         pc.setSecurityModel(gmodel);
220         
221         String JavaDoc[] allGroups = this.request.getParameterValues("groups");
222         
223         // Access
224
String JavaDoc[] groups = this.request.getParameterValues("groupsAccess");
225         if (groups != null) {
226             this.addRole(pc, SecurityConstants.PERM_FORUM, f.getId(), groups, false);
227         }
228         else {
229             this.addRole(pc, SecurityConstants.PERM_FORUM, f.getId(), allGroups, true);
230         }
231         
232         // Anonymous posts
233
groups = this.request.getParameterValues("groupsAnonymous");
234         if (groups != null) {
235             this.addRole(pc, SecurityConstants.PERM_ANONYMOUS_POST, f.getId(), groups, false);
236         }
237         else {
238             this.addRole(pc, SecurityConstants.PERM_ANONYMOUS_POST, f.getId(), allGroups, true);
239         }
240         
241         // Read-only
242
groups = this.request.getParameterValues("groupsReadOnly");
243         if (groups != null) {
244             this.addRole(pc, SecurityConstants.PERM_READ_ONLY_FORUMS, f.getId(), groups, false);
245         }
246         else {
247             this.addRole(pc, SecurityConstants.PERM_READ_ONLY_FORUMS, f.getId(), allGroups, true);
248         }
249         
250         // Reply-only
251
this.addRole(pc, SecurityConstants.PERM_REPLY_ONLY, f.getId(), allGroups, true);
252         
253         // HTML
254
groups = this.request.getParameterValues("groupsHtml");
255         if (groups != null) {
256             this.addRole(pc, SecurityConstants.PERM_HTML_DISABLED, f.getId(), groups, false);
257         }
258         else {
259             this.addRole(pc, SecurityConstants.PERM_HTML_DISABLED, f.getId(), allGroups, true);
260         }
261         
262         SecurityRepository.clean();
263         RolesRepository.clear();
264
265         this.list();
266     }
267     
268     private void addRole(PermissionControl pc, String JavaDoc roleName, int forumId, String JavaDoc[] groups, boolean allow) throws Exception JavaDoc
269     {
270         Role role = new Role();
271         role.setName(roleName);
272         
273         for (int i = 0; i < groups.length; i++) {
274             int groupId = Integer.parseInt(groups[i]);
275             RoleValueCollection roleValues = new RoleValueCollection();
276             
277             RoleValue rv = new RoleValue();
278             rv.setType(allow ? PermissionControl.ROLE_ALLOW : PermissionControl.ROLE_DENY);
279             rv.setValue(Integer.toString(forumId));
280             roleValues.add(rv);
281             
282             pc.addRoleValue(groupId, role, roleValues);
283         }
284     }
285 }
286
Popular Tags