KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > RoleController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.exolab.castor.jdo.Database;
33 import org.infoglue.cms.entities.kernel.BaseEntityVO;
34 import org.infoglue.cms.entities.management.Role;
35 import org.infoglue.cms.entities.management.RoleVO;
36 import org.infoglue.cms.entities.management.SystemUser;
37 import org.infoglue.cms.entities.management.impl.simple.RoleImpl;
38 import org.infoglue.cms.exception.Bug;
39 import org.infoglue.cms.exception.ConstraintException;
40 import org.infoglue.cms.exception.SystemException;
41 import org.infoglue.cms.util.ConstraintExceptionBuffer;
42
43 /**
44  * RoleHelper.java
45  * Created on 2002-aug-28
46  * @author Stefan Sik, ss@frovi.com
47  *
48  * This class is a helper class for the use case handle roles
49  */

50 public class RoleController extends BaseController
51 {
52     private final static Logger logger = Logger.getLogger(RoleController.class.getName());
53
54     /**
55      * Factory method
56      */

57
58     public static RoleController getController()
59     {
60         return new RoleController();
61     }
62     
63     public Role getRoleWithId(Integer JavaDoc roleId, Database db) throws SystemException, Bug
64     {
65         return (Role) getObjectWithId(RoleImpl.class, roleId, db);
66     }
67
68     public Role getRoleWithName(String JavaDoc roleName, Database db) throws SystemException, Bug
69     {
70         return (Role)getObjectWithId(RoleImpl.class, roleName, db);
71     }
72     
73     /*
74     public static List getRoleVOList(Database db) throws SystemException, Bug
75     {
76         return getAllVOObjects(RoleImpl.class, db);
77     }
78     */

79     
80     public RoleVO getRoleVOWithId(Integer JavaDoc roleId) throws SystemException, Bug
81     {
82         return (RoleVO) getVOWithId(RoleImpl.class, roleId);
83     }
84
85     public RoleVO getRoleVOWithId(String JavaDoc roleName) throws SystemException, Bug
86     {
87         return (RoleVO) getVOWithId(RoleImpl.class, roleName);
88     }
89
90     public RoleVO getRoleVOWithId(String JavaDoc roleName, Database db) throws SystemException, Bug
91     {
92         return (RoleVO) getVOWithId(RoleImpl.class, roleName, db);
93     }
94
95     // Simple, without db
96
/*
97     public static Role getRoleWithId(Integer roleId) throws SystemException, Bug
98     {
99         return (Role) getObjectWithId(RoleImpl.class, roleId);
100     }
101     */

102     
103     public List JavaDoc getRoleVOList() throws SystemException, Bug
104     {
105         return getAllVOObjects(RoleImpl.class, "roleName");
106     }
107
108     public List JavaDoc getRoleVOList(Database db) throws SystemException, Bug
109     {
110         return getAllVOObjects(RoleImpl.class, "roleName", db);
111     }
112
113     public RoleVO create(RoleVO roleVO) throws ConstraintException, SystemException
114     {
115         Role role = new RoleImpl();
116         role.setValueObject(roleVO);
117         role = (Role) createEntity(role);
118         return role.getValueObject();
119     }
120
121     public Role create(RoleVO roleVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
122     {
123         Role role = new RoleImpl();
124         role.setValueObject(roleVO);
125         role = (Role) createEntity(role, db);
126         return role;
127     }
128
129     public void delete(RoleVO roleVO) throws ConstraintException, SystemException
130     {
131         deleteEntity(RoleImpl.class, roleVO.getRoleName());
132     }
133
134     public void delete(RoleVO roleVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
135     {
136         deleteEntity(RoleImpl.class, roleVO.getRoleName(), db);
137     }
138
139     public void delete(String JavaDoc roleName) throws ConstraintException, SystemException
140     {
141         deleteEntity(RoleImpl.class, roleName);
142     }
143
144     public void delete(String JavaDoc roleName, Database db) throws ConstraintException, SystemException, Exception JavaDoc
145     {
146         deleteEntity(RoleImpl.class, roleName, db);
147     }
148
149     // Get list of users accosiated with this role
150
public List JavaDoc getRoleSystemUserVOList(String JavaDoc userName, Database db) throws SystemException, Bug
151     {
152         Collection JavaDoc systemUsers = null;
153         List JavaDoc systemUsersVO = new ArrayList JavaDoc();
154         Role role = null;
155         
156         try
157         {
158             role = getRoleWithName(userName, db);
159             systemUsers = role.getSystemUsers();
160             
161             Iterator JavaDoc it = systemUsers.iterator();
162             while (it.hasNext())
163             {
164                 SystemUser systemUser = (SystemUser) it.next();
165                 systemUsersVO.add(systemUser.getValueObject());
166             }
167         }
168         catch( Exception JavaDoc e)
169         {
170             throw new SystemException("An error occurred when we tried to fetch a list of users in this role. Reason:" + e.getMessage(), e);
171         }
172         
173         return systemUsersVO;
174     }
175
176     public List JavaDoc getRoleSystemUserVOList(String JavaDoc roleName) throws SystemException, Bug
177     {
178         List JavaDoc systemUsersVO = null;
179         Database db = CastorDatabaseService.getDatabase();
180         try
181         {
182             beginTransaction(db);
183             
184             systemUsersVO = getRoleSystemUserVOList(roleName, db);
185             
186             commitTransaction(db);
187         }
188         catch ( Exception JavaDoc e )
189         {
190             rollbackTransaction(db);
191             throw new SystemException("An error occurred when we tried to fetch a list of users in this role. Reason:" + e.getMessage(), e);
192         }
193         return systemUsersVO;
194     }
195
196     public RoleVO update(RoleVO roleVO) throws ConstraintException, SystemException
197     {
198         return (RoleVO) updateEntity(RoleImpl.class, (BaseEntityVO) roleVO);
199     }
200
201
202     public RoleVO update(RoleVO roleVO, String JavaDoc[] systemUsers) throws ConstraintException, SystemException
203     {
204         Database db = CastorDatabaseService.getDatabase();
205         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
206
207         Role role = null;
208
209         beginTransaction(db);
210
211         try
212         {
213             //add validation here if needed
214
role = update(roleVO, systemUsers, db);
215
216             //If any of the validations or setMethods reported an error, we throw them up now before create.
217
ceb.throwIfNotEmpty();
218             
219             commitTransaction(db);
220         }
221         catch(ConstraintException ce)
222         {
223             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
224             rollbackTransaction(db);
225             throw ce;
226         }
227         catch(Exception JavaDoc e)
228         {
229             logger.error("An error occurred so we should not complete the transaction:" + e, e);
230             rollbackTransaction(db);
231             throw new SystemException(e.getMessage());
232         }
233
234         return role.getValueObject();
235     }
236
237     public Role update(RoleVO roleVO, String JavaDoc[] systemUsers, Database db) throws ConstraintException, SystemException
238     {
239         Role role = getRoleWithName(roleVO.getRoleName(), db);
240         role.getSystemUsers().clear();
241         
242         if(systemUsers != null)
243         {
244             for (int i=0; i < systemUsers.length; i++)
245             {
246                 SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(systemUsers[i], db);
247                 
248                 role.getSystemUsers().add(systemUser);
249                 systemUser.getRoles().add(role);
250             }
251         }
252         
253         role.setValueObject(roleVO);
254
255         return role;
256     }
257
258     
259     /**
260      * This method gets a list of Roles for a particular systemUser.
261      * @param systemUserId
262      * @return
263      * @throws SystemException
264      * @throws Bug
265      */

266     
267     public List JavaDoc getRoleVOList(String JavaDoc userName) throws SystemException, Bug
268     {
269         List JavaDoc roleVOList = null;
270         
271         Database db = CastorDatabaseService.getDatabase();
272         try
273         {
274             beginTransaction(db);
275             
276             SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(userName, db);
277             roleVOList = toVOList(systemUser.getRoles());
278             
279             commitTransaction(db);
280         }
281         catch(Exception JavaDoc e)
282         {
283             rollbackTransaction(db);
284             throw new SystemException("An error occurred when we tried to fetch a list of users in this role. Reason:" + e.getMessage(), e);
285         }
286         
287         return roleVOList;
288     }
289     
290     /**
291      * This method gets a list of Roles for a particular systemUser.
292      * @param systemUserId
293      * @return
294      * @throws SystemException
295      * @throws Bug
296      */

297     
298     public Collection JavaDoc getRoleList(String JavaDoc userName, Database db) throws SystemException, Bug
299     {
300         Collection JavaDoc roleList = null;
301         
302         SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(userName, db);
303         roleList = systemUser.getRoles();
304         
305         return roleList;
306     }
307
308     /**
309      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
310      * is handling.
311      */

312
313     public BaseEntityVO getNewVO()
314     {
315         return new RoleVO();
316     }
317
318 }
319  
Popular Tags