KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > categories > ManageCategories


1 package org.jahia.engines.categories;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Set JavaDoc;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpSession JavaDoc;
10
11 import javax.swing.JTree JavaDoc;
12 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
13 import javax.swing.tree.DefaultTreeModel JavaDoc;
14 import javax.swing.tree.MutableTreeNode JavaDoc;
15 import javax.swing.tree.TreePath JavaDoc;
16
17 import org.jahia.content.ObjectKey;
18 import org.jahia.engines.JahiaEngine;
19 import org.jahia.exceptions.JahiaException;
20 import org.jahia.exceptions.JahiaSessionExpirationException;
21 import org.jahia.params.ParamBean;
22 import org.jahia.registries.ServicesRegistry;
23 import org.jahia.services.categories.Category;
24 import org.jahia.utils.GUITreeTools;
25 import org.jahia.content.ContentDefinition;
26
27 /**
28  * <p>Title: Category management sub engine</p> <p>Description: </p> <p>Copyright: Copyright (c)
29  * 2002</p> <p>Company: Jahia Ltd</p>
30  *
31  * @author Serge Huber
32  * @version 1.0
33  */

34
35 public class ManageCategories {
36
37     private static final org.apache.log4j.Logger logger =
38             org.apache.log4j.Logger.getLogger (ManageCategories.class);
39
40     private static ManageCategories singletonInstance = null;
41     private static final String JavaDoc JSP_FILE =
42             "/jsp/jahia/engines/categories/manage_categories.jsp";
43
44     private static final String JavaDoc SELECTEDCATEGORIES_ENGINEMAPKEY = "selectedCategories";
45     private static final String JavaDoc DEFAULTCATEGORIES_SESSIONKEYPREFIX = "defaultCategories_";
46     private static final String JavaDoc CATEGORYTREE_ENGINEMAPKEY = "categoryTree";
47     private static final String JavaDoc FLATCATEGORYLIST_ENGINEMAPKEY = "flatCategoryList";
48
49     private static final String JavaDoc CATEGORYPREFIX_HTMLPARAMETER = "category_";
50
51     private ManageCategories () {
52         logger.debug (
53                 "***** Starting " + ManageCategories.class.getName () + " engine *****");
54     }
55
56     /**
57      * @return the singleton instance of the object
58      */

59     public static synchronized ManageCategories getInstance () {
60         if (singletonInstance == null) {
61             singletonInstance = new ManageCategories ();
62         }
63         return singletonInstance;
64     }
65
66     public boolean handleActions (ParamBean jParams, int mode, Map JavaDoc engineMap,
67                                   ObjectKey objectKey, ContentDefinition contentDefinition,
68                                   boolean useDefaults)
69             throws JahiaException, JahiaSessionExpirationException {
70         HttpSession JavaDoc session = jParams.getSession ();
71         switch (mode) {
72             case (JahiaEngine.LOAD_MODE):
73                 return loadCategories (jParams, engineMap, objectKey, contentDefinition,
74                         useDefaults);
75             case (JahiaEngine.UPDATE_MODE):
76                 return updateCategories (jParams, engineMap, objectKey, contentDefinition,
77                         useDefaults);
78             case (JahiaEngine.SAVE_MODE):
79                 return saveCategories (jParams, engineMap, objectKey, contentDefinition,
80                         useDefaults);
81         }
82         return false;
83     }
84
85     private boolean loadCategories (ParamBean jParams, Map JavaDoc engineMap,
86                                     ObjectKey objectKey, ContentDefinition contentDefinition,
87                                     boolean useDefaults)
88             throws JahiaException {
89
90         logger.debug ("Loading categories for object key " + objectKey);
91
92         HttpServletRequest JavaDoc request = jParams.getRequest ();
93         HttpSession JavaDoc session = jParams.getSession ();
94
95         ArrayList JavaDoc selectedCategories = (ArrayList JavaDoc) engineMap.get (
96                 SELECTEDCATEGORIES_ENGINEMAPKEY);
97
98         JTree JavaDoc tree = null;
99
100         if (request.getParameterMap ().containsKey ("guitree")) {
101             // we only load the tree from the session if we were doing
102
// control operations on it.
103
tree = (JTree JavaDoc) engineMap.get (CATEGORYTREE_ENGINEMAPKEY);
104         } else {
105             engineMap.remove (CATEGORYTREE_ENGINEMAPKEY);
106         }
107         if (tree == null) {
108             // Root Node
109
DefaultMutableTreeNode JavaDoc top =
110                     new DefaultMutableTreeNode JavaDoc (Category.getRootCategory (), true);
111             DefaultTreeModel JavaDoc treeModel = new DefaultTreeModel JavaDoc (top, true);
112             tree = new JTree JavaDoc (treeModel);
113             buildCategoryTree (top, Category.getRootCategory ());
114             engineMap.put (CATEGORYTREE_ENGINEMAPKEY, tree);
115             if (selectedCategories == null) {
116                 selectedCategories = new ArrayList JavaDoc ();
117                 Set JavaDoc existingCategories = Category.getObjectCategories (objectKey);
118                 Iterator JavaDoc existingCategoriesIter = existingCategories.iterator ();
119                 while (existingCategoriesIter.hasNext ()) {
120                     Category curCategory = (Category) existingCategoriesIter.next ();
121                     selectedCategories.add (curCategory.getKey ());
122                 }
123                 engineMap.put (SELECTEDCATEGORIES_ENGINEMAPKEY, selectedCategories);
124             }
125             if ((selectedCategories.size () == 0) && (useDefaults)) {
126                 // this mechanism allows templates to set the currently
127
// selected categories into the session and we can use these
128
// values to initialize the selected categories.
129
selectedCategories = (ArrayList JavaDoc) session.getAttribute (
130                         DEFAULTCATEGORIES_SESSIONKEYPREFIX + contentDefinition.getObjectKey ());
131                 logger.debug (
132                         "Looking for default selected categories in session key " +
133                         DEFAULTCATEGORIES_SESSIONKEYPREFIX +
134                         contentDefinition.getObjectKey ());
135                 if (selectedCategories != null) {
136                     logger.debug (
137                             "Found default categories " + selectedCategories +
138                             "for session key " +
139                             DEFAULTCATEGORIES_SESSIONKEYPREFIX +
140                             contentDefinition.getObjectKey ());
141                 } else {
142                     selectedCategories = new ArrayList JavaDoc ();
143                 }
144                 engineMap.put (SELECTEDCATEGORIES_ENGINEMAPKEY, selectedCategories);
145             }
146         }
147
148         // first let's make sure all the selected categories have their
149
// path expanded.
150
expandSelectedCategories (tree,
151                 (DefaultMutableTreeNode JavaDoc) tree.getModel ().getRoot (),
152                 selectedCategories);
153
154         // now let's modify any expansions according to the user's request.
155
GUITreeTools.updateGUITree (tree, request);
156
157         ArrayList JavaDoc flatCategoryList = null;
158         if (tree != null) {
159             DefaultMutableTreeNode JavaDoc rootNode =
160                     (DefaultMutableTreeNode JavaDoc) tree.getModel ().getRoot ();
161             if (rootNode != null) {
162                 flatCategoryList = GUITreeTools.getFlatTree (tree, rootNode);
163             }
164         }
165
166         engineMap.put (FLATCATEGORYLIST_ENGINEMAPKEY, flatCategoryList);
167
168         engineMap.put ("fieldForm", ServicesRegistry.getInstance ().
169                 getJahiaFetcherService ().fetchServlet (jParams, JSP_FILE));
170         return true;
171     }
172
173     private boolean updateCategories (ParamBean jParams, Map JavaDoc engineMap,
174                                       ObjectKey objectKey, ContentDefinition contentDefinition,
175                                       boolean useDefaults) {
176         logger.debug ("Processing categories update");
177
178         JTree JavaDoc tree = (JTree JavaDoc) engineMap.get (CATEGORYTREE_ENGINEMAPKEY);
179
180         Map JavaDoc parameterMap = jParams.getRequest ().getParameterMap ();
181         Iterator JavaDoc paramNameIter = parameterMap.keySet ().iterator ();
182         ArrayList JavaDoc newSelectedCategories = new ArrayList JavaDoc ();
183         while (paramNameIter.hasNext ()) {
184             String JavaDoc curParamName = (String JavaDoc) paramNameIter.next ();
185             if (curParamName.startsWith (CATEGORYPREFIX_HTMLPARAMETER)) {
186                 String JavaDoc curCategoryName = curParamName.substring (
187                         CATEGORYPREFIX_HTMLPARAMETER.length ());
188                 newSelectedCategories.add (curCategoryName);
189                 logger.debug ("Submitted category key : " + curCategoryName);
190             }
191         }
192
193         ArrayList JavaDoc oldSelectedCategories = (ArrayList JavaDoc) engineMap.get (
194                 SELECTEDCATEGORIES_ENGINEMAPKEY);
195         Iterator JavaDoc oldSelectedIter = oldSelectedCategories.iterator ();
196         while (oldSelectedIter.hasNext ()) {
197             String JavaDoc curOldSelectedCategoryKey = (String JavaDoc) oldSelectedIter.next ();
198             if (newSelectedCategories.contains (curOldSelectedCategoryKey)) {
199                 // nothing to do here
200
} else {
201                 DefaultMutableTreeNode JavaDoc categoryNode = findNodeByCategoryKey (tree,
202                         curOldSelectedCategoryKey);
203                 if (!tree.isVisible (new TreePath JavaDoc (categoryNode.getPath ()))) {
204                     newSelectedCategories.add (curOldSelectedCategoryKey);
205                 }
206             }
207         }
208         engineMap.put (SELECTEDCATEGORIES_ENGINEMAPKEY, newSelectedCategories);
209         return true;
210     }
211
212     private boolean saveCategories (ParamBean jParams, Map JavaDoc engineMap,
213                                     ObjectKey objectKey, ContentDefinition contentDefinition,
214                                     boolean useDefaults)
215             throws JahiaException {
216         logger.debug ("Saving categories selection");
217         ArrayList JavaDoc selectedCategories = (ArrayList JavaDoc) engineMap.get (
218                 SELECTEDCATEGORIES_ENGINEMAPKEY);
219
220         // selectedCategories will be null here if we never went into the
221
// categories tab
222

223         if ((useDefaults) && (selectedCategories == null)) {
224             // this mechanism allows templates to set the currently
225
// selected categories into the session and we can use these
226
// values to initialize the selected categories.
227
selectedCategories = (ArrayList JavaDoc) jParams.getSession ().
228                     getAttribute (
229                             DEFAULTCATEGORIES_SESSIONKEYPREFIX +
230                     contentDefinition.getObjectKey ());
231             logger.debug (
232                     "Looking for default selected categories in session key " +
233                     DEFAULTCATEGORIES_SESSIONKEYPREFIX +
234                     contentDefinition.getObjectKey ());
235             if (selectedCategories != null) {
236                 logger.debug ("Found default categories " +
237                         selectedCategories + "for session key " +
238                         DEFAULTCATEGORIES_SESSIONKEYPREFIX +
239                         contentDefinition.getObjectKey ());
240             }
241         }
242
243         if (selectedCategories == null) {
244             return false;
245         }
246
247         // now we must not only add the selected categories for the current
248
// object but we must also remove any categories that were not selected
249
// because this means the user has removed them.
250

251         // so first let's remove the categories.
252
Set JavaDoc existingCategories = Category.getObjectCategories (objectKey);
253         Iterator JavaDoc existingCategoriesIter = existingCategories.iterator ();
254         while (existingCategoriesIter.hasNext ()) {
255             Category curCategory = (Category) existingCategoriesIter.next ();
256             if (!selectedCategories.contains (curCategory.getKey ())) {
257                 // remove association with object from category.
258
curCategory.removeChildObjectKey (objectKey);
259             }
260         }
261
262         // now we can add the current object key to all the selected
263
// categories.
264
Iterator JavaDoc selectedCategoriesIter = selectedCategories.iterator ();
265         while (selectedCategoriesIter.hasNext ()) {
266             String JavaDoc curCategoryKey = (String JavaDoc) selectedCategoriesIter.next ();
267             Category curCategory = Category.getCategory (curCategoryKey);
268             curCategory.addChildObjectKey (objectKey);
269         }
270
271         engineMap.remove (SELECTEDCATEGORIES_ENGINEMAPKEY);
272
273         return true;
274     }
275
276     private void buildCategoryTree (MutableTreeNode JavaDoc curNode, Category currentCategory)
277             throws JahiaException {
278         ArrayList JavaDoc childCategories = currentCategory.getChildCategories ();
279         Iterator JavaDoc childIter = childCategories.iterator ();
280         while (childIter.hasNext ()) {
281             Category curChildCategory = (Category) childIter.next ();
282             DefaultMutableTreeNode JavaDoc newNode = new DefaultMutableTreeNode JavaDoc (curChildCategory);
283             curNode.insert (newNode, 0);
284             buildCategoryTree (newNode, curChildCategory);
285         }
286     }
287
288     private void expandSelectedCategories (JTree JavaDoc tree, DefaultMutableTreeNode JavaDoc node,
289                                            ArrayList JavaDoc selectedCategories) {
290         Category curCategory = (Category) node.getUserObject ();
291         if (selectedCategories.contains (curCategory.getKey ())) {
292             tree.expandPath (new TreePath JavaDoc (node.getPath ()));
293         }
294         Enumeration JavaDoc children = node.children ();
295         DefaultMutableTreeNode JavaDoc childNode = null;
296         while (children.hasMoreElements ()) {
297             childNode = (DefaultMutableTreeNode JavaDoc) children.nextElement ();
298             expandSelectedCategories (tree, childNode, selectedCategories);
299         }
300     }
301
302     private DefaultMutableTreeNode JavaDoc findNodeByCategoryKey (JTree JavaDoc tree, String JavaDoc categoryKey) {
303         DefaultMutableTreeNode JavaDoc root = (DefaultMutableTreeNode JavaDoc) tree.getModel ().getRoot ();
304         Enumeration JavaDoc depthNodeEnum = root.depthFirstEnumeration ();
305         while (depthNodeEnum.hasMoreElements ()) {
306             DefaultMutableTreeNode JavaDoc curNode = (DefaultMutableTreeNode JavaDoc) depthNodeEnum.nextElement ();
307             Category curCategory = (Category) curNode.getUserObject ();
308             if (curCategory.getKey ().equals (categoryKey)) {
309                 return curNode;
310             }
311         }
312         return null;
313     }
314
315 }
Popular Tags