1 13 package org.jahia.engines.containerlistproperties; 14 15 import java.util.Enumeration ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Properties ; 19 import java.util.Set ; 20 import java.util.Vector ; 21 22 import javax.servlet.http.HttpSession ; 23 24 import org.apache.regexp.RE; 25 import org.apache.regexp.RESyntaxException; 26 27 import org.jahia.content.ContentContainerListsXRefManager; 28 import org.jahia.data.JahiaData; 29 import org.jahia.data.containers.JahiaContainer; 30 import org.jahia.data.containers.JahiaContainerDefinition; 31 import org.jahia.data.containers.JahiaContainerList; 32 import org.jahia.data.containers.JahiaContentContainerFacade; 33 import org.jahia.data.events.JahiaEvent; 34 import org.jahia.data.fields.JahiaField; 35 import org.jahia.data.fields.JahiaFieldDefinition; 36 import org.jahia.data.fields.LoadFlags; 37 import org.jahia.engines.EngineToolBox; 38 import org.jahia.engines.JahiaEngine; 39 import org.jahia.engines.lock.LockEngine; 40 import org.jahia.engines.rights.ManageRights; 41 import org.jahia.exceptions.JahiaException; 42 import org.jahia.exceptions.JahiaForbiddenAccessException; 43 import org.jahia.exceptions.JahiaSessionExpirationException; 44 import org.jahia.params.ParamBean; 45 import org.jahia.registries.JahiaContainerDefinitionsRegistry; 46 import org.jahia.registries.ServicesRegistry; 47 import org.jahia.services.acl.JahiaBaseACL; 48 import org.jahia.services.audit.LoggingEventListener; 49 import org.jahia.services.containers.ContentContainerList; 50 import org.jahia.services.lock.LockKey; 51 import org.jahia.services.lock.LockService; 52 import org.jahia.services.pages.ContentPage; 53 import org.jahia.services.usermanager.JahiaUser; 54 import org.jahia.services.cache.HtmlCache; 55 import org.jahia.services.cache.CacheFactory; 56 import org.jahia.content.ContentContainerListKey; 57 import org.jahia.engines.categories.ManageCategories; 58 59 60 public class ContainerListProperties_Engine implements JahiaEngine { 61 62 63 public static final String ENGINE_NAME = "containerlistproperties"; 64 65 66 private static ContainerListProperties_Engine instance = null; 67 68 69 private static final org.apache.log4j.Logger logger = 70 org.apache.log4j.Logger.getLogger (ContainerListProperties_Engine.class); 71 72 private static final String TEMPLATE_JSP = "container_list_properties"; 73 74 private EngineToolBox toolBox; 75 76 79 private ContainerListProperties_Engine () { 80 logger.debug ("***** Starting " + ContainerListProperties_Engine.class.getName () + 81 " engine *****"); 82 toolBox = EngineToolBox.getInstance (); 83 } 85 90 public static ContainerListProperties_Engine getInstance () { 91 if (instance == null) { 92 instance = new ContainerListProperties_Engine (); 93 } 94 return instance; 95 } 96 97 100 public boolean authoriseRender (ParamBean jParams) { 101 return toolBox.authoriseRender (jParams); 102 } 103 104 107 public String renderLink (ParamBean jParams, Object theObj) 108 throws JahiaException { 109 Properties extraParams = new Properties (); 110 String params = EMPTY_STRING; 111 ContentContainerList contentContainerList = (ContentContainerList) 112 theObj; 113 JahiaContainerDefinition containerListDefinition = 114 JahiaContainerDefinitionsRegistry.getInstance (). 115 getDefinition (contentContainerList.getContainerListDefinitionID ()); 116 String containerListName = containerListDefinition.getName (); 117 Vector ctnIDs = ServicesRegistry.getInstance (). 118 getJahiaContainersService ().getctnidsInList ( 119 contentContainerList.getID (), jParams.getEntryLoadRequest ()); 120 String scrollStr = Integer.toString (ctnIDs.size ()) + "_0"; 121 if (scrollStr != null) { 122 if (scrollStr.length () > 0) { 123 extraParams.setProperty ("ctnscroll_" + containerListName, 124 scrollStr); 125 } 126 } 127 params += "?mode=display"; 128 params += "&clid=" + contentContainerList.getID (); 129 return jParams.composeEngineUrl (ENGINE_NAME, extraParams, params); 130 } 131 132 135 public boolean needsJahiaData (ParamBean jParams) { 136 return false; 137 } 138 139 145 public void handleActions (ParamBean jParams, JahiaData jData) 146 throws JahiaException, 147 JahiaSessionExpirationException, 148 JahiaForbiddenAccessException { 149 HashMap engineMap = initEngineMap (jParams); 151 152 JahiaContainerList theContainerList = (JahiaContainerList) engineMap. 154 get ("theContainerList"); 155 JahiaUser theUser = jParams.getUser (); 156 157 if (theContainerList.checkAdminAccess (theUser)) { 158 engineMap.put ("adminAccess", Boolean.TRUE); 159 engineMap.put ("enableRightView", Boolean.TRUE); 160 engineMap.put ("writeAccess", Boolean.TRUE); 161 162 } else if (theContainerList.checkWriteAccess (theUser)) { 163 engineMap.put ("writeAccess", Boolean.TRUE); 164 } 165 166 if (engineMap.get ("writeAccess") != null) { 167 LockService lockRegistry = ServicesRegistry.getInstance (). 169 getLockService (); 170 if (jParams.settings ().areLocksActivated ()) { 171 LockKey lockKey = LockKey.composeLockKey (LockKey. 172 UPDATE_CONTAINERLIST_TYPE, 173 theContainerList.getID (), theContainerList.getPageID ()); 174 if (lockRegistry.acquire (lockKey, jParams.getUser (), 175 jParams.getSessionID (), 176 jParams.getSession (). 177 getMaxInactiveInterval ())) { 178 flushPageCacheThatDisplayContainerList (jParams, theContainerList); 180 181 processLastScreen (jParams, engineMap); 182 processCurrentScreen (jParams, engineMap); 183 184 } else { 186 LockEngine.getInstance ().redirect (jParams, engineMap, 189 lockKey); 190 } 191 } 192 } else { 194 throw new JahiaForbiddenAccessException (); 195 } 196 toolBox.displayScreen (jParams, engineMap); 198 199 } 200 201 202 207 public final String getName () { 208 return ENGINE_NAME; 209 } 210 211 212 217 public void processLastScreen (ParamBean jParams, HashMap engineMap) 218 throws JahiaException, 219 JahiaForbiddenAccessException { 220 logger.debug ("started"); 221 222 JahiaContainerList theContainerList = (JahiaContainerList) engineMap. 224 get ("theContainerList"); 225 226 String lastScreen = jParams.getRequest ().getParameter ("lastscreen"); 229 engineMap.put ("lastscreen", lastScreen); 230 if (lastScreen == null) { 231 lastScreen = "edit"; 232 } 233 234 int mode = JahiaEngine.UPDATE_MODE; 236 237 if (lastScreen.equals ("edit")) { 239 if (!updateContainerListData (jParams, engineMap)) { 240 engineMap.put ("screen", lastScreen); 242 engineMap.put ("jspSource", TEMPLATE_JSP); 243 } 244 } else if (lastScreen.equals ("rightsMgmt") || 245 lastScreen.equals ("ctneditview_rights")) { 246 if (engineMap.get ("adminAccess") != null) { 247 engineMap.put ("logObjectType", 248 Integer.toString (LoggingEventListener. 249 CONTAINERLIST_TYPE)); 250 engineMap.put ("logObject", theContainerList); 251 if (lastScreen.equals ("rightsMgmt")) { 252 ManageRights.getInstance ().handleActions (jParams, mode, 253 engineMap, 254 theContainerList.getAclID ()); 255 } else { 256 HashMap acls = (HashMap ) engineMap.get ("fieldAcls"); 257 if (acls == null) 258 return; 259 String val = jParams.getRequest ().getParameter ( 260 "lastaclfield"); 261 Integer I = null; 262 if (val != null) { 263 I = (Integer ) acls.get (val); 264 if (I != null) { 265 ManageRights.getInstance ().handleActions (jParams, 266 mode, engineMap, 267 I.intValue ()); 268 } 269 } 270 } 271 } else { 272 throw new JahiaForbiddenAccessException (); 273 } 274 } else if (lastScreen.equals ("categories")) { 275 ManageCategories.getInstance ().handleActions (jParams, mode, 276 engineMap, new ContentContainerListKey (theContainerList.getID ()), 277 theContainerList.getDefinition (), false); 278 } 279 } 280 281 286 public void processCurrentScreen (ParamBean jParams, HashMap engineMap) 287 throws JahiaException, 288 JahiaForbiddenAccessException { 289 logger.debug ("Started"); 290 291 JahiaContainerList theContainerList = (JahiaContainerList) engineMap. 293 get ("theContainerList"); 294 295 String theScreen = (String ) engineMap.get ("screen"); 298 299 int mode = JahiaEngine.LOAD_MODE; 301 302 LockKey lockKey = LockKey.composeLockKey (LockKey. 304 UPDATE_CONTAINERLIST_TYPE, 305 theContainerList.getID (), 306 theContainerList.getPageID ()); 307 LockService lockRegistry = ServicesRegistry.getInstance (). 308 getLockService (); 309 311 if (theScreen.equals ("edit")) { 313 loadContainerListData (jParams, engineMap); 314 315 } else if (theScreen.equals ("logs")) { 316 if (engineMap.get ("lastscreen") != null && 317 engineMap.get ("lastscreen").equals ("edit")) { 318 saveContainersRank (jParams, engineMap); 320 } 321 toolBox.loadLogData (jParams, 322 LoggingEventListener.CONTAINERLIST_TYPE, 323 engineMap); 324 325 } else if (theScreen.equals ("categories")) { 326 ManageCategories.getInstance ().handleActions (jParams, mode, 327 engineMap, new ContentContainerListKey (theContainerList.getID ()), 328 theContainerList.getDefinition (), false); 329 330 } else if (theScreen.equals ("rightsMgmt") || 331 theScreen.equals ("ctneditview_rights")) { 332 333 if (engineMap.get ("lastscreen") != null && 334 engineMap.get ("lastscreen").equals ("edit")) { 335 saveContainersRank (jParams, engineMap); 337 } 338 if (engineMap.get ("adminAccess") != null) { 339 if (theScreen.equals ("rightsMgmt")) { 340 ManageRights.getInstance ().handleActions (jParams, mode, 341 engineMap, 342 theContainerList.getAclID ()); 343 } else { 344 logger.debug ("Step1"); 345 346 HashMap acls = (HashMap ) engineMap.get ("fieldAcls"); 347 if (acls == null) 348 return; 349 String val = jParams.getRequest ().getParameter ( 350 "aclfieldname"); 351 logger.debug ("aclfieldname=" + val); 352 353 Integer I = null; 354 if (val == null) { 355 val = (String ) engineMap.get ("fieldAclDefaultField"); 357 } 358 if (val != null) { 359 engineMap.put ("aclfieldname", val); 361 engineMap.put ("fieldAclDefaultField", val); 362 I = (Integer ) acls.get (val); 363 if (I != null) { 364 logger.debug ("Step3"); 365 ManageRights.getInstance ().handleActions (jParams, 366 mode, engineMap, 367 I.intValue ()); 368 } 369 } else { 370 engineMap.put ("aclfieldname", EMPTY_STRING); 371 return; 372 } 373 } 374 } else { 375 throw new JahiaForbiddenAccessException (); 376 } 377 } else if (theScreen.equals ("save") || theScreen.equals ("apply")) { 378 379 if (jParams.settings ().areLocksActivated () && 382 lockRegistry.isStealedInContext (lockKey, jParams.getUser (), 383 jParams.getSessionID ())) { 384 engineMap.put ("screen", 385 jParams.getRequest ().getParameter ("lastscreen")); 386 engineMap.put ("jspSource", "apply"); 387 return; 388 } 389 391 mode = JahiaEngine.SAVE_MODE; 392 ServicesRegistry.getInstance ().getJahiaContainersService (). 394 saveContainerListInfo (theContainerList, 0); 395 if (engineMap.get ("lastscreen") != null && 396 engineMap.get ("lastscreen").equals ("edit")) { 397 saveContainersRank (jParams, engineMap); 399 } 400 Integer aclID = null; 402 if (engineMap.get ("adminAccess") != null) { 403 if ("ctneditview_rights".equals (engineMap.get ("lastscreen"))) { 405 String aclfieldname = jParams.getRequest ().getParameter ( 406 "aclfieldname"); 407 logger.debug ("Save container list field ACL : " + 408 aclfieldname); 409 HashMap acls = (HashMap ) engineMap.get ("fieldAcls"); 411 if (acls != null) { 412 aclID = (Integer ) acls.get (aclfieldname); 413 ManageRights.getInstance ().handleActions (jParams, mode, 414 engineMap, aclID.intValue ()); 415 } 416 } else { 417 ManageRights.getInstance ().handleActions (jParams, mode, 419 engineMap, 420 theContainerList.getAclID ()); 421 } 422 } 423 424 ManageCategories.getInstance ().handleActions (jParams, mode, 426 engineMap, 427 new ContentContainerListKey (theContainerList.getID ()), 428 theContainerList.getDefinition (), false); 429 430 flushPageCacheThatDisplayContainerList (jParams, theContainerList); 431 432 JahiaEvent theEvent = new JahiaEvent (this, jParams, 434 theContainerList); 435 ServicesRegistry.getInstance ().getJahiaEventService (). 436 fireSetContainerListProperties (theEvent); 437 if (theScreen.equals ("apply")) { 438 engineMap.put ("screen", 439 jParams.getRequest ().getParameter ("lastscreen")); 440 String lastScreen = (String ) engineMap.get ("lastscreen"); 441 if ("ctneditview_rights".equals (lastScreen)) { 442 ManageRights.getInstance ().handleActions (jParams, 443 JahiaEngine.LOAD_MODE, 444 engineMap, aclID.intValue ()); 445 } else if ("rightsMgmt".equals (lastScreen)) { 446 ManageRights.getInstance ().handleActions (jParams, 447 JahiaEngine.LOAD_MODE, 448 engineMap, theContainerList.getAclID ()); 449 } 450 } 451 else { 453 if (jParams.settings ().areLocksActivated ()) { 454 lockRegistry.release (lockKey, jParams.getUser (), 455 jParams.getSessionID ()); 456 } 457 } 458 logger.debug ("Container list saved"); 460 } 461 else if (theScreen.equals ("cancel")) { 463 if (jParams.settings ().areLocksActivated ()) { 464 lockRegistry.release (lockKey, jParams.getUser (), 465 jParams.getSessionID ()); 466 flushPageCacheThatDisplayContainerList (jParams, theContainerList); 467 } 468 } 469 471 } 472 473 private void flushPageCacheThatDisplayContainerList (ParamBean jParams, 474 JahiaContainerList theContainerList) 475 throws JahiaException { 476 HtmlCache htmlCache = CacheFactory.getHtmlCache (); 478 if (htmlCache == null) 479 logger.warn ("Could not get the HTML cache instance!!"); 480 481 if (theContainerList != null) { 485 Set containerPageRefs = ContentContainerListsXRefManager. 486 getInstance (). 487 getAbsoluteContainerListPageIDs ( 488 theContainerList.getID ()); 489 if (containerPageRefs != null) { 490 Iterator pageRefIDs = containerPageRefs.iterator (); 491 while (pageRefIDs.hasNext ()) { 492 Integer curPageID = (Integer ) pageRefIDs.next (); 493 if (htmlCache != null) 494 htmlCache.invalidatePageEntries (curPageID.toString (),jParams.getEntryLoadRequest().getWorkflowState()); 495 } 496 } else { 497 logger.debug ("Why is cross ref list empty ?"); 498 } 499 } else { 500 logger.debug ("Couldn't retrieve parent containerList, why is that ?"); 501 } 502 503 if (htmlCache != null) 506 htmlCache.invalidatePageEntries (Integer.toString (jParams.getPageID ()),jParams.getEntryLoadRequest().getWorkflowState()); 507 } 508 509 520 private HashMap initEngineMap (ParamBean jParams) 521 throws JahiaException, 522 JahiaSessionExpirationException { 523 524 HashMap engineMap = new HashMap (); 525 JahiaContainerList theContainerList = null; 526 527 String theScreen = jParams.getRequest ().getParameter ("screen"); 528 String cListIDStr = jParams.getRequest ().getParameter ("clid"); 529 int cListID = 0; 530 try { 531 cListID = Integer.parseInt (cListIDStr); 532 } catch (Exception e) { 533 throw new JahiaException ("Error in parameters", 534 "ContainerListProperties_Engine : error in parameters", 535 JahiaException.PARAMETER_ERROR, 536 JahiaException.CRITICAL_SEVERITY); 537 } 538 539 HttpSession theSession = jParams.getSession (); 542 543 if (theScreen != null) { 544 engineMap = (HashMap ) theSession.getAttribute ( 546 "jahia_session_engineMap"); 547 548 if (engineMap == null) { 556 throw new JahiaSessionExpirationException (); 557 } 558 559 theContainerList = (JahiaContainerList) engineMap.get ( 560 "theContainerList"); 561 } else { 562 theScreen = "edit"; 563 engineMap = new HashMap (); 565 theContainerList = ServicesRegistry.getInstance (). 566 getJahiaContainersService ().loadContainerList ( 567 cListID, LoadFlags.NOTHING, jParams); 568 engineMap.put ("theContainerList", theContainerList); 569 570 Properties props = theContainerList.getProperties (); 572 Enumeration enumeration = props.propertyNames (); 573 String p = EMPTY_STRING; 574 while (enumeration.hasMoreElements ()) { 575 p = (String ) enumeration.nextElement (); 576 logger.debug ("Properties : " + props.getProperty (p)); 577 } 578 579 Vector containers = new Vector (); 581 Enumeration containerList = theContainerList.getContainers (); 582 JahiaContainer theContainer = null; 583 Vector fieldInfoToDisplay = new Vector (); 585 String fieldInfo = EMPTY_STRING; 586 Vector sortInfo4Date = new Vector (); 588 String sortInfo = EMPTY_STRING; 589 int cnt = 0; 590 591 while (containerList.hasMoreElements ()) { 592 cnt++; 593 theContainer = (JahiaContainer) containerList.nextElement (); 594 theContainer.setLanguageCode (jParams.getEntryLoadRequest (). 597 getFirstLocale (true).toString ()); 598 theContainer.fieldsStructureCheck (jParams); 599 containers.add (theContainer); 600 fieldInfo = getFieldInfoToDisplay (0, theContainer, jParams); 601 if (fieldInfo != null) { 602 if (fieldInfo.equals (EMPTY_STRING)) { 603 fieldInfo = "-- Item " + cnt + " --"; 604 } 605 } else { 606 fieldInfo = "-- Item " + cnt + " --"; 607 } 608 fieldInfoToDisplay.add (fieldInfo); 609 610 sortInfo = getSortInfo4Date (0, theContainer, jParams); 611 if (sortInfo != null) { 612 sortInfo4Date.add (sortInfo); 613 } 614 } 615 616 JahiaField theField = null; 617 Enumeration fieldsList = null; 618 Vector fields = null; 619 620 if (theContainer == null) { 621 ContentPage contentPage = 624 ContentPage.getPage (theContainerList.getPageID (), false); 625 JahiaContentContainerFacade contentContainerFacade = 626 new JahiaContentContainerFacade (0, 627 contentPage.getJahiaID (), 628 theContainerList.getPageID (), 629 theContainerList.getID (), 630 theContainerList.getctndefid (), 631 0, 633 jParams, 634 jParams.getSite ().getLanguageSettingsAsLocales (false)); 635 if (contentContainerFacade.getContainers ().hasMoreElements ()) { 636 theContainer = (JahiaContainer) 637 contentContainerFacade.getContainers (). 638 nextElement (); 639 if (theContainer != null) { 640 theContainerList.addContainer (theContainer); 641 } 642 } 643 } 644 645 if (theContainer != null) { 646 fieldsList = theContainer.getFields (); 647 648 fields = new Vector (); 649 650 while (fieldsList.hasMoreElements ()) { 651 theField = (JahiaField) fieldsList.nextElement (); 652 fields.add (theField); 653 } 654 } 655 656 engineMap.put ("containers", containers); 657 engineMap.put ("fields", fields); 658 engineMap.put ("cursorField", new Integer (0)); 659 engineMap.put ("fieldInfoToDisplay", fieldInfoToDisplay); 660 engineMap.put ("containerSelected", new Integer (0)); 661 662 HashMap fieldAcls = buildFieldDefAcls (theContainerList, jParams, 664 engineMap); 665 engineMap.put ("fieldAcls", fieldAcls); 666 } 667 668 if (theContainerList == null) { 669 throw new JahiaException ("ContainerList not found", 670 "ContainerListProperties_Engine : ContainerList not found", 671 JahiaException.PARAMETER_ERROR, 672 JahiaException.CRITICAL_SEVERITY); 673 } 674 675 Properties extraParams = new Properties (); 676 String containerListName = theContainerList.getDefinition ().getName (); 677 String scrollStr = jParams.getParameter ("ctnscroll_" + 678 containerListName); 679 if (scrollStr != null) { 680 if (scrollStr.length () > 0) { 681 extraParams.setProperty ("ctnscroll_" + containerListName, 682 scrollStr); 683 } 684 } 685 engineMap.put (RENDER_TYPE_PARAM, new Integer (JahiaEngine.RENDERTYPE_FORWARD)); 686 engineMap.put (ENGINE_NAME_PARAM, ENGINE_NAME); 687 engineMap.put (ENGINE_URL_PARAM, 688 jParams.composeEngineUrl (ENGINE_NAME, extraParams, 689 "&clid=" + 690 theContainerList.getID ())); 691 theSession.setAttribute ("jahia_session_engineMap", engineMap); 692 693 engineMap.put ("screen", theScreen); 695 if (theScreen.equals ("save")) { 696 engineMap.put ("jspSource", "close"); 697 } else if (theScreen.equals ("apply")) { 698 engineMap.put ("jspSource", "apply"); 699 } else if (theScreen.equals ("cancel")) { 700 engineMap.put ("jspSource", "close"); 701 } else { 702 engineMap.put ("jspSource", TEMPLATE_JSP); 703 } 704 720 engineMap.put ("doAddEditViewRight", new Boolean (true)); 721 722 if (theContainerList.getID () != 0) { 723 engineMap.put ("enableCategories", new Boolean (true)); 724 } 725 726 jParams.getRequest ().setAttribute ("engineTitle", 728 "Container List Settings"); 729 jParams.getRequest ().setAttribute ("org.jahia.engines.EngineHashMap", 730 engineMap); 731 732 return engineMap; 733 } 734 735 741 private void loadContainerListData (ParamBean jParams, HashMap engineMap) 742 { 744 } 746 747 759 private boolean updateContainerListData (ParamBean jParams, 760 HashMap engineMap) 761 throws JahiaException { 762 boolean okay = true; 763 765 767 String updMode = jParams.getRequest ().getParameter ("updMode"); 768 Vector containers = (Vector ) engineMap.get ("containers"); 769 Vector fieldInfoToDisplay = (Vector ) engineMap.get ("fieldInfoToDisplay"); 770 Vector sortInfo4Date = (Vector ) engineMap.get ("sortInfo4Date"); 771 772 if (updMode != null) { 773 if (updMode.equals ("cursor")) { 775 String curFieldStr = jParams.getRequest ().getParameter ( 776 "cursorField"); 777 int curField = 0; 778 if (curFieldStr != null) { 779 try { 780 curField = Integer.parseInt (curFieldStr); 781 } catch (NumberFormatException nfe) { 782 ; 783 } 784 } 785 786 fieldInfoToDisplay = new Vector (); 787 String fieldInfo = EMPTY_STRING; 788 sortInfo4Date = new Vector (); 789 String sortInfo = EMPTY_STRING; 790 791 for (int i = 0; i < containers.size (); i++) { 792 fieldInfo = getFieldInfoToDisplay (curField, 793 (JahiaContainer) containers.get (i), jParams); 794 if (fieldInfo.equals (EMPTY_STRING)) { 795 fieldInfo = "-- Item " + (i + 1) + " --"; 796 } 797 fieldInfoToDisplay.add (fieldInfo); 798 799 sortInfo = getSortInfo4Date (curField, 800 (JahiaContainer) 801 containers.get (i), jParams); 802 if (sortInfo != null) { 803 sortInfo4Date.add (sortInfo); 804 } 805 } 806 807 engineMap.put ("fieldInfoToDisplay", fieldInfoToDisplay); 808 engineMap.put ("sortInfo4Date", sortInfo4Date); 809 engineMap.put ("cursorField", new Integer (curField)); 810 811 } 812 814 878 879 else if (updMode.equals ("automatic")) { 881 engineMap.put ("containerSelected", new Integer (0)); 882 String containerSort = jParams.getRequest ().getParameter ( 883 "autRank"); 884 engineMap.put ("automatic", containerSort); 885 shell_sort (jParams, engineMap, containerSort); 886 } 887 } 889 890 return okay; 891 } 892 893 899 private void saveContainersRank (ParamBean jParams, HashMap engineMap) 900 throws JahiaException { 901 Vector containers_beforeSort = (Vector ) engineMap.get ("containers"); 902 Vector containers = new Vector (); 903 904 String [] manRank = jParams.getRequest ().getParameterValues ("manRank"); 905 if (manRank != null) { 906 int num = 0; 907 for (int j = 0; j < manRank.length; j++) { 908 num = Integer.parseInt (manRank[j]); 909 containers.add (containers_beforeSort.get (num)); 911 } 912 } 913 int counter = containers.size (); 914 JahiaContainer c; 915 916 for (int i = 0; i < containers.size (); i++) { 919 c = (JahiaContainer) containers.get (i); 920 c.setRank (counter * (-1)); 921 ServicesRegistry.getInstance ().getJahiaContainersService (). 922 saveContainerInfo (c, 0, 0, jParams); 923 counter--; 924 } 925 926 } 927 928 934 private String getFieldInfoToDisplay (int cursorField, 935 JahiaContainer theContainer, 936 ParamBean jParams) 937 throws JahiaException { 938 String fieldInfo = EMPTY_STRING; 939 int counter = 0; 940 941 Enumeration fList = theContainer.getFields (); 942 943 while (fList.hasMoreElements ()) { 944 JahiaField f = (JahiaField) fList.nextElement (); 945 f = ServicesRegistry.getInstance (). 946 getJahiaFieldService ().loadField (f.getID (), LoadFlags.ALL, 947 jParams); 948 949 if (f != null && (counter == cursorField)) { 950 fieldInfo = f.getFieldContent4Ranking (); 951 952 1016 } 1017 counter++; 1018 } 1019 1020 return fieldInfo; 1021 } 1022 1023 1029 private String getSortInfo4Date (int cursorField, 1030 JahiaContainer theContainer, 1031 ParamBean jParams) 1032 throws JahiaException { 1033 String sortInfo = null; 1034 int counter = 0; 1035 1036 Enumeration fList = theContainer.getFields (); 1037 while (fList.hasMoreElements ()) { 1038 JahiaField f = (JahiaField) fList.nextElement (); 1039 1042 if (counter == cursorField) { 1043 if (f.getType () == 4) { 1044 sortInfo = (String ) f.getObject (); 1045 } 1046 } 1047 counter++; 1048 } 1049 1050 return sortInfo; 1051 } 1052 1053 1065 private void shell_sort (ParamBean jParams, HashMap engineMap, 1066 String containerSort) 1067 throws JahiaException { 1068 Vector containers = (Vector ) engineMap.get ("containers"); 1069 Vector fieldInfoToDisplay = (Vector ) engineMap.get ("fieldInfoToDisplay"); 1070 Vector sortInfo4Date = (Vector ) engineMap.get ("sortInfo4Date"); 1071 1072 int n = containers.size (); 1073 int incr; 1074 int j; 1075 JahiaContainer c = null; 1076 String str = EMPTY_STRING; 1077 boolean flag = false; 1078 1079 for (incr = n / 2; incr > 0; incr = incr / 2) { 1080 for (int i = incr; i < n; i++) { 1081 c = (JahiaContainer) containers.get (i); 1082 str = (String ) fieldInfoToDisplay.get (i); 1083 String str2 = EMPTY_STRING; 1084 String str4Test = str; 1085 String str4Test2 = EMPTY_STRING; 1086 if (sortInfo4Date != null && !sortInfo4Date.isEmpty ()) { 1087 str2 = (String ) sortInfo4Date.get (i); 1088 str4Test = str2; 1089 } 1091 1092 for (j = i; j >= incr; j = j - incr) { 1093 if (containerSort.equals ("insertAsc")) { 1094 if (c.getID () < 1095 ((JahiaContainer) containers.get (j - incr)).getID ()) { 1096 flag = true; 1097 } else { 1098 flag = false; 1099 } 1100 } else if (containerSort.equals ("insertDesc")) { 1101 if (c.getID () >= 1102 ((JahiaContainer) containers.get (j - incr)).getID ()) { 1103 flag = true; 1104 } else { 1105 flag = false; 1106 } 1107 } else if (containerSort.equals ("alphAsc")) { 1108 str4Test2 = (String ) fieldInfoToDisplay.get (j - incr); 1109 if (sortInfo4Date != null && !sortInfo4Date.isEmpty ()) { 1110 str4Test2 = (String ) sortInfo4Date.get (j - incr); 1111 } 1112 1113 try { 1114 Double d1 = new Double (str4Test); 1115 Double d2 = new Double (str4Test2); 1116 1117 int comp2 = d1.compareTo (d2); 1118 if (comp2 < 0) { 1119 flag = true; 1120 } else { 1121 flag = false; 1122 } 1123 } catch (NumberFormatException e) { 1124 String value = (String ) fieldInfoToDisplay.get (j - 1125 incr); 1126 try { 1127 str = (new RE ("<(.*?)>")).subst (str, EMPTY_STRING); 1128 value = (new RE ("<(.*?)>")).subst (value, EMPTY_STRING); 1129 } catch (RESyntaxException re) { 1130 logger.debug (re); 1131 } catch (Throwable t) { 1132 logger.debug (t); 1133 } 1134 1135 int comp = str.compareToIgnoreCase (value); 1136 1137 if (comp < 0) { 1138 flag = true; 1139 } else { 1140 flag = false; 1141 } 1142 } 1143 1144 } else if (containerSort.equals ("alphDesc")) { 1145 str4Test2 = (String ) fieldInfoToDisplay.get (j - incr); 1146 if (sortInfo4Date != null && !sortInfo4Date.isEmpty ()) { 1147 str4Test2 = (String ) sortInfo4Date.get (j - incr); 1148 } 1149 1150 try { 1151 Double d1 = new Double (str4Test); 1152 Double d2 = new Double (str4Test2); 1153 1154 int comp2 = d1.compareTo (d2); 1155 if (comp2 >= 0) { 1156 flag = true; 1157 } else { 1158 flag = false; 1159 } 1160 } catch (NumberFormatException e) { 1161 int comp = str.compareToIgnoreCase ((String ) 1162 fieldInfoToDisplay.get (j - incr)); 1163 if (comp >= 0) { 1164 flag = true; 1165 } else { 1166 flag = false; 1167 } 1168 } 1169 1170 } 1171 1172 if (flag) { 1173 1174 containers.set (j, containers.get (j - incr)); 1175 fieldInfoToDisplay.set (j, 1176 fieldInfoToDisplay.get (j - incr)); 1177 if (sortInfo4Date != null && !sortInfo4Date.isEmpty ()) { 1178 sortInfo4Date.set (j, sortInfo4Date.get (j - incr)); 1179 } 1180 } else { 1181 break; 1182 } 1183 } 1184 containers.set (j, c); 1185 fieldInfoToDisplay.set (j, str); 1186 if (sortInfo4Date != null && !sortInfo4Date.isEmpty ()) { 1187 sortInfo4Date.set (j, str2); 1188 } 1189 } 1190 } 1191 engineMap.put ("fieldInfoToDisplay", fieldInfoToDisplay); 1192 engineMap.put ("sortInfo4Date", sortInfo4Date); 1193 engineMap.put ("containers", containers); 1194 1195 } 1196 1197 1208 private HashMap buildFieldDefAcls (JahiaContainerList cList, 1209 ParamBean jParams, 1210 HashMap engineMap) 1211 throws JahiaException { 1212 HashMap hash = new HashMap (); 1213 1214 if (cList == null || jParams.getPage () == null) 1215 return hash; 1216 1217 JahiaContainer theContainer = null; 1218 1219 Enumeration enumeration = cList.getContainers (); 1220 if (enumeration.hasMoreElements ()) 1221 theContainer = (JahiaContainer) enumeration.nextElement (); 1223 if (theContainer == null) 1224 return hash; 1225 1226 String prop = null; 1227 boolean done = false; 1228 1229 JahiaField aField = null; 1230 JahiaFieldDefinition theDef = null; 1231 1232 Enumeration fList = theContainer.getFields (); 1233 while (fList.hasMoreElements ()) { 1234 aField = (JahiaField) fList.nextElement (); 1235 1236 theDef = aField.getDefinition (); 1237 if (theDef != null) { 1238 prop = cList.getProperty ("view_field_acl_" + theDef.getName ()); 1239 if (prop != null) { 1240 try { 1241 int aclID = Integer.parseInt (prop); 1242 hash.put (theDef.getName (), new Integer (aclID)); 1243 if (!done) { 1244 engineMap.put ("fieldAclDefaultField", 1245 theDef.getName ()); 1246 done = true; 1247 } 1248 } catch (Throwable t) { 1249 logger.debug (" requested acl ( view_field_acl_" + 1250 theDef.getName () + "), fail "); 1251 } 1252 } else { 1253 JahiaBaseACL newAcl = null; 1255 newAcl = new JahiaBaseACL (); 1256 newAcl.create (cList.getAclID ()); 1257 cList.setProperty ("view_field_acl_" + theDef.getName (), 1258 String.valueOf (newAcl.getID ())); 1259 ServicesRegistry.getInstance (). 1260 getJahiaContainersService ().saveContainerListInfo (cList, 1261 jParams.getPage ().getAclID ()); 1262 1263 hash.put (theDef.getName (), new Integer (newAcl.getID ())); 1264 if (!done) { 1265 engineMap.put ("fieldAclDefaultField", theDef.getName ()); 1266 done = true; 1267 } 1268 } 1269 } 1270 } 1271 1272 logger.debug ("Returned hashMap"); 1273 theContainer = null; 1274 return hash; 1275 } 1276} 1277 | Popular Tags |