KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.exolab.castor.jdo.Database;
37 import org.exolab.castor.jdo.OQLQuery;
38 import org.exolab.castor.jdo.QueryResults;
39 import org.infoglue.cms.applications.contenttool.actions.databeans.AccessRightsUserRow;
40 import org.infoglue.cms.entities.kernel.BaseEntityVO;
41 import org.infoglue.cms.entities.management.AccessRight;
42 import org.infoglue.cms.entities.management.AccessRightGroup;
43 import org.infoglue.cms.entities.management.AccessRightGroupVO;
44 import org.infoglue.cms.entities.management.AccessRightRole;
45 import org.infoglue.cms.entities.management.AccessRightRoleVO;
46 import org.infoglue.cms.entities.management.AccessRightUser;
47 import org.infoglue.cms.entities.management.AccessRightUserVO;
48 import org.infoglue.cms.entities.management.AccessRightVO;
49 import org.infoglue.cms.entities.management.InterceptionPoint;
50 import org.infoglue.cms.entities.management.InterceptionPointVO;
51 import org.infoglue.cms.entities.management.impl.simple.AccessRightGroupImpl;
52 import org.infoglue.cms.entities.management.impl.simple.AccessRightImpl;
53 import org.infoglue.cms.entities.management.impl.simple.AccessRightRoleImpl;
54 import org.infoglue.cms.entities.management.impl.simple.AccessRightUserImpl;
55 import org.infoglue.cms.exception.Bug;
56 import org.infoglue.cms.exception.ConstraintException;
57 import org.infoglue.cms.exception.SystemException;
58 import org.infoglue.cms.security.InfoGlueGroup;
59 import org.infoglue.cms.security.InfoGluePrincipal;
60 import org.infoglue.cms.security.InfoGlueRole;
61 import org.infoglue.deliver.util.CacheController;
62
63 /**
64  * This class is a helper class for the use case handle Accesss
65  *
66  * @author Mattias Bogeblad
67  */

68
69 public class AccessRightController extends BaseController
70 {
71     private final static Logger logger = Logger.getLogger(AccessRightController.class.getName());
72
73     /**
74      * Factory method
75      */

76
77     public static AccessRightController getController()
78     {
79         return new AccessRightController();
80     }
81     
82     public AccessRight getAccessRightWithId(Integer JavaDoc accessRightId, Database db) throws SystemException, Bug
83     {
84         return (AccessRight) getObjectWithId(AccessRightImpl.class, accessRightId, db);
85     }
86
87     public AccessRightVO getAccessRightVOWithId(Integer JavaDoc accessRightId) throws SystemException, Bug
88     {
89         return (AccessRightVO) getVOWithId(AccessRightImpl.class, accessRightId);
90     }
91   
92     public List JavaDoc getAccessRightVOList() throws SystemException, Bug
93     {
94         return getAllVOObjects(AccessRightImpl.class, "accessRightId");
95     }
96
97     public List JavaDoc getAccessRightVOList(Database db) throws SystemException, Bug
98     {
99         return this.getAllVOObjects(AccessRightImpl.class, "accessRightId", db);
100     }
101
102     public List JavaDoc getAccessRightUserVOList(Database db) throws SystemException, Bug
103     {
104         return this.getAllVOObjects(AccessRightUserImpl.class, "accessRightUserId", db);
105     }
106
107     public List JavaDoc getAccessRightRoleVOList(Database db) throws SystemException, Bug
108     {
109         return this.getAllVOObjects(AccessRightRoleImpl.class, "accessRightRoleId", db);
110     }
111
112     public List JavaDoc getAccessRightGroupVOList(Database db) throws SystemException, Bug
113     {
114         return this.getAllVOObjects(AccessRightGroupImpl.class, "accessRightGroupId", db);
115     }
116
117     public List JavaDoc getAccessRightGroupVOList(Integer JavaDoc accessRightId) throws SystemException, Bug
118     {
119         List JavaDoc accessRightGroupVOList = new ArrayList JavaDoc();
120         
121         Database db = CastorDatabaseService.getDatabase();
122
123         try
124         {
125             beginTransaction(db);
126             
127             AccessRight accessRight = this.getAccessRightWithId(accessRightId, db);
128             if(accessRight != null)
129                 accessRightGroupVOList = toVOList(accessRight.getGroups());
130             
131             logger.info("accessRightGroupVOList:" + accessRightGroupVOList.size());
132             
133             commitTransaction(db);
134         }
135         catch (Exception JavaDoc e)
136         {
137             e.printStackTrace();
138             logger.info("An error occurred so we should not complete the transaction:" + e);
139             rollbackTransaction(db);
140             throw new SystemException(e.getMessage());
141         }
142         
143         return accessRightGroupVOList;
144     }
145     
146     public List JavaDoc getAccessRightVOList(Integer JavaDoc interceptionPointId, String JavaDoc parameters, String JavaDoc roleName) throws SystemException, Bug
147     {
148         List JavaDoc accessRightVOList = null;
149         
150         Database db = CastorDatabaseService.getDatabase();
151
152         try
153         {
154             beginTransaction(db);
155             
156             accessRightVOList = getAccessRightVOList(db, interceptionPointId, parameters, roleName);
157
158             logger.info("accessRightVOList:" + accessRightVOList.size());
159             
160             commitTransaction(db);
161         }
162         catch (Exception JavaDoc e)
163         {
164             e.printStackTrace();
165             logger.info("An error occurred so we should not complete the transaction:" + e);
166             rollbackTransaction(db);
167             throw new SystemException(e.getMessage());
168         }
169         
170         return accessRightVOList;
171     }
172
173     
174     public List JavaDoc getAccessRightVOList(Database db, Integer JavaDoc interceptionPointId, String JavaDoc parameters, String JavaDoc roleName) throws SystemException, Bug
175     {
176         List JavaDoc accessRightVOList = null;
177         
178         InterceptionPointVO interceptionPointVO = InterceptionPointController.getController().getInterceptionPointVOWithId(interceptionPointId);
179         if(interceptionPointVO.getUsesExtraDataForAccessControl().booleanValue())
180             accessRightVOList = toVOList(getAccessRightList(interceptionPointId, parameters, roleName, db));
181         else
182             accessRightVOList = toVOList(getAccessRightList(interceptionPointId, roleName, db));
183
184         logger.info("accessRightVOList:" + accessRightVOList.size());
185         
186         return accessRightVOList;
187     }
188
189     public List JavaDoc getAccessRightVOListOnly(Integer JavaDoc interceptionPointId, String JavaDoc parameters) throws SystemException, Bug
190     {
191         List JavaDoc accessRightVOList = null;
192         
193         Database db = CastorDatabaseService.getDatabase();
194
195         try
196         {
197             beginTransaction(db);
198             
199             accessRightVOList = getAccessRightVOListOnly(db, interceptionPointId, parameters);
200
201             logger.info("accessRightVOList:" + accessRightVOList.size());
202             
203             commitTransaction(db);
204         }
205         catch (Exception JavaDoc e)
206         {
207             e.printStackTrace();
208             logger.info("An error occurred so we should not complete the transaction:" + e);
209             rollbackTransaction(db);
210             throw new SystemException(e.getMessage());
211         }
212         
213         return accessRightVOList;
214     }
215
216     public List JavaDoc getAccessRightVOListOnly(Database db, Integer JavaDoc interceptionPointId, String JavaDoc parameters) throws SystemException, Bug
217     {
218         List JavaDoc accessRightVOList = null;
219         
220         InterceptionPointVO interceptionPointVO = InterceptionPointController.getController().getInterceptionPointVOWithId(interceptionPointId);
221         if(interceptionPointVO.getUsesExtraDataForAccessControl().booleanValue())
222             accessRightVOList = toVOList(getAccessRightListOnlyReadOnly(interceptionPointId, parameters, db));
223         else
224             accessRightVOList = toVOList(getAccessRightList(interceptionPointId, db));
225
226         logger.info("accessRightVOList:" + accessRightVOList.size());
227         
228         return accessRightVOList;
229     }
230
231     public List JavaDoc getAccessRightList(String JavaDoc interceptionPointName, String JavaDoc parameters, String JavaDoc roleName, Database db) throws SystemException, Bug
232     {
233         List JavaDoc accessRightList = getAccessRightList(InterceptionPointController.getController().getInterceptionPointVOWithName(interceptionPointName).getId(), parameters, roleName, db);
234         
235         return accessRightList;
236     }
237     
238     public List JavaDoc getAccessRightList(Integer JavaDoc interceptionPointId, String JavaDoc parameters, String JavaDoc roleName, Database db) throws SystemException, Bug
239     {
240         List JavaDoc accessRightList = new ArrayList JavaDoc();
241         
242         try
243         {
244             OQLQuery oql = null;
245             
246             if(parameters == null || parameters.length() == 0)
247             {
248                 oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.interceptionPoint = $1 AND (is_undefined(f.parameters) OR f.parameters = $2) AND f.roles.roleName = $3");
249                 oql.bind(interceptionPointId);
250                 oql.bind(parameters);
251                 oql.bind(roleName);
252             }
253             else
254             {
255                 oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.interceptionPoint = $1 AND f.parameters = $2 AND f.roles.roleName = $3");
256                 oql.bind(interceptionPointId);
257                 oql.bind(parameters);
258                 oql.bind(roleName);
259             }
260             
261             QueryResults results = oql.execute();
262             this.logger.info("Fetching entity in read/write mode" + interceptionPointId);
263
264             while (results.hasMore())
265             {
266                 AccessRight accessRight = (AccessRight)results.next();
267                 accessRightList.add(accessRight);
268             }
269             
270             results.close();
271             oql.close();
272         }
273         catch(Exception JavaDoc e)
274         {
275             e.printStackTrace();
276             throw new SystemException("An error occurred when we tried to fetch a list of Access rights. Reason:" + e.getMessage(), e);
277         }
278         
279         return accessRightList;
280     }
281
282     public List JavaDoc getAccessRightListOnly(Integer JavaDoc interceptionPointId, String JavaDoc parameters, Database db) throws SystemException, Bug
283     {
284         List JavaDoc accessRightList = new ArrayList JavaDoc();
285         
286         try
287         {
288             OQLQuery oql = null;
289             
290             if(parameters == null || parameters.length() == 0)
291             {
292                 oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.interceptionPoint = $1 AND (is_undefined(f.parameters) OR f.parameters = $2)");
293                 oql.bind(interceptionPointId);
294                 oql.bind(parameters);
295             }
296             else
297             {
298                 oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.interceptionPoint = $1 AND f.parameters = $2");
299                 oql.bind(interceptionPointId);
300                 oql.bind(parameters);
301             }
302             
303             QueryResults results = oql.execute();
304             this.logger.info("Fetching entity in read/write mode");
305
306             while (results.hasMore())
307             {
308                 AccessRight accessRight = (AccessRight)results.next();
309                 accessRightList.add(accessRight);
310             }
311             
312             results.close();
313             oql.close();
314         }
315         catch(Exception JavaDoc e)
316         {
317             e.printStackTrace();
318             throw new SystemException("An error occurred when we tried to fetch a list of Access rights. Reason:" + e.getMessage(), e);
319         }
320         
321         return accessRightList;
322     }
323
324     
325     public List JavaDoc getAccessRightListOnlyReadOnly(Integer JavaDoc interceptionPointId, String JavaDoc parameters, Database db) throws SystemException, Bug
326     {
327         List JavaDoc accessRightList = new ArrayList JavaDoc();
328         
329         try
330         {
331             OQLQuery oql = null;
332             
333             if(parameters == null || parameters.length() == 0)
334             {
335                 oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.interceptionPoint = $1 AND (is_undefined(f.parameters) OR f.parameters = $2)");
336                 oql.bind(interceptionPointId);
337                 oql.bind(parameters);
338             }
339             else
340             {
341                 oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.interceptionPoint = $1 AND f.parameters = $2");
342                 oql.bind(interceptionPointId);
343                 oql.bind(parameters);
344             }
345             
346             QueryResults results = oql.execute(Database.ReadOnly);
347             
348             while (results.hasMore())
349             {
350                 AccessRight accessRight = (AccessRight)results.next();
351                 accessRightList.add(accessRight);
352             }
353             
354             results.close();
355             oql.close();
356         }
357         catch(Exception JavaDoc e)
358         {
359             e.printStackTrace();
360             throw new SystemException("An error occurred when we tried to fetch a list of Access rights. Reason:" + e.getMessage(), e);
361         }
362         
363         return accessRightList;
364     }
365
366     public List JavaDoc getAccessRightListForEntity(Integer JavaDoc interceptionPointId, String JavaDoc parameters, Database db) throws SystemException, Bug
367     {
368         List JavaDoc accessRightList = new ArrayList JavaDoc();
369         
370         try
371         {
372             //logger.info("getAccessRightListForEntity(Integer interceptionPointId, String parameters, Database db)");
373
//logger.info("interceptionPointId:" + interceptionPointId);
374
//logger.info("parameters:" + parameters);
375

376             OQLQuery oql = null;
377             
378             if(parameters == null || parameters.length() == 0)
379             {
380                 oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.interceptionPoint = $1 AND (is_undefined(f.parameters) OR f.parameters = $2)");
381                 oql.bind(interceptionPointId);
382                 oql.bind(parameters);
383             }
384             else
385             {
386                 oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.interceptionPoint = $1 AND f.parameters = $2");
387                 oql.bind(interceptionPointId);
388                 oql.bind(parameters);
389             }
390                         
391             QueryResults results = oql.execute();
392             this.logger.info("Fetching entity in read/write mode");
393
394             while (results.hasMore())
395             {
396                 AccessRight accessRight = (AccessRight)results.next();
397                 //logger.info("accessRight:" + accessRight.getAccessRightId());
398
accessRightList.add(accessRight);
399             }
400             
401             results.close();
402             oql.close();
403         }
404         catch(Exception JavaDoc e)
405         {
406             throw new SystemException("An error occurred when we tried to fetch a list of Function. Reason:" + e.getMessage(), e);
407         }
408         
409         return accessRightList;
410     }
411     
412     
413     public List JavaDoc getAccessRightList(Integer JavaDoc interceptionPointId, Database db) throws SystemException, Bug
414     {
415         List JavaDoc accessRightList = new ArrayList JavaDoc();
416         
417         try
418         {
419             logger.info("getAccessRightList(Integer interceptionPointId, Database db)");
420             logger.info("interceptionPointId: " + interceptionPointId);
421             
422             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.interceptionPoint = $1");
423             oql.bind(interceptionPointId);
424             
425             QueryResults results = oql.execute();
426             this.logger.info("Fetching entity in read/write mode");
427
428             while (results.hasMore())
429             {
430                 AccessRight accessRight = (AccessRight)results.next();
431                 logger.info("accessRight:" + accessRight.getAccessRightId());
432                 accessRightList.add(accessRight);
433             }
434             
435             results.close();
436             oql.close();
437         }
438         catch(Exception JavaDoc e)
439         {
440             throw new SystemException("An error occurred when we tried to fetch a list of Function. Reason:" + e.getMessage(), e);
441         }
442         
443         return accessRightList;
444     }
445
446     public List JavaDoc getAccessRightList(String JavaDoc roleName, Database db) throws SystemException, Bug
447     {
448         List JavaDoc accessRightList = new ArrayList JavaDoc();
449         
450         try
451         {
452             logger.info("getAccessRightList(String roleName, Database db)");
453             logger.info("roleName: " + roleName);
454             
455             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.roles.roleName = $1");
456             oql.bind(roleName);
457             
458             QueryResults results = oql.execute();
459             this.logger.info("Fetching entity in read/write mode");
460
461             while (results.hasMore())
462             {
463                 AccessRight accessRight = (AccessRight)results.next();
464                 logger.info("accessRight:" + accessRight.getAccessRightId());
465                 accessRightList.add(accessRight);
466             }
467
468             results.close();
469             oql.close();
470         }
471         catch(Exception JavaDoc e)
472         {
473             throw new SystemException("An error occurred when we tried to fetch a list of Function. Reason:" + e.getMessage(), e);
474         }
475         
476         return accessRightList;
477     }
478     
479
480     public List JavaDoc getAccessRightList(Integer JavaDoc interceptionPointId, String JavaDoc roleName, Database db) throws SystemException, Bug
481     {
482         List JavaDoc accessRightList = new ArrayList JavaDoc();
483         
484         try
485         {
486             logger.info("getAccessRightList(Integer interceptionPointId, String roleName, Database db)");
487             logger.info("interceptionPointId: " + interceptionPointId);
488             logger.info("roleName: " + roleName);
489             
490             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightImpl f WHERE f.interceptionPoint = $1 AND f.roles.roleName = $2");
491             oql.bind(interceptionPointId);
492             oql.bind(roleName);
493             
494             QueryResults results = oql.execute();
495             this.logger.info("Fetching entity in read/write mode");
496
497             while (results.hasMore())
498             {
499                 AccessRight accessRight = (AccessRight)results.next();
500                 logger.info("accessRight:" + accessRight.getAccessRightId());
501                 accessRightList.add(accessRight);
502             }
503
504             results.close();
505             oql.close();
506         }
507         catch(Exception JavaDoc e)
508         {
509             throw new SystemException("An error occurred when we tried to fetch a list of Function. Reason:" + e.getMessage(), e);
510         }
511         
512         return accessRightList;
513     }
514     
515     /**
516      * This method creates an access note.
517      *
518      * @param accessRightVO
519      * @param db
520      * @return
521      * @throws SystemException
522      * @throws Exception
523      */

524     
525     public AccessRight create(AccessRightVO accessRightVO, InterceptionPoint interceptionPoint, Database db) throws SystemException, Exception JavaDoc
526     {
527         AccessRight accessRight = new AccessRightImpl();
528         accessRight.setValueObject(accessRightVO);
529         
530         accessRight.setInterceptionPoint(interceptionPoint);
531         
532         db.create(accessRight);
533                     
534         return accessRight;
535     }
536
537     
538     public AccessRightVO update(AccessRightVO AccessRightVO) throws ConstraintException, SystemException
539     {
540         return (AccessRightVO) updateEntity(AccessRightImpl.class, AccessRightVO);
541     }
542
543     
544     public void update(String JavaDoc parameters, HttpServletRequest JavaDoc request) throws ConstraintException, SystemException
545     {
546         Database db = CastorDatabaseService.getDatabase();
547         
548         logger.info("parameters:" + parameters);
549         
550         try
551         {
552             beginTransaction(db);
553
554             int interceptionPointIndex = 0;
555             String JavaDoc interceptionPointIdString = request.getParameter(interceptionPointIndex + "_InterceptionPointId");
556             while(interceptionPointIdString != null)
557             {
558                 delete(new Integer JavaDoc(interceptionPointIdString), parameters, false, db);
559
560                 AccessRightVO accessRightVO = new AccessRightVO();
561                 accessRightVO.setParameters(parameters);
562
563                 AccessRight accessRight = null;
564                 
565                 int roleIndex = 0;
566                 String JavaDoc roleName = request.getParameter(interceptionPointIdString + "_" + roleIndex + "_roleName");
567                 while(roleName != null)
568                 {
569                     String JavaDoc hasAccess = request.getParameter(interceptionPointIdString + "_" + roleName + "_hasAccess");
570                     
571                     if(hasAccess != null)
572                     {
573                         if(accessRight == null)
574                         {
575                             InterceptionPoint interceptionPoint = InterceptionPointController.getController().getInterceptionPointWithId(new Integer JavaDoc(interceptionPointIdString), db);
576                             accessRight = create(accessRightVO, interceptionPoint, db);
577                         }
578                         
579                         AccessRightRoleVO accessRightRoleVO = new AccessRightRoleVO();
580                         accessRightRoleVO.setRoleName(roleName);
581                         AccessRightRole accessRightRole = createAccessRightRole(db, accessRightRoleVO, accessRight);
582                         accessRight.getRoles().add(accessRightRole);
583                     }
584                     
585                     roleIndex++;
586                     roleName = request.getParameter(interceptionPointIdString + "_" + roleIndex + "_roleName");
587                 }
588
589                 int groupIndex = 0;
590                 String JavaDoc groupName = request.getParameter(interceptionPointIdString + "_" + groupIndex + "_groupName");
591
592                 while(groupName != null)
593                 {
594                     if(accessRight == null)
595                     {
596                         InterceptionPoint interceptionPoint = InterceptionPointController.getController().getInterceptionPointWithId(new Integer JavaDoc(interceptionPointIdString), db);
597                         //logger.info("Creating access for:" + interceptionPoint.getName() + "_" + parameters);
598
accessRight = create(accessRightVO, interceptionPoint, db);
599                     }
600                     
601                     AccessRightGroupVO accessRightGroupVO = new AccessRightGroupVO();
602                     accessRightGroupVO.setGroupName(groupName);
603                     AccessRightGroup accessRightGroup = createAccessRightGroup(db, accessRightGroupVO, accessRight);
604                     accessRight.getGroups().add(accessRightGroup);
605                     
606                     groupIndex++;
607                     groupName = request.getParameter(interceptionPointIdString + "_" + groupIndex + "_groupName");
608                     //logger.info("groupName:" + groupName);
609
}
610
611                 interceptionPointIndex++;
612                 interceptionPointIdString = request.getParameter(interceptionPointIndex + "_InterceptionPointId");
613             }
614             commitTransaction(db);
615         }
616         catch (Exception JavaDoc e)
617         {
618             e.printStackTrace();
619             logger.info("An error occurred so we should not complete the transaction:" + e);
620             rollbackTransaction(db);
621             throw new SystemException(e.getMessage());
622         }
623     }
624     
625     
626     public void updateGroups(Integer JavaDoc accessRightId, String JavaDoc parameters, String JavaDoc[] groupNames) throws ConstraintException, SystemException
627     {
628         Database db = CastorDatabaseService.getDatabase();
629         
630         logger.info("parameters:" + parameters);
631         
632         try
633         {
634             beginTransaction(db);
635             
636             AccessRight accessRight = this.getAccessRightWithId(accessRightId, db);
637
638             Iterator JavaDoc groupsIterator = accessRight.getGroups().iterator();
639             while(groupsIterator.hasNext())
640             {
641                 AccessRightGroup accessRightGroup = (AccessRightGroup)groupsIterator.next();
642                 groupsIterator.remove();
643                 db.remove(accessRightGroup);
644             }
645             
646             if(groupNames != null)
647             {
648                 for(int i=0; i < groupNames.length; i++)
649                 {
650                     String JavaDoc groupName = groupNames[i];
651                     AccessRightGroupVO accessRightGroupVO = new AccessRightGroupVO();
652                     accessRightGroupVO.setGroupName(groupName);
653                     AccessRightGroup accessRightGroup = createAccessRightGroup(db, accessRightGroupVO, accessRight);
654                     accessRight.getGroups().add(accessRightGroup);
655                 }
656             }
657             
658             commitTransaction(db);
659         }
660         catch (Exception JavaDoc e)
661         {
662             e.printStackTrace();
663             logger.info("An error occurred so we should not complete the transaction:" + e);
664             rollbackTransaction(db);
665             throw new SystemException(e.getMessage());
666         }
667     }
668     
669     /**
670      * Adds a user to have access
671      *
672      * @param accessRightId
673      * @param parameters
674      * @param userName
675      * @throws ConstraintException
676      * @throws SystemException
677      */

678     
679     public void addUser(String JavaDoc interceptionPointCategory, String JavaDoc parameters, String JavaDoc userName, HttpServletRequest JavaDoc request) throws ConstraintException, SystemException
680     {
681         Database db = CastorDatabaseService.getDatabase();
682         
683         logger.info("parameters:" + parameters);
684         
685         try
686         {
687             beginTransaction(db);
688             
689             try
690             {
691                 InfoGluePrincipal infoGluePrincipal = UserControllerProxy.getController(db).getUser(userName);
692                 if(infoGluePrincipal == null)
693                     throw new SystemException("The user named " + userName + " does not exist in the system.");
694             }
695             catch(Exception JavaDoc e)
696             {
697                 throw new SystemException("The user named " + userName + " does not exist in the system.");
698             }
699             
700             List JavaDoc accessRightsUsers = getAccessRightsUsers(interceptionPointCategory, parameters, userName, db);
701             Iterator JavaDoc accessRightsUsersIterator = accessRightsUsers.iterator();
702             while(accessRightsUsersIterator.hasNext())
703             {
704                 AccessRightUser accessRightUser = (AccessRightUser)accessRightsUsersIterator.next();
705
706                 db.remove(accessRightUser.getAccessRight());
707
708                 accessRightsUsersIterator.remove();
709                 db.remove(accessRightUser);
710             }
711             
712             int interceptionPointIndex = 0;
713             String JavaDoc interceptionPointIdString = request.getParameter(interceptionPointIndex + "_InterceptionPointId");
714             while(interceptionPointIdString != null)
715             {
716                 String JavaDoc hasAccess = request.getParameter(interceptionPointIdString + "_hasAccess");
717                 
718                 AccessRight accessRight = null;
719                  
720                 if(hasAccess != null)
721                 {
722                     List JavaDoc accessRights = getAccessRightListForEntity(new Integer JavaDoc(interceptionPointIdString), parameters, db);
723                     if(accessRights == null || accessRights.size() == 0)
724                     {
725                         AccessRightVO accessRightVO = new AccessRightVO();
726                         accessRightVO.setParameters(parameters);
727
728                         InterceptionPoint interceptionPoint = InterceptionPointController.getController().getInterceptionPointWithId(new Integer JavaDoc(interceptionPointIdString), db);
729                         accessRight = create(accessRightVO, interceptionPoint, db);
730                     }
731                     else
732                     {
733                         accessRight = (AccessRight)accessRights.get(0);
734                     }
735                     
736                     if(userName != null && accessRight != null)
737                     {
738                         AccessRightUserVO accessRightUserVO = new AccessRightUserVO();
739                         accessRightUserVO.setUserName(userName);
740                         AccessRightUser accessRightUser = createAccessRightUser(db, accessRightUserVO, accessRight);
741                         accessRight.getUsers().add(accessRightUser);
742                     }
743                 }
744                 
745                 interceptionPointIndex++;
746                 interceptionPointIdString = request.getParameter(interceptionPointIndex + "_InterceptionPointId");
747             }
748             
749             commitTransaction(db);
750         }
751         catch (Exception JavaDoc e)
752         {
753             logger.info("An error occurred so we should not complete the transaction:" + e);
754             rollbackTransaction(db);
755             throw new SystemException(e.getMessage());
756         }
757     }
758
759     /**
760      * Adds a user to have access
761      *
762      * @param accessRightId
763      * @param parameters
764      * @param userName
765      * @throws ConstraintException
766      * @throws SystemException
767      */

768     
769     public void deleteUser(String JavaDoc interceptionPointCategory, String JavaDoc parameters, String JavaDoc userName, HttpServletRequest JavaDoc request) throws ConstraintException, SystemException
770     {
771         Database db = CastorDatabaseService.getDatabase();
772         
773         logger.info("parameters:" + parameters);
774         
775         try
776         {
777             beginTransaction(db);
778             
779             List JavaDoc accessRightsUsers = getAccessRightsUsers(interceptionPointCategory, parameters, userName, db);
780             Iterator JavaDoc accessRightsUsersIterator = accessRightsUsers.iterator();
781             while(accessRightsUsersIterator.hasNext())
782             {
783                 AccessRightUser accessRightUser = (AccessRightUser)accessRightsUsersIterator.next();
784
785                 //if(accessRightUser.getAccessRight().)
786
//db.remove(accessRightUser.getAccessRight());
787

788                 accessRightsUsersIterator.remove();
789                 db.remove(accessRightUser);
790             }
791             
792             commitTransaction(db);
793         }
794         catch (Exception JavaDoc e)
795         {
796             e.printStackTrace();
797             logger.info("An error occurred so we should not complete the transaction:" + e);
798             rollbackTransaction(db);
799             throw new SystemException(e.getMessage());
800         }
801     }
802
803     /**
804      * This method creates a AccessRightRole-object in the database.
805      * @param db
806      * @param accessRightRoleVO
807      * @return
808      * @throws SystemException
809      */

810     
811     public AccessRightRole createAccessRightRole(Database db, AccessRightRoleVO accessRightRoleVO, AccessRight accessRight) throws SystemException, Exception JavaDoc
812     {
813         AccessRightRole accessRightRole = new AccessRightRoleImpl();
814         accessRightRole.setValueObject(accessRightRoleVO);
815         accessRightRole.setAccessRight(accessRight);
816         
817         db.create(accessRightRole);
818         
819         return accessRightRole;
820     }
821
822     /**
823      * This method creates a AccessRightGroup-object in the database.
824      * @param db
825      * @param accessRightGroupVO
826      * @return
827      * @throws SystemException
828      */

829     
830     public AccessRightGroup createAccessRightGroup(Database db, AccessRightGroupVO accessRightGroupVO, AccessRight accessRight) throws SystemException, Exception JavaDoc
831     {
832         AccessRightGroup accessRightGroup = new AccessRightGroupImpl();
833         accessRightGroup.setValueObject(accessRightGroupVO);
834         accessRightGroup.setAccessRight(accessRight);
835         
836         db.create(accessRightGroup);
837         
838         return accessRightGroup;
839     }
840
841     /**
842      * This method creates a AccessRightUser-object in the database.
843      * @param db
844      * @param accessRightUserVO
845      * @return
846      * @throws SystemException
847      */

848     
849     public AccessRightUser createAccessRightUser(Database db, AccessRightUserVO accessRightUserVO, AccessRight accessRight) throws SystemException, Exception JavaDoc
850     {
851         AccessRightUser accessRightUser = new AccessRightUserImpl();
852         accessRightUser.setValueObject(accessRightUserVO);
853         accessRightUser.setAccessRight(accessRight);
854         
855         db.create(accessRightUser);
856         
857         return accessRightUser;
858     }
859     
860     /*
861         public void update(String parameters, HttpServletRequest request) throws ConstraintException, SystemException
862     {
863         Database db = CastorDatabaseService.getDatabase();
864         
865         logger.info("parameters:" + parameters);
866         
867         try
868         {
869             beginTransaction(db);
870
871             int roleIndex = 0;
872             String roleName = request.getParameter(roleIndex + "_roleName");
873             while(roleName != null)
874             {
875                 logger.info("roleName:" + roleName);
876                 
877                 int interceptionPointIndex = 0;
878                 String interceptionPointIdString = request.getParameter(roleName + "_" + interceptionPointIndex + "_InterceptionPointId");
879                 while(interceptionPointIdString != null)
880                 {
881                     logger.info("interceptionPointIdString:" + interceptionPointIdString);
882                     
883                     delete(new Integer(interceptionPointIdString), parameters, roleName, db);
884
885                     String hasAccess = request.getParameter(roleName + "_" + interceptionPointIdString + "_hasAccess");
886                     
887                     if(hasAccess != null)
888                     {
889                         AccessRightVO accessRightVO = new AccessRightVO();
890                         accessRightVO.setRoleName(roleName);
891                         accessRightVO.setParameters(parameters);
892
893                         InterceptionPoint interceptionPoint = InterceptionPointController.getController().getInterceptionPointWithId(new Integer(interceptionPointIdString), db);
894                         logger.info("Creating access for roleName:" + roleName + ":" + parameters + "_" + interceptionPoint.getName());
895                         
896                         create(accessRightVO, interceptionPoint, db);
897                     }
898                     
899                     interceptionPointIndex++;
900                     interceptionPointIdString = request.getParameter(roleName + "_" + interceptionPointIndex + "_InterceptionPointId");
901                 }
902                                 
903                 roleIndex++;
904                 roleName = request.getParameter(roleIndex + "_roleName");
905             }
906             commitTransaction(db);
907         }
908         catch (Exception e)
909         {
910             logger.info("An error occurred so we should not complete the transaction:" + e);
911             rollbackTransaction(db);
912             throw new SystemException(e.getMessage());
913         }
914     }
915      */

916     
917     /**
918      * This method deletes all occurrencies of AccessRight which has the interceptionPointId.
919      *
920      * @param roleName
921      * @throws ConstraintException
922      * @throws SystemException
923      */

924
925     public void delete(String JavaDoc roleName) throws SystemException, Exception JavaDoc
926     {
927         Database db = CastorDatabaseService.getDatabase();
928         
929         logger.info("roleName:" + roleName);
930         
931         try
932         {
933             beginTransaction(db);
934
935             List JavaDoc accessRightList = getAccessRightList(roleName, db);
936             Iterator JavaDoc i = accessRightList.iterator();
937             while(i.hasNext())
938             {
939                 AccessRight accessRight = (AccessRight)i.next();
940                 db.remove(accessRight);
941             }
942             
943             commitTransaction(db);
944         }
945         catch (Exception JavaDoc e)
946         {
947             logger.info("An error occurred so we should not complete the transaction:" + e);
948             rollbackTransaction(db);
949             throw new SystemException(e.getMessage());
950         }
951     }
952     
953     /**
954      * This method deletes all occurrencies of AccessRight which has the interceptionPointId.
955      *
956      * @param roleName
957      * @throws ConstraintException
958      * @throws SystemException
959      */

960
961     public void delete(Integer JavaDoc interceptionPointId, String JavaDoc parameters, boolean deleteUsers, Database db) throws SystemException, Exception JavaDoc
962     {
963         List JavaDoc accessRightList = getAccessRightListOnly(interceptionPointId, parameters, db);
964         Iterator JavaDoc i = accessRightList.iterator();
965         while(i.hasNext())
966         {
967             AccessRight accessRight = (AccessRight)i.next();
968             
969             Iterator JavaDoc rolesIterator = accessRight.getRoles().iterator();
970             while(rolesIterator.hasNext())
971             {
972                 AccessRightRole accessRightRole = (AccessRightRole)rolesIterator.next();
973                 rolesIterator.remove();
974                 db.remove(accessRightRole);
975             }
976             
977             Iterator JavaDoc groupsIterator = accessRight.getGroups().iterator();
978             while(groupsIterator.hasNext())
979             {
980                 AccessRightGroup accessRightGroup = (AccessRightGroup)groupsIterator.next();
981                 groupsIterator.remove();
982                 db.remove(accessRightGroup);
983             }
984
985             if(deleteUsers)
986             {
987                 Iterator JavaDoc usersIterator = accessRight.getUsers().iterator();
988                 while(usersIterator.hasNext())
989                 {
990                     AccessRightUser accessRightUser = (AccessRightUser)usersIterator.next();
991                     usersIterator.remove();
992                     db.remove(accessRightUser);
993                 }
994     
995                 db.remove(accessRight);
996             }
997             else
998             {
999                 if(accessRight.getUsers() == null || accessRight.getUsers().size() == 0)
1000                    db.remove(accessRight);
1001            }
1002        }
1003        
1004    }
1005
1006    
1007    
1008    /**
1009     * This method checks if a role has access to an entity. It takes name and id of the entity.
1010     */

1011    public boolean getIsPrincipalAuthorized(InfoGluePrincipal infoGluePrincipal, String JavaDoc interceptionPointName, String JavaDoc parameters) throws SystemException
1012    {
1013        if(infoGluePrincipal == null)
1014            return false;
1015            
1016        if(infoGluePrincipal != null && infoGluePrincipal.getIsAdministrator())
1017            return true;
1018            
1019        boolean isPrincipalAuthorized = false;
1020        
1021        Database db = CastorDatabaseService.getDatabase();
1022
1023        try
1024        {
1025            beginTransaction(db);
1026            
1027            isPrincipalAuthorized = getIsPrincipalAuthorized(db, infoGluePrincipal, interceptionPointName, parameters);
1028        
1029            commitTransaction(db);
1030        }
1031        catch (Exception JavaDoc e)
1032        {
1033            logger.info("An error occurred so we should not complete the transaction:" + e);
1034            rollbackTransaction(db);
1035            throw new SystemException(e.getMessage());
1036        }
1037                    
1038        return isPrincipalAuthorized;
1039    }
1040    
1041    
1042    /**
1043     * This method checks if a role has access to an entity. It takes name and id of the entity.
1044     */

1045    public boolean getIsPrincipalAuthorized(Database db, InfoGluePrincipal infoGluePrincipal, String JavaDoc interceptionPointName, String JavaDoc extraParameters) throws SystemException
1046    {
1047        if(infoGluePrincipal == null)
1048          return false;
1049        
1050        if(infoGluePrincipal != null && infoGluePrincipal.getIsAdministrator())
1051            return true;
1052        
1053        //TODO
1054

1055        String JavaDoc key = "" + infoGluePrincipal.getName() + "_" + interceptionPointName + "_" + extraParameters;
1056        logger.info("key:" + key);
1057        Boolean JavaDoc cachedIsPrincipalAuthorized = (Boolean JavaDoc)CacheController.getCachedObject("authorizationCache", key);
1058        if(cachedIsPrincipalAuthorized != null)
1059        {
1060            return cachedIsPrincipalAuthorized.booleanValue();
1061        }
1062
1063        boolean isPrincipalAuthorized = false;
1064        boolean limitOnGroups = false;
1065        boolean principalHasRole = false;
1066        boolean principalHasGroup = false;
1067           
1068        Collection JavaDoc roles = infoGluePrincipal.getRoles();
1069        Collection JavaDoc groups = infoGluePrincipal.getGroups();
1070        logger.info("roles:" + roles.size());
1071        logger.info("groups:" + groups.size());
1072
1073        InterceptionPointVO interceptionPointVO = InterceptionPointController.getController().getInterceptionPointVOWithName(interceptionPointName, db);
1074        if(interceptionPointVO == null)
1075            return true;
1076                
1077        List JavaDoc accessRightList = this.getAccessRightListOnlyReadOnly(interceptionPointVO.getId(), extraParameters, db);
1078        
1079        Iterator JavaDoc accessRightListIterator = accessRightList.iterator();
1080        while(accessRightListIterator.hasNext() && !isPrincipalAuthorized)
1081        {
1082            AccessRight accessRight = (AccessRight)accessRightListIterator.next();
1083            Collection JavaDoc approvedRoles = accessRight.getRoles();
1084            Collection JavaDoc approvedGroups = accessRight.getGroups();
1085            Collection JavaDoc approvedUsers = accessRight.getUsers();
1086
1087            Iterator JavaDoc approvedUsersIterator = approvedUsers.iterator();
1088            while(approvedUsersIterator.hasNext())
1089            {
1090                AccessRightUser accessRightUser = (AccessRightUser)approvedUsersIterator.next();
1091                if(accessRightUser.getUserName().equals(infoGluePrincipal.getName()))
1092                {
1093                    isPrincipalAuthorized = true;
1094                }
1095            }
1096
1097            if(!isPrincipalAuthorized)
1098            {
1099                
1100                Iterator JavaDoc rolesIterator = roles.iterator();
1101                outer:while(rolesIterator.hasNext())
1102                {
1103                    InfoGlueRole role = (InfoGlueRole)rolesIterator.next();
1104                    logger.info("role:" + role.getName());
1105                    
1106                    Iterator JavaDoc approvedRolesIterator = approvedRoles.iterator();
1107                    while(approvedRolesIterator.hasNext())
1108                    {
1109                        AccessRightRole accessRightRole = (AccessRightRole)approvedRolesIterator.next();
1110                        if(accessRightRole.getRoleName().equals(role.getName()))
1111                        {
1112                            principalHasRole = true;
1113                            break outer;
1114                        }
1115                    }
1116                }
1117     
1118                Iterator JavaDoc approvedGroupsIterator = approvedGroups.iterator();
1119                outer:while(approvedGroupsIterator.hasNext())
1120                {
1121                    AccessRightGroup accessRightGroup = (AccessRightGroup)approvedGroupsIterator.next();
1122                    logger.info("accessRightGroup:" + accessRightGroup.getGroupName());
1123    
1124                    limitOnGroups = true;
1125    
1126                    Iterator JavaDoc groupsIterator = groups.iterator();
1127                    while(groupsIterator.hasNext())
1128                    {
1129                        InfoGlueGroup group = (InfoGlueGroup)groupsIterator.next();
1130                        if(accessRightGroup.getGroupName().equals(group.getName()))
1131                        {
1132                            principalHasGroup = true;
1133                            break outer;
1134                        }
1135                    }
1136                }
1137            }
1138        }
1139
1140        if((principalHasRole && principalHasGroup) || (principalHasRole && !limitOnGroups))
1141            isPrincipalAuthorized = true;
1142        
1143        CacheController.cacheObject("authorizationCache", key, new Boolean JavaDoc(isPrincipalAuthorized));
1144
1145        return isPrincipalAuthorized;
1146    }
1147    
1148    /**
1149     * This method checks if a role has access to an entity. It takes name and id of the entity.
1150     */

1151
1152    public boolean getIsPrincipalAuthorized(InfoGluePrincipal infoGluePrincipal, String JavaDoc interceptionPointName) throws SystemException
1153    {
1154        if(infoGluePrincipal.getIsAdministrator())
1155            return true;
1156            
1157        String JavaDoc key = "" + infoGluePrincipal.getName() + "_" + interceptionPointName;
1158        logger.info("key:" + key);
1159        Boolean JavaDoc cachedIsPrincipalAuthorized = (Boolean JavaDoc)CacheController.getCachedObject("authorizationCache", key);
1160        if(cachedIsPrincipalAuthorized != null)
1161        {
1162            logger.info("There was an cached authorization:" + cachedIsPrincipalAuthorized);
1163            return cachedIsPrincipalAuthorized.booleanValue();
1164        }
1165        
1166        boolean isPrincipalAuthorized = false;
1167        
1168        Database db = CastorDatabaseService.getDatabase();
1169
1170        try
1171        {
1172            beginTransaction(db);
1173            
1174            isPrincipalAuthorized = getIsPrincipalAuthorized(db, infoGluePrincipal, interceptionPointName);
1175            
1176            CacheController.cacheObject("authorizationCache", key, new Boolean JavaDoc(isPrincipalAuthorized));
1177            
1178            commitTransaction(db);
1179        }
1180        catch (Exception JavaDoc e)
1181        {
1182            logger.info("An error occurred so we should not complete the transaction:" + e);
1183            rollbackTransaction(db);
1184            throw new SystemException(e.getMessage());
1185        }
1186                    
1187        return isPrincipalAuthorized;
1188    }
1189    
1190    /**
1191     * This method checks if a role has access to an entity. It takes name and id of the entity.
1192     */

1193
1194    public boolean getIsPrincipalAuthorized(Database db, InfoGluePrincipal infoGluePrincipal, String JavaDoc interceptionPointName) throws SystemException
1195    {
1196        if(infoGluePrincipal.getIsAdministrator())
1197            return true;
1198
1199        boolean isPrincipalAuthorized = false;
1200        boolean limitOnGroups = false;
1201        boolean principalHasRole = false;
1202        boolean principalHasGroup = false;
1203           
1204        Collection JavaDoc roles = infoGluePrincipal.getRoles();
1205        Collection JavaDoc groups = infoGluePrincipal.getGroups();
1206        InterceptionPoint interceptionPoint = InterceptionPointController.getController().getInterceptionPointWithName(interceptionPointName, db);
1207        List JavaDoc accessRightList = this.getAccessRightList(interceptionPoint.getId(), db);
1208
1209        Iterator JavaDoc accessRightListIterator = accessRightList.iterator();
1210        while(accessRightListIterator.hasNext() && !isPrincipalAuthorized)
1211        {
1212            AccessRight accessRight = (AccessRight)accessRightListIterator.next();
1213            Collection JavaDoc approvedRoles = accessRight.getRoles();
1214            Collection JavaDoc approvedGroups = accessRight.getGroups();
1215            Collection JavaDoc approvedUsers = accessRight.getUsers();
1216
1217            Iterator JavaDoc approvedUsersIterator = approvedUsers.iterator();
1218            while(approvedUsersIterator.hasNext())
1219            {
1220                AccessRightUser accessRightUser = (AccessRightUser)approvedUsersIterator.next();
1221                if(accessRightUser.getUserName().equals(infoGluePrincipal.getName()))
1222                {
1223                    isPrincipalAuthorized = true;
1224                }
1225            }
1226
1227            if(!isPrincipalAuthorized)
1228            {
1229                Iterator JavaDoc rolesIterator = roles.iterator();
1230                outer:while(rolesIterator.hasNext())
1231                {
1232                    InfoGlueRole role = (InfoGlueRole)rolesIterator.next();
1233                    logger.info("role:" + role.getName());
1234                    
1235                    Iterator JavaDoc approvedRolesIterator = approvedRoles.iterator();
1236                    while(approvedRolesIterator.hasNext())
1237                    {
1238                        AccessRightRole accessRightRole = (AccessRightRole)approvedRolesIterator.next();
1239                        if(accessRightRole.getRoleName().equals(role.getName()))
1240                        {
1241                            principalHasRole = true;
1242                            break outer;
1243                        }
1244                    }
1245                }
1246     
1247                Iterator JavaDoc approvedGroupsIterator = approvedGroups.iterator();
1248                outer:while(approvedGroupsIterator.hasNext())
1249                {
1250                    AccessRightGroup accessRightGroup = (AccessRightGroup)approvedGroupsIterator.next();
1251                    logger.info("accessRightGroup:" + accessRightGroup.getGroupName());
1252    
1253                    limitOnGroups = true;
1254    
1255                    Iterator JavaDoc groupsIterator = groups.iterator();
1256                    while(groupsIterator.hasNext())
1257                    {
1258                        InfoGlueGroup group = (InfoGlueGroup)groupsIterator.next();
1259                        if(accessRightGroup.getGroupName().equals(group.getName()))
1260                        {
1261                            principalHasGroup = true;
1262                            break outer;
1263                        }
1264                    }
1265                }
1266            }
1267        }
1268        
1269        if((principalHasRole && principalHasGroup) || (principalHasRole && !limitOnGroups))
1270            isPrincipalAuthorized = true;
1271        
1272        logger.info("isPrincipalAuthorized:" + isPrincipalAuthorized);
1273        
1274        return isPrincipalAuthorized;
1275    }
1276
1277    //TEST
1278

1279    
1280    public Collection JavaDoc getAccessRightsUserRows(String JavaDoc interceptionPointCategory, String JavaDoc parameters) throws SystemException, Bug
1281    {
1282        Collection JavaDoc principalVOList = null;
1283        
1284        Database db = CastorDatabaseService.getDatabase();
1285
1286        try
1287        {
1288            beginTransaction(db);
1289            
1290            principalVOList = getAccessRightsUserRows(interceptionPointCategory, parameters, db);
1291
1292            commitTransaction(db);
1293        }
1294        catch (Exception JavaDoc e)
1295        {
1296            logger.warn("An error occurred so we should not complete the transaction:" + e, e);
1297            rollbackTransaction(db);
1298            throw new SystemException(e.getMessage());
1299        }
1300        
1301        return principalVOList;
1302    }
1303
1304    public Collection JavaDoc getAccessRightsUserRows(String JavaDoc interceptionPointCategory, String JavaDoc parameters, Database db) throws SystemException, Bug
1305    {
1306        Map JavaDoc accessRightsUserRows = new HashMap JavaDoc();
1307        
1308        try
1309        {
1310            List JavaDoc accessRightUsers = getAccessRightsUsers(interceptionPointCategory, parameters, db);
1311
1312            Iterator JavaDoc accessRightUsersIterator = accessRightUsers.iterator();
1313            while (accessRightUsersIterator.hasNext())
1314            {
1315                try
1316                {
1317                    AccessRightUser accessRightUser = (AccessRightUser)accessRightUsersIterator.next();
1318                    
1319                    AccessRightsUserRow accessRightsUserRow = (AccessRightsUserRow)accessRightsUserRows.get(accessRightUser.getUserName());
1320                    if(accessRightsUserRow == null)
1321                    {
1322                        InfoGluePrincipal infoGluePrincipal = UserControllerProxy.getController(db).getUser(accessRightUser.getUserName());
1323                        if(infoGluePrincipal != null)
1324                        {
1325                            AccessRightsUserRow newAccessRightsUserRow = new AccessRightsUserRow();
1326                            newAccessRightsUserRow.setUserName(infoGluePrincipal.getName());
1327                            newAccessRightsUserRow.getAccessRights().put(accessRightUser.getAccessRight().getInterceptionPoint().getId(), new Boolean JavaDoc(true));
1328                            accessRightsUserRows.put(infoGluePrincipal.getName(), newAccessRightsUserRow);
1329                        }
1330                    }
1331                    else
1332                    {
1333                        accessRightsUserRow.getAccessRights().put(accessRightUser.getAccessRight().getInterceptionPoint().getId(), new Boolean JavaDoc(true));
1334                    }
1335                }
1336                catch(Exception JavaDoc e)
1337                {
1338                    logger.info("An user did not exist although given access rights:" + e.getMessage());
1339                }
1340            }
1341        }
1342        catch(Exception JavaDoc e)
1343        {
1344            throw new SystemException("An error occurred when we tried to fetch a list of Access rights. Reason:" + e.getMessage(), e);
1345        }
1346        
1347        return accessRightsUserRows.values();
1348    }
1349
1350    public List JavaDoc getAccessRightsUsers(String JavaDoc interceptionPointCategory, String JavaDoc parameters, Database db) throws SystemException, Bug
1351    {
1352        List JavaDoc accessRightsUsers = new ArrayList JavaDoc();
1353        
1354        try
1355        {
1356            OQLQuery oql = null;
1357            
1358            if(parameters == null || parameters.length() == 0)
1359            {
1360                oql = db.getOQLQuery("SELECT aru FROM org.infoglue.cms.entities.management.impl.simple.AccessRightUserImpl aru WHERE aru.accessRight.interceptionPoint.category = $1 AND (is_undefined(aru.accessRight.parameters) OR aru.accessRight.parameters = $2)");
1361                oql.bind(interceptionPointCategory);
1362                oql.bind(parameters);
1363            }
1364            else
1365            {
1366                oql = db.getOQLQuery("SELECT aru FROM org.infoglue.cms.entities.management.impl.simple.AccessRightUserImpl aru WHERE aru.accessRight.interceptionPoint.category = $1 AND aru.accessRight.parameters = $2");
1367                oql.bind(interceptionPointCategory);
1368                oql.bind(parameters);
1369            }
1370            
1371            QueryResults results = oql.execute();
1372
1373            while (results.hasMore())
1374            {
1375                AccessRightUser accessRightUser = (AccessRightUser)results.next();
1376                accessRightsUsers.add(accessRightUser);
1377            }
1378            
1379            results.close();
1380            oql.close();
1381        }
1382        catch(Exception JavaDoc e)
1383        {
1384            throw new SystemException("An error occurred when we tried to fetch a list of Access rights. Reason:" + e.getMessage(), e);
1385        }
1386        
1387        return accessRightsUsers;
1388    }
1389    
1390    public List JavaDoc getAccessRightsUsers(String JavaDoc interceptionPointCategory, String JavaDoc parameters, String JavaDoc userName, Database db) throws SystemException, Bug
1391    {
1392        List JavaDoc accessRightsUsers = new ArrayList JavaDoc();
1393        
1394        try
1395        {
1396            OQLQuery oql = null;
1397            
1398            if(parameters == null || parameters.length() == 0)
1399            {
1400                oql = db.getOQLQuery("SELECT aru FROM org.infoglue.cms.entities.management.impl.simple.AccessRightUserImpl aru WHERE aru.accessRight.interceptionPoint.category = $1 AND (is_undefined(aru.accessRight.parameters) OR aru.accessRight.parameters = $2) AND aru.userName = $3");
1401                oql.bind(interceptionPointCategory);
1402                oql.bind(parameters);
1403                oql.bind(userName);
1404            }
1405            else
1406            {
1407                oql = db.getOQLQuery("SELECT aru FROM org.infoglue.cms.entities.management.impl.simple.AccessRightUserImpl aru WHERE aru.accessRight.interceptionPoint.category = $1 AND aru.accessRight.parameters = $2 AND aru.userName = $3");
1408                oql.bind(interceptionPointCategory);
1409                oql.bind(parameters);
1410                oql.bind(userName);
1411            }
1412
1413            QueryResults results = oql.execute();
1414
1415            while (results.hasMore())
1416            {
1417                AccessRightUser accessRightUser = (AccessRightUser)results.next();
1418                accessRightsUsers.add(accessRightUser);
1419            }
1420
1421            results.close();
1422            oql.close();
1423        }
1424        catch(Exception JavaDoc e)
1425        {
1426            e.printStackTrace();
1427            throw new SystemException("An error occurred when we tried to fetch a list of Access rights. Reason:" + e.getMessage(), e);
1428        }
1429        
1430        return accessRightsUsers;
1431    }
1432    
1433    //TEST
1434

1435    
1436    public List JavaDoc getAccessRightUserList(String JavaDoc userName, Database db) throws SystemException, Bug
1437    {
1438        List JavaDoc accessRightUserList = new ArrayList JavaDoc();
1439        
1440        try
1441        {
1442            OQLQuery oql = null;
1443            
1444            oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightUserImpl f WHERE f.userName = $1");
1445            oql.bind(userName);
1446
1447            QueryResults results = oql.execute();
1448
1449            while (results.hasMore())
1450            {
1451                AccessRightUser accessRightUser = (AccessRightUser)results.next();
1452                accessRightUserList.add(accessRightUser);
1453            }
1454            
1455            results.close();
1456            oql.close();
1457        }
1458        catch(Exception JavaDoc e)
1459        {
1460            e.printStackTrace();
1461            throw new SystemException("An error occurred when we tried to fetch a list of Access rights users. Reason:" + e.getMessage(), e);
1462        }
1463        
1464        return accessRightUserList;
1465    }
1466
1467    public List JavaDoc getAccessRightRoleList(String JavaDoc roleName, Database db, boolean readOnly) throws SystemException, Bug
1468    {
1469        List JavaDoc accessRightRoleList = new ArrayList JavaDoc();
1470        
1471        try
1472        {
1473            OQLQuery oql = null;
1474            
1475            oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightRoleImpl f WHERE f.roleName = $1");
1476            oql.bind(roleName);
1477
1478            QueryResults results;
1479            if(readOnly)
1480                results = oql.execute(Database.ReadOnly);
1481            else
1482                results = oql.execute();
1483                
1484            while (results.hasMore())
1485            {
1486                AccessRightRole accessRightRole = (AccessRightRole)results.next();
1487                if(!readOnly && accessRightRole.getAccessRight() == null)
1488                    db.remove(accessRightRole);
1489                else
1490                    accessRightRoleList.add(accessRightRole);
1491            }
1492            
1493            results.close();
1494            oql.close();
1495        }
1496        catch(Exception JavaDoc e)
1497        {
1498            e.printStackTrace();
1499            throw new SystemException("An error occurred when we tried to fetch a list of Access rights users. Reason:" + e.getMessage(), e);
1500        }
1501        
1502        return accessRightRoleList;
1503    }
1504
1505    public List JavaDoc getAccessRightGroupList(String JavaDoc groupName, Database db) throws SystemException, Bug
1506    {
1507        List JavaDoc accessRightGroupList = new ArrayList JavaDoc();
1508        
1509        try
1510        {
1511            OQLQuery oql = null;
1512            
1513            oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.AccessRightGroupImpl f WHERE f.groupName = $1");
1514            oql.bind(groupName);
1515
1516            QueryResults results = oql.execute();
1517
1518            while (results.hasMore())
1519            {
1520                AccessRightGroup accessRightGroup = (AccessRightGroup)results.next();
1521                accessRightGroupList.add(accessRightGroup);
1522            }
1523            
1524            results.close();
1525            oql.close();
1526        }
1527        catch(Exception JavaDoc e)
1528        {
1529            e.printStackTrace();
1530            throw new SystemException("An error occurred when we tried to fetch a list of Access rights users. Reason:" + e.getMessage(), e);
1531        }
1532        
1533        return accessRightGroupList;
1534    }
1535
1536    
1537    /**
1538     * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
1539     * is handling.
1540     */

1541
1542    public BaseEntityVO getNewVO()
1543    {
1544        return new AccessRightVO();
1545    }
1546
1547}
1548 
Popular Tags