1 package org.jahia.engines.categories; 2 3 import java.util.ArrayList ; 4 import java.util.Enumeration ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 import java.util.Set ; 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpSession ; 10 11 import javax.swing.JTree ; 12 import javax.swing.tree.DefaultMutableTreeNode ; 13 import javax.swing.tree.DefaultTreeModel ; 14 import javax.swing.tree.MutableTreeNode ; 15 import javax.swing.tree.TreePath ; 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 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 JSP_FILE = 42 "/jsp/jahia/engines/categories/manage_categories.jsp"; 43 44 private static final String SELECTEDCATEGORIES_ENGINEMAPKEY = "selectedCategories"; 45 private static final String DEFAULTCATEGORIES_SESSIONKEYPREFIX = "defaultCategories_"; 46 private static final String CATEGORYTREE_ENGINEMAPKEY = "categoryTree"; 47 private static final String FLATCATEGORYLIST_ENGINEMAPKEY = "flatCategoryList"; 48 49 private static final String CATEGORYPREFIX_HTMLPARAMETER = "category_"; 50 51 private ManageCategories () { 52 logger.debug ( 53 "***** Starting " + ManageCategories.class.getName () + " engine *****"); 54 } 55 56 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 engineMap, 67 ObjectKey objectKey, ContentDefinition contentDefinition, 68 boolean useDefaults) 69 throws JahiaException, JahiaSessionExpirationException { 70 HttpSession 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 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 request = jParams.getRequest (); 93 HttpSession session = jParams.getSession (); 94 95 ArrayList selectedCategories = (ArrayList ) engineMap.get ( 96 SELECTEDCATEGORIES_ENGINEMAPKEY); 97 98 JTree tree = null; 99 100 if (request.getParameterMap ().containsKey ("guitree")) { 101 tree = (JTree ) engineMap.get (CATEGORYTREE_ENGINEMAPKEY); 104 } else { 105 engineMap.remove (CATEGORYTREE_ENGINEMAPKEY); 106 } 107 if (tree == null) { 108 DefaultMutableTreeNode top = 110 new DefaultMutableTreeNode (Category.getRootCategory (), true); 111 DefaultTreeModel treeModel = new DefaultTreeModel (top, true); 112 tree = new JTree (treeModel); 113 buildCategoryTree (top, Category.getRootCategory ()); 114 engineMap.put (CATEGORYTREE_ENGINEMAPKEY, tree); 115 if (selectedCategories == null) { 116 selectedCategories = new ArrayList (); 117 Set existingCategories = Category.getObjectCategories (objectKey); 118 Iterator 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 selectedCategories = (ArrayList ) 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 (); 143 } 144 engineMap.put (SELECTEDCATEGORIES_ENGINEMAPKEY, selectedCategories); 145 } 146 } 147 148 expandSelectedCategories (tree, 151 (DefaultMutableTreeNode ) tree.getModel ().getRoot (), 152 selectedCategories); 153 154 GUITreeTools.updateGUITree (tree, request); 156 157 ArrayList flatCategoryList = null; 158 if (tree != null) { 159 DefaultMutableTreeNode rootNode = 160 (DefaultMutableTreeNode ) 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 engineMap, 174 ObjectKey objectKey, ContentDefinition contentDefinition, 175 boolean useDefaults) { 176 logger.debug ("Processing categories update"); 177 178 JTree tree = (JTree ) engineMap.get (CATEGORYTREE_ENGINEMAPKEY); 179 180 Map parameterMap = jParams.getRequest ().getParameterMap (); 181 Iterator paramNameIter = parameterMap.keySet ().iterator (); 182 ArrayList newSelectedCategories = new ArrayList (); 183 while (paramNameIter.hasNext ()) { 184 String curParamName = (String ) paramNameIter.next (); 185 if (curParamName.startsWith (CATEGORYPREFIX_HTMLPARAMETER)) { 186 String curCategoryName = curParamName.substring ( 187 CATEGORYPREFIX_HTMLPARAMETER.length ()); 188 newSelectedCategories.add (curCategoryName); 189 logger.debug ("Submitted category key : " + curCategoryName); 190 } 191 } 192 193 ArrayList oldSelectedCategories = (ArrayList ) engineMap.get ( 194 SELECTEDCATEGORIES_ENGINEMAPKEY); 195 Iterator oldSelectedIter = oldSelectedCategories.iterator (); 196 while (oldSelectedIter.hasNext ()) { 197 String curOldSelectedCategoryKey = (String ) oldSelectedIter.next (); 198 if (newSelectedCategories.contains (curOldSelectedCategoryKey)) { 199 } else { 201 DefaultMutableTreeNode categoryNode = findNodeByCategoryKey (tree, 202 curOldSelectedCategoryKey); 203 if (!tree.isVisible (new TreePath (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 engineMap, 213 ObjectKey objectKey, ContentDefinition contentDefinition, 214 boolean useDefaults) 215 throws JahiaException { 216 logger.debug ("Saving categories selection"); 217 ArrayList selectedCategories = (ArrayList ) engineMap.get ( 218 SELECTEDCATEGORIES_ENGINEMAPKEY); 219 220 223 if ((useDefaults) && (selectedCategories == null)) { 224 selectedCategories = (ArrayList ) 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 251 Set existingCategories = Category.getObjectCategories (objectKey); 253 Iterator existingCategoriesIter = existingCategories.iterator (); 254 while (existingCategoriesIter.hasNext ()) { 255 Category curCategory = (Category) existingCategoriesIter.next (); 256 if (!selectedCategories.contains (curCategory.getKey ())) { 257 curCategory.removeChildObjectKey (objectKey); 259 } 260 } 261 262 Iterator selectedCategoriesIter = selectedCategories.iterator (); 265 while (selectedCategoriesIter.hasNext ()) { 266 String curCategoryKey = (String ) 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 curNode, Category currentCategory) 277 throws JahiaException { 278 ArrayList childCategories = currentCategory.getChildCategories (); 279 Iterator childIter = childCategories.iterator (); 280 while (childIter.hasNext ()) { 281 Category curChildCategory = (Category) childIter.next (); 282 DefaultMutableTreeNode newNode = new DefaultMutableTreeNode (curChildCategory); 283 curNode.insert (newNode, 0); 284 buildCategoryTree (newNode, curChildCategory); 285 } 286 } 287 288 private void expandSelectedCategories (JTree tree, DefaultMutableTreeNode node, 289 ArrayList selectedCategories) { 290 Category curCategory = (Category) node.getUserObject (); 291 if (selectedCategories.contains (curCategory.getKey ())) { 292 tree.expandPath (new TreePath (node.getPath ())); 293 } 294 Enumeration children = node.children (); 295 DefaultMutableTreeNode childNode = null; 296 while (children.hasMoreElements ()) { 297 childNode = (DefaultMutableTreeNode ) children.nextElement (); 298 expandSelectedCategories (tree, childNode, selectedCategories); 299 } 300 } 301 302 private DefaultMutableTreeNode findNodeByCategoryKey (JTree tree, String categoryKey) { 303 DefaultMutableTreeNode root = (DefaultMutableTreeNode ) tree.getModel ().getRoot (); 304 Enumeration depthNodeEnum = root.depthFirstEnumeration (); 305 while (depthNodeEnum.hasMoreElements ()) { 306 DefaultMutableTreeNode curNode = (DefaultMutableTreeNode ) 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 |