KickJava   Java API By Example, From Geeks To Geeks.

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


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.Group;
35 import org.infoglue.cms.entities.management.GroupVO;
36 import org.infoglue.cms.entities.management.SystemUser;
37 import org.infoglue.cms.entities.management.impl.simple.GroupImpl;
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 import org.infoglue.deliver.util.CacheController;
43
44 /**
45  * GroupHelper.java
46  * Created on 2002-aug-28
47  * @author Stefan Sik, ss@frovi.com
48  *
49  * This class is a helper class for the use case handle groups
50  */

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

58
59     public static GroupController getController()
60     {
61         return new GroupController();
62     }
63     
64     public Group getGroupWithId(Integer JavaDoc groupId, Database db) throws SystemException, Bug
65     {
66         return (Group) getObjectWithId(GroupImpl.class, groupId, db);
67     }
68
69     public Group getGroupWithName(String JavaDoc groupName, Database db) throws SystemException, Bug
70     {
71         return (Group)getObjectWithId(GroupImpl.class, groupName, db);
72     }
73     
74     /*
75     public static List getGroupVOList(Database db) throws SystemException, Bug
76     {
77         return getAllVOObjects(GroupImpl.class, db);
78     }
79     */

80     
81     public GroupVO getGroupVOWithId(Integer JavaDoc groupId) throws SystemException, Bug
82     {
83         return (GroupVO) getVOWithId(GroupImpl.class, groupId);
84     }
85
86     public GroupVO getGroupVOWithId(String JavaDoc groupName) throws SystemException, Bug
87     {
88         return (GroupVO) getVOWithId(GroupImpl.class, groupName);
89     }
90
91     public GroupVO getGroupVOWithId(String JavaDoc groupName, Database db) throws SystemException, Bug
92     {
93         return (GroupVO) getVOWithId(GroupImpl.class, groupName, db);
94     }
95
96     // Simple, without db
97
/*
98     public static Group getGroupWithId(Integer groupId) throws SystemException, Bug
99     {
100         return (Group) getObjectWithId(GroupImpl.class, groupId);
101     }
102     */

103     
104     public List JavaDoc getGroupVOList() throws SystemException, Bug
105     {
106         return getAllVOObjects(GroupImpl.class, "groupName");
107     }
108
109     public List JavaDoc getGroupVOList(Database db) throws SystemException, Bug
110     {
111         String JavaDoc cacheKey = "allGroupVO";
112         logger.info("cacheKey in getGroupVOList:" + cacheKey);
113         List JavaDoc groupVOList = (List JavaDoc)CacheController.getCachedObject("groupVOListCache", cacheKey);
114         if(groupVOList != null)
115         {
116             logger.info("There was an cached list of GroupVO:" + groupVOList.size());
117         }
118         else
119         {
120             groupVOList = getAllVOObjects(GroupImpl.class, "groupName", db);
121             if(groupVOList != null)
122                 CacheController.cacheObject("groupVOListCache", cacheKey, groupVOList);
123         }
124         
125         return groupVOList;
126     }
127
128     public GroupVO create(GroupVO groupVO) throws ConstraintException, SystemException
129     {
130         Group group = new GroupImpl();
131         group.setValueObject(groupVO);
132         group = (Group) createEntity(group);
133         return group.getValueObject();
134     }
135
136     public Group create(GroupVO groupVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
137     {
138         Group group = new GroupImpl();
139         group.setValueObject(groupVO);
140         group = (Group) createEntity(group, db);
141         return group;
142     }
143
144     public void delete(GroupVO groupVO) throws ConstraintException, SystemException
145     {
146         deleteEntity(GroupImpl.class, groupVO.getGroupName());
147     }
148
149     public void delete(String JavaDoc groupName) throws ConstraintException, SystemException
150     {
151         deleteEntity(GroupImpl.class, groupName);
152     }
153
154     public void delete(String JavaDoc groupName, Database db) throws ConstraintException, SystemException, Exception JavaDoc
155     {
156         deleteEntity(GroupImpl.class, groupName, db);
157     }
158
159     // Get list of users accosiated with this group
160
public List JavaDoc getGroupSystemUserVOList(String JavaDoc userName, Database db) throws SystemException, Bug
161     {
162         Collection JavaDoc systemUsers = null;
163         List JavaDoc systemUsersVO = new ArrayList JavaDoc();
164         Group group = null;
165         
166         try
167         {
168             group = getGroupWithName(userName, db);
169             systemUsers = group.getSystemUsers();
170             
171             Iterator JavaDoc it = systemUsers.iterator();
172             while (it.hasNext())
173             {
174                 SystemUser systemUser = (SystemUser) it.next();
175                 systemUsersVO.add(systemUser.getValueObject());
176             }
177         }
178         catch( Exception JavaDoc e)
179         {
180             throw new SystemException("An error occurred when we tried to fetch a list of users in this group. Reason:" + e.getMessage(), e);
181         }
182         
183         return systemUsersVO;
184     }
185
186     public List JavaDoc getGroupSystemUserVOList(String JavaDoc groupName) throws SystemException, Bug
187     {
188         List JavaDoc systemUsersVO = null;
189         Database db = CastorDatabaseService.getDatabase();
190         try
191         {
192             beginTransaction(db);
193             
194             systemUsersVO = getGroupSystemUserVOList(groupName, db);
195             
196             commitTransaction(db);
197         }
198         catch ( Exception JavaDoc e )
199         {
200             rollbackTransaction(db);
201             throw new SystemException("An error occurred when we tried to fetch a list of users in this group. Reason:" + e.getMessage(), e);
202         }
203         return systemUsersVO;
204     }
205
206     public GroupVO update(GroupVO groupVO) throws ConstraintException, SystemException
207     {
208         return (GroupVO) updateEntity(GroupImpl.class, (BaseEntityVO) groupVO);
209     }
210
211     public GroupVO update(GroupVO groupVO, Database db) throws ConstraintException, SystemException
212     {
213         return (GroupVO) updateEntity(GroupImpl.class, (BaseEntityVO) groupVO, db);
214     }
215
216
217     public GroupVO update(GroupVO groupVO, String JavaDoc[] systemUsers) throws ConstraintException, SystemException
218     {
219         Database db = CastorDatabaseService.getDatabase();
220         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
221
222         Group group = null;
223
224         beginTransaction(db);
225
226         try
227         {
228             //add validation here if needed
229

230             group = update(groupVO, systemUsers, db);
231
232             //If any of the validations or setMethods reported an error, we throw them up now before create.
233
ceb.throwIfNotEmpty();
234             
235             commitTransaction(db);
236         }
237         catch(ConstraintException ce)
238         {
239             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
240             rollbackTransaction(db);
241             throw ce;
242         }
243         catch(Exception JavaDoc e)
244         {
245             logger.error("An error occurred so we should not complete the transaction:" + e, e);
246             rollbackTransaction(db);
247             throw new SystemException(e.getMessage());
248         }
249
250         return group.getValueObject();
251     }
252
253     public Group update(GroupVO groupVO, String JavaDoc[] systemUsers, Database db) throws ConstraintException, SystemException
254     {
255         Group group = getGroupWithName(groupVO.getGroupName(), db);
256         group.getSystemUsers().clear();
257         
258         if(systemUsers != null)
259         {
260             for (int i=0; i < systemUsers.length; i++)
261             {
262                 SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(systemUsers[i], db);
263                 
264                 group.getSystemUsers().add(systemUser);
265                 systemUser.getGroups().add(group);
266             }
267         }
268         
269         group.setValueObject(groupVO);
270
271         return group;
272     }
273
274     
275     /**
276      * This method gets a list of Groups for a particular systemUser.
277      * @param systemUserId
278      * @return
279      * @throws SystemException
280      * @throws Bug
281      */

282     
283     public List JavaDoc getGroupVOList(String JavaDoc userName) throws SystemException, Bug
284     {
285         List JavaDoc groupVOList = null;
286         
287         Database db = CastorDatabaseService.getDatabase();
288         try
289         {
290             beginTransaction(db);
291             
292             SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(userName, db);
293             groupVOList = toVOList(systemUser.getGroups());
294             
295             commitTransaction(db);
296         }
297         catch(Exception JavaDoc e)
298         {
299             rollbackTransaction(db);
300             throw new SystemException("An error occurred when we tried to fetch a list of users in this group. Reason:" + e.getMessage(), e);
301         }
302         
303         return groupVOList;
304     }
305     
306     /**
307      * This method gets a list of Groups for a particular systemUser.
308      * @param systemUserId
309      * @return
310      * @throws SystemException
311      * @throws Bug
312      */

313     
314     public Collection JavaDoc getGroupList(String JavaDoc userName, Database db) throws SystemException, Bug
315     {
316         Collection JavaDoc groupList = null;
317         
318         SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(userName, db);
319         groupList = systemUser.getGroups();
320         
321         return groupList;
322     }
323
324     /**
325      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
326      * is handling.
327      */

328
329     public BaseEntityVO getNewVO()
330     {
331         return new GroupVO();
332     }
333
334 }
335  
Popular Tags