KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.admin.categories;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.http.HttpServletResponse JavaDoc;
5 import javax.servlet.http.HttpSession JavaDoc;
6 import org.jahia.data.JahiaData;
7 import org.jahia.params.ParamBean;
8 import org.jahia.registries.ServicesRegistry;
9 import org.jahia.services.sites.JahiaSite;
10 import org.jahia.services.usermanager.JahiaUser;
11 import org.jahia.services.sites.JahiaSiteTools;
12 import org.jahia.resourcebundle.JahiaResourceBundle;
13 import org.jahia.bin.JahiaAdministration;
14 import java.io.IOException JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import java.util.Set JavaDoc;
17 import org.jahia.services.sites.SiteLanguagesPersistance;
18 import org.jahia.exceptions.JahiaException;
19 import java.util.Locale JavaDoc;
20 import org.jahia.bin.Jahia;
21 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
22 import javax.swing.tree.DefaultTreeModel JavaDoc;
23 import javax.swing.JTree JavaDoc;
24 import org.jahia.services.categories.Category;
25 import java.util.ArrayList JavaDoc;
26 import org.jahia.utils.GUITreeTools;
27 import java.util.Map JavaDoc;
28 import java.util.TreeMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import org.jahia.utils.LanguageCodeConverters;
31 import javax.swing.tree.MutableTreeNode JavaDoc;
32 import java.util.Properties JavaDoc;
33 import org.jahia.security.license.License;
34 import org.jahia.data.beans.RequestBean;
35 import org.jahia.gui.GuiBean;
36
37 /**
38  * <p>Title: Category administration user interface</p>
39  * <p>Description: </p>
40  * <p>Copyright: Copyright (c) 2002</p>
41  * <p>Company: Jahia Ltd</p>
42  * @author Serge Huber
43  * @version 1.0
44  */

45
46 public class ManageCategories {
47
48     private static org.apache.log4j.Logger logger =
49         org.apache.log4j.Logger.getLogger(ManageCategories.class);
50
51     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
52     private static final String JavaDoc CURRENTCATEGORY_SESSIONKEY =
53         "currentCategoryKey";
54     private static final String JavaDoc CURRENTCATEGORYCHILDS_SESSIONKEY =
55         "currentCategoryChilds";
56     private static final String JavaDoc CATEGORYTREE_SESSIONKEY = "categoryTree";
57
58     private JahiaSite site;
59     private JahiaUser user;
60     private ServicesRegistry sReg;
61     private License coreLicense;
62
63     /**
64      * Default constructor.
65      * @author Alexandre Kraft
66      *
67      * @param request Servlet request.
68      * @param response Servlet response.
69      * @param session Servlet session for the current user.
70      */

71     public ManageCategories (HttpServletRequest JavaDoc request,
72                              HttpServletResponse JavaDoc response,
73                              HttpSession JavaDoc session)
74         throws Throwable JavaDoc {
75
76         JahiaData jData = (JahiaData) request.getAttribute(
77             "org.jahia.data.JahiaData");
78         ParamBean jParams = null;
79         if (jData != null) {
80             jParams = jData.params();
81         }
82         coreLicense = Jahia.getCoreLicense();
83         if (coreLicense == null) {
84             // set request attributes...
85
String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
86                 "org.jahia.admin.JahiaDisplayMessage.invalidLicenseKey.label",
87                 jParams, jParams.getLocale());
88             request.setAttribute("jahiaDisplayMessage", dspMsg);
89             // redirect...
90
JahiaAdministration.doRedirect(request, response, session,
91                                            JSP_PATH + "menu.jsp");
92             return;
93         }
94
95         userRequestDispatcher(request, response, session);
96     } // end constructor
97

98     //-------------------------------------------------------------------------
99
/**
100      * This method is used like a dispatcher for user requests.
101      * @author Alexandre Kraft
102      *
103      * @param request Servlet request.
104      * @param response Servlet response.
105      * @param session Servlet session for the current user.
106      */

107     private void userRequestDispatcher (HttpServletRequest JavaDoc request,
108                                         HttpServletResponse JavaDoc response,
109                                         HttpSession JavaDoc session)
110         throws Throwable JavaDoc {
111         JahiaData jData = (JahiaData) request.getAttribute(
112             "org.jahia.data.JahiaData");
113         ParamBean jParams = null;
114         if (jData != null) {
115             jParams = jData.params();
116         }
117
118         String JavaDoc operation = request.getParameter("sub");
119
120         sReg = ServicesRegistry.getInstance();
121
122         // check if the user has really admin access to this site...
123
user = (JahiaUser) session.getAttribute(ParamBean.SESSION_USER);
124         site = (JahiaSite) session.getAttribute(ParamBean.SESSION_SITE);
125
126         if (site != null && user != null && sReg != null) {
127
128             if (JahiaSiteTools.getAdminGroup(site).isMember(user)) {
129
130                 // set the new site id to administrate...
131
request.setAttribute("site", site);
132
133                 if (operation.equals("display")) {
134                     displayCategories(request, response, session);
135                 } else if (operation.equals("commit")) {
136                     commitChanges(request, response, session);
137                 } else if (operation.equals("editCategory")) {
138                     displayEditCategory(request, response, session, null);
139                 } else if (operation.equals("addExistingCategory")) {
140                     commitChanges(request, response, session);
141                 } else if (operation.equals("commitEditCategory")) {
142                     commitEditCategory(request, response, session);
143                 } else if (operation.equals("deleteCategory")) {
144                     displayDeleteCategory(request, response, session);
145                 } else if (operation.equals("commitDeleteCategory")) {
146                     commitDeleteCategory(request, response, session);
147                 } else if (operation.equals("moveCategory")) {
148                     displayMoveCategory(request, response, session);
149                 } else if (operation.equals("commitMoveCategory")) {
150                     commitMoveCategory(request, response, session);
151                 } else if (operation.equals("commitEditProperty")) {
152                     commitEditProperty(request, response, session);
153                 } else if (operation.equals("commitDeleteProperty")) {
154                     commitDeleteProperty(request, response, session);
155                 }
156
157             } else {
158                 String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
159                     "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
160                     jParams, jParams.getLocale());
161                 request.setAttribute("jahiaDisplayMessage", dspMsg);
162                 JahiaAdministration.doRedirect(request,
163                                                response,
164                                                session, JSP_PATH + "menu.jsp");
165             }
166         } else {
167             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
168                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
169                 jParams, jParams.getLocale());
170             request.setAttribute("jahiaDisplayMessage", dspMsg);
171             JahiaAdministration.doRedirect(request,
172                                            response,
173                                            session, JSP_PATH + "menu.jsp");
174         }
175     } // userRequestDispatcher
176

177     //-------------------------------------------------------------------------
178
private void displayCategories (HttpServletRequest JavaDoc request,
179                                     HttpServletResponse JavaDoc response,
180                                     HttpSession JavaDoc session)
181         throws IOException JavaDoc, ServletException JavaDoc {
182         JahiaData jData = (JahiaData) request.getAttribute(
183             "org.jahia.data.JahiaData");
184         ParamBean jParams = null;
185         if (jData != null) {
186             jParams = jData.params();
187         }
188         try {
189
190             String JavaDoc curCategoryKey = request.getParameter("currentCategoryKey");
191             if (curCategoryKey != null) {
192                 session.setAttribute(CURRENTCATEGORY_SESSIONKEY, curCategoryKey);
193             }
194
195             Set JavaDoc allLanguageCodes = SiteLanguagesPersistance.getInstance().
196                                    getAllSitesLanguages();
197
198             Locale JavaDoc[] availableLocales = Locale.getAvailableLocales();
199
200             JTree JavaDoc tree = null;
201             if (request.getParameterMap().containsKey("guitree")) {
202                 // we only load the tree from the session if we were doing
203
// control operations on it.
204
tree = (JTree JavaDoc) session.getAttribute(
205                     CATEGORYTREE_SESSIONKEY);
206             } else {
207                 session.removeAttribute(CATEGORYTREE_SESSIONKEY);
208             }
209             if (tree == null) {
210                 // Root Node
211
DefaultMutableTreeNode JavaDoc top =
212                     new DefaultMutableTreeNode JavaDoc(Category.getRootCategory(), true);
213                 DefaultTreeModel JavaDoc treeModel = new DefaultTreeModel JavaDoc(top, true);
214                 tree = new JTree JavaDoc(treeModel);
215                 buildCategoryTree(top, Category.getRootCategory(), null);
216                 session.setAttribute(CATEGORYTREE_SESSIONKEY, tree);
217             }
218
219             GUITreeTools.updateGUITree(tree, request);
220
221             ArrayList JavaDoc flatCategoryList = null;
222             if (tree != null) {
223                 DefaultMutableTreeNode JavaDoc rootNode =
224                     (DefaultMutableTreeNode JavaDoc) tree.getModel().getRoot();
225                 if (rootNode != null) {
226                     flatCategoryList = GUITreeTools.getFlatTree(tree, rootNode);
227                 }
228             }
229
230             // For JSP Output
231
String JavaDoc currentCategoryKey = (String JavaDoc) session.getAttribute(
232                 CURRENTCATEGORY_SESSIONKEY);
233             if (currentCategoryKey == null) {
234                 currentCategoryKey = Category.getRootCategory().getKey();
235                 session.setAttribute(CURRENTCATEGORY_SESSIONKEY,
236                                      currentCategoryKey);
237             }
238             // test if we are able to load the current category key
239
Category currentCategory = Category.getCategory(currentCategoryKey);
240             if (currentCategory == null) {
241                 // if not we default to root category.
242
logger.warn("Couldn't find category key " + currentCategoryKey + " defaulting back to root category");
243                 currentCategoryKey = Category.getRootCategory().getKey();
244                 currentCategory = Category.getCategory(currentCategoryKey);
245                 session.setAttribute(CURRENTCATEGORY_SESSIONKEY,
246                                      currentCategoryKey);
247             }
248             session.setAttribute(CURRENTCATEGORYCHILDS_SESSIONKEY,
249                                  currentCategory.getChildCategories());
250
251             request.setAttribute("flatCategoryList", flatCategoryList);
252             request.setAttribute("categoryTree", tree);
253
254             JahiaAdministration.doRedirect(request,
255                                            response,
256                                            session,
257                                            JSP_PATH + "manage_categories.jsp");
258
259         } catch (JahiaException je) {
260             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
261                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
262                 jParams, jParams.getLocale());
263             request.setAttribute("jahiaDisplayMessage", dspMsg);
264             JahiaAdministration.doRedirect(request,
265                                            response,
266                                            session,
267                                            JSP_PATH + "menu.jsp");
268         }
269
270     }
271
272     //-------------------------------------------------------------------------
273
private void commitChanges (HttpServletRequest JavaDoc request,
274                                 HttpServletResponse JavaDoc response,
275                                 HttpSession JavaDoc session)
276         throws IOException JavaDoc, ServletException JavaDoc {
277
278         JahiaData jData = (JahiaData) request.getAttribute(
279             "org.jahia.data.JahiaData");
280         ParamBean jParams = null;
281         if (jData != null) {
282             jParams = jData.params();
283         }
284
285         try {
286
287             request.setAttribute("warningMsg", "");
288             Set JavaDoc allLanguageCodes = SiteLanguagesPersistance.getInstance().
289                                    getAllSitesLanguages();
290
291             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
292                 "org.jahia.admin.JahiaDisplayMessage.changeCommitted.label",
293                 jParams, jParams.getLocale());
294             request.setAttribute("jahiaDisplayMessage", dspMsg);
295             displayCategories(request, response, session);
296
297         } catch (JahiaException je) {
298             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
299                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
300                 jParams, jParams.getLocale());
301             request.setAttribute("jahiaDisplayMessage", dspMsg);
302             JahiaAdministration.doRedirect(request,
303                                            response,
304                                            session,
305                                            JSP_PATH + "menu.jsp");
306         }
307
308     } // end addComponent
309

310     //-------------------------------------------------------------------------
311
private void displayEditCategory (HttpServletRequest JavaDoc request,
312                                       HttpServletResponse JavaDoc response,
313                                       HttpSession JavaDoc session,
314                                       String JavaDoc currentCategoryKey)
315         throws IOException JavaDoc, ServletException JavaDoc {
316         JahiaData jData = (JahiaData) request.getAttribute(
317             "org.jahia.data.JahiaData");
318         ParamBean jParams = null;
319         if (jData != null) {
320             jParams = jData.params();
321         }
322         try {
323
324             if (currentCategoryKey == null) {
325                 currentCategoryKey = request.getParameter(
326                     "currentCategoryKey");
327             }
328             Set JavaDoc allLanguageCodes = SiteLanguagesPersistance.getInstance().
329                                    getAllSitesLanguages();
330             Map JavaDoc titleMap = new TreeMap JavaDoc();
331             Properties JavaDoc categoryProperties = new Properties JavaDoc();
332
333             if (currentCategoryKey != null) {
334                 Category currentCategory = Category.getCategory(
335                     currentCategoryKey);
336                 session.setAttribute(CURRENTCATEGORY_SESSIONKEY,
337                                      currentCategoryKey);
338                 Iterator JavaDoc allLanguageCodeIter = allLanguageCodes.iterator();
339                 while (allLanguageCodeIter.hasNext()) {
340                     String JavaDoc curLanguageCode = (String JavaDoc) allLanguageCodeIter.next();
341                     titleMap.put(curLanguageCode,
342                                  currentCategory.getTitle(
343                         LanguageCodeConverters.
344                         languageCodeToLocale(curLanguageCode)));
345                 }
346                 categoryProperties = currentCategory.getProperties();
347             } else {
348                 Iterator JavaDoc allLanguageCodeIter = allLanguageCodes.iterator();
349                 while (allLanguageCodeIter.hasNext()) {
350                     String JavaDoc curLanguageCode = (String JavaDoc) allLanguageCodeIter.next();
351                     titleMap.put(curLanguageCode, "");
352                 }
353             }
354
355             Locale JavaDoc[] availableLocales = Locale.getAvailableLocales();
356
357             request.setAttribute("categoryTitleMap", titleMap);
358             request.setAttribute("categoryKey", currentCategoryKey);
359             request.setAttribute("categoryProperties", categoryProperties);
360             request.setAttribute("parentCategoryKey",
361                                  request.getParameter("parentCategoryKey"));
362
363             JahiaAdministration.doRedirect(request,
364                                            response,
365                                            session,
366                                            JSP_PATH +
367                                            "manage_categories_editcategory.jsp");
368
369         } catch (JahiaException je) {
370             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
371                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
372                 jParams, jParams.getLocale());
373             request.setAttribute("jahiaDisplayMessage", dspMsg);
374             JahiaAdministration.doRedirect(request,
375                                            response,
376                                            session,
377                                            JSP_PATH + "menu.jsp");
378         }
379
380     }
381
382     //-------------------------------------------------------------------------
383
private void commitEditCategory (HttpServletRequest JavaDoc request,
384                                      HttpServletResponse JavaDoc response,
385                                      HttpSession JavaDoc session)
386         throws IOException JavaDoc, ServletException JavaDoc {
387
388         JahiaData jData = (JahiaData) request.getAttribute(
389             "org.jahia.data.JahiaData");
390         ParamBean jParams = null;
391         if (jData != null) {
392             jParams = jData.params();
393         }
394
395         try {
396
397             request.setAttribute("warningMsg", "");
398             Set JavaDoc allLanguageCodes = SiteLanguagesPersistance.getInstance().
399                                    getAllSitesLanguages();
400
401             String JavaDoc parentCategoryKey = request.getParameter("parentCategoryKey");
402             Category parentCategory = Category.getCategory(parentCategoryKey);
403
404             String JavaDoc categoryKey = request.getParameter("categoryKey");
405             boolean addingNewCategory = false;
406             Category currentCategory = null;
407             if (categoryKey == null) {
408                 // we are editing an existing category
409
categoryKey = (String JavaDoc) session.getAttribute(
410                     CURRENTCATEGORY_SESSIONKEY);
411                 currentCategory = Category.getCategory(categoryKey);
412             } else {
413                 // we are adding a new category.
414
addingNewCategory = true;
415                 if (Category.getCategory(categoryKey) != null) {
416                     String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
417                         "org.jahia.admin.categories.ManageCategories.editCategory.categoryAlreadyExists.label",
418                         jParams, jParams.getLocale());
419                     session.setAttribute(JahiaAdministration.CLASS_NAME +
420                                          "jahiaDisplayMessage", dspMsg);
421                     displayEditCategory(request, response, session, null);
422                     return;
423                 }
424                 currentCategory = Category.createCategory(categoryKey,
425                     parentCategory);
426             }
427
428             Map JavaDoc parameterMap = request.getParameterMap();
429             Iterator JavaDoc keyIter = parameterMap.keySet().iterator();
430             while (keyIter.hasNext()) {
431                 String JavaDoc curKey = (String JavaDoc) keyIter.next();
432                 if (curKey.startsWith("title_")) {
433                     String JavaDoc languageCode = curKey.substring("title_".length());
434                     String JavaDoc newTitle = request.getParameter(curKey);
435                     if ( (newTitle != null) && (!"".equals(newTitle))) {
436                         currentCategory.setTitle(LanguageCodeConverters.
437                                                  languageCodeToLocale(
438                             languageCode), newTitle);
439                     }
440                 }
441             }
442
443             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
444                 "org.jahia.admin.JahiaDisplayMessage.changeCommitted.label",
445                 jParams, jParams.getLocale());
446             request.setAttribute("jahiaDisplayMessage", dspMsg);
447             displayEditCategory(request, response, session, categoryKey);
448
449         } catch (JahiaException je) {
450             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
451                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
452                 jParams, jParams.getLocale());
453             request.setAttribute("jahiaDisplayMessage", dspMsg);
454             JahiaAdministration.doRedirect(request,
455                                            response,
456                                            session,
457                                            JSP_PATH + "menu.jsp");
458         }
459
460     }
461
462     //-------------------------------------------------------------------------
463
private void displayDeleteCategory (HttpServletRequest JavaDoc request,
464                                         HttpServletResponse JavaDoc response,
465                                         HttpSession JavaDoc session)
466         throws IOException JavaDoc, ServletException JavaDoc {
467         JahiaData jData = (JahiaData) request.getAttribute(
468             "org.jahia.data.JahiaData");
469         ParamBean jParams = null;
470         if (jData != null) {
471             jParams = jData.params();
472         }
473         try {
474
475             String JavaDoc curCategoryKey = request.getParameter("currentCategoryKey");
476             String JavaDoc parentCategoryKey = request.getParameter("parentCategoryKey");
477             Category currentCategory = Category.getCategory(curCategoryKey);
478
479             Locale JavaDoc[] availableLocales = Locale.getAvailableLocales();
480
481             JTree JavaDoc tree = null;
482             // Root Node
483
DefaultMutableTreeNode JavaDoc top =
484                 new DefaultMutableTreeNode JavaDoc(currentCategory, true);
485             DefaultTreeModel JavaDoc treeModel = new DefaultTreeModel JavaDoc(top, true);
486             tree = new JTree JavaDoc(treeModel);
487
488             buildCategoryTree(top, currentCategory, null);
489
490             GUITreeTools.expandAllPath(tree, top);
491
492             ArrayList JavaDoc flatCategoryList = null;
493             if (tree != null) {
494                 DefaultMutableTreeNode JavaDoc rootNode =
495                     (DefaultMutableTreeNode JavaDoc) tree.getModel().getRoot();
496                 if (rootNode != null) {
497                     flatCategoryList = GUITreeTools.getFlatTree(tree, rootNode);
498                 }
499             }
500
501             // For JSP Output
502

503             request.setAttribute("flatCategoryList", flatCategoryList);
504             request.setAttribute("categoryTree", tree);
505             request.setAttribute("categoryKey", curCategoryKey);
506             request.setAttribute("parentCategoryKey", parentCategoryKey);
507
508             JahiaAdministration.doRedirect(request,
509                                            response,
510                                            session,
511                                            JSP_PATH +
512                 "manage_categories_deletecategory.jsp");
513
514         } catch (JahiaException je) {
515             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
516                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
517                 jParams, jParams.getLocale());
518             request.setAttribute("jahiaDisplayMessage", dspMsg);
519             JahiaAdministration.doRedirect(request,
520                                            response,
521                                            session,
522                                            JSP_PATH + "menu.jsp");
523         }
524
525     }
526
527     //-------------------------------------------------------------------------
528
private void commitDeleteCategory (HttpServletRequest JavaDoc request,
529                                        HttpServletResponse JavaDoc response,
530                                        HttpSession JavaDoc session)
531         throws IOException JavaDoc, ServletException JavaDoc {
532         JahiaData jData = (JahiaData) request.getAttribute(
533             "org.jahia.data.JahiaData");
534         ParamBean jParams = null;
535         if (jData != null) {
536             jParams = jData.params();
537         }
538         try {
539
540             String JavaDoc curCategoryKey = request.getParameter("targetCategoryKey");
541             Category currentCategory = Category.getCategory(curCategoryKey);
542
543             recursiveDeleteCategory(currentCategory);
544
545             Locale JavaDoc[] availableLocales = Locale.getAvailableLocales();
546
547             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
548                 "org.jahia.admin.categories.ManageCategories.deleteCategory.categoryDeletedSuccessfully.label",
549                 jParams, jParams.getLocale());
550             request.setAttribute("jahiaDisplayMessage", dspMsg);
551
552             session.removeAttribute(CURRENTCATEGORY_SESSIONKEY);
553             session.removeAttribute(CURRENTCATEGORYCHILDS_SESSIONKEY);
554
555             displayCategories(request, response, session);
556
557         } catch (JahiaException je) {
558             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
559                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
560                 jParams, jParams.getLocale());
561             request.setAttribute("jahiaDisplayMessage", dspMsg);
562             JahiaAdministration.doRedirect(request,
563                                            response,
564                                            session,
565                                            JSP_PATH + "menu.jsp");
566         }
567
568     }
569
570     //-------------------------------------------------------------------------
571
private void commitEditProperty (HttpServletRequest JavaDoc request,
572                                      HttpServletResponse JavaDoc response,
573                                      HttpSession JavaDoc session)
574         throws IOException JavaDoc, ServletException JavaDoc {
575
576         JahiaData jData = (JahiaData) request.getAttribute(
577             "org.jahia.data.JahiaData");
578         ParamBean jParams = null;
579         if (jData != null) {
580             jParams = jData.params();
581         }
582
583         try {
584
585             request.setAttribute("warningMsg", "");
586             Set JavaDoc allLanguageCodes = SiteLanguagesPersistance.getInstance().
587                                    getAllSitesLanguages();
588
589             String JavaDoc parentCategoryKey = request.getParameter("parentCategoryKey");
590             Category parentCategory = Category.getCategory(parentCategoryKey);
591             Category currentCategory = null;
592
593             String JavaDoc categoryKey = (String JavaDoc) session.getAttribute(
594                 CURRENTCATEGORY_SESSIONKEY);
595             currentCategory = Category.getCategory(categoryKey);
596
597             Map JavaDoc parameterMap = request.getParameterMap();
598             Iterator JavaDoc keyIter = parameterMap.keySet().iterator();
599             while (keyIter.hasNext()) {
600                 String JavaDoc curKey = (String JavaDoc) keyIter.next();
601                 if (curKey.startsWith("setProperty_")) {
602                     String JavaDoc propertyName = curKey.substring("setProperty_".
603                         length());
604                     String JavaDoc propertyValue = request.getParameter(curKey);
605                     if ( (propertyName != null) && (!"".equals(propertyValue))) {
606                         logger.debug("Setting property name=[" + propertyName +
607                                      "] value=[" + propertyValue +
608                                      "] for category [" +
609                                      currentCategory.getKey() + "]");
610                         currentCategory.setProperty(propertyName, propertyValue);
611                     }
612                 }
613             }
614
615             String JavaDoc newPropertyName = request.getParameter("newPropertyName");
616             if (newPropertyName != null) {
617                 if (!"".equals(newPropertyName)) {
618                     String JavaDoc newPropertyValue = request.getParameter(
619                         "newPropertyValue");
620                     if (newPropertyValue != null) {
621                         logger.debug("Setting property name=[" +
622                                      newPropertyName + "] value=[" +
623                                      newPropertyValue + "] for category [" +
624                                      currentCategory.getKey() + "]");
625                         currentCategory.setProperty(newPropertyName,
626                             newPropertyValue);
627                     }
628                 }
629             }
630
631             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
632                 "org.jahia.admin.JahiaDisplayMessage.changeCommitted.label",
633                 jParams, jParams.getLocale());
634             request.setAttribute("jahiaDisplayMessage", dspMsg);
635             displayEditCategory(request, response, session, categoryKey);
636
637         } catch (JahiaException je) {
638             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
639                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
640                 jParams, jParams.getLocale());
641             request.setAttribute("jahiaDisplayMessage", dspMsg);
642             JahiaAdministration.doRedirect(request,
643                                            response,
644                                            session,
645                                            JSP_PATH + "menu.jsp");
646         }
647
648     }
649
650     //-------------------------------------------------------------------------
651
private void commitDeleteProperty (HttpServletRequest JavaDoc request,
652                                        HttpServletResponse JavaDoc response,
653                                        HttpSession JavaDoc session)
654         throws IOException JavaDoc, ServletException JavaDoc {
655
656         JahiaData jData = (JahiaData) request.getAttribute(
657             "org.jahia.data.JahiaData");
658         ParamBean jParams = null;
659         if (jData != null) {
660             jParams = jData.params();
661         }
662
663         try {
664
665             request.setAttribute("warningMsg", "");
666             Set JavaDoc allLanguageCodes = SiteLanguagesPersistance.getInstance().
667                                    getAllSitesLanguages();
668
669             String JavaDoc parentCategoryKey = request.getParameter("parentCategoryKey");
670             Category parentCategory = Category.getCategory(parentCategoryKey);
671             Category currentCategory = null;
672
673             String JavaDoc categoryKey = (String JavaDoc) session.getAttribute(
674                 CURRENTCATEGORY_SESSIONKEY);
675             currentCategory = Category.getCategory(categoryKey);
676
677             String JavaDoc targetPropertyName = request.getParameter("property");
678             if (targetPropertyName != null) {
679                 currentCategory.removeProperty(targetPropertyName);
680             }
681
682             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
683                 "org.jahia.admin.JahiaDisplayMessage.changeCommitted.label",
684                 jParams, jParams.getLocale());
685             request.setAttribute("jahiaDisplayMessage", dspMsg);
686             displayEditCategory(request, response, session, categoryKey);
687
688         } catch (JahiaException je) {
689             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
690                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
691                 jParams, jParams.getLocale());
692             request.setAttribute("jahiaDisplayMessage", dspMsg);
693             JahiaAdministration.doRedirect(request,
694                                            response,
695                                            session,
696                                            JSP_PATH + "menu.jsp");
697         }
698
699     }
700
701     //-------------------------------------------------------------------------
702
private void displayMoveCategory (HttpServletRequest JavaDoc request,
703                                       HttpServletResponse JavaDoc response,
704                                       HttpSession JavaDoc session)
705         throws IOException JavaDoc, ServletException JavaDoc {
706         JahiaData jData = (JahiaData) request.getAttribute(
707             "org.jahia.data.JahiaData");
708         ParamBean jParams = null;
709         if (jData != null) {
710             jParams = jData.params();
711         }
712
713         // needed by the TreeTag component.
714
request.setAttribute("currentRequest", new RequestBean(new GuiBean(jParams), jParams));
715
716         try {
717
718             String JavaDoc curCategoryKey = request.getParameter("currentCategoryKey");
719             if (curCategoryKey != null) {
720                 session.setAttribute(CURRENTCATEGORY_SESSIONKEY, curCategoryKey);
721             }
722             String JavaDoc parentCategoryKey = request.getParameter("parentCategoryKey");
723
724             Set JavaDoc allLanguageCodes = SiteLanguagesPersistance.getInstance().
725                                    getAllSitesLanguages();
726
727             Locale JavaDoc[] availableLocales = Locale.getAvailableLocales();
728
729             JTree JavaDoc tree = null;
730             if (request.getParameterMap().containsKey("guitree")) {
731                 // we only load the tree from the session if we were doing
732
// control operations on it.
733
tree = (JTree JavaDoc) session.getAttribute(
734                     CATEGORYTREE_SESSIONKEY);
735             } else {
736                 session.removeAttribute(CATEGORYTREE_SESSIONKEY);
737             }
738             if (tree == null) {
739                 // Root Node
740
DefaultMutableTreeNode JavaDoc top =
741                     new DefaultMutableTreeNode JavaDoc(Category.getRootCategory(), true);
742                 DefaultTreeModel JavaDoc treeModel = new DefaultTreeModel JavaDoc(top, true);
743                 tree = new JTree JavaDoc(treeModel);
744                 buildCategoryTree(top, Category.getRootCategory(),
745                                   Category.getCategory(curCategoryKey));
746                 session.setAttribute(CATEGORYTREE_SESSIONKEY, tree);
747             }
748
749             GUITreeTools.updateGUITree(tree, request);
750
751             ArrayList JavaDoc flatCategoryList = null;
752             if (tree != null) {
753                 DefaultMutableTreeNode JavaDoc rootNode =
754                     (DefaultMutableTreeNode JavaDoc) tree.getModel().getRoot();
755                 if (rootNode != null) {
756                     flatCategoryList = GUITreeTools.getFlatTree(tree, rootNode);
757                 }
758             }
759
760             // For JSP Output
761
String JavaDoc currentCategoryKey = (String JavaDoc) session.getAttribute(
762                 CURRENTCATEGORY_SESSIONKEY);
763             if (currentCategoryKey == null) {
764                 currentCategoryKey = Category.getRootCategory().getKey();
765                 session.setAttribute(CURRENTCATEGORY_SESSIONKEY,
766                                      currentCategoryKey);
767             }
768             Category currentCategory = Category.getCategory(currentCategoryKey);
769
770             // setup dispatching environment.
771

772             session.setAttribute(CURRENTCATEGORYCHILDS_SESSIONKEY,
773                                  currentCategory.getChildCategories());
774
775             request.setAttribute("flatCategoryList", flatCategoryList);
776             request.setAttribute("categoryTree", tree);
777             request.setAttribute("currentCategoryKey", currentCategoryKey);
778             request.setAttribute("parentCategoryKey", parentCategoryKey);
779
780             JahiaAdministration.doRedirect(request,
781                                            response,
782                                            session,
783                                            JSP_PATH + "manage_categories_movecategory.jsp");
784
785         } catch (JahiaException je) {
786             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
787                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
788                 jParams, jParams.getLocale());
789             request.setAttribute("jahiaDisplayMessage", dspMsg);
790             JahiaAdministration.doRedirect(request,
791                                            response,
792                                            session,
793                                            JSP_PATH + "menu.jsp");
794         }
795
796     }
797
798     //-------------------------------------------------------------------------
799
private void commitMoveCategory (HttpServletRequest JavaDoc request,
800                                      HttpServletResponse JavaDoc response,
801                                      HttpSession JavaDoc session)
802         throws IOException JavaDoc, ServletException JavaDoc {
803
804         JahiaData jData = (JahiaData) request.getAttribute(
805             "org.jahia.data.JahiaData");
806         ParamBean jParams = null;
807         if (jData != null) {
808             jParams = jData.params();
809         }
810
811         try {
812
813             String JavaDoc parentCategoryKey = request.getParameter("parentCategoryKey");
814             Category parentCategory = Category.getCategory(parentCategoryKey);
815
816             String JavaDoc categoryKey = request.getParameter("currentCategoryKey");
817             Category currentCategory = Category.getCategory(categoryKey);
818
819             // now let's retrieve the selected category from the tree.
820
String JavaDoc newParentKey = request.getParameter("newParentKey");
821             Category newParentCategory = Category.getCategory(newParentKey);
822
823             // now that we've got the category, let's change the parenting.
824
parentCategory.removeChildObjectKey(currentCategory.getObjectKey());
825             newParentCategory.addChildObjectKey(currentCategory.getObjectKey());
826
827             // finally let's setup the dispatching environment.
828

829             request.setAttribute("warningMsg", "");
830             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
831                 "org.jahia.admin.JahiaDisplayMessage.changeCommitted.label",
832                 jParams, jParams.getLocale());
833             request.setAttribute("jahiaDisplayMessage", dspMsg);
834             displayCategories(request, response, session);
835
836         } catch (JahiaException je) {
837             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource(
838                 "org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
839                 jParams, jParams.getLocale());
840             request.setAttribute("jahiaDisplayMessage", dspMsg);
841             JahiaAdministration.doRedirect(request,
842                                            response,
843                                            session,
844                                            JSP_PATH + "menu.jsp");
845         }
846
847     }
848
849
850     private void buildCategoryTree (MutableTreeNode JavaDoc curNode,
851                                     Category currentCategory, Category stopCategory)
852         throws JahiaException {
853         if ( (stopCategory != null) &&
854              (currentCategory.getKey().equals(stopCategory.getKey()))
855              ) {
856             return;
857         }
858         ArrayList JavaDoc childCategories = currentCategory.getChildCategories();
859         Iterator JavaDoc childIter = childCategories.iterator();
860         while (childIter.hasNext()) {
861             Category curChildCategory = (Category) childIter.next();
862             if ( ((stopCategory != null) &&
863                  ((!curChildCategory.getKey().equals(stopCategory.getKey()))))
864                  ||
865                  (stopCategory == null)
866                 ) {
867                 DefaultMutableTreeNode JavaDoc newNode = new DefaultMutableTreeNode JavaDoc(
868                     curChildCategory);
869                 curNode.insert(newNode, 0);
870                 buildCategoryTree(newNode, curChildCategory, stopCategory);
871             }
872         }
873     }
874
875     private void recursiveDeleteCategory (Category currentCategory)
876         throws JahiaException {
877         ArrayList JavaDoc childCategories = currentCategory.getChildCategories();
878         Iterator JavaDoc childIter = childCategories.iterator();
879         while (childIter.hasNext()) {
880             Category curChildCategory = (Category) childIter.next();
881             recursiveDeleteCategory(curChildCategory);
882         }
883         currentCategory.delete();
884     }
885
886 }
887
Popular Tags