KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > beans > CategoryBean


1 package org.jahia.data.beans;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import org.jahia.content.comparators.ContainerComparator;
8 import org.jahia.content.comparators.ContainerListComparator;
9 import org.jahia.content.comparators.ObjectTypeDispatcherComparator;
10 import org.jahia.content.comparators.PageByTitleComparator;
11 import org.jahia.exceptions.JahiaException;
12 import org.jahia.params.ParamBean;
13 import org.jahia.services.categories.Category;
14
15 import java.util.Properties JavaDoc;
16 import org.jahia.services.fields.ContentField;
17 import org.jahia.services.containers.ContentContainer;
18 import org.jahia.services.containers.ContentContainerList;
19 import org.jahia.content.*;
20 import org.jahia.registries.ServicesRegistry;
21 import org.jahia.data.fields.JahiaField;
22 import org.jahia.data.fields.LoadFlags;
23 import org.jahia.data.containers.JahiaContainer;
24 import org.jahia.data.containers.JahiaContainerList;
25 import org.jahia.services.pages.ContentPage;
26 import org.jahia.services.pages.JahiaPage;
27
28 /**
29  * <p>Title: A wrapper JavaBean compliant class that uses the current
30  * request context to display values back to the output. </p>
31  * <p>Description: This class encapsulates a ParamBean and a Category class
32  * to provide helper methods for template developers to access using Struts
33  * or JSTL accessors to beans.</p>
34  * <p>Copyright: Copyright (c) 2002</p>
35  * <p>Company: Jahia Ltd</p>
36  * @author Serge Huber
37  * @version 1.0
38  */

39
40 public class CategoryBean extends AbstractJahiaObjectBean {
41
42     private static org.apache.log4j.Logger logger =
43         org.apache.log4j.Logger.getLogger(CategoryBean.class);
44
45     private ParamBean paramBean;
46     private Category category;
47
48     static {
49         registerType(Category.class.getName(), CategoryBean.class.getName());
50     }
51
52     /**
53      * Empty constructor to follow JavaBean compliance rules
54      */

55     public CategoryBean () {
56     }
57
58     /**
59      * Constructor for wrapper
60      * @param category the category to wrap
61      * @param paramBean the paramBean containing the request context to use
62      * for this wrapper
63      */

64     public CategoryBean (Category category, ParamBean paramBean) {
65         this.category = category;
66         this.paramBean = paramBean;
67     }
68
69     /**
70      * Static instantiator called from the getInstance() method of the
71      * AbstractJahiaObjectBean class.
72      * @param jahiaObject the JahiaObject instance to build this wrapper for
73      * @param paramBean the ParamBean instance representing the request
74      * context to build this wrapper for.
75          * @return an instance of an AbstractJahiaObjectBean descendant corresponding
76      * to the JahiaObject type and request context
77      */

78     public static AbstractJahiaObjectBean getChildInstance (JahiaObject
79         jahiaObject,
80         ParamBean paramBean) {
81         return new CategoryBean( (Category) jahiaObject, paramBean);
82     }
83
84     /**
85      * @return the enclosed ParamBean instance
86      */

87     public ParamBean getParamBean () {
88         return paramBean;
89     }
90
91     /**
92      * @return the enclosed Category instance
93      */

94     public Category getCategory () {
95         return category;
96     }
97
98     /**
99      * Retrieves a sorted list of children, filtered by the type that was
100      * specified. The type corresponds to the recognized JahiaObject types
101      * that are used in descendant classes of ObjectKeys. Examples of types
102      * are : ContentPage, Category, ContentContainer, ContentContainerList,
103      * ContentField, etc...
104      * @param type a String specifying the type by which to filter the
105      * JahiaObject
106      * @return an ArrayList containing JahiaObjects of the specified types,
107      * and sorted according to the type specific comparators.
108      */

109     public ArrayList JavaDoc getChildsOfType (String JavaDoc type) {
110         ArrayList JavaDoc filteredChildsOfType = new ArrayList JavaDoc();
111         ArrayList JavaDoc fullChildsOfType = null;
112         try {
113             fullChildsOfType= getSortedChildJahiaObjects(type);
114         } catch (Throwable JavaDoc t) {
115             logger.error("Error while retrieving child objects", t);
116             return filteredChildsOfType;
117         }
118         Iterator JavaDoc childsIter = fullChildsOfType.iterator();
119         /**
120          * @todo this code is ugly, can we do this a cleaner way ? We would
121          * probably need some kind of common interface for checks on content
122          * objects to clean it up.
123          */

124         while (childsIter.hasNext()) {
125             JahiaObject curJahiaObject = (JahiaObject) childsIter.next();
126             if (curJahiaObject instanceof ContentObject) {
127                 ContentObject curContentObject = (ContentObject) curJahiaObject;
128
129                 /**
130                  * @todo this code is ugly, because it mixes the new and the
131                  * old backend. This is a problem because it means the old
132                  * backend offers functions that the new one doesn't yet !
133                  * Plus the other problem is that we hard code the different
134                  * content objects here, making it very difficult to add new
135                  * ones or remove them. We should probably have on ContentObjects
136                  * something similar to the ContentPage.getPage() method which
137                  * we use below.
138                  */

139                 boolean acceptObject = false;
140                 try {
141                     if (curContentObject instanceof ContentField) {
142                         int fieldID = ( (ContentField) curContentObject).getID();
143                         JahiaField jahiaField = ServicesRegistry.getInstance().
144                                                 getJahiaFieldService().
145                                                 loadField(fieldID,
146                             LoadFlags.ALL, paramBean,
147                                                 paramBean.getEntryLoadRequest());
148
149                         if (jahiaField != null) {
150                             acceptObject = true;
151                         }
152                     } else if (curContentObject instanceof ContentContainer) {
153                         ContentContainer contentContainer = (ContentContainer) curContentObject;
154                         JahiaContainer jahiaContainer = contentContainer.getJahiaContainer(paramBean, paramBean.getEntryLoadRequest());
155                         if (jahiaContainer != null) {
156                             acceptObject = true;
157                         }
158                     } else if (curContentObject instanceof ContentContainerList) {
159                         ContentContainerList contentContainerList = (ContentContainerList) curContentObject;
160                         JahiaContainerList jahiaContainerList = contentContainerList.getJahiaContainerList(paramBean, paramBean.getEntryLoadRequest());
161                         if (jahiaContainerList != null) {
162                             acceptObject = true;
163                         }
164                     } else if (curContentObject instanceof ContentPage) {
165                         ContentPage contentPage = (ContentPage) curContentObject;
166                         JahiaPage jahiaPage = contentPage.getPage(paramBean.getEntryLoadRequest(), paramBean.getOperationMode(), paramBean.getUser());
167                         if (jahiaPage != null) {
168                             acceptObject = true;
169                         }
170                     }
171                 } catch (JahiaException je) {
172                     // this is not necessarily an error. We might have an
173
// exception telling us we don't have access to the
174
// object because of the mode or something.
175
/*
176                     logger.debug(
177                         "Error while checking availability of content object " +
178                         curContentObject.getObjectKey() +
179                         ", not adding to children of category " +
180                         getCategory().getKey(), je);
181                     */

182                     acceptObject = false;
183                 }
184
185                 if (acceptObject) {
186                     try {
187                         AbstractJahiaObjectBean curObjectBean =
188                             AbstractJahiaObjectBean.
189                             getInstance(curContentObject, paramBean);
190                         filteredChildsOfType.add(curObjectBean);
191                     } catch (ClassNotFoundException JavaDoc cnfe) {
192                         logger.error("Class not found while converting category child object to beans", cnfe);
193                     }
194                 }
195             }
196         }
197
198         return filteredChildsOfType;
199     }
200
201     /**
202      * Retrieves a sorted list of children, filtered by the type that was
203      * specified. The type corresponds to the recognized JahiaObject types
204      * that are used in descendant classes of ObjectKeys. Examples of types
205      * are : ContentPage, Category, ContentContainer, ContentContainerList,
206      * ContentField, etc...
207      * Note : this method does not do any checks against ACLs or operation
208      * modes. Use the getChildsOfType() method if you need these checks (if
209      * you're not sure you should probably be suing getChildsOfType).
210      * @param type a String specifying the type by which to filter the
211      * JahiaObject
212      * @return an ArrayList containing JahiaObjects of the specified types,
213      * and sorted according to the type specific comparators.
214      */

215     public ArrayList JavaDoc getChildsOfTypeNoChecks (String JavaDoc type) {
216         ArrayList JavaDoc childsOfType = new ArrayList JavaDoc();
217         try {
218             ArrayList JavaDoc childJahiaObjects = getSortedChildJahiaObjects(type);
219
220             childsOfType = jahiaObjectToBeans(childJahiaObjects);
221         } catch (ClassNotFoundException JavaDoc cnfe) {
222             logger.error("Error while retrieving category children of type [" +
223                          type + "]", cnfe);
224         } catch (JahiaException je) {
225             logger.error("Error while retrieving category children of type [" +
226                          type + "]", je);
227         }
228         return childsOfType;
229     }
230
231     private ArrayList JavaDoc jahiaObjectToBeans (ArrayList JavaDoc childJahiaObjects)
232         throws ClassNotFoundException JavaDoc {
233         ArrayList JavaDoc beanList = new ArrayList JavaDoc();
234         Iterator JavaDoc sortedJahiaObjectIter = childJahiaObjects.iterator();
235         while (sortedJahiaObjectIter.hasNext()) {
236             JahiaObject curObject = (JahiaObject) sortedJahiaObjectIter.
237                                     next();
238             AbstractJahiaObjectBean curObjectBean = AbstractJahiaObjectBean.
239                 getInstance(curObject, paramBean);
240             beanList.add(curObjectBean);
241         }
242         return beanList;
243     }
244
245     private ArrayList JavaDoc getSortedChildJahiaObjects (String JavaDoc type)
246         throws ClassNotFoundException JavaDoc, JahiaException {
247         ArrayList JavaDoc allChildrenObjectKeys = category.getChildObjectKeys();
248         Iterator JavaDoc allChildObjectKeysIter = allChildrenObjectKeys.iterator();
249         ArrayList JavaDoc childJahiaObjects = new ArrayList JavaDoc();
250         while (allChildObjectKeysIter.hasNext()) {
251             ObjectKey curObjectKey = (ObjectKey) allChildObjectKeysIter.
252                                      next();
253             if (curObjectKey.getType().equals(type)) {
254                 // we have found a matching type object.
255
JahiaObject curObject = JahiaObject.getInstance(
256                     curObjectKey);
257                 // Workaround for deleted container not removed from category but removed from container
258
if(curObject!=null)
259                 childJahiaObjects.add(curObject);
260             }
261         }
262         /**
263          * @todo for the moment this is hardcoded but we should introduce
264          * a builder that uses configuration files to build standard
265          * comparators
266          */

267         ObjectTypeDispatcherComparator comparator = new
268             ObjectTypeDispatcherComparator();
269         comparator.addTypeComparator(ContentContainerKey.CONTAINER_TYPE,
270                                      new ContainerComparator(paramBean));
271         comparator.addTypeComparator(ContentContainerListKey.
272                                      CONTAINERLIST_TYPE,
273                                      new ContainerListComparator());
274         comparator.addTypeComparator(ContentPageKey.PAGE_TYPE,
275                                      new PageByTitleComparator(paramBean));
276         Collections.sort(childJahiaObjects, comparator);
277         return childJahiaObjects;
278     }
279
280     /**
281      * @return an ArrayList of CategoryBean objects that are the children of
282      * the current category. May return an empty array if there are no
283      * children but never returns null.
284      */

285     public ArrayList JavaDoc getChildCategoryBeans () {
286         ArrayList JavaDoc childCategoryBeans = new ArrayList JavaDoc();
287         try {
288             ArrayList JavaDoc childCategories = category.getChildCategories();
289             Iterator JavaDoc childCategoriesIter = childCategories.iterator();
290             while (childCategoriesIter.hasNext()) {
291                 Category curCategory = (Category) childCategoriesIter.next();
292                 CategoryBean curCategoryBean = new CategoryBean(curCategory,
293                     paramBean);
294                 childCategoryBeans.add(curCategoryBean);
295             }
296         } catch (JahiaException je) {
297             logger.error("Error while retrieving child categories of category " +
298                          category, je);
299         }
300         return childCategoryBeans;
301     }
302
303     /**
304      * @return the title of the category for the current locale accessed through
305      * the ParamBean.getLocale() method.
306      */

307     public String JavaDoc getTitle () {
308         return category.getTitle(paramBean.getLocale());
309     }
310
311     /**
312      * @return the full set of properties
313      */

314     public Properties JavaDoc getProperties() {
315         return category.getProperties();
316     }
317
318 }
Popular Tags