KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.log4j.Logger;
34 import org.exolab.castor.jdo.Database;
35 import org.exolab.castor.jdo.OQLQuery;
36 import org.exolab.castor.jdo.QueryResults;
37 import org.infoglue.cms.entities.kernel.BaseEntityVO;
38 import org.infoglue.cms.entities.management.AccessRight;
39 import org.infoglue.cms.entities.management.AccessRightGroup;
40 import org.infoglue.cms.entities.management.AccessRightRole;
41 import org.infoglue.cms.entities.management.AccessRightUser;
42 import org.infoglue.cms.entities.management.InterceptionPoint;
43 import org.infoglue.cms.entities.management.InterceptionPointVO;
44 import org.infoglue.cms.entities.management.Interceptor;
45 import org.infoglue.cms.entities.management.impl.simple.InterceptionPointImpl;
46 import org.infoglue.cms.exception.Bug;
47 import org.infoglue.cms.exception.ConstraintException;
48 import org.infoglue.cms.exception.SystemException;
49 import org.infoglue.cms.util.ConstraintExceptionBuffer;
50 import org.infoglue.deliver.util.NullObject;
51 import org.infoglue.deliver.util.CacheController;
52
53 /**
54  * This class is a helper class for the use case handle InterceptionPoint
55  *
56  * @author Mattias Bogeblad
57  */

58
59 public class InterceptionPointController extends BaseController
60 {
61     private final static Logger logger = Logger.getLogger(InterceptionPointController.class.getName());
62
63     public final static Map JavaDoc systemInterceptionPoints = new HashMap JavaDoc();
64     
65     static
66     {
67         systemInterceptionPoints.put("Category.Read", new InterceptionPointVO("Category", "Category.Read", "This intercepts any read towards a category", true));
68         systemInterceptionPoints.put("Content.Read", new InterceptionPointVO("Content", "Content.Read", "Intercepts the read of a content", true));
69         systemInterceptionPoints.put("Content.Write", new InterceptionPointVO("Content", "Content.Write", "Intercepts the write of a content", true));
70         systemInterceptionPoints.put("Content.Create", new InterceptionPointVO("Content", "Content.Create", "Intercepts the creation of a new content or folder", true));
71         systemInterceptionPoints.put("Content.Delete", new InterceptionPointVO("Content", "Content.Delete", "Intercepts the deletion of a content", true));
72         systemInterceptionPoints.put("Content.Move", new InterceptionPointVO("Content", "Content.Move", "Intercepts the movement of a content", true));
73         systemInterceptionPoints.put("Content.SubmitToPublish", new InterceptionPointVO("Content", "Content.SubmitToPublish", "Intercepts the submittance to publish of all content versions", true));
74         systemInterceptionPoints.put("Content.ChangeAccessRights", new InterceptionPointVO("Content", "Content.ChangeAccessRights", "Intercepts the attempt to change access rights", true));
75         systemInterceptionPoints.put("Content.CreateVersion", new InterceptionPointVO("Content", "Content.CreateVersion", "Intercepts the creation of a new contentversion", true));
76         systemInterceptionPoints.put("ContentTool.Read", new InterceptionPointVO("ContentTool", "ContentTool.Read", "Gives a user access to the content tool", false));
77         systemInterceptionPoints.put("ContentTypeDefinition.Read", new InterceptionPointVO("ContentTypeDefinition", "ContentTypeDefinition.Read", "This point checks access to read/use a content type definition", true));
78         systemInterceptionPoints.put("ContentVersion.Delete", new InterceptionPointVO("ContentVersion", "ContentVersion.Delete", "Intercepts the deletion of a contentversion", true));
79         systemInterceptionPoints.put("ContentVersion.Write", new InterceptionPointVO("ContentVersion", "ContentVersion.Write", "Intercepts the editing of a contentversion", true));
80         systemInterceptionPoints.put("ContentVersion.Read", new InterceptionPointVO("ContentVersion", "ContentVersion.Read", "Intercepts the read of a contentversion", true));
81         systemInterceptionPoints.put("ContentVersion.Publish", new InterceptionPointVO("ContentVersion", "ContentVersion.Publish", "Intercepts the direct publishing of a content version", true));
82         systemInterceptionPoints.put("ManagementTool.Read", new InterceptionPointVO("ManagementTool", "ManagementTool.Read", "Gives a user access to the management tool", false));
83         systemInterceptionPoints.put("MyDesktopTool.Read", new InterceptionPointVO("MyDesktopTool", "MyDesktopTool.Read", "Gives the user access to the mydesktop tool", false));
84         systemInterceptionPoints.put("PublishingTool.Read", new InterceptionPointVO("PublishingTool", "PublishingTool.Read", "Gives the user access to the publishing tool", false));
85         systemInterceptionPoints.put("Repository.Read", new InterceptionPointVO("Repository", "Repository.Read", "Gives a user access to look at a repository", true));
86         systemInterceptionPoints.put("Repository.ReadForBinding", new InterceptionPointVO("Repository", "Repository.ReadForBinding", "This point intercepts when a user tries to read the repository in a binding dialog", true));
87         systemInterceptionPoints.put("SiteNodeVersion.Read", new InterceptionPointVO("SiteNodeVersion", "SiteNodeVersion.Read", "Intercepts the read of a SiteNodeVersion", true));
88         systemInterceptionPoints.put("SiteNodeVersion.Write", new InterceptionPointVO("SiteNodeVersion", "SiteNodeVersion.Write", "Intercepts the write of a SiteNodeVersion", true));
89         systemInterceptionPoints.put("SiteNodeVersion.CreateSiteNode", new InterceptionPointVO("SiteNodeVersion", "SiteNodeVersion.CreateSiteNode", "Intercepts the creation of a new sitenode", true));
90         systemInterceptionPoints.put("SiteNodeVersion.DeleteSiteNode", new InterceptionPointVO("SiteNodeVersion", "SiteNodeVersion.DeleteSiteNode", "Intercepts the deletion of a sitenode", true));
91         systemInterceptionPoints.put("SiteNodeVersion.MoveSiteNode", new InterceptionPointVO("SiteNodeVersion", "SiteNodeVersion.MoveSiteNode", "Intercepts the movement of a sitenode", true));
92         systemInterceptionPoints.put("SiteNodeVersion.SubmitToPublish", new InterceptionPointVO("SiteNodeVersion", "SiteNodeVersion.SubmitToPublish", "Intercepts the submittance to publish of all content versions", true));
93         systemInterceptionPoints.put("SiteNodeVersion.ChangeAccessRights", new InterceptionPointVO("SiteNodeVersion", "SiteNodeVersion.ChangeAccessRights", "Intercepts the attempt to change access rights", true));
94         systemInterceptionPoints.put("SiteNodeVersion.Publish", new InterceptionPointVO("SiteNodeVersion", "SiteNodeVersion.Publish", "Intercepts the direct publishing of a siteNode version", true));
95         systemInterceptionPoints.put("StructureTool.Read", new InterceptionPointVO("StructureTool", "StructureTool.Read", "Gives a user access to the structure tool", false));
96         systemInterceptionPoints.put("StructureTool.SaveTemplate", new InterceptionPointVO("StructureTool", "StructureTool.SaveTemplate", "This interception point limits who get the save-button in the toolbar", false));
97         systemInterceptionPoints.put("StructureTool.Palette", new InterceptionPointVO("StructureTool", "StructureTool.Palette", "This interception point limits who sees the toolbar", false));
98         systemInterceptionPoints.put("ComponentEditor.ChangeSlotAccess", new InterceptionPointVO("ComponentEditor", "ComponentEditor.ChangeSlotAccess", "This interception point limits who can set access rights to a slot", false));
99         systemInterceptionPoints.put("ComponentEditor.AddComponent", new InterceptionPointVO("ComponentEditor", "ComponentEditor.AddComponent", "This interception point limits who can add a component to a specific slot", true));
100         systemInterceptionPoints.put("ComponentEditor.DeleteComponent", new InterceptionPointVO("ComponentEditor", "ComponentEditor.DeleteComponent", "This interception point limits who can delete a component in a specific slot", true));
101         systemInterceptionPoints.put("ComponentPropertyEditor.EditProperty", new InterceptionPointVO("ComponentPropertyEditor", "ComponentPropertyEditor.EditProperty", "This interception point limits who can edit a specific component property", true));
102         systemInterceptionPoints.put("Publication.Write", new InterceptionPointVO("Publication", "Publication.Write", "This interception point intercepts publications", true));
103     }
104     
105     /**
106      * Factory method
107      */

108
109     public static InterceptionPointController getController()
110     {
111         return new InterceptionPointController();
112     }
113     
114     public InterceptionPoint getInterceptionPointWithId(Integer JavaDoc interceptionPointId, Database db) throws SystemException, Bug
115     {
116         return (InterceptionPoint) getObjectWithId(InterceptionPointImpl.class, interceptionPointId, db);
117     }
118     
119     public InterceptionPointVO getInterceptionPointVOWithId(Integer JavaDoc interceptionPointId) throws SystemException, Bug
120     {
121         return (InterceptionPointVO) getVOWithId(InterceptionPointImpl.class, interceptionPointId);
122     }
123   
124     public List JavaDoc getInterceptionPointVOList() throws SystemException, Bug
125     {
126         return getAllVOObjects(InterceptionPointImpl.class, "interceptionPointId");
127     }
128
129     public List JavaDoc getSortedInterceptionPointVOList() throws SystemException, Bug
130     {
131         return getAllVOObjects(InterceptionPointImpl.class, "category", "asc");
132     }
133     
134     public List JavaDoc getInterceptorsVOList(Integer JavaDoc interceptionPointId) throws SystemException, Bug
135     {
136         List JavaDoc interceptorVOList = null;
137         
138         Database db = CastorDatabaseService.getDatabase();
139
140         try
141         {
142             beginTransaction(db);
143
144             interceptorVOList = getInterceptorsVOList(interceptionPointId, db);
145
146             commitTransaction(db);
147         }
148         catch (Exception JavaDoc e)
149         {
150             logger.info("An error occurred so we should not complete the transaction:" + e);
151             rollbackTransaction(db);
152             throw new SystemException(e.getMessage());
153         }
154         
155         return interceptorVOList;
156     }
157     
158     /**
159      * Gets the interceptors for this interceptionPoint withing a transaction
160      *
161      * @param interceptionPointId
162      * @param db
163      * @return
164      * @throws SystemException
165      * @throws Bug
166      */

167     
168     public List JavaDoc getInterceptorsVOList(Integer JavaDoc interceptionPointId, Database db) throws SystemException, Bug
169     {
170         String JavaDoc key = "" + interceptionPointId;
171         logger.info("key:" + key);
172         List JavaDoc cachedInterceptorVOList = (List JavaDoc)CacheController.getCachedObject("interceptorsCache", key);
173         if(cachedInterceptorVOList != null)
174         {
175             logger.info("There was an cached InterceptorVOList:" + cachedInterceptorVOList.size());
176             return cachedInterceptorVOList;
177         }
178         
179         List JavaDoc interceptorsVOList = null;
180         
181         InterceptionPoint interceptionPoint = this.getInterceptionPointWithId(interceptionPointId, db);
182         
183         Collection JavaDoc interceptors = interceptionPoint.getInterceptors();
184         
185         interceptorsVOList = toVOList(interceptors);
186         
187         CacheController.cacheObject("interceptorsCache", key, interceptorsVOList);
188
189         return interceptorsVOList;
190     }
191
192
193     public InterceptionPointVO getInterceptionPointVOWithName(String JavaDoc interceptorPointName) throws SystemException, Bug
194     {
195         InterceptionPointVO interceptionPointVO = null;
196         
197         Database db = CastorDatabaseService.getDatabase();
198
199         try
200         {
201             beginTransaction(db);
202
203             InterceptionPoint interceptionPoint = getInterceptionPointWithName(interceptorPointName, db);
204             if(interceptionPoint != null)
205                 interceptionPointVO = interceptionPoint.getValueObject();
206
207             commitTransaction(db);
208         }
209         catch (Exception JavaDoc e)
210         {
211             logger.info("An error occurred so we should not complete the transaction:" + e);
212             rollbackTransaction(db);
213             throw new SystemException(e.getMessage());
214         }
215         
216         return interceptionPointVO;
217     }
218
219     public InterceptionPointVO getInterceptionPointVOWithName(String JavaDoc interceptorPointName, Database db) throws SystemException, Bug
220     {
221         String JavaDoc key = "" + interceptorPointName;
222         logger.info("key:" + key);
223         
224         InterceptionPointVO interceptionPointVO = null;
225         
226         Object JavaDoc object = CacheController.getCachedObject("interceptionPointCache", key);
227         
228         if(object instanceof NullObject)
229         {
230             return null;
231         }
232         else if(object != null)
233         {
234             interceptionPointVO = (InterceptionPointVO)object;
235         }
236         else
237         {
238
239         /*
240         InterceptionPointVO interceptionPointVO = (InterceptionPointVO)CacheController.getCachedObject("interceptionPointCache", key);
241         if(interceptionPointVO != null)
242         {
243             System.out.println("interceptionPointVO:" + interceptionPointVO.getName());
244             logger.info("There was an cached interceptionPointVO:" + interceptionPointVO);
245         }
246         else
247         {
248         */

249             InterceptionPoint interceptorPoint = null;
250             
251             try
252             {
253                 OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.InterceptionPointImpl f WHERE f.name = $1");
254                 oql.bind(interceptorPointName);
255                 
256                 QueryResults results = oql.execute(Database.ReadOnly);
257                 if(results.hasMore())
258                 {
259                     interceptorPoint = (InterceptionPoint)results.next();
260                     interceptionPointVO = interceptorPoint.getValueObject();
261                                                  
262                     CacheController.cacheObject("interceptionPointCache", key, interceptionPointVO);
263                     //System.out.println("Caching interceptionPointVO:" + key + ":" + interceptionPointVO.getName());
264
}
265                 else
266                 {
267                     CacheController.cacheObject("interceptionPointCache", key, new NullObject());
268                 }
269                 
270                 results.close();
271                 oql.close();
272             }
273             catch(Exception JavaDoc e)
274             {
275                 throw new SystemException("An error occurred when we tried to fetch an InterceptionPointVO. Reason:" + e.getMessage(), e);
276             }
277         }
278         
279         return interceptionPointVO;
280     }
281
282
283     public InterceptionPoint getInterceptionPointWithName(String JavaDoc interceptorPointName, Database db) throws SystemException, Bug
284     {
285         InterceptionPoint interceptorPoint = null;
286         
287         try
288         {
289             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.InterceptionPointImpl f WHERE f.name = $1");
290             oql.bind(interceptorPointName);
291             
292             QueryResults results = oql.execute();
293             this.logger.info("Fetching entity in read/write mode:" + interceptorPointName);
294             if(results.hasMore())
295             {
296                 interceptorPoint = (InterceptionPoint)results.next();
297             }
298             
299             results.close();
300             oql.close();
301         }
302         catch(Exception JavaDoc e)
303         {
304             throw new SystemException("An error occurred when we tried to fetch an InterceptionPointVO. Reason:" + e.getMessage(), e);
305         }
306         
307         return interceptorPoint;
308     }
309     
310     
311     public List JavaDoc getInterceptionPointVOList(String JavaDoc category) throws SystemException, Bug
312     {
313         List JavaDoc interceptionPointVOList = null;
314         
315         Database db = CastorDatabaseService.getDatabase();
316
317         try
318         {
319             beginTransaction(db);
320
321             interceptionPointVOList = toVOList(getInterceptionPointList(category, db));
322
323             commitTransaction(db);
324         }
325         catch (Exception JavaDoc e)
326         {
327             logger.info("An error occurred so we should not complete the transaction:" + e);
328             rollbackTransaction(db);
329             throw new SystemException(e.getMessage());
330         }
331         
332         return interceptionPointVOList;
333     }
334     
335     
336     public List JavaDoc getInterceptionPointList(String JavaDoc category, Database db) throws SystemException, Bug
337     {
338         List JavaDoc interceptionPoints = new ArrayList JavaDoc();
339         
340         try
341         {
342             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.InterceptionPointImpl f WHERE f.category = $1");
343             oql.bind(category);
344             
345             QueryResults results = oql.execute();
346             this.logger.info("Fetching entity in read/write mode:" + category);
347
348             while(results.hasMore())
349             {
350                 InterceptionPoint interceptionPoint = (InterceptionPoint)results.next();
351                 interceptionPoints.add(interceptionPoint);
352             }
353             
354             results.close();
355             oql.close();
356         }
357         catch(Exception JavaDoc e)
358         {
359             throw new SystemException("An error occurred when we tried to fetch an InterceptionPointVO. Reason:" + e.getMessage(), e);
360         }
361         
362         return interceptionPoints;
363     }
364
365     /**
366      * Creates a new Interception point
367      *
368      * @param interceptionPointVO
369      * @return
370      * @throws ConstraintException
371      * @throws SystemException
372      */

373     
374     public InterceptionPointVO create(InterceptionPointVO interceptionPointVO) throws ConstraintException, SystemException
375     {
376         InterceptionPointVO newInterceptionPointVO = null;
377         
378         Database db = CastorDatabaseService.getDatabase();
379
380         try
381         {
382             beginTransaction(db);
383
384             newInterceptionPointVO = create(interceptionPointVO, db);
385                 
386             commitTransaction(db);
387         }
388         catch (Exception JavaDoc e)
389         {
390             logger.info("An error occurred so we should not complete the transaction:" + e);
391             rollbackTransaction(db);
392             throw new SystemException(e.getMessage());
393         }
394         
395         return newInterceptionPointVO;
396     }
397     
398     /**
399      * Creates a new Interception point within a transaction
400      *
401      * @param interceptionPointVO
402      * @return
403      * @throws ConstraintException
404      * @throws SystemException
405      */

406     
407     public InterceptionPointVO create(InterceptionPointVO interceptionPointVO, Database db) throws SystemException, Exception JavaDoc
408     {
409         InterceptionPoint interceptionPoint = new InterceptionPointImpl();
410         interceptionPoint.setValueObject(interceptionPointVO);
411         
412         db.create(interceptionPoint);
413                     
414         return interceptionPoint.getValueObject();
415     }
416
417     
418     public InterceptionPointVO update(InterceptionPointVO interceptionPointVO) throws ConstraintException, SystemException
419     {
420         return (InterceptionPointVO) updateEntity(InterceptionPointImpl.class, interceptionPointVO);
421     }
422
423     
424     public void update(InterceptionPointVO interceptionPointVO, String JavaDoc[] values) throws ConstraintException, SystemException
425     {
426         Database db = CastorDatabaseService.getDatabase();
427
428         try
429         {
430             beginTransaction(db);
431             
432             ConstraintExceptionBuffer ceb = interceptionPointVO.validate();
433             ceb.throwIfNotEmpty();
434             
435             InterceptionPoint interceptionPoint = this.getInterceptionPointWithId(interceptionPointVO.getInterceptionPointId(), db);
436
437             interceptionPoint.setValueObject(interceptionPointVO);
438             
439             Collection JavaDoc interceptors = interceptionPoint.getInterceptors();
440             Iterator JavaDoc interceptorsIterator = interceptors.iterator();
441             while(interceptorsIterator.hasNext())
442             {
443                 Interceptor interceptor = (Interceptor)interceptorsIterator.next();
444                 interceptor.getInterceptionPoints().remove(interceptor);
445             }
446             
447             interceptionPoint.getInterceptors().clear();
448             
449             if(values != null)
450             {
451                 for(int i=0; i<values.length; i++)
452                 {
453                     String JavaDoc interceptorId = values[i];
454                     Interceptor interceptor = InterceptorController.getController().getInterceptorWithId(new Integer JavaDoc(interceptorId), db);
455                     interceptionPoint.getInterceptors().add(interceptor);
456                     interceptor.getInterceptionPoints().add(interceptionPoint);
457                 }
458             }
459             
460             logger.info("Interceptors:" + interceptionPoint.getInterceptors().size());
461             
462             commitTransaction(db);
463         }
464         catch (Exception JavaDoc e)
465         {
466             logger.info("An error occurred so we should not complete the transaction:" + e);
467             rollbackTransaction(db);
468             throw new SystemException(e.getMessage());
469         }
470     }
471     
472     
473     public void delete(InterceptionPointVO interceptionPointVO) throws ConstraintException, SystemException
474     {
475         Database db = CastorDatabaseService.getDatabase();
476         
477         beginTransaction(db);
478
479         try
480         {
481             InterceptionPoint interceptionPoint = this.getInterceptionPointWithId(interceptionPointVO.getInterceptionPointId(), db);
482             
483             List JavaDoc accessRights = AccessRightController.getController().getAccessRightList(interceptionPoint.getInterceptionPointId(), db);
484             Iterator JavaDoc accessRightsIterator = accessRights.iterator();
485             while(accessRightsIterator.hasNext())
486             {
487                 AccessRight accessRight = (AccessRight)accessRightsIterator.next();
488                 
489                 Iterator JavaDoc groupIterator = accessRight.getGroups().iterator();
490                 while(groupIterator.hasNext())
491                 {
492                     AccessRightGroup group = (AccessRightGroup)groupIterator.next();
493                     groupIterator.remove();
494                     db.remove(group);
495                 }
496                 
497                 Iterator JavaDoc roleIterator = accessRight.getRoles().iterator();
498                 while(roleIterator.hasNext())
499                 {
500                     AccessRightRole role = (AccessRightRole)roleIterator.next();
501                     roleIterator.remove();
502                     db.remove(role);
503                 }
504
505                 Iterator JavaDoc userIterator = accessRight.getUsers().iterator();
506                 while(userIterator.hasNext())
507                 {
508                     AccessRightUser user = (AccessRightUser)userIterator.next();
509                     userIterator.remove();
510                     db.remove(user);
511                 }
512
513                 db.remove(accessRight);
514                 accessRightsIterator.remove();
515             }
516             
517             db.remove(interceptionPoint);
518     
519             commitTransaction(db);
520         }
521         catch(Exception JavaDoc e)
522         {
523             logger.error("An error occurred so we should not complete the transaction:" + e, e);
524             rollbackTransaction(db);
525             throw new SystemException(e.getMessage());
526         }
527     
528     }
529
530     /*
531     public void delete(String name, String value, Database db) throws SystemException, Exception
532     {
533         List AccessList = getAccessList(name, value, db);
534         Iterator i = AccessList.iterator();
535         while(i.hasNext())
536         {
537             Access Access = (Access)i.next();
538             db.remove(Access);
539         }
540         
541     }
542     */

543
544     /**
545      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
546      * is handling.
547      */

548
549     public BaseEntityVO getNewVO()
550     {
551         return new InterceptionPointVO();
552     }
553
554 }
555  
556
Popular Tags