1 14 package org.jahia.services.pages; 15 16 import org.jahia.content.ContentObject; 17 import org.jahia.content.ContentPageXRefManager; 18 import org.jahia.exceptions.JahiaException; 19 import org.jahia.exceptions.JahiaTemplateNotFoundException; 20 import org.jahia.params.ParamBean; 21 import org.jahia.registries.ServicesRegistry; 22 import org.jahia.services.acl.ACLResourceInterface; 23 import org.jahia.services.acl.JahiaACLException; 24 import org.jahia.services.acl.JahiaBaseACL; 25 import org.jahia.services.cache.CacheFactory; 26 import org.jahia.services.cache.HtmlCache; 27 import org.jahia.services.sites.JahiaSite; 28 import org.jahia.services.usermanager.JahiaUser; 29 import org.jahia.services.usermanager.JahiaUserManagerService; 30 import org.jahia.services.version.ActivationTestResults; 31 import org.jahia.services.version.EntryLoadRequest; 32 import org.jahia.services.version.JahiaSaveVersion; 33 import org.jahia.services.version.StateModificationContext; 34 import org.jahia.utils.JahiaTools; 35 36 import java.util.*; 37 import java.io.Serializable ; 38 39 40 55 public class JahiaPage implements PageInfoInterface, ACLResourceInterface, Comparator, Serializable { 56 private static org.apache.log4j.Logger logger = 57 org.apache.log4j.Logger.getLogger (JahiaPage.class); 58 59 60 private static final String LOCK_USER_PARAMETER = "user"; 62 private static final String LOCK_PAGE_PARAMETER = "page"; 63 private static final String LOCK_ENGINE_NAME_PARAMETER = "engineName"; 64 65 private ContentPage mContentPage = null; 67 private JahiaPageDefinition mPageTemplate; 68 private JahiaBaseACL mACL; 69 private EntryLoadRequest mEntryLoadRequest = null; 70 71 private JahiaPageDefinition mTempPageTemplate; 72 private Map mProperties = null; 73 74 private boolean declared = false; 75 76 77 protected JahiaPage (ContentPage contentPage, 79 JahiaPageDefinition pageTemplate, 80 JahiaBaseACL acl, 81 EntryLoadRequest loadRequest) 82 throws JahiaException { 83 84 mContentPage = contentPage; 85 86 mPageTemplate = pageTemplate; 87 mACL = acl; 88 mEntryLoadRequest = loadRequest; 89 90 EntryLoadRequest newLoadRequest = loadRequest; 95 if (contentPage != null && !contentPage.hasActiveEntries () 96 && loadRequest != null && loadRequest.isCurrent ()) { 97 newLoadRequest = 98 new EntryLoadRequest (EntryLoadRequest.STAGING_WORKFLOW_STATE, 99 0, loadRequest.getLocales ()); 100 newLoadRequest.setWithDeleted (loadRequest.isWithDeleted ()); 101 newLoadRequest.setWithMarkedForDeletion (loadRequest.isWithMarkedForDeletion ()); 102 mEntryLoadRequest = newLoadRequest; 103 } 104 if (mPageTemplate == null) { 105 mPageTemplate = contentPage.getPageTemplate (newLoadRequest); 107 } 108 109 mTempPageTemplate = mPageTemplate; 110 111 } 112 113 118 public ContentPage getContentPage () { 119 return this.mContentPage; 120 } 121 122 private boolean checkAccess (JahiaUser user, int permission, boolean checkChilds) { 124 if (user == null) { 125 return false; 126 } 127 128 boolean result = false; 130 try { 131 result = mACL.getPermission (user, permission); 132 if(!result && checkChilds) { 133 List childs = getContentPage().getChilds(user, (EntryLoadRequest)null); 134 for (int i = 0; i < childs.size() && !result; i++) { 135 ContentObject contentObject = (ContentObject) childs.get(i); 136 if(!(contentObject instanceof ContentPage)) 137 result = contentObject.checkAccess(user, permission,checkChilds); 138 } 139 } 140 } catch (JahiaACLException ex) { 141 } catch (JahiaException ex) { 143 } 145 146 156 return result; 157 } 158 159 160 172 public final boolean checkAdminAccess (JahiaUser user) { 173 return checkAccess (user, JahiaBaseACL.ADMIN_RIGHTS, false); 174 } 175 176 public final boolean checkAdminAccess (JahiaUser user,boolean checkChilds) { 177 return checkAccess (user, JahiaBaseACL.ADMIN_RIGHTS, checkChilds); 178 } 179 180 181 191 public final boolean checkReadAccess (JahiaUser user) { 192 return checkAccess (user, JahiaBaseACL.READ_RIGHTS, false); 193 } 194 195 196 206 public final boolean checkWriteAccess (JahiaUser user) { 207 return checkAccess (user, JahiaBaseACL.WRITE_RIGHTS, false); 208 } 209 210 public final boolean checkWriteAccess (JahiaUser user,boolean checkChilds) { 211 return checkAccess (user, JahiaBaseACL.WRITE_RIGHTS,checkChilds); 212 } 213 214 223 public final boolean checkGuestAccess (int siteID) { 224 JahiaUserManagerService userMgr = ServicesRegistry.getInstance (). 226 getJahiaUserManagerService (); 227 if (userMgr == null) 228 return false; 229 230 JahiaUser theUser = userMgr.lookupUser (siteID, JahiaUserManagerService.GUEST_USERNAME); 231 if (theUser == null) 232 return false; 233 234 return checkAccess (theUser, JahiaBaseACL.READ_RIGHTS, false); 235 } 236 237 238 251 public void commitChanges (boolean flushCaches) 252 throws JahiaException { 253 logger.debug ("called."); 254 255 if (flushCaches) { 256 int siteID = getJahiaID (); 258 JahiaSite site = ServicesRegistry.getInstance ().getJahiaSitesService ().getSite ( 259 siteID); 260 if (site == null) { 261 logger.debug ("Invalid site for page, cannot flush cache."); 262 } else { 263 Set refPages = ContentPageXRefManager.getInstance ().getPageIDs (getID ()); 264 Iterator refPagesIter = refPages.iterator (); 265 266 HtmlCache htmlCache = CacheFactory.getHtmlCache (); 268 if (htmlCache == null) 269 logger.warn ("Could not get the HTML cache instance!!"); 270 271 while (refPagesIter.hasNext ()) { 272 Integer curPageID = (Integer ) refPagesIter.next (); 273 if (htmlCache != null) 274 htmlCache.invalidatePageEntries (curPageID.toString ()); 275 } 276 277 if (htmlCache != null) 279 htmlCache.invalidatePageEntries (Integer.toString (getID ())); 280 } 281 } 282 283 mContentPage.commitChanges (flushCaches); 284 mPageTemplate = mTempPageTemplate; 285 } 286 287 288 294 public final JahiaBaseACL getACL () { 295 return mACL; 296 } 297 298 299 305 public final int getAclID () { 306 int id = -1; 307 try { 308 id = mACL.getID (); 309 } catch (JahiaACLException ex) { 310 } 312 return id; 313 } 314 315 316 322 public final int getCounter () { 323 return mContentPage.getCounter (mEntryLoadRequest); 324 } 325 326 327 334 public final String getCreator () { 335 return mContentPage.getCreator (); 336 } 337 338 339 345 public final String getDoc () { 346 return mContentPage.getDoc (); 347 } 348 349 350 356 public final int getID () { 357 return mContentPage.getID (); 358 } 359 360 361 367 public final int getJahiaID () { 368 return mContentPage.getJahiaID (); 369 } 370 371 372 378 public final JahiaPageDefinition getPageTemplate () { 379 return mPageTemplate; 380 } 381 382 383 389 public final int getPageTemplateID () { 390 return mContentPage.getPageTemplateID (mEntryLoadRequest); 391 } 392 393 400 public final int getPageLinkID () { 401 return mContentPage.getPageLinkID (mEntryLoadRequest); 402 } 403 404 405 411 public final int getPageType () { 412 return mContentPage.getPageType (mEntryLoadRequest); 413 } 414 415 416 422 public final int getParentID () { 423 return mContentPage.getParentID (mEntryLoadRequest); 424 } 425 426 427 434 public final String getRemoteURL () { 435 return mContentPage.getRemoteURL (mEntryLoadRequest); 436 } 437 438 439 445 public final String getTitle () { 446 return JahiaTools.text2html (mContentPage.getTitle (mEntryLoadRequest)); 447 } 448 449 450 454 public final void incrementCounter () { 455 mContentPage.incrementCounter (mEntryLoadRequest); 456 } 457 458 470 public void setPageTemplateID (int value) 471 throws JahiaException, JahiaTemplateNotFoundException { 472 mContentPage.setPageTemplateID (value, mEntryLoadRequest); 473 } 474 475 476 public void setPageTemplate (JahiaPageDefinition value) 478 throws JahiaTemplateNotFoundException, JahiaException { 479 mContentPage.setPageTemplate (value, mEntryLoadRequest); 480 } 481 482 483 489 public final void setPageLinkID (int value) { 490 mContentPage.setPageLinkID (value, mEntryLoadRequest); 491 } 492 493 494 503 public final void setPageType (int value) 504 throws JahiaException { 505 mContentPage.setPageType (value, mEntryLoadRequest); 506 } 507 508 509 519 public final void setAclID (int aclID) 520 throws JahiaException { 521 mContentPage.setAclID (aclID, mEntryLoadRequest); 522 } 523 524 530 public final void setRemoteURL (String value) 531 throws JahiaException { 532 mContentPage.setRemoteURL (value, mEntryLoadRequest); 533 } 534 535 536 542 public final void setTitle (String value) { 543 mContentPage.setTitle (value, mEntryLoadRequest); 544 } 545 546 553 public final void setTitle (String languageCode, String title) 554 throws JahiaException { 555 mContentPage.setTitle (languageCode, title, mEntryLoadRequest); 556 } 557 558 565 public final void setTitles (Set languagesSet, Hashtable titles) 566 throws JahiaException { 567 mContentPage.setTitles (languagesSet, titles, mEntryLoadRequest); 568 } 569 570 580 public String getUrl (ParamBean jParams) 581 throws JahiaException { 582 return getURL (jParams); 583 } 585 586 592 public String getURL (ParamBean jParams) 593 throws JahiaException { 594 String outURL = ""; 595 switch (getPageType ()) { 596 case (TYPE_DIRECT): 597 if (jParams != null) { 598 outURL = jParams.composePageUrl (getID ()); 599 } 600 break; 601 602 case (TYPE_LINK): 603 if (jParams != null) { 604 int linkPageID = -1; 605 try { 606 linkPageID = getPageLinkID (); 607 if (linkPageID != -1) { 608 ContentPage linkPage = ContentPage.getPage (linkPageID, false); 609 if (linkPage.checkReadAccess (jParams.getUser ())) { 611 outURL = jParams.composePageUrl (linkPageID); 612 } else { 613 } 615 } 616 } catch (Throwable t) { 617 logger.debug ( 618 "Exception creating link url with page[" + linkPageID + "]", t); 619 } 620 } 621 break; 622 case (TYPE_URL): 623 outURL = getRemoteURL (); 624 break; 625 } 626 return outURL; 627 } 629 630 631 643 public Enumeration getPagePath (String operationMode, JahiaUser user) 644 throws JahiaException { 645 Vector thePath = ServicesRegistry.getInstance ().getJahiaPageService ().getPagePath (getID (), mEntryLoadRequest, operationMode, 646 user); 647 if (thePath != null) { 648 return thePath.elements (); 649 } 650 return null; 651 } 653 663 public Enumeration getContentPagePath (String operationMode, JahiaUser user) 664 throws JahiaException { 665 Vector thePath = ServicesRegistry.getInstance ().getJahiaPageService ().getContentPagePath (getID (), mEntryLoadRequest, 666 operationMode, user); 667 if (thePath != null) { 668 return thePath.elements (); 669 } 670 return null; 671 } 673 689 public Enumeration getPagePath (int levels, String operationMode, JahiaUser user) 690 throws JahiaException { 691 Vector thePath = ServicesRegistry.getInstance ().getJahiaPageService ().getPagePath (getID (), mEntryLoadRequest, operationMode, 692 user); 693 if (thePath != null) { 694 int fromIndex = 0; 695 if ((thePath.size () - levels) > 0) { 696 fromIndex = thePath.size () - levels; 697 } 698 List theShortPathList = thePath.subList (fromIndex, thePath.size ()); 699 Vector theShortPathVector = new Vector (theShortPathList); 700 return theShortPathVector.elements (); 705 } 706 return null; 707 } 709 723 public Enumeration getContentPagePath (int levels, String operationMode, JahiaUser user) 724 throws JahiaException { 725 Vector thePath = ServicesRegistry.getInstance ().getJahiaPageService ().getContentPagePath (getID (), mEntryLoadRequest, 726 operationMode, user); 727 if (thePath != null) { 728 int fromIndex = 0; 729 if ((thePath.size () - levels) > 0) { 730 fromIndex = thePath.size () - levels; 731 } 732 List theShortPathList = thePath.subList (fromIndex, thePath.size ()); 733 Vector theShortPathVector = new Vector (theShortPathList); 734 return theShortPathVector.elements (); 735 } 736 return null; 737 } 739 740 750 public Enumeration getChilds () 751 throws JahiaException { 752 Vector childs = ServicesRegistry.getInstance ().getJahiaPageService ().getPageChilds (getID (), PageLoadFlags.ALL, 753 mEntryLoadRequest); 754 if (childs != null) { 755 return childs.elements (); 756 } 757 return null; 758 } 760 public Enumeration getChilds (JahiaUser user) 761 throws JahiaException { 762 Vector childs = ServicesRegistry.getInstance ().getJahiaPageService ().getPageChilds (getID (), PageLoadFlags.ALL, user); 763 if (childs != null) { 765 return childs.elements (); 766 } 767 return null; 768 } 770 781 public int compare (Object c1, Object c2) throws ClassCastException { 782 783 return ((JahiaPage) c1) 784 .getTitle ().toLowerCase () 785 .compareTo (((JahiaPage) c2).getTitle ().toLowerCase ()); 786 787 } 788 789 790 public String toString () { 792 StringBuffer output = new StringBuffer (); 793 output.append ("Detail of page ["); 794 output.append (getID ()); 795 output.append ("] :\n"); 796 output.append (" - Site ID ["); 797 output.append (getJahiaID ()); 798 output.append ("]\n"); 799 output.append (" - Parent ID ["); 800 output.append (getParentID ()); 801 output.append ("]\n"); 802 output.append (" - Type ["); 803 output.append (PAGE_TYPE_NAMES[getPageType ()]); 804 output.append ("]\n"); 805 output.append (" - Ttitle ["); 806 output.append (getTitle ()); 807 output.append ("]\n"); 808 output.append (" - Template ID ["); 809 output.append (getPageTemplateID ()); 810 output.append ("]\n"); 811 output.append (" - Remote URL ["); 812 output.append (getRemoteURL ()); 813 output.append ("]\n"); 814 output.append (" - Link ID ["); 815 output.append (getPageLinkID ()); 816 output.append ("]\n"); 817 output.append (" - Creator ["); 818 output.append (getCreator ()); 819 output.append ("]\n"); 820 output.append (" - Creation date ["); 821 output.append (getDoc ()); 822 output.append ("]\n"); 823 output.append (" - Counter ["); 824 output.append (getCounter ()); 825 output.append ("]\n"); 826 output.append (" - ACL ID ["); 827 output.append (getAclID ()); 828 output.append ("]\n"); 829 output.append (" - EntryLoadRequest=" + mEntryLoadRequest + "\n"); 830 831 return output.toString (); 832 } 833 834 private boolean checkPropertiesAvailability () 835 throws JahiaException { 836 if (mProperties == null) { 837 mProperties = PagePropertyDB.getInstance ().getPageProperties (getID ()); 839 } 840 if (mProperties == null) { 841 logger.debug ("Error while loading page properties !"); 842 return false; 843 } 844 return true; 845 } 846 847 848 860 public PageProperty getPageLocalProperty (String name) 861 throws JahiaException { 862 if (checkPropertiesAvailability ()) { 863 PageProperty prop = (PageProperty) mProperties.get (name); 864 return prop; 865 } else { 866 logger.debug ("Error accessing page property " + name + 867 " probably doesn't exist yet..."); 868 return null; 869 } 870 } 871 872 888 public String getProperty (String name) 889 throws JahiaException { 890 PageProperty curProp = getPageLocalProperty (name); 891 if (curProp != null) { 892 return curProp.getValue (); 893 } else { 894 if (getParentID () > 0) { 897 JahiaPage parentPage = ServicesRegistry.getInstance ().getJahiaPageService ().lookupPage (getParentID (), 900 mEntryLoadRequest); 901 if (parentPage != null) { 902 return parentPage.getProperty (name); 905 } 906 } 907 return null; 908 } 909 } 910 911 926 public String getProperty (String name, String languageCode) 927 throws JahiaException { 928 PageProperty curProp = getPageLocalProperty (name); 929 if (curProp != null) { 930 return curProp.getValue (languageCode); 931 } else { 932 if (getParentID () > 0) { 935 JahiaPage parentPage = ServicesRegistry.getInstance ().getJahiaPageService ().lookupPage (getParentID (), 938 mEntryLoadRequest); 939 if (parentPage != null) { 940 return parentPage.getProperty (name, languageCode); 943 } 944 } 945 return null; 946 } 947 } 948 949 960 public void setProperty (String name, String value) 961 throws JahiaException { 962 PagePropertyDB.getInstance ().setPageProperty (getID(), name, value); 963 } 964 965 979 public void setProperty (String name, String languageCode, String value) 980 throws JahiaException { 981 PagePropertyDB.getInstance ().setPageProperty (getID(), name, languageCode, value); 982 } 983 984 994 public void removeProperty (String name) 995 throws JahiaException { 996 PageProperty targetProperty = null; 997 if (mProperties != null) { 998 targetProperty = getPageLocalProperty(name); 999 if (targetProperty != null) { 1000 mProperties.remove(name); 1001 PagePropertyDB.getInstance().removePageProperty(targetProperty); 1002 } 1003 } 1004 } 1005 1006 1021 public ActivationTestResults activeStagingVersion ( 1022 Set languageCodes, 1023 JahiaSaveVersion saveVersion, 1024 JahiaUser user, 1025 ParamBean jParams, 1026 StateModificationContext stateModifContext) 1027 throws JahiaException { 1028 1029 boolean versioningActive = ServicesRegistry.getInstance ().getJahiaVersionService () 1030 .isVersioningEnabled (jParams.getSiteID ()); 1031 return mContentPage.activeStagingEntries (languageCodes, versioningActive, saveVersion, 1032 user, jParams, stateModifContext); 1033 } 1034 1035 1049 public ActivationTestResults isValidForActivation ( 1050 Set languageCodes, 1051 JahiaSaveVersion saveVersion, 1052 JahiaUser user, 1053 ParamBean jParams, 1054 StateModificationContext stateModifContext) 1055 throws JahiaException { 1056 boolean versioningActive = ServicesRegistry.getInstance ().getJahiaVersionService () 1057 .isVersioningEnabled (jParams.getSiteID ()); 1058 return mContentPage.isValidForActivation (languageCodes, versioningActive, saveVersion, 1059 user, jParams, stateModifContext); 1060 } 1061 1062 1071 public void changeStagingStatus (Set languageCodes, int newVersionStatus, 1072 ParamBean jParams, 1073 StateModificationContext stateModifContext) 1074 throws JahiaException { 1075 mContentPage.changeStagingStatus (languageCodes, newVersionStatus, 1076 jParams, stateModifContext); 1077 } 1078 1079 1085 public boolean hasActiveEntries () { 1086 return mContentPage.hasActiveEntries (); 1087 } 1088 1089 1100 public boolean hasEntry (int pageInfosFlag, String languageCode) { 1101 return mContentPage.hasEntries (pageInfosFlag, languageCode); 1102 } 1103 1104 public Map getLanguagesStates (boolean withContent) { 1105 return mContentPage.getLanguagesStates (withContent); 1106 } 1107 1108} 1109 | Popular Tags |