KickJava   Java API By Example, From Geeks To Geeks.

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


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 10, 2003 / 8:49:51 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.security.GroupSecurityDAO;
51 import net.jforum.entities.Category;
52 import net.jforum.repository.ForumRepository;
53 import net.jforum.repository.SecurityRepository;
54 import net.jforum.repository.RolesRepository;
55 import net.jforum.security.PermissionControl;
56 import net.jforum.security.Role;
57 import net.jforum.security.RoleValue;
58 import net.jforum.security.RoleValueCollection;
59 import net.jforum.security.SecurityConstants;
60 import net.jforum.util.I18n;
61 import net.jforum.util.TreeGroup;
62 import net.jforum.util.preferences.TemplateKeys;
63 import net.jforum.view.admin.common.ModerationCommon;
64
65 /**
66  * ViewHelper for category administration.
67  *
68  * @author Rafael Steil
69  * @version $Id: CategoryAction.java,v 1.22 2005/09/15 22:47:32 vmal Exp $
70  */

71 public class CategoryAction extends AdminCommand
72 {
73     private CategoryDAO cm = DataAccessDriver.getInstance().newCategoryDAO();
74     
75     // Listing
76
public void list() throws Exception JavaDoc
77     {
78         this.context.put("categories", DataAccessDriver.getInstance().newCategoryDAO().selectAll());
79         this.context.put("repository", new ForumRepository());
80         this.setTemplateName(TemplateKeys.CATEGORY_LIST);
81     }
82     
83     // One more, one more
84
public void insert() throws Exception JavaDoc
85     {
86         this.context.put("groups", new TreeGroup().getNodes());
87         this.context.put("selectedList", new ArrayList JavaDoc());
88         this.setTemplateName(TemplateKeys.CATEGORY_INSERT);
89         this.context.put("action", "insertSave");
90     }
91     
92     // Edit
93
public void edit() throws Exception JavaDoc
94     {
95         this.context.put("category", this.cm.selectById(this.request.getIntParameter("category_id")));
96         this.setTemplateName(TemplateKeys.CATEGORY_EDIT);
97         this.context.put("action", "editSave");
98     }
99     
100     // Save information
101
public void editSave() throws Exception JavaDoc
102     {
103         Category c = new Category(ForumRepository.getCategory(
104                 this.request.getIntParameter("categories_id")));
105         c.setName(this.request.getParameter("category_name"));
106         c.setModerated("1".equals(this.request.getParameter("moderate")));
107             
108         this.cm.update(c);
109         ForumRepository.reloadCategory(c);
110         
111         new ModerationCommon().setForumsModerationStatus(c, c.isModerated());
112         
113         this.list();
114     }
115     
116     // Delete
117
public void delete() throws Exception JavaDoc
118     {
119         String JavaDoc ids[] = this.request.getParameterValues("categories_id");
120         List JavaDoc errors = new ArrayList JavaDoc();
121         
122         if (ids != null) {
123             for (int i = 0; i < ids.length; i++){
124                 if (this.cm.canDelete(Integer.parseInt(ids[i]))) {
125                     int id = Integer.parseInt(ids[i]);
126                     Category c = this.cm.selectById(id);
127                     this.cm.delete(id);
128                     
129                     ForumRepository.removeCategory(c);
130                 }
131                 else {
132                     errors.add(I18n.getMessage(I18n.CANNOT_DELETE_CATEGORY, new Object JavaDoc[] { new Integer JavaDoc(ids[i]) }));
133                 }
134             }
135         }
136
137         if (errors.size() > 0) {
138             this.context.put("errorMessage", errors);
139         }
140         
141         this.list();
142     }
143     
144     // A new one
145
public void insertSave() throws Exception JavaDoc
146     {
147         Category c = new Category();
148         c.setName(this.request.getParameter("category_name"));
149         c.setModerated("1".equals(this.request.getParameter("moderated")));
150             
151         int categoryId = this.cm.addNew(c);
152         c.setId(categoryId);
153
154         ForumRepository.addCategory(c);
155         
156         String JavaDoc[] groups = this.request.getParameterValues("groups");
157         if (groups != null) {
158             GroupSecurityDAO gmodel = DataAccessDriver.getInstance().newGroupSecurityDAO();
159             PermissionControl pc = new PermissionControl();
160             pc.setSecurityModel(gmodel);
161
162             Role role = new Role();
163             role.setName(SecurityConstants.PERM_CATEGORY);
164
165             for (int i = 0; i < groups.length; i++) {
166                 int groupId = Integer.parseInt(groups[i]);
167                 RoleValueCollection roleValues = new RoleValueCollection();
168                 
169                 RoleValue rv = new RoleValue();
170                 rv.setType(PermissionControl.ROLE_ALLOW);
171                 rv.setValue(Integer.toString(categoryId));
172                 
173                 roleValues.add(rv);
174                 
175                 pc.addRoleValue(groupId, role, roleValues);
176             }
177             
178             SecurityRepository.clean();
179             RolesRepository.clear();
180         }
181             
182         this.list();
183     }
184     
185     public void up() throws Exception JavaDoc
186     {
187         this.processOrdering(true);
188     }
189     
190     public void down() throws Exception JavaDoc
191     {
192         this.processOrdering(false);
193     }
194     
195     private void processOrdering(boolean up) throws Exception JavaDoc
196     {
197         Category toChange = new Category(ForumRepository.getCategory(Integer.parseInt(
198                 this.request.getParameter("category_id"))));
199         
200         List JavaDoc categories = ForumRepository.getAllCategories();
201         
202         int index = categories.indexOf(toChange);
203         if (index == -1 || (up && index == 0) || (!up && index + 1 == categories.size())) {
204             this.list();
205             return;
206         }
207         
208         if (up) {
209             // Get the category which comes *before* the category we want to change
210
Category otherCategory = new Category((Category)categories.get(index - 1));
211             this.cm.setOrderUp(toChange, otherCategory);
212         }
213         else {
214             // Get the category which comes *after* the category we want to change
215
Category otherCategory = new Category((Category)categories.get(index + 1));
216             this.cm.setOrderDown(toChange, otherCategory);
217         }
218         
219         ForumRepository.reloadCategory(toChange);
220         this.list();
221     }
222 }
223
Popular Tags