KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Id: CategoryController.java,v 1.15 2006/10/09 21:41:09 mattias Exp $
24  */

25 package org.infoglue.cms.controllers.kernel.impl.simple;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.exolab.castor.jdo.Database;
33 import org.infoglue.cms.entities.kernel.BaseEntityVO;
34 import org.infoglue.cms.entities.management.Category;
35 import org.infoglue.cms.entities.management.CategoryVO;
36 import org.infoglue.cms.entities.management.impl.simple.CategoryImpl;
37 import org.infoglue.cms.exception.SystemException;
38 import org.infoglue.cms.security.InfoGluePrincipal;
39
40
41 /**
42  * The CategoryController manages all actions related to persistence
43  * and querying for Categories.
44  *
45  * TODO: When we convert have Hibernate manage all of these relationships, it will pull it
46  * TODO: all back with one query and be a helluva lot faster than this pasic implementation
47  *
48  * @author Frank Febbraro (frank@phase2technology.com)
49  */

50 public class CategoryController extends BaseController
51 {
52     private final static Logger logger = Logger.getLogger(CategoryController.class.getName());
53
54     private static final CategoryController instance = new CategoryController();
55     private static final ContentCategoryController contentCategoryStore = ContentCategoryController.getController();
56
57     private static final String JavaDoc findByParent = new StringBuffer JavaDoc("SELECT c ")
58             .append("FROM org.infoglue.cms.entities.management.impl.simple.CategoryImpl c ")
59             .append("WHERE c.parentId = $1 ")
60             .append("ORDER BY c.name ASC").toString();
61
62     private static final String JavaDoc findActiveByParent = new StringBuffer JavaDoc("SELECT c ")
63             .append("FROM org.infoglue.cms.entities.management.impl.simple.CategoryImpl c ")
64             .append("WHERE c.parentId = $1 ")
65             .append("AND c.active = $2 ")
66             .append("ORDER BY c.name ASC").toString();
67
68     private static final String JavaDoc findRootCategories = new StringBuffer JavaDoc("SELECT c ")
69             .append("FROM org.infoglue.cms.entities.management.impl.simple.CategoryImpl c ")
70             .append("WHERE is_undefined(c.parentId) ")
71             .append("ORDER BY c.name ASC").toString();
72
73     private static final String JavaDoc findActiveRootCategories = new StringBuffer JavaDoc("SELECT c ")
74             .append("FROM org.infoglue.cms.entities.management.impl.simple.CategoryImpl c ")
75             .append("WHERE is_undefined(c.parentId) ")
76             .append("AND c.active = $1 ")
77             .append("ORDER BY c.name ASC").toString();
78
79     public static CategoryController getController()
80     { return instance; }
81
82     private CategoryController()
83     {}
84
85     /**
86      * Find a Category by it's identifier.
87      *
88      * @param id The id of the Category to find
89      * @return The CategoryVO identified by the provided id
90      * @throws SystemException If an error happens
91      */

92     public CategoryVO findById(Integer JavaDoc id) throws SystemException
93     {
94         return (CategoryVO)getVOWithId(CategoryImpl.class, id);
95     }
96
97     /**
98      * Find a Category by it's identifier.
99      *
100      * @param id The id of the Category to find
101      * @return The Category identified by the provided id
102      * @throws SystemException If an error happens
103      */

104     public Category findById(Integer JavaDoc id, Database db) throws SystemException
105     {
106         return (Category)getObjectWithId(CategoryImpl.class, id, db);
107     }
108
109     /**
110      * Find a Category by it's name path.
111      *
112      * @param path The path of the Category to find in the form /categoryName/categoryName/categoryName
113      * @return The CategoryVO identified by the provided path
114      * @throws SystemException If an error happens
115      */

116     public CategoryVO findByPath(String JavaDoc path) throws SystemException
117     {
118         CategoryVO categoryVO = null;
119         
120         String JavaDoc[] nodes = path.substring(1).split("/");
121         
122         if(nodes.length > 0)
123         {
124             List JavaDoc rootCategories = findRootCategories();
125             String JavaDoc name = nodes[0];
126             categoryVO = getCategoryVOWithNameInList(rootCategories, name);
127             
128             for(int i = 1; i < nodes.length; i++)
129             {
130                 categoryVO = getCategoryVOWithNameInList(findByParent(categoryVO.getId()), nodes[i]);
131             }
132         }
133         
134         return categoryVO;
135     }
136
137     /**
138      * Find a Category by it's name path.
139      *
140      * @param path The path of the Category to find in the form /categoryName/categoryName/categoryName
141      * @return The CategoryVO identified by the provided path
142      * @throws SystemException If an error happens
143      */

144     public CategoryVO findByPath(String JavaDoc path, Database db) throws SystemException
145     {
146         CategoryVO categoryVO = null;
147         
148         String JavaDoc[] nodes = path.substring(1).split("/");
149         
150         if(nodes.length > 0)
151         {
152             List JavaDoc rootCategories = findRootCategoryVOList(db);
153             String JavaDoc name = nodes[0];
154             categoryVO = getCategoryVOWithNameInList(rootCategories, name);
155             
156             for(int i = 1; i < nodes.length; i++)
157             {
158                 categoryVO = getCategoryVOWithNameInList(findByParent(categoryVO.getId(), db), nodes[i]);
159             }
160         }
161         
162         return categoryVO;
163     }
164
165     /**
166      * Iterates the list of categories and returns the first one that matches your name.
167      * @param categoryVOList
168      * @param name
169      * @return
170      */

171
172     private CategoryVO getCategoryVOWithNameInList(List JavaDoc categoryVOList, String JavaDoc name)
173     {
174         CategoryVO categoryVO = null;
175         
176         Iterator JavaDoc categoryVOListIterator = categoryVOList.iterator();
177         while(categoryVOListIterator.hasNext())
178         {
179             CategoryVO currentCategoryVO = (CategoryVO)categoryVOListIterator.next();
180             logger.info("currentCategoryVO:" + currentCategoryVO.getName() + "=" + name);
181             if(currentCategoryVO.getName().equalsIgnoreCase(name))
182             {
183                 categoryVO = currentCategoryVO;
184                 break;
185             }
186         }
187         
188         return categoryVO;
189     }
190
191
192     /**
193      * Find a List of Categories by parent.
194      *
195      * @param parentId The parent id of the Category to find
196      * @return A list of CategoryVOs that have the provided parentId
197      * @throws SystemException If an error happens
198      */

199     public List JavaDoc findByParent(Integer JavaDoc parentId) throws SystemException
200     {
201         List JavaDoc params = new ArrayList JavaDoc();
202         params.add(parentId);
203         return executeQuery(findByParent, params);
204     }
205
206     /**
207      * Find a List of Categories by parent.
208      *
209      * @param parentId The parent id of the Category to find
210      * @return A list of CategoryVOs that have the provided parentId
211      * @throws SystemException If an error happens
212      */

213     public List JavaDoc findByParent(Integer JavaDoc parentId, Database db) throws SystemException
214     {
215         List JavaDoc params = new ArrayList JavaDoc();
216         params.add(parentId);
217         return executeQuery(findByParent, params, db);
218     }
219
220     /**
221      * Find a List of active Categories by parent.
222      *
223      * @param parentId The parent id of the Category to find
224      * @return A list of CategoryVOs that have the provided parentId
225      * @throws SystemException If an error happens
226      */

227     public List JavaDoc findActiveByParent(Integer JavaDoc parentId) throws SystemException
228     {
229         List JavaDoc params = new ArrayList JavaDoc();
230         params.add(parentId);
231         params.add(Boolean.TRUE);
232         return executeQuery(findActiveByParent, params);
233     }
234
235     /**
236      * Find a Category with it's children populated.
237      *
238      * @param id The id of the Category to find
239      * @return A list of CategoryVOs that are at the root of the category tree
240      * @throws SystemException If an error happens
241      */

242     public CategoryVO findWithChildren(Integer JavaDoc id) throws SystemException
243     {
244         CategoryVO c = findById(id);
245         c.setChildren(findByParent(c.getId()));
246         return c;
247     }
248
249     /**
250      * Find a Category with it's children populated.
251      *
252      * @param id The id of the Category to find
253      * @return A list of CategoryVOs that are at the root of the category tree
254      * @throws SystemException If an error happens
255      */

256     public CategoryVO findWithChildren(Integer JavaDoc id, Database db) throws SystemException
257     {
258         Category c = findById(id, db);
259         c.getValueObject().setChildren(toVOList(findByParent(c.getId(), db)));
260         return c.getValueObject();
261     }
262
263     /**
264      * Find a List of Categories that have no parent.
265      *
266      * @return A list of CategoryVOs that are at the root of the category tree
267      * @throws SystemException If an error happens
268      */

269     public List JavaDoc findRootCategories() throws SystemException
270     {
271         return executeQuery(findRootCategories);
272     }
273
274     /**
275      * Find a List of Categories that have no parent.
276      *
277      * @return A list of CategoryVOs that are at the root of the category tree
278      * @throws SystemException If an error happens
279      */

280     public List JavaDoc findRootCategories(Database db) throws SystemException
281     {
282         return executeQuery(findRootCategories, db);
283     }
284
285     /**
286      * Find a List of Categories that have no parent.
287      *
288      * @return A list of CategoryVOs that are at the root of the category tree
289      * @throws SystemException If an error happens
290      */

291     public List JavaDoc findRootCategoryVOList(Database db) throws SystemException
292     {
293         List JavaDoc categories = executeQuery(findRootCategories, db);
294         return (categories != null) ? toVOList(categories) : null;
295     }
296
297     /**
298      * Find a list of all Categories in the system.
299      *
300      * @return A list of CategoryVOs starting at the root of the category tree
301      * @throws SystemException If an error happens
302      */

303     public List JavaDoc findAllActiveCategories() throws SystemException
304     {
305         List JavaDoc params = new ArrayList JavaDoc();
306         params.add(Boolean.TRUE);
307         List JavaDoc roots = executeQuery(findActiveRootCategories, params);
308         for (Iterator JavaDoc iter = roots.iterator(); iter.hasNext();)
309         {
310             CategoryVO root = (CategoryVO) iter.next();
311             root.setChildren(findAllActiveChildren(root.getId()));
312         }
313         return roots;
314     }
315
316     
317     /**
318      * Finds all authorized categories parent id, recursively until no children are found.
319      *
320      * @return A list of children nodes, with thier children populated
321      */

322     public List JavaDoc getAuthorizedActiveChildren(Integer JavaDoc parentId, InfoGluePrincipal infogluePrincipal) throws SystemException
323     {
324         List JavaDoc children = findActiveByParent(parentId);
325         for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();)
326         {
327             CategoryVO child = (CategoryVO) iter.next();
328             if(!getIsAccessApproved(child.getCategoryId(), infogluePrincipal))
329             {
330                 iter.remove();
331             }
332             
333             List JavaDoc subChildren = findAllActiveChildren(child.getId());
334             Iterator JavaDoc subChildrenIterator = subChildren.iterator();
335             while(subChildrenIterator.hasNext())
336             {
337                 CategoryVO subChild = (CategoryVO) subChildrenIterator.next();
338                 if(getIsAccessApproved(subChild.getCategoryId(), infogluePrincipal))
339                 {
340                     child.getChildren().add(subChild);
341                 }
342             }
343         }
344         return children;
345     }
346     
347     /**
348      * Finds all children for a given parent id, recursively until no children are found.
349      *
350      * @return A list of children nodes, with thier children populated
351      */

352     public List JavaDoc findAllActiveChildren(Integer JavaDoc parentId) throws SystemException
353     {
354         List JavaDoc children = findActiveByParent(parentId);
355         for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();)
356         {
357             CategoryVO child = (CategoryVO) iter.next();
358             child.setChildren(findAllActiveChildren(child.getId()));
359         }
360         return children;
361     }
362
363     /**
364      * Saves a CategoryVO whether it is new or not.
365      *
366      * @param c The CategoryVO to save
367      * @return The saved CategoryVO
368      * @throws SystemException If an error happens
369      */

370     public CategoryVO save(CategoryVO c) throws SystemException
371     {
372         return (c.isUnsaved())
373                     ? create(c)
374                     : (CategoryVO)updateEntity(CategoryImpl.class, c);
375     }
376
377     /**
378      * Creates a Category from a CategoryVO
379      */

380     private CategoryVO create(CategoryVO c) throws SystemException
381     {
382         CategoryImpl impl = new CategoryImpl(c);
383         return ((CategoryImpl)createEntity(impl)).getValueObject();
384     }
385
386     /**
387      * Moves a CategoryVO to a different parent category
388      *
389      * @param categoryId The id of the CategoryVO to move
390      * @param newParentId The id of the parent to move the CategoryVO
391      * @return The saved CategoryVO
392      * @throws SystemException If an error happens
393      */

394     public CategoryVO moveCategory(Integer JavaDoc categoryId, Integer JavaDoc newParentId) throws SystemException
395     {
396         CategoryVO category = findById(categoryId);
397         category.setParentId(newParentId);
398         return save(category);
399     }
400
401     /**
402      * Deletes a CategoryVO, and all children.
403      *
404      * TODO: The reason we delete the ContentCategory first is that once the Category
405      * TODO: is gone, Castor will never find them again. When we move to Hibernate we
406      * TODO: can probalby put this afterwards, in it's more logical place.
407      *
408      * @param id The id of the Category to delete
409      * @throws SystemException If an error happens
410      */

411     public void delete(Integer JavaDoc id) throws SystemException
412     {
413         contentCategoryStore.deleteByCategory(id);
414         deleteEntity(CategoryImpl.class, id);
415         deleteChildren(id);
416     }
417
418     /**
419      * Deletes the children of the supplied category
420      */

421     private void deleteChildren(Integer JavaDoc id) throws SystemException
422     {
423         List JavaDoc children = findByParent(id);
424         for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();)
425             delete(((CategoryVO) iter.next()).getId());
426     }
427
428     /**
429      * Implemented for BaseController
430      */

431     public BaseEntityVO getNewVO()
432     {
433         return new CategoryVO();
434     }
435     
436     
437     /**
438      * This method returns true if the user should have access to the contentTypeDefinition sent in.
439      */

440     
441     public boolean getIsAccessApproved(Integer JavaDoc categoryId, InfoGluePrincipal infoGluePrincipal) throws SystemException
442     {
443         logger.info("getIsAccessApproved for " + categoryId + " AND " + infoGluePrincipal);
444         boolean hasAccess = false;
445         
446         Database db = CastorDatabaseService.getDatabase();
447        
448         beginTransaction(db);
449
450         try
451         {
452             hasAccess = AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "Category.Read", categoryId.toString());
453         
454             commitTransaction(db);
455         }
456         catch(Exception JavaDoc e)
457         {
458             logger.error("An error occurred so we should not complete the transaction:" + e, e);
459             rollbackTransaction(db);
460             throw new SystemException(e.getMessage());
461         }
462     
463         return hasAccess;
464     }
465 }
466
Popular Tags