KickJava   Java API By Example, From Geeks To Geeks.

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


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: Mar 3, 2003 / 11:07:02 AM
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.DataAccessDriver;
49 import net.jforum.dao.GroupDAO;
50 import net.jforum.dao.security.GroupSecurityDAO;
51 import net.jforum.entities.Group;
52 import net.jforum.repository.ForumRepository;
53 import net.jforum.repository.RolesRepository;
54 import net.jforum.repository.SecurityRepository;
55 import net.jforum.security.PermissionControl;
56 import net.jforum.security.XMLPermissionControl;
57 import net.jforum.util.I18n;
58 import net.jforum.util.TreeGroup;
59 import net.jforum.util.preferences.ConfigKeys;
60 import net.jforum.util.preferences.SystemGlobals;
61 import net.jforum.util.preferences.TemplateKeys;
62
63 /**
64  * ViewHelper class for group administration.
65  *
66  * @author Rafael Steil
67  * @version $Id: GroupAction.java,v 1.18 2005/11/30 13:17:10 rafaelsteil Exp $
68  */

69 public class GroupAction extends AdminCommand
70 {
71     // Listing
72
public void list() throws Exception JavaDoc
73     {
74         this.context.put("groups", new TreeGroup().getNodes());
75         this.setTemplateName(TemplateKeys.GROUP_LIST);
76     }
77     
78     // Insert
79
public void insert() throws Exception JavaDoc
80     {
81         this.context.put("groups", new TreeGroup().getNodes());
82         this.context.put("action", "insertSave");
83         this.context.put("selectedList", new ArrayList JavaDoc());
84         this.setTemplateName(TemplateKeys.GROUP_INSERT);
85     }
86     
87     // Save information for an existing group
88
public void editSave() throws Exception JavaDoc
89     {
90         int groupId = this.request.getIntParameter("group_id");
91             
92         Group g = new Group();
93         g.setDescription(this.request.getParameter("group_description"));
94         g.setId(groupId);
95         
96         int parentId = this.request.getIntParameter("parent_id");
97         
98         if (parentId == g.getId()) {
99             parentId = 0;
100         }
101         
102         g.setParentId(parentId);
103         g.setName(this.request.getParameter("group_name"));
104
105         DataAccessDriver.getInstance().newGroupDAO().update(g);
106             
107         this.list();
108     }
109     
110     // Edit a group
111
public void edit() throws Exception JavaDoc
112     {
113         int groupId = this.request.getIntParameter("group_id");
114         GroupDAO gm = DataAccessDriver.getInstance().newGroupDAO();
115         
116         this.setTemplateName(TemplateKeys.GROUP_EDIT);
117                     
118         this.context.put("group", gm.selectById(groupId));
119         this.context.put("groups", new TreeGroup().getNodes());
120         this.context.put("selectedList", new ArrayList JavaDoc());
121         this.context.put("action", "editSave");
122     }
123     
124     // Deletes a group
125
public void delete() throws Exception JavaDoc
126     {
127         String JavaDoc groupId[] = this.request.getParameterValues("group_id");
128         
129         if (groupId == null) {
130             this.list();
131             
132             return;
133         }
134         
135         List JavaDoc errors = new ArrayList JavaDoc();
136         GroupDAO gm = DataAccessDriver.getInstance().newGroupDAO();
137             
138         for (int i = 0; i < groupId.length; i++) {
139             int id = Integer.parseInt(groupId[i]);
140             
141             if (gm.canDelete(id)) {
142                 gm.delete(id);
143             }
144             else {
145                 errors.add(I18n.getMessage(I18n.CANNOT_DELETE_GROUP, new Object JavaDoc[] { new Integer JavaDoc(id) }));
146             }
147         }
148         
149         if (errors.size() > 0) {
150             this.context.put("errorMessage", errors);
151         }
152             
153         this.list();
154     }
155     
156     // Saves a new group
157
public void insertSave() throws Exception JavaDoc
158     {
159         GroupDAO gm = DataAccessDriver.getInstance().newGroupDAO();
160         
161         Group g = new Group();
162         g.setDescription(this.request.getParameter("group_description"));
163         g.setParentId(this.request.getIntParameter("parent_id"));
164         g.setName(this.request.getParameter("group_name"));
165             
166         gm.addNew(g);
167             
168         this.list();
169     }
170     
171     // Permissions
172
public void permissions() throws Exception JavaDoc
173     {
174         int id = this.request.getIntParameter("group_id");
175         
176         PermissionControl pc = new PermissionControl();
177         pc.setRoles(DataAccessDriver.getInstance().newGroupSecurityDAO().loadRoles(id));
178         
179         List JavaDoc sections = new XMLPermissionControl(pc).loadConfigurations(
180                 SystemGlobals.getValue(ConfigKeys.CONFIG_DIR) + "/permissions.xml");
181         
182         GroupDAO gm = DataAccessDriver.getInstance().newGroupDAO();
183
184         this.context.put("sections", sections);
185         this.context.put("group", gm.selectById(id));
186         this.setTemplateName(TemplateKeys.GROUP_PERMISSIONS);
187     }
188     
189     public void permissionsSave() throws Exception JavaDoc
190     {
191         int id = this.request.getIntParameter("id");
192         
193         GroupSecurityDAO gmodel = DataAccessDriver.getInstance().newGroupSecurityDAO();
194         
195         PermissionControl pc = new PermissionControl();
196         pc.setSecurityModel(gmodel);
197         
198         new PermissionProcessHelper(pc, id, true).processData();
199
200         SecurityRepository.clean();
201         RolesRepository.clear();
202         ForumRepository.clearModeratorList();
203         
204         this.list();
205     }
206 }
207
Popular Tags