KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.log4j.Logger;
31 import org.exolab.castor.jdo.Database;
32 import org.exolab.castor.jdo.OQLQuery;
33 import org.exolab.castor.jdo.QueryResults;
34 import org.infoglue.cms.entities.content.ContentVO;
35 import org.infoglue.cms.entities.kernel.BaseEntityVO;
36 import org.infoglue.cms.entities.management.Language;
37 import org.infoglue.cms.entities.management.Repository;
38 import org.infoglue.cms.entities.management.RepositoryLanguage;
39 import org.infoglue.cms.entities.management.RepositoryVO;
40 import org.infoglue.cms.entities.management.impl.simple.RepositoryImpl;
41 import org.infoglue.cms.entities.structure.SiteNodeVO;
42 import org.infoglue.cms.exception.Bug;
43 import org.infoglue.cms.exception.ConstraintException;
44 import org.infoglue.cms.exception.SystemException;
45 import org.infoglue.cms.security.InfoGluePrincipal;
46 import org.infoglue.cms.util.ConstraintExceptionBuffer;
47 import org.infoglue.deliver.util.CacheController;
48
49 public class RepositoryController extends BaseController
50 {
51     private final static Logger logger = Logger.getLogger(RepositoryController.class.getName());
52
53     /**
54      * Factory method
55      */

56
57     public static RepositoryController getController()
58     {
59         return new RepositoryController();
60     }
61     
62     public RepositoryVO create(RepositoryVO vo) throws ConstraintException, SystemException
63     {
64         Repository ent = new RepositoryImpl();
65         ent.setValueObject(vo);
66         ent = (Repository) createEntity(ent);
67         return ent.getValueObject();
68     }
69
70     /**
71      * This method removes a Repository from the system and also cleans out all depending repositoryLanguages.
72      */

73     
74     public void delete(RepositoryVO repositoryVO, String JavaDoc userName) throws ConstraintException, SystemException
75     {
76         Database db = CastorDatabaseService.getDatabase();
77         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
78
79         Repository repository = null;
80     
81         beginTransaction(db);
82
83         try
84         {
85             repository = getRepositoryWithId(repositoryVO.getRepositoryId(), db);
86             
87             RepositoryLanguageController.getController().deleteRepositoryLanguages(repository, db);
88             
89             ContentVO contentVO = ContentControllerProxy.getController().getRootContentVO(repositoryVO.getRepositoryId(), userName, false);
90             if(contentVO != null)
91                 ContentController.getContentController().delete(contentVO, db);
92             
93             SiteNodeVO siteNodeVO = SiteNodeController.getController().getRootSiteNodeVO(repositoryVO.getRepositoryId());
94             if(siteNodeVO != null)
95                 SiteNodeController.getController().delete(siteNodeVO, db);
96             
97             deleteEntity(RepositoryImpl.class, repositoryVO.getRepositoryId(), db);
98     
99             //If any of the validations or setMethods reported an error, we throw them up now before create.
100
ceb.throwIfNotEmpty();
101     
102             commitTransaction(db);
103         }
104         catch(ConstraintException ce)
105         {
106             logger.warn("An error occurred so we should not completes the transaction:" + ce, ce);
107             rollbackTransaction(db);
108             throw ce;
109         }
110         catch(Exception JavaDoc e)
111         {
112             logger.error("An error occurred so we should not completes the transaction:" + e, e);
113             rollbackTransaction(db);
114             throw new SystemException(e.getMessage());
115         }
116     }
117     
118     
119     public RepositoryVO update(RepositoryVO vo) throws ConstraintException, SystemException
120     {
121         return (RepositoryVO) updateEntity(RepositoryImpl.class, (BaseEntityVO) vo);
122     }
123     
124     public RepositoryVO update(RepositoryVO repositoryVO, String JavaDoc[] languageValues) throws ConstraintException, SystemException
125     {
126         Database db = CastorDatabaseService.getDatabase();
127         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
128
129         beginTransaction(db);
130
131         try
132         {
133             Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryVO.getRepositoryId(), db);
134             
135             RepositoryLanguageController.getController().deleteRepositoryLanguages(repository, db);
136
137             //add validation here if needed
138
List JavaDoc repositoryLanguageList = new ArrayList JavaDoc();
139             if(languageValues != null)
140             {
141                 for (int i=0; i < languageValues.length; i++)
142                 {
143                     Language language = LanguageController.getController().getLanguageWithId(new Integer JavaDoc(languageValues[i]), db);
144                     RepositoryLanguage repositoryLanguage = RepositoryLanguageController.getController().create(repositoryVO.getRepositoryId(), new Integer JavaDoc(languageValues[i]), new Integer JavaDoc(i), db);
145                     repositoryLanguageList.add(repositoryLanguage);
146                     language.getRepositoryLanguages().add(repositoryLanguage);
147                 }
148             }
149             
150             repository.setValueObject(repositoryVO);
151             repository.setRepositoryLanguages(repositoryLanguageList);
152             
153             repositoryVO = repository.getValueObject();
154             
155             //If any of the validations or setMethods reported an error, we throw them up now before create.
156
ceb.throwIfNotEmpty();
157             
158             commitTransaction(db);
159         }
160         catch(ConstraintException ce)
161         {
162             logger.warn("An error occurred so we should not completes the transaction:" + ce, ce);
163             rollbackTransaction(db);
164             throw ce;
165         }
166         catch(Exception JavaDoc e)
167         {
168             logger.error("An error occurred so we should not completes the transaction:" + e, e);
169             rollbackTransaction(db);
170             throw new SystemException(e.getMessage());
171         }
172
173         return repositoryVO;
174     }
175     
176     // Singe object
177
public Repository getRepositoryWithId(Integer JavaDoc id, Database db) throws SystemException, Bug
178     {
179         return (Repository) getObjectWithId(RepositoryImpl.class, id, db);
180     }
181
182     public RepositoryVO getRepositoryVOWithId(Integer JavaDoc repositoryId) throws ConstraintException, SystemException, Bug
183     {
184         return (RepositoryVO) getVOWithId(RepositoryImpl.class, repositoryId);
185     }
186     
187     
188     /**
189      * Returns the RepositoryVO with the given name.
190      *
191      * @param name
192      * @return
193      * @throws SystemException
194      * @throws Bug
195      */

196     
197     public RepositoryVO getRepositoryVOWithName(String JavaDoc name) throws SystemException, Bug
198     {
199         RepositoryVO repositoryVO = null;
200         
201         Database db = CastorDatabaseService.getDatabase();
202
203         try
204         {
205             beginTransaction(db);
206
207             Repository repository = getRepositoryWithName(name, db);
208             if(repository != null)
209                 repositoryVO = repository.getValueObject();
210             
211             commitTransaction(db);
212         }
213         catch (Exception JavaDoc e)
214         {
215             logger.info("An error occurred so we should not complete the transaction:" + e);
216             rollbackTransaction(db);
217             throw new SystemException(e.getMessage());
218         }
219         
220         return repositoryVO;
221     }
222     
223     /**
224      * Returns the Repository with the given name fetched within a given transaction.
225      *
226      * @param name
227      * @param db
228      * @return
229      * @throws SystemException
230      * @throws Bug
231      */

232
233     public Repository getRepositoryWithName(String JavaDoc name, Database db) throws SystemException, Bug
234     {
235         Repository repository = null;
236         
237         try
238         {
239             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl f WHERE f.name = $1");
240             oql.bind(name);
241             
242             QueryResults results = oql.execute();
243             this.logger.info("Fetching entity in read/write mode" + name);
244
245             if (results.hasMore())
246             {
247                 repository = (Repository)results.next();
248             }
249             
250             results.close();
251             oql.close();
252         }
253         catch(Exception JavaDoc e)
254         {
255             throw new SystemException("An error occurred when we tried to fetch a named repository. Reason:" + e.getMessage(), e);
256         }
257         
258         return repository;
259     }
260
261     /**
262      * This method can be used by actions and use-case-controllers that only need to have simple access to the
263      * functionality. They don't get the transaction-safety but probably just wants to show the info.
264      */

265     
266     public List JavaDoc getRepositoryVOList() throws ConstraintException, SystemException, Bug
267     {
268         String JavaDoc key = "repositoryVOList";
269         logger.info("key:" + key);
270         List JavaDoc cachedRepositoryVOList = (List JavaDoc)CacheController.getCachedObject("repositoryCache", key);
271         if(cachedRepositoryVOList != null)
272         {
273             logger.info("There was an cached authorization:" + cachedRepositoryVOList.size());
274             return cachedRepositoryVOList;
275         }
276                 
277         List JavaDoc repositoryVOList = getAllVOObjects(RepositoryImpl.class, "repositoryId");
278
279         CacheController.cacheObject("repositoryCache", key, repositoryVOList);
280             
281         return repositoryVOList;
282     }
283
284     
285     /**
286      * This method can be used by actions and use-case-controllers that only need to have simple access to the
287      * functionality. They don't get the transaction-safety but probably just wants to show the info.
288      */

289     
290     public List JavaDoc getAuthorizedRepositoryVOList(InfoGluePrincipal infoGluePrincipal, boolean isBindingDialog) throws ConstraintException, SystemException, Bug
291     {
292         List JavaDoc accessableRepositories = new ArrayList JavaDoc();
293         
294         List JavaDoc allRepositories = this.getRepositoryVOList();
295         Iterator JavaDoc i = allRepositories.iterator();
296         while(i.hasNext())
297         {
298             RepositoryVO repositoryVO = (RepositoryVO)i.next();
299             if(getIsAccessApproved(repositoryVO.getRepositoryId(), infoGluePrincipal, isBindingDialog))
300             {
301                 accessableRepositories.add(repositoryVO);
302             }
303         }
304         
305         return accessableRepositories;
306     }
307
308
309
310     
311     /**
312      * Return the first of all repositories.
313      */

314     
315     public RepositoryVO getFirstRepositoryVO() throws SystemException, Bug
316     {
317         Database db = CastorDatabaseService.getDatabase();
318         RepositoryVO repositoryVO = null;
319         
320         try
321         {
322             beginTransaction(db);
323         
324             OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl r ORDER BY r.repositoryId");
325             QueryResults results = oql.execute();
326             this.logger.info("Fetching entity in read/write mode");
327
328             if (results.hasMore())
329             {
330                 Repository repository = (Repository)results.next();
331                 repositoryVO = repository.getValueObject();
332             }
333             
334             results.close();
335             oql.close();
336
337             commitTransaction(db);
338         }
339         catch ( Exception JavaDoc e)
340         {
341             throw new SystemException("An error occurred when we tried to fetch a list of roles in the repository. Reason:" + e.getMessage(), e);
342         }
343         return repositoryVO;
344     }
345     
346
347
348     /**
349      * This method deletes the Repository sent in from the system.
350      */

351     public void delete(Integer JavaDoc repositoryId, Database db) throws SystemException, Bug
352     {
353         try
354         {
355             db.remove(getRepositoryWithId(repositoryId, db));
356         }
357         catch(Exception JavaDoc e)
358         {
359             throw new SystemException("An error occurred when we tried to delete Repository in the database. Reason: " + e.getMessage(), e);
360         }
361     }
362
363
364     /**
365      * This method returns true if the user should have access to the repository sent in.
366      */

367     
368     public boolean getIsAccessApproved(Integer JavaDoc repositoryId, InfoGluePrincipal infoGluePrincipal, boolean isBindingDialog) throws SystemException
369     {
370         logger.info("getIsAccessApproved for " + repositoryId + " AND " + infoGluePrincipal + " AND " + isBindingDialog);
371         boolean hasAccess = false;
372         
373         Database db = CastorDatabaseService.getDatabase();
374        
375         beginTransaction(db);
376
377         try
378         {
379             if(isBindingDialog)
380                 hasAccess = (AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "Repository.Read", repositoryId.toString()) || AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "Repository.ReadForBinding", repositoryId.toString()));
381             else
382                 hasAccess = AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "Repository.Read", repositoryId.toString());
383                 
384             commitTransaction(db);
385         }
386         catch(Exception JavaDoc e)
387         {
388             logger.error("An error occurred so we should not complete the transaction:" + e, e);
389             rollbackTransaction(db);
390             throw new SystemException(e.getMessage());
391         }
392         
393         return hasAccess;
394     }
395     
396     
397     /**
398      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
399      * is handling.
400      */

401
402     public BaseEntityVO getNewVO()
403     {
404         return new RepositoryVO();
405     }
406         
407 }
408  
409
Popular Tags