1 package org.jahia.admin.categories; 2 3 import javax.servlet.http.HttpServletRequest ; 4 import javax.servlet.http.HttpServletResponse ; 5 import javax.servlet.http.HttpSession ; 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 ; 15 import javax.servlet.ServletException ; 16 import java.util.Set ; 17 import org.jahia.services.sites.SiteLanguagesPersistance; 18 import org.jahia.exceptions.JahiaException; 19 import java.util.Locale ; 20 import org.jahia.bin.Jahia; 21 import javax.swing.tree.DefaultMutableTreeNode ; 22 import javax.swing.tree.DefaultTreeModel ; 23 import javax.swing.JTree ; 24 import org.jahia.services.categories.Category; 25 import java.util.ArrayList ; 26 import org.jahia.utils.GUITreeTools; 27 import java.util.Map ; 28 import java.util.TreeMap ; 29 import java.util.Iterator ; 30 import org.jahia.utils.LanguageCodeConverters; 31 import javax.swing.tree.MutableTreeNode ; 32 import java.util.Properties ; 33 import org.jahia.security.license.License; 34 import org.jahia.data.beans.RequestBean; 35 import org.jahia.gui.GuiBean; 36 37 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 JSP_PATH = JahiaAdministration.JSP_PATH; 52 private static final String CURRENTCATEGORY_SESSIONKEY = 53 "currentCategoryKey"; 54 private static final String CURRENTCATEGORYCHILDS_SESSIONKEY = 55 "currentCategoryChilds"; 56 private static final String CATEGORYTREE_SESSIONKEY = "categoryTree"; 57 58 private JahiaSite site; 59 private JahiaUser user; 60 private ServicesRegistry sReg; 61 private License coreLicense; 62 63 71 public ManageCategories (HttpServletRequest request, 72 HttpServletResponse response, 73 HttpSession session) 74 throws Throwable { 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 String dspMsg = JahiaResourceBundle.getAdminResource( 86 "org.jahia.admin.JahiaDisplayMessage.invalidLicenseKey.label", 87 jParams, jParams.getLocale()); 88 request.setAttribute("jahiaDisplayMessage", dspMsg); 89 JahiaAdministration.doRedirect(request, response, session, 91 JSP_PATH + "menu.jsp"); 92 return; 93 } 94 95 userRequestDispatcher(request, response, session); 96 } 98 107 private void userRequestDispatcher (HttpServletRequest request, 108 HttpServletResponse response, 109 HttpSession session) 110 throws Throwable { 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 operation = request.getParameter("sub"); 119 120 sReg = ServicesRegistry.getInstance(); 121 122 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 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 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 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 } 177 private void displayCategories (HttpServletRequest request, 179 HttpServletResponse response, 180 HttpSession session) 181 throws IOException , ServletException { 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 curCategoryKey = request.getParameter("currentCategoryKey"); 191 if (curCategoryKey != null) { 192 session.setAttribute(CURRENTCATEGORY_SESSIONKEY, curCategoryKey); 193 } 194 195 Set allLanguageCodes = SiteLanguagesPersistance.getInstance(). 196 getAllSitesLanguages(); 197 198 Locale [] availableLocales = Locale.getAvailableLocales(); 199 200 JTree tree = null; 201 if (request.getParameterMap().containsKey("guitree")) { 202 tree = (JTree ) session.getAttribute( 205 CATEGORYTREE_SESSIONKEY); 206 } else { 207 session.removeAttribute(CATEGORYTREE_SESSIONKEY); 208 } 209 if (tree == null) { 210 DefaultMutableTreeNode top = 212 new DefaultMutableTreeNode (Category.getRootCategory(), true); 213 DefaultTreeModel treeModel = new DefaultTreeModel (top, true); 214 tree = new JTree (treeModel); 215 buildCategoryTree(top, Category.getRootCategory(), null); 216 session.setAttribute(CATEGORYTREE_SESSIONKEY, tree); 217 } 218 219 GUITreeTools.updateGUITree(tree, request); 220 221 ArrayList flatCategoryList = null; 222 if (tree != null) { 223 DefaultMutableTreeNode rootNode = 224 (DefaultMutableTreeNode ) tree.getModel().getRoot(); 225 if (rootNode != null) { 226 flatCategoryList = GUITreeTools.getFlatTree(tree, rootNode); 227 } 228 } 229 230 String currentCategoryKey = (String ) session.getAttribute( 232 CURRENTCATEGORY_SESSIONKEY); 233 if (currentCategoryKey == null) { 234 currentCategoryKey = Category.getRootCategory().getKey(); 235 session.setAttribute(CURRENTCATEGORY_SESSIONKEY, 236 currentCategoryKey); 237 } 238 Category currentCategory = Category.getCategory(currentCategoryKey); 240 if (currentCategory == null) { 241 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 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 private void commitChanges (HttpServletRequest request, 274 HttpServletResponse response, 275 HttpSession session) 276 throws IOException , ServletException { 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 allLanguageCodes = SiteLanguagesPersistance.getInstance(). 289 getAllSitesLanguages(); 290 291 String 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 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 } 310 private void displayEditCategory (HttpServletRequest request, 312 HttpServletResponse response, 313 HttpSession session, 314 String currentCategoryKey) 315 throws IOException , ServletException { 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 allLanguageCodes = SiteLanguagesPersistance.getInstance(). 329 getAllSitesLanguages(); 330 Map titleMap = new TreeMap (); 331 Properties categoryProperties = new Properties (); 332 333 if (currentCategoryKey != null) { 334 Category currentCategory = Category.getCategory( 335 currentCategoryKey); 336 session.setAttribute(CURRENTCATEGORY_SESSIONKEY, 337 currentCategoryKey); 338 Iterator allLanguageCodeIter = allLanguageCodes.iterator(); 339 while (allLanguageCodeIter.hasNext()) { 340 String curLanguageCode = (String ) allLanguageCodeIter.next(); 341 titleMap.put(curLanguageCode, 342 currentCategory.getTitle( 343 LanguageCodeConverters. 344 languageCodeToLocale(curLanguageCode))); 345 } 346 categoryProperties = currentCategory.getProperties(); 347 } else { 348 Iterator allLanguageCodeIter = allLanguageCodes.iterator(); 349 while (allLanguageCodeIter.hasNext()) { 350 String curLanguageCode = (String ) allLanguageCodeIter.next(); 351 titleMap.put(curLanguageCode, ""); 352 } 353 } 354 355 Locale [] 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 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 private void commitEditCategory (HttpServletRequest request, 384 HttpServletResponse response, 385 HttpSession session) 386 throws IOException , ServletException { 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 allLanguageCodes = SiteLanguagesPersistance.getInstance(). 399 getAllSitesLanguages(); 400 401 String parentCategoryKey = request.getParameter("parentCategoryKey"); 402 Category parentCategory = Category.getCategory(parentCategoryKey); 403 404 String categoryKey = request.getParameter("categoryKey"); 405 boolean addingNewCategory = false; 406 Category currentCategory = null; 407 if (categoryKey == null) { 408 categoryKey = (String ) session.getAttribute( 410 CURRENTCATEGORY_SESSIONKEY); 411 currentCategory = Category.getCategory(categoryKey); 412 } else { 413 addingNewCategory = true; 415 if (Category.getCategory(categoryKey) != null) { 416 String 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 parameterMap = request.getParameterMap(); 429 Iterator keyIter = parameterMap.keySet().iterator(); 430 while (keyIter.hasNext()) { 431 String curKey = (String ) keyIter.next(); 432 if (curKey.startsWith("title_")) { 433 String languageCode = curKey.substring("title_".length()); 434 String 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 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 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 private void displayDeleteCategory (HttpServletRequest request, 464 HttpServletResponse response, 465 HttpSession session) 466 throws IOException , ServletException { 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 curCategoryKey = request.getParameter("currentCategoryKey"); 476 String parentCategoryKey = request.getParameter("parentCategoryKey"); 477 Category currentCategory = Category.getCategory(curCategoryKey); 478 479 Locale [] availableLocales = Locale.getAvailableLocales(); 480 481 JTree tree = null; 482 DefaultMutableTreeNode top = 484 new DefaultMutableTreeNode (currentCategory, true); 485 DefaultTreeModel treeModel = new DefaultTreeModel (top, true); 486 tree = new JTree (treeModel); 487 488 buildCategoryTree(top, currentCategory, null); 489 490 GUITreeTools.expandAllPath(tree, top); 491 492 ArrayList flatCategoryList = null; 493 if (tree != null) { 494 DefaultMutableTreeNode rootNode = 495 (DefaultMutableTreeNode ) tree.getModel().getRoot(); 496 if (rootNode != null) { 497 flatCategoryList = GUITreeTools.getFlatTree(tree, rootNode); 498 } 499 } 500 501 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 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 private void commitDeleteCategory (HttpServletRequest request, 529 HttpServletResponse response, 530 HttpSession session) 531 throws IOException , ServletException { 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 curCategoryKey = request.getParameter("targetCategoryKey"); 541 Category currentCategory = Category.getCategory(curCategoryKey); 542 543 recursiveDeleteCategory(currentCategory); 544 545 Locale [] availableLocales = Locale.getAvailableLocales(); 546 547 String 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 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 private void commitEditProperty (HttpServletRequest request, 572 HttpServletResponse response, 573 HttpSession session) 574 throws IOException , ServletException { 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 allLanguageCodes = SiteLanguagesPersistance.getInstance(). 587 getAllSitesLanguages(); 588 589 String parentCategoryKey = request.getParameter("parentCategoryKey"); 590 Category parentCategory = Category.getCategory(parentCategoryKey); 591 Category currentCategory = null; 592 593 String categoryKey = (String ) session.getAttribute( 594 CURRENTCATEGORY_SESSIONKEY); 595 currentCategory = Category.getCategory(categoryKey); 596 597 Map parameterMap = request.getParameterMap(); 598 Iterator keyIter = parameterMap.keySet().iterator(); 599 while (keyIter.hasNext()) { 600 String curKey = (String ) keyIter.next(); 601 if (curKey.startsWith("setProperty_")) { 602 String propertyName = curKey.substring("setProperty_". 603 length()); 604 String 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 newPropertyName = request.getParameter("newPropertyName"); 616 if (newPropertyName != null) { 617 if (!"".equals(newPropertyName)) { 618 String 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 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 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 private void commitDeleteProperty (HttpServletRequest request, 652 HttpServletResponse response, 653 HttpSession session) 654 throws IOException , ServletException { 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 allLanguageCodes = SiteLanguagesPersistance.getInstance(). 667 getAllSitesLanguages(); 668 669 String parentCategoryKey = request.getParameter("parentCategoryKey"); 670 Category parentCategory = Category.getCategory(parentCategoryKey); 671 Category currentCategory = null; 672 673 String categoryKey = (String ) session.getAttribute( 674 CURRENTCATEGORY_SESSIONKEY); 675 currentCategory = Category.getCategory(categoryKey); 676 677 String targetPropertyName = request.getParameter("property"); 678 if (targetPropertyName != null) { 679 currentCategory.removeProperty(targetPropertyName); 680 } 681 682 String 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 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 private void displayMoveCategory (HttpServletRequest request, 703 HttpServletResponse response, 704 HttpSession session) 705 throws IOException , ServletException { 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 request.setAttribute("currentRequest", new RequestBean(new GuiBean(jParams), jParams)); 715 716 try { 717 718 String curCategoryKey = request.getParameter("currentCategoryKey"); 719 if (curCategoryKey != null) { 720 session.setAttribute(CURRENTCATEGORY_SESSIONKEY, curCategoryKey); 721 } 722 String parentCategoryKey = request.getParameter("parentCategoryKey"); 723 724 Set allLanguageCodes = SiteLanguagesPersistance.getInstance(). 725 getAllSitesLanguages(); 726 727 Locale [] availableLocales = Locale.getAvailableLocales(); 728 729 JTree tree = null; 730 if (request.getParameterMap().containsKey("guitree")) { 731 tree = (JTree ) session.getAttribute( 734 CATEGORYTREE_SESSIONKEY); 735 } else { 736 session.removeAttribute(CATEGORYTREE_SESSIONKEY); 737 } 738 if (tree == null) { 739 DefaultMutableTreeNode top = 741 new DefaultMutableTreeNode (Category.getRootCategory(), true); 742 DefaultTreeModel treeModel = new DefaultTreeModel (top, true); 743 tree = new JTree (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 flatCategoryList = null; 752 if (tree != null) { 753 DefaultMutableTreeNode rootNode = 754 (DefaultMutableTreeNode ) tree.getModel().getRoot(); 755 if (rootNode != null) { 756 flatCategoryList = GUITreeTools.getFlatTree(tree, rootNode); 757 } 758 } 759 760 String currentCategoryKey = (String ) 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 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 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 private void commitMoveCategory (HttpServletRequest request, 800 HttpServletResponse response, 801 HttpSession session) 802 throws IOException , ServletException { 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 parentCategoryKey = request.getParameter("parentCategoryKey"); 814 Category parentCategory = Category.getCategory(parentCategoryKey); 815 816 String categoryKey = request.getParameter("currentCategoryKey"); 817 Category currentCategory = Category.getCategory(categoryKey); 818 819 String newParentKey = request.getParameter("newParentKey"); 821 Category newParentCategory = Category.getCategory(newParentKey); 822 823 parentCategory.removeChildObjectKey(currentCategory.getObjectKey()); 825 newParentCategory.addChildObjectKey(currentCategory.getObjectKey()); 826 827 829 request.setAttribute("warningMsg", ""); 830 String 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 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 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 childCategories = currentCategory.getChildCategories(); 859 Iterator 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 newNode = new DefaultMutableTreeNode ( 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 childCategories = currentCategory.getChildCategories(); 878 Iterator 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 |