KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > categories > Category


1 package org.jahia.services.categories;
2
3 import org.jahia.content.*;
4 import org.jahia.exceptions.JahiaException;
5 import org.jahia.registries.ServicesRegistry;
6
7 import java.util.*;
8
9 /**
10  * <p>Title: Category object</p>
11  * <p>Description: This Category class is used to associate any objects within
12  * Jahia, be they Content Objects, Categories or others. It also provides
13  * static methods to manipulate Categories in the persistence back-end.
14  * This class is self-sufficient for most operations as it uses the category
15  * service back-end to perform most of it's work.</p>
16  * <p>Copyright: Copyright (c) 2002</p>
17  * <p>Company: Jahia Ltd</p>
18  *
19  * @author Serge Huber
20  * @version 1.0
21  */

22
23 public class Category extends JahiaObject implements PropertiesInterface {
24
25     private static org.apache.log4j.Logger logger =
26             org.apache.log4j.Logger.getLogger (Category.class);
27
28     private CategoryBean categoryBean = null;
29     private Properties properties = null;
30     private boolean propertiesLoaded = false;
31
32     protected Category (CategoryBean categoryBean) {
33         super (new CategoryKey (categoryBean.getId ()));
34         this.categoryBean = categoryBean;
35     }
36
37     /**
38      * New category constructor.
39      *
40      * @param key the unique key name for the new category
41      * @param parentCategory the parent category for this new category,
42      * it may be null to create an un-associated category
43      *
44      * @return the newly created category
45      *
46      * @throws JahiaException if there was an error while communicating with
47      * the database
48      */

49     static public Category createCategory (String JavaDoc key, Category parentCategory)
50             throws JahiaException {
51         CategoryBean categoryBean = new CategoryBean ();
52         categoryBean.setKey (key);
53         Category category = new Category (categoryBean);
54         ServicesRegistry.getInstance ().getCategoryService ().addCategory (
55                 category, parentCategory);
56         return category;
57     }
58
59     /**
60      * @return the root category object that corresponds to the start point
61      * of the category tree.
62      *
63      * @throws JahiaException thrown if there was a problem communicating with
64      * the database
65      */

66     static public Category getRootCategory ()
67             throws JahiaException {
68         return ServicesRegistry.getInstance ().getCategoryService ().
69                 getRootCategory ();
70     }
71
72     /**
73      * @param key the key for the category to retrieve
74      *
75      * @return the category corresponding to the key if it exists in the
76      * database
77      *
78      * @throws JahiaException thrown if there was a problem communicating with
79      * the database
80      */

81     static public Category getCategory (String JavaDoc key)
82             throws JahiaException {
83         return ServicesRegistry.getInstance ().getCategoryService ().getCategory (
84                 key);
85     }
86
87     /**
88      * @param categoryID the category ID for the category to retrieve
89      *
90      * @return the category corresponding to the key if it exists in the
91      * database
92      *
93      * @throws JahiaException thrown if there was a problem communicating with
94      * the database
95      */

96     static public Category getCategory (int categoryID)
97             throws JahiaException {
98         return ServicesRegistry.getInstance ().getCategoryService ().getCategory (categoryID);
99     }
100
101     /**
102      * Instance generator. Build an instance of the appropriate
103      * class corresponding to the ObjectKey passed described.
104      *
105      * @param objectKey an ObjectKey instance for the object we want to retrieve
106      * an instance of.
107      *
108      * @returns a Category class instance that corresponds to the given
109      * object key.
110      */

111     static public JahiaObject getChildInstance (ObjectKey objectKey) {
112         try {
113             return ServicesRegistry.getInstance ().getCategoryService ().
114                     getCategory (objectKey.getIdInType ());
115         } catch (JahiaException je) {
116             logger.error ("Error while trying to load category from object key " +
117                     objectKey.toString (), je);
118             return null;
119         }
120     }
121
122     /**
123      * Returns a set of categories with which this object is associated.
124      *
125      * @param objectKey the object key for the object for which to retrieve the
126      * categories
127      *
128      * @return a Set of Category objects.
129      *
130      * @throws JahiaException thrown if there was a problem communicating with
131      * the database.
132      */

133     static public Set getObjectCategories (ObjectKey objectKey)
134             throws JahiaException {
135         return ServicesRegistry.getInstance ().getCategoryService ().getObjectCategories (
136                 objectKey);
137     }
138
139     /**
140      * Finds categories by specifying property name and
141      * property value. These will be used to build LIKE SQL queries to find
142      * all the categories that contain strings that match the properties. It
143      * is allowed to use "%" characters here to perform partial String matching
144      * instead of full String matching.
145      *
146      * @param propName a property name that will be used to
147      * retrieve categories by their ID. Use "%" character for partial matching.
148      * @param propValue a partial property value that will be used to
149      * retrieve categories by their ID. Use "%" character for partial matching.
150      *
151      * @return an ArrayList containing Integer objects that are the category
152      * IDs that correspond to the matched properties.
153      *
154      * @throws JahiaException generated if there were problems executing the
155      * query or communicating with the database.
156      */

157     static public ArrayList findCategoriesByPropNameAndValue (String JavaDoc propName,
158                                                               String JavaDoc propValue) {
159         ArrayList foundCategories = new ArrayList ();
160         try {
161             ArrayList categoryIDs = CategoryPropDB.getInstance ().
162                     findCategoryIDsByPropNameAndValue (propName,
163                             propValue);
164             Iterator categoryIDIter = categoryIDs.iterator ();
165             while (categoryIDIter.hasNext ()) {
166                 Integer JavaDoc curCategoryID = (Integer JavaDoc) categoryIDIter.next ();
167                 Category curCategory = getCategory (curCategoryID.intValue ());
168                 foundCategories.add (curCategory);
169             }
170         } catch (JahiaException je) {
171             logger.error (
172                     "Error while trying to find categories by property name " + propName + "and property value " + propValue,
173                     je);
174         }
175         return foundCategories;
176     }
177
178     /**
179      * Returns the date of last modification of *any* category. This is useful
180      * notably to invalidate trees of categories if any category was modified.
181      * @return Date the date of the last modification on any category.
182      */

183     static public Date getLastModificationDate() {
184         return ServicesRegistry.getInstance().getCategoryService().getLastModificationDate();
185     }
186
187     public static Category getChildInstance (String JavaDoc IDInType) {
188         try {
189             return getCategory (Integer.parseInt (IDInType));
190         } catch (JahiaException je) {
191             logger.debug ("Error retrieving container instance for id : " + IDInType, je);
192         }
193         return null;
194     }
195
196     /**
197      * @return the unique key name identifying this category
198      */

199     public String JavaDoc getKey () {
200         if (this.categoryBean == null) {
201             return null;
202         }
203         return categoryBean.getKey ();
204     }
205
206     protected CategoryBean getCategoryBean () {
207         return categoryBean;
208     }
209
210     /**
211      * @return a CategoryKey that is an instance of an ObjectKey for this
212      * category and may be used for associating with this category
213      */

214     public ObjectKey getObjectKey () {
215         if (categoryBean == null) {
216             return null;
217         }
218         if (categoryBean.getId () == 0) {
219             return null;
220         }
221         return new CategoryKey (categoryBean.getId ());
222     }
223
224     /**
225      * Retrieves the list of child categories for this category
226      *
227      * @return the list contains Category objects that correspond to the child
228      * categories for this category.
229      *
230      * @throws JahiaException thrown if there was a problem communicating with
231      * the database
232      */

233     public ArrayList getChildCategories ()
234             throws JahiaException {
235         ArrayList childKeys = getChildObjectKeys ();
236         ArrayList childCategories = new ArrayList ();
237         Iterator childKeyIter = childKeys.iterator ();
238         while (childKeyIter.hasNext ()) {
239             ObjectKey curKey = (ObjectKey) childKeyIter.next ();
240             if (curKey instanceof CategoryKey) {
241                 Category curChildCategory = (Category) Category.getChildInstance (curKey);
242                 childCategories.add (curChildCategory);
243             }
244         }
245         return childCategories;
246     }
247
248     /**
249      * Retrieves the list of child object keys for this category. This may be
250      * a mixture of any type of ObjectKey sub-classes, such as CategoryKey or
251      * ContentObjectKey classes
252      *
253      * @return an ArrayList containing ObjectKey classes or sub-classes
254      *
255      * @throws JahiaException thrown if there was a problem communicating with
256      * the database
257      */

258     public ArrayList getChildObjectKeys ()
259             throws JahiaException {
260         return ServicesRegistry.getInstance ().getCategoryService ().
261                 getCategoryChildKeys (this);
262     }
263
264     /**
265      * Retrieves the list of child content objects for this category
266      *
267      * @return the list contains ContentObject objects that correspond to the
268      * child content objects associated with this category.
269      *
270      * @throws JahiaException thrown if there was a problem communicating with
271      * the database
272      */

273     public ArrayList getChildContentObjects ()
274             throws JahiaException {
275         ArrayList childKeys = getChildObjectKeys ();
276         ArrayList childContentObjects = new ArrayList ();
277         Iterator childKeyIter = childKeys.iterator ();
278         while (childKeyIter.hasNext ()) {
279             ObjectKey curKey = (ObjectKey) childKeyIter.next ();
280             if (curKey instanceof ContentObjectKey) {
281                 try {
282                     JahiaObject curChildCategory = JahiaObject.getInstance (
283                             curKey);
284                     childContentObjects.add (curChildCategory);
285                 } catch (ClassNotFoundException JavaDoc cnfe) {
286                     logger.error (
287                             "Error while loading content object for object key " +
288                             curKey, cnfe);
289                 }
290             }
291         }
292         return childContentObjects;
293     }
294
295     /**
296      * Returns the title in the specified language if it exists, or null if
297      * it doesn't exist
298      *
299      * @param locale the locale which specifies the language for which to
300      * retrieve the title.
301      *
302      * @return a String containing the category title in the specified language,
303      * or null if it doesn't exist.
304      */

305     public String JavaDoc getTitle (Locale locale) {
306         try {
307             return ServicesRegistry.getInstance ().getCategoryService ().
308                     getTitleForCategory (this, locale);
309         } catch (JahiaException je) {
310             logger.error (
311                     "Error while trying to retrieve title for category " + this.getObjectKey ()
312                     .toString (),
313                     je);
314             return null;
315         }
316     }
317
318     /**
319      * Sets a title for this category in a given locale
320      *
321      * @param locale the locale in which we want to set the title
322      * @param title the String containing the title for the specified locale.
323      */

324     public void setTitle (Locale locale, String JavaDoc title) {
325         try {
326             ServicesRegistry.getInstance ().getCategoryService ().setTitleForCategory (this,
327                     locale, title);
328         } catch (JahiaException je) {
329             logger.error (
330                     "Error while trying to set title " + title + " for category " + this.getObjectKey ()
331                     .toString (),
332                     je);
333         }
334     }
335
336     /**
337      * Deletes this category and all associated data (but does not recursively
338      * delete categories !), notably associations with child and parent objects,
339      * titles in all languages, and finally the category itself.
340      *
341      * @throws JahiaException thrown if there was an error while removing any
342      * of the associations, titles or the category itself from the database.
343      */

344     public void delete ()
345             throws JahiaException {
346         CategoryPropDB.getInstance ().removeProperties (categoryBean.getId ());
347         ServicesRegistry.getInstance ().getCategoryService ().removeCategory (this);
348     }
349
350     /**
351      * Associates a child object key with this category. This method does
352      * nothing if the child object key already exists
353      *
354      * @param childObjectKey the object key to add as a child of this category
355      *
356      * @throws JahiaException thrown in case there was a problem communicating
357      * with the database
358      */

359     public void addChildObjectKey (ObjectKey childObjectKey)
360             throws JahiaException {
361         ServicesRegistry.getInstance ().getCategoryService ().addObjectKeyToCategory (this,
362                 childObjectKey);
363     }
364
365     /**
366      * Removes a child object key from this category. This method does nothing
367      * if the object key was not a child of the category.
368      *
369      * @param childObjectKey the object key to remove from the child list of
370      * this category.
371      *
372      * @throws JahiaException thrown in case there was a problem communicating
373      * with the database
374      */

375     public void removeChildObjectKey (ObjectKey childObjectKey)
376             throws JahiaException {
377         ServicesRegistry.getInstance ().getCategoryService ().removeObjectKeyFromCategory (
378                 this, childObjectKey);
379     }
380
381     /**
382      * @return the properties for this category
383      */

384     public Properties getProperties () {
385         if (propertiesLoaded) {
386             return properties;
387         } else {
388             try {
389                 properties = CategoryPropDB.getInstance ().getProperties (categoryBean.getId ());
390             } catch (JahiaException je) {
391                 logger.error (
392                         "Error while loading category " + categoryBean.getId () + " properties",
393                         je);
394             }
395             propertiesLoaded = true;
396             return properties;
397         }
398     }
399
400     /**
401      * Sets a whole new set of properties for this category. Warning, all
402      * previously existing properties are erased by this method.
403      *
404      * @param newProperties the full set of properties to set for this
405      * category.
406      */

407     public void setProperties (Properties newProperties) {
408         properties = newProperties;
409         try {
410             CategoryPropDB.getInstance ().setProperties (categoryBean.getId (), properties);
411             ServicesRegistry.getInstance ().getCategoryService ().setLastModificationDate();
412             propertiesLoaded = true;
413         } catch (JahiaException je) {
414             logger.error (
415                     "Error while setting new properties for category " + categoryBean.getId (),
416                     je);
417         }
418     }
419
420     /**
421      * @param propertyName the name of the property for which to retrieve the
422      * value
423      *
424      * @return a value for the property if it exists in this category
425      */

426     public String JavaDoc getProperty (String JavaDoc propertyName) {
427         if (getProperties () != null) {
428             return getProperties ().getProperty (propertyName);
429         } else {
430             return null;
431         }
432     }
433
434     /**
435      * Sets a single property. This will override any previously existing
436      * value for this propertyName.
437      *
438      * @param propertyName the name of the property to set
439      * @param propertyValue the value to set for this property
440      */

441     public void setProperty (String JavaDoc propertyName, String JavaDoc propertyValue) {
442         if (getProperties () == null) {
443             properties = new Properties ();
444             propertiesLoaded = true;
445         }
446         try {
447             getProperties ().setProperty (propertyName, propertyValue);
448             CategoryPropDB.getInstance ().setProperty (categoryBean.getId (), propertyName, propertyValue);
449             ServicesRegistry.getInstance ().getCategoryService ().setLastModificationDate();
450         } catch (JahiaException e) {
451             logger.error (
452                     "Error while setting new property for category " + categoryBean.getId (),
453                     e);
454         }
455     }
456
457     /**
458      * Removes a property. This implementation will directly remove this
459      * property from the database backend.
460      *
461      * @param propertyName the name of the property to remove.
462      */

463     public void removeProperty (String JavaDoc propertyName) {
464         if (getProperties () != null) {
465             getProperties ().remove (propertyName);
466             try {
467                 CategoryPropDB.getInstance ().removeProperty (categoryBean.getId (), propertyName);
468                 ServicesRegistry.getInstance ().getCategoryService ().setLastModificationDate();
469             } catch (JahiaException e) {
470                 logger.error (
471                         "Error while removing property for category " + categoryBean.getId (),
472                         e);
473             }
474         }
475     }
476 }
477
Popular Tags