KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.services.categories;
2
3 import org.apache.log4j.Logger;
4 import org.jahia.content.CategoryKey;
5 import org.jahia.content.ObjectKey;
6 import org.jahia.content.ObjectLink;
7 import org.jahia.exceptions.JahiaException;
8 import org.jahia.exceptions.JahiaInitializationException;
9 import org.jahia.resourcebundle.DatabaseResourceBean;
10 import org.jahia.resourcebundle.DatabaseResourcesDB;
11 import org.jahia.settings.SettingsBean;
12
13 import java.util.*;
14 import org.jahia.services.cache.CacheFactory;
15 import org.jahia.services.cache.Cache;
16 import org.jahia.services.cache.CacheListener;
17
18 /**
19  * <p>Title: Category management service database implementation</p>
20  * <p>Description: Category management service database implementation,
21  * allows browsing of categories as well as navigation and manipulating
22  * categories and associated objects.
23  * This service should not be used directly but rather the Category class
24  * should be used to manipulate categories and associations.</p>
25  * <p>Copyright: Copyright (c) 2002</p>
26  * <p>Company: Jahia Ltd</p>
27  *
28  * @author Serge Huber
29  * @version 1.0
30  */

31
32 public class CategoryServiceImpl extends CategoryService implements CacheListener {
33
34     final private static Logger logger = Logger.getLogger (CategoryServiceImpl.class);
35
36     private static CategoryServiceImpl singletonInstance;
37
38     private CategoryDB catDB;
39
40     // we use a cache in a special way. Basically we use it to synchronize
41
// last modification dates on all the nodes of the cluster. For sync
42
// messages we use the flush() event.
43
// Cache that holds the last modification information on all categories
44
public static final String JavaDoc CATEGORY_LASTMODIF_STATUS_CACHE = "CategoryLastModifStatusCache";
45     private Cache lastModifCache = null;
46     private Date lastModifDate = null;
47
48     private static final String JavaDoc ROOT_CATEGORY_KEY = "root";
49     private static final String JavaDoc CATEGORY_LINKTYPE = "category";
50     private static final String JavaDoc CATEGORY_RESOURCEKEY_PREFIX = "org.jahia.category.title.";
51
52     private CategoryServiceImpl () {
53         catDB = CategoryDB.getInstance ();
54     }
55
56     /**
57      * Return the unique service instance. If the instance does not exist,
58      * a new instance is created.
59      *
60      * @return The unique service instance.
61      */

62     public synchronized static CategoryServiceImpl getInstance () {
63         if (singletonInstance == null) {
64             singletonInstance = new CategoryServiceImpl ();
65         }
66         return singletonInstance;
67     }
68
69     public void init (SettingsBean jSettings)
70             throws JahiaInitializationException {
71         try {
72             lastModifCache = CacheFactory.createCache(CATEGORY_LASTMODIF_STATUS_CACHE);
73             lastModifCache.registerListener(this);
74             if (getRootCategory () == null) {
75                 Category.createCategory (ROOT_CATEGORY_KEY, null);
76             }
77         } catch (JahiaException je) {
78             logger.error ("Error while checking existence of root category", je);
79             throw new JahiaInitializationException (
80                     "Error while checking existence of root category", je);
81         }
82     }
83
84     public Category getRootCategory ()
85             throws JahiaException {
86         CategoryBean rootCategoryBean = catDB.findCategoryByKey (ROOT_CATEGORY_KEY);
87         if (rootCategoryBean == null) {
88             return null;
89         }
90         return new Category (rootCategoryBean);
91     }
92
93     public Category getCategory (String JavaDoc key)
94             throws JahiaException {
95         CategoryBean categoryBean = catDB.findCategoryByKey (key);
96         if (categoryBean == null) {
97             return null;
98         }
99         return new Category (categoryBean);
100     }
101
102     public Category getCategory (int categoryID)
103             throws JahiaException {
104         CategoryBean categoryBean = catDB.getCategory (categoryID);
105         if (categoryBean == null) {
106             return null;
107         }
108         return new Category (categoryBean);
109     }
110
111     private ArrayList getCategoryChildLinks (Category parentCategory)
112             throws JahiaException {
113         ArrayList links = ObjectLink.findByTypeAndLeftObjectKey (CATEGORY_LINKTYPE,
114                 parentCategory.getObjectKey ());
115         return links;
116     }
117
118     private ArrayList getCategoryParentLinks (Category childCategory)
119             throws JahiaException {
120         ArrayList links = ObjectLink.findByTypeAndRightObjectKey (CATEGORY_LINKTYPE,
121                 childCategory.getObjectKey ());
122         return links;
123     }
124
125     public ArrayList getCategoryChildKeys (Category parentCategory)
126             throws JahiaException {
127         ArrayList links = getCategoryChildLinks (parentCategory);
128         ArrayList rightObjectKeys = new ArrayList ();
129         Iterator linkIter = links.iterator ();
130         while (linkIter.hasNext ()) {
131             ObjectLink curLink = (ObjectLink) linkIter.next ();
132             rightObjectKeys.add (curLink.getRightObjectKey ());
133         }
134         return rightObjectKeys;
135     }
136
137     public void addCategory (Category newCategory, Category parentCategory)
138             throws JahiaException {
139         Category existingCategory = getCategory (newCategory.getKey ());
140         if (existingCategory != null) {
141             throw new JahiaException ("Category " + newCategory.getKey () +
142                     " already exists",
143                     "Category " + newCategory.getKey () +
144                     " already exists",
145                     JahiaException.DATA_ERROR,
146                     JahiaException.ERROR_SEVERITY);
147         }
148         catDB.createCategory (newCategory.getCategoryBean ());
149         lastModifCache.flush();
150         if (parentCategory != null) {
151             addObjectKeyToCategory (parentCategory, newCategory.getObjectKey ());
152         }
153     }
154
155     public void removeCategory (Category category)
156             throws JahiaException {
157         // first we must remove all the child object associations with the category
158
ArrayList catChilds = getCategoryChildLinks (category);
159         Iterator catChildIter = catChilds.iterator ();
160         while (catChildIter.hasNext ()) {
161             ObjectLink curLink = (ObjectLink) catChildIter.next ();
162             curLink.remove ();
163         }
164         // first we must remove all the parent object associations with the category
165
ArrayList catParents = getCategoryParentLinks (category);
166         Iterator catParentIter = catParents.iterator ();
167         while (catParentIter.hasNext ()) {
168             ObjectLink curLink = (ObjectLink) catParentIter.next ();
169             curLink.remove ();
170         }
171         // next we must remove all the titles in all the languages
172
ArrayList allTitleResources = DatabaseResourcesDB.getInstance ().getAllDatabaseResource (
173                 category.getKey ());
174         Iterator titleResourceIter = allTitleResources.iterator ();
175         while (titleResourceIter.hasNext ()) {
176             DatabaseResourceBean curResource = (DatabaseResourceBean) titleResourceIter.next ();
177             DatabaseResourcesDB.getInstance ().updateDatabaseResource (curResource);
178         }
179         // now we can remove the category
180
catDB.removeCategory (category.getCategoryBean ());
181         lastModifCache.flush();
182     }
183
184     public void addObjectKeyToCategory (Category parentCategory, ObjectKey childKey)
185             throws JahiaException {
186         ArrayList existingChildKeys = getCategoryChildKeys (parentCategory);
187         if (existingChildKeys.contains (childKey)) {
188             return;
189         }
190
191         Date currentDate = new Date ();
192         ObjectLink.createLink (parentCategory.getObjectKey (),
193                                                     childKey,
194                                                     CATEGORY_LINKTYPE, 1, currentDate,
195                                                     "root:0", currentDate, "root:0",
196                                                     new HashMap (), new HashMap (),
197                                                     new HashMap ());
198         lastModifCache.flush();
199     }
200
201     public void removeObjectKeyFromCategory (Category parentCategory, ObjectKey childKey)
202             throws JahiaException {
203         ArrayList resultList = ObjectLink.findByTypeAndLeftAndRightObjectKeys (
204                 CATEGORY_LINKTYPE, parentCategory.getObjectKey (), childKey);
205         if (resultList.size () == 0) {
206             // not found, we do nothing...
207
return;
208         }
209
210         // we now remove all results, but we expect only one...
211
Iterator resultIter = resultList.iterator ();
212         while (resultIter.hasNext ()) {
213             ObjectLink curLink = (ObjectLink) resultIter.next ();
214             curLink.remove ();
215         }
216         lastModifCache.flush();
217     }
218
219     public Set getObjectCategories (ObjectKey objectKey)
220             throws JahiaException {
221         Set categorySet = new HashSet ();
222         ArrayList links = ObjectLink.findByTypeAndRightObjectKey (CATEGORY_LINKTYPE, objectKey);
223         Iterator linkIter = links.iterator ();
224         while (linkIter.hasNext ()) {
225             ObjectLink curLink = (ObjectLink) linkIter.next ();
226             ObjectKey leftKey = curLink.getLeftObjectKey ();
227             if (leftKey instanceof CategoryKey) {
228                 CategoryKey curCatKey = (CategoryKey) leftKey;
229                 Category curCategory = getCategory (curCatKey.getIdInType ());
230                 if (curCategory != null) {
231                     categorySet.add (curCategory);
232                 }
233             }
234         }
235         return categorySet;
236     }
237
238     public Map getTitlesForCategory(Category category) throws JahiaException {
239         Map result = new HashMap();
240         List resources = DatabaseResourcesDB.getInstance().getAllDatabaseResource(
241                 CATEGORY_RESOURCEKEY_PREFIX + category.getKey());
242         for (Iterator iterator = resources.iterator(); iterator.hasNext();) {
243             DatabaseResourceBean jahiaResource = (DatabaseResourceBean) iterator.next();
244             result.put(jahiaResource.getLanguageCode(), jahiaResource.getValue());
245         }
246         return result;
247     }
248     
249     public String JavaDoc getTitleForCategory (Category category, Locale locale)
250             throws JahiaException {
251         DatabaseResourceBean dbResource = DatabaseResourcesDB.getInstance ()
252                 .getDatabaseResource (CATEGORY_RESOURCEKEY_PREFIX + category.getKey (),
253                         locale.toString ());
254         if (dbResource != null) {
255             return dbResource.getValue ();
256         } else {
257             return null;
258         }
259     }
260
261     public void setTitleForCategory (Category category, Locale locale, String JavaDoc title)
262             throws JahiaException {
263         DatabaseResourceBean dbResource = DatabaseResourcesDB.getInstance ()
264                 .getDatabaseResource (CATEGORY_RESOURCEKEY_PREFIX + category.getKey (),
265                         locale.toString ());
266         if (dbResource != null) {
267             dbResource.setValue (title);
268             DatabaseResourcesDB.getInstance ().updateDatabaseResource (dbResource);
269         } else {
270             dbResource =
271                     new DatabaseResourceBean (CATEGORY_RESOURCEKEY_PREFIX + category.getKey (),
272                             title, locale.toString ());
273             DatabaseResourcesDB.getInstance ().createDatabaseResource (dbResource);
274         }
275         lastModifCache.flush();
276     }
277
278     public void removeTitleForCategory (Category category, Locale locale)
279             throws JahiaException {
280         DatabaseResourceBean dbResource = DatabaseResourcesDB.getInstance ()
281                 .getDatabaseResource (CATEGORY_RESOURCEKEY_PREFIX + category.getKey (),
282                         locale.toString ());
283         if (dbResource != null) {
284             DatabaseResourcesDB.getInstance ().removeDatabaseResource (dbResource);
285         }
286         lastModifCache.flush();
287     }
288
289     public Date getLastModificationDate() {
290         return lastModifDate;
291     }
292
293     public void setLastModificationDate() {
294         lastModifCache.flush();
295     }
296
297     /**
298      * This method is called each time the cache flushes its items.
299      *
300      * @param cacheName the name of the cache which flushed its items.
301      */

302     public void onCacheFlush (String JavaDoc cacheName) {
303         if (CATEGORY_LASTMODIF_STATUS_CACHE.equals(cacheName)) {
304             lastModifDate = new Date();
305         }
306     }
307
308     public void onCachePut(String JavaDoc cacheName, Object JavaDoc entryKey) {
309         // do nothing;
310
}
311
312
313 }
314
Popular Tags