1 13 49 50 package org.jahia.services.sitemap; 51 52 import java.lang.reflect.InvocationTargetException ; 53 import java.lang.reflect.Method ; 54 import java.util.ArrayList ; 55 import java.util.HashMap ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 import java.util.Set ; 59 import java.util.StringTokenizer ; 60 61 import org.jahia.data.fields.JahiaField; 62 import org.jahia.data.fields.JahiaFieldDefinitionProperties; 63 import org.jahia.data.viewhelper.sitemap.FlatSiteMapViewHelper; 64 import org.jahia.data.viewhelper.sitemap.LimitedTemplatesFilter; 65 import org.jahia.data.viewhelper.sitemap.PagesFilter; 66 import org.jahia.data.viewhelper.sitemap.SiteMapViewHelper; 67 import org.jahia.data.viewhelper.sitemap.TreeSiteMapViewHelper; 68 import org.jahia.engines.selectpage.SelectPage_Engine; 69 import org.jahia.exceptions.JahiaException; 70 import org.jahia.params.ParamBean; 71 import org.jahia.services.JahiaInitializableService; 72 import org.jahia.services.pages.ContentPage; 73 import org.jahia.services.pages.JahiaPageBaseService; 74 import org.jahia.services.usermanager.JahiaUser; 75 import org.jahia.settings.SettingsBean; 76 77 88 public class JahiaSiteMapService extends JahiaInitializableService { 89 90 private boolean cacheSiteMap = true; 91 92 95 public static final String PAGES_FILTER = "pagesFilter"; 96 public static final String FILTER_INIT_PARAMETER = "filterInitParameter"; 97 98 106 public static synchronized JahiaSiteMapService getInstance() 107 throws JahiaException 108 { 109 if (singletonInstance == null) { 110 singletonInstance = new JahiaSiteMapService(); 111 } 112 return singletonInstance; 113 } 114 115 120 public static PagesFilter getCurrentPagesFilter(JahiaField theField, ParamBean jParams) throws JahiaException { 121 PagesFilter pagesFilter = null; 122 123 if (jParams != null) { 124 final String pagesFilterClassName = (theField != null ? theField.getDefinition().getProperty(JahiaFieldDefinitionProperties.PAGE_SELECTION_FILTER_PROP) : 125 jParams.getParameter(JahiaSiteMapService.PAGES_FILTER)); 126 final JahiaPageBaseService jahiaPageBaseService = JahiaPageBaseService.getInstance(); 127 String fieldDefValue = ""; 128 if (theField != null) { 129 ContentPage contentPage = jahiaPageBaseService.lookupContentPage(theField.getPageID(), false); 130 if (contentPage != null) { 131 fieldDefValue = theField.getDefinition(). 132 getDefaultValue(contentPage.getPageTemplateID(jParams)); 133 } 134 } else { 135 fieldDefValue = jParams.getParameter(JahiaSiteMapService.FILTER_INIT_PARAMETER); 136 } 137 138 if (pagesFilterClassName != null 139 && pagesFilterClassName.length() > 0) { 140 try { 141 pagesFilter = (PagesFilter) Class.forName( 142 pagesFilterClassName).newInstance(); 143 pagesFilter.setFieldDefaultValue(fieldDefValue); 144 } catch (Exception e) { 145 logger.warn("Cannot instantiate pages filter!", e); 146 pagesFilter = new LimitedTemplatesFilter(); 147 pagesFilter.setFieldDefaultValue(fieldDefValue); 148 } 149 } else if (fieldDefValue != null && fieldDefValue.length() > 0) { 150 pagesFilter = new LimitedTemplatesFilter(); 151 pagesFilter.setFieldDefaultValue(fieldDefValue); 152 } 153 } 154 return (pagesFilter); 155 } 156 157 162 public void init(SettingsBean jSettings) { 163 logger.debug("Start Jahia Site map Service"); 164 Class siteMapViewHelperClass = TreeSiteMapViewHelper.class; 165 Method [] siteMapViewHelperClassMethods = siteMapViewHelperClass.getMethods(); 166 for (int i = 0; i < siteMapViewHelperClassMethods.length; i++) { 167 Method aSiteMapViewHelperMethod = siteMapViewHelperClassMethods[i]; 168 Class [] paramTypes = aSiteMapViewHelperMethod.getParameterTypes(); 169 Class returnType = aSiteMapViewHelperMethod.getReturnType(); 170 171 if (paramTypes.length == 1 && paramTypes[0] == int.class && returnType == void.class) { 173 _siteMapMethods.put(aSiteMapViewHelperMethod.getName(), aSiteMapViewHelperMethod); 174 } 175 } 176 Class [] cl = { String .class, int.class, int.class }; 178 Class flatSiteMapViewHelperClass = FlatSiteMapViewHelper.class; 179 try { 180 Method sortSiteMapMethod = flatSiteMapViewHelperClass.getMethod("sortSiteMap", cl); 181 _siteMapMethods.put(sortSiteMapMethod.getName(), sortSiteMapMethod); 182 } catch (NoSuchMethodException nsme) {} 183 logger.debug(_siteMapMethods.toString()); 184 } 185 186 204 public SiteMapViewHelper getTreeSiteMapViewHelper(JahiaUser user, ContentPage page, 205 String sessionID, int pageInfosFlag, String languageCode, int defaultMaxLevel, 206 boolean directPageOnly, PagesFilter pagesFilter, ParamBean jParams) { 207 List pages = new ArrayList (); 208 pages.add(page); 209 return getTreeSiteMapViewHelper(user,pages,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,directPageOnly,pagesFilter,jParams); 210 } 211 212 public SiteMapViewHelper getTreeSiteMapViewHelper(JahiaUser user, List pages, 213 String sessionID, int pageInfosFlag, String languageCode, int defaultMaxLevel, 214 boolean directPageOnly, PagesFilter pagesFilter, ParamBean jParams) { 215 String pagesFilterClass = (jParams != null && pagesFilter != null ? pagesFilter.getClass().getName() : null); 216 StringBuffer pageIDBuffer = new StringBuffer (); 217 for (Iterator i = pages.iterator(); i.hasNext();) { 218 pageIDBuffer.append(((ContentPage)i.next()).getID()).append(":"); 219 } 220 String pageIDs = pageIDBuffer.toString(); 221 String context = user.getUserKey() + ":" + sessionID + ":" + pageIDs + 222 pageInfosFlag + ":" + languageCode + 223 (pagesFilterClass == null ? "" : ":" + pagesFilterClass); 224 SiteMapViewHelper siteMapViewHelper = 225 (SiteMapViewHelper)_treeSiteMapViewHelper.get(context); 226 if (siteMapViewHelper == null) { 227 String message = "Rebuild site map view helper for user '" + user.getUserKey() + 228 "', page ID '" + pageIDs + "', page infos flag '" + 229 pageInfosFlag + "' and language code '" + languageCode + "'"; 230 logger.debug(message); 231 siteMapViewHelper = new TreeSiteMapViewHelper(user, pages, pageInfosFlag, languageCode, defaultMaxLevel, directPageOnly, pagesFilter, jParams); 232 if ( cacheSiteMap ){ 233 _treeSiteMapViewHelper.put(context, siteMapViewHelper); 234 } 235 } 236 return siteMapViewHelper; 237 } 238 239 256 public SiteMapViewHelper getTreeSiteMapViewHelper(JahiaUser user, ContentPage page, 257 String sessionID, int pageInfosFlag, String languageCode, int defaultMaxLevel, boolean directPageOnly) { 258 return getTreeSiteMapViewHelper(user,page,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,directPageOnly,null,null); } 259 260 278 public SiteMapViewHelper getTreeSiteMapViewHelper(JahiaUser user, ContentPage page, 279 String sessionID, int pageInfosFlag, String languageCode, int defaultMaxLevel, 280 boolean directPagesOnly, ParamBean jParams) { 281 return getTreeSiteMapViewHelper(user,page,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,directPagesOnly,null,jParams); 282 } 283 public SiteMapViewHelper getTreeSiteMapViewHelper(JahiaUser user, ContentPage page, 284 String sessionID, int pageInfosFlag, String languageCode, int defaultMaxLevel, 285 PagesFilter pagesFilter, ParamBean jParams) { 286 return getTreeSiteMapViewHelper(user,page,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,true,pagesFilter,jParams); 287 } 288 289 306 public SiteMapViewHelper getTreeSiteMapViewHelper(JahiaUser user, ContentPage page, 307 String sessionID, int pageInfosFlag, String languageCode, int defaultMaxLevel) { 308 return getTreeSiteMapViewHelper(user,page,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,true,null,null); 309 } 310 311 328 public SiteMapViewHelper getFlatSiteMapViewHelper(JahiaUser user, ContentPage page, 329 String sessionID, int pageInfosFlag, String languageCode, int defaultMaxLevel) { 330 return getFlatSiteMapViewHelper(user,page,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,null,null); 331 } 332 333 351 public SiteMapViewHelper getFlatSiteMapViewHelper(JahiaUser user, ContentPage page, 352 String sessionID, int pageInfosFlag, String languageCode, int defaultMaxLevel, 353 PagesFilter pagesFilter, ParamBean jParams) { 354 List pages = new ArrayList (); 355 pages.add(page); 356 return getFlatSiteMapViewHelper(user,pages,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,pagesFilter,jParams); 357 } 358 359 public SiteMapViewHelper getFlatSiteMapViewHelper(JahiaUser user, List pages, 360 String sessionID, int pageInfosFlag, String languageCode, int defaultMaxLevel, 361 PagesFilter pagesFilter, ParamBean jParams) { 362 String pagesFilterClass = (jParams != null && pagesFilter != null ? pagesFilter.getClass().getName() : null); 363 StringBuffer pageIDBuffer = new StringBuffer (); 364 for (Iterator i = pages.iterator(); i.hasNext();) { 365 pageIDBuffer.append(((ContentPage)i.next()).getID()).append(":"); 366 } 367 String pageIDs = pageIDBuffer.toString(); 368 String context = user.getUserKey() + ":" + sessionID + ":" + pageIDs + 369 pageInfosFlag + ":" + languageCode + 370 (pagesFilterClass == null ? "" : ":" + pagesFilterClass); 371 SiteMapViewHelper siteMapViewHelper = 372 (SiteMapViewHelper)_flatSiteMapViewHelper.get(context); 373 if (siteMapViewHelper == null) { 374 String message = "Rebuild site map view helper for user '" + user.getUserKey() + 375 "', page ID '" + pageIDs + "', page infos flag '" + 376 pageInfosFlag + "' and language code '" + languageCode + "'"; 377 logger.debug(message); 378 siteMapViewHelper = new FlatSiteMapViewHelper(user, pages, pageInfosFlag, languageCode, defaultMaxLevel, pagesFilter, jParams); 379 _flatSiteMapViewHelper.put(context, siteMapViewHelper); 380 } 381 return siteMapViewHelper; 382 } 383 384 389 public void resetSiteMap() { 390 logger.debug("Reset site map view helper"); 391 _treeSiteMapViewHelper.clear(); 392 _flatSiteMapViewHelper.clear(); 393 } 394 395 401 public void removeUserSiteMap(String userKey) { 402 logger.debug("Remove user " + userKey + " from site map view helper"); 403 Set contextKeys = _treeSiteMapViewHelper.keySet(); 404 Iterator it = contextKeys.iterator(); 405 while (it.hasNext()) { 406 String context = (String )it.next(); 407 if (context.indexOf(userKey) != -1) { 408 _treeSiteMapViewHelper.remove(context); 409 contextKeys = _treeSiteMapViewHelper.keySet(); 411 it = contextKeys.iterator(); 412 } 413 } 414 contextKeys = _flatSiteMapViewHelper.keySet(); 415 it = contextKeys.iterator(); 416 while (it.hasNext()) { 417 String context = (String )it.next(); 418 if (context.indexOf(userKey) != -1) { 419 _flatSiteMapViewHelper.remove(context); 420 contextKeys = _flatSiteMapViewHelper.keySet(); 422 it = contextKeys.iterator(); 423 } 424 } 425 } 426 427 439 public void invokeTreeSiteMapViewHelperMethod(JahiaUser user, ContentPage page, 440 String sessionID, int pageInfosFlag, String languageCode, String siteMapParam) { 441 invokeTreeSiteMapViewHelperMethod(user, page, sessionID, pageInfosFlag, languageCode, siteMapParam, true, null,null); 442 } 443 444 457 public void invokeTreeSiteMapViewHelperMethod(JahiaUser user, ContentPage page, 458 String sessionID, int pageInfosFlag, String languageCode, String siteMapParam, 459 PagesFilter pagesFilter, ParamBean jParams) { 460 invokeTreeSiteMapViewHelperMethod(user, page, sessionID, pageInfosFlag, languageCode, siteMapParam, true, pagesFilter,jParams); 461 } 462 public void invokeTreeSiteMapViewHelperMethod(JahiaUser user, ContentPage page, 463 String sessionID, int pageInfosFlag, String languageCode, String siteMapParam, 464 boolean directPageOnly, PagesFilter pagesFilter, ParamBean jParams) { 465 List pages = new ArrayList (); 466 pages.add(page); 467 invokeTreeSiteMapViewHelperMethod(user, pages, sessionID, pageInfosFlag, languageCode, siteMapParam, directPageOnly, pagesFilter,jParams); 468 } 469 public void invokeTreeSiteMapViewHelperMethod(JahiaUser user, List pages, 470 String sessionID, int pageInfosFlag, String languageCode, String siteMapParam, 471 boolean directPageOnly, PagesFilter pagesFilter, ParamBean jParams) { 472 StringTokenizer siteMapParamTokens = new StringTokenizer (siteMapParam, "|"); 473 String methodName = (String )_urlParams.get(siteMapParamTokens.nextToken()); 474 Method method = (Method )_siteMapMethods.get(methodName); 475 SiteMapViewHelper siteMapViewHelper = (jParams != null) ? 476 getTreeSiteMapViewHelper(user, pages, 477 sessionID, pageInfosFlag, languageCode, TreeSiteMapViewHelper.DISPLAY_ALL_LEVEL, directPageOnly, pagesFilter, jParams) : 478 getTreeSiteMapViewHelper(user, pages, 479 sessionID, pageInfosFlag, languageCode, TreeSiteMapViewHelper.DISPLAY_ALL_LEVEL, directPageOnly, null, null); 480 try { 481 if (siteMapParamTokens.countTokens() == 1) { 482 Object [] args = { new Integer (siteMapParamTokens.nextToken()) }; 483 method.invoke(siteMapViewHelper, args); 484 } 485 } catch(InvocationTargetException ite) { 486 logger.debug("Cannot invoke method " + methodName, ite); 487 } catch(IllegalAccessException iae) { 488 logger.debug("Bad access to method " + methodName, iae); 489 } 490 } 491 492 504 public void invokeFlatSiteMapViewHelperMethod(JahiaUser user, ContentPage page, 505 String sessionID, int pageInfosFlag, String languageCode, String siteMapParam) { 506 invokeFlatSiteMapViewHelperMethod(user, page, sessionID, pageInfosFlag, languageCode, siteMapParam, null, null); 507 } 508 509 522 public void invokeFlatSiteMapViewHelperMethod(JahiaUser user, ContentPage page, 523 String sessionID, int pageInfosFlag, String languageCode, String siteMapParam, 524 PagesFilter pagesFilter, ParamBean jParams) { 525 List pages = new ArrayList (); 526 pages.add(page); 527 invokeFlatSiteMapViewHelperMethod(user, pages, sessionID, pageInfosFlag, languageCode, siteMapParam, pagesFilter, jParams); 528 } 529 public void invokeFlatSiteMapViewHelperMethod(JahiaUser user, List pages, 530 String sessionID, int pageInfosFlag, String languageCode, String siteMapParam, 531 PagesFilter pagesFilter, ParamBean jParams) { 532 StringTokenizer siteMapParamTokens = new StringTokenizer (siteMapParam, "|"); 533 String methodName = (String )_urlParams.get(siteMapParamTokens.nextToken()); 534 Method method = (Method )_siteMapMethods.get(methodName); 535 SiteMapViewHelper siteMapViewHelper = (jParams != null) ? 536 getFlatSiteMapViewHelper(user, pages, 537 sessionID, pageInfosFlag, languageCode, SiteMapViewHelper.DISPLAY_ALL_LEVEL, pagesFilter, jParams) : 538 getFlatSiteMapViewHelper(user, pages, 539 sessionID, pageInfosFlag, languageCode, SiteMapViewHelper.DISPLAY_ALL_LEVEL, null, null); 540 try { 541 if (siteMapParamTokens.countTokens() == 3) { 542 Object [] args = { new String (siteMapParamTokens.nextToken()), 543 new Integer (siteMapParamTokens.nextToken()), 544 new Integer (siteMapParamTokens.nextToken()) }; 545 method.invoke(siteMapViewHelper, args); 546 } 547 } catch(InvocationTargetException ite) { 548 logger.debug("Cannot invoke method " + methodName, ite); 549 } catch(IllegalAccessException iae) { 550 logger.debug("Bad access to method " + methodName, iae); 551 } 552 } 553 554 555 private JahiaSiteMapService() { 556 _urlParams.put("collapse", "collapsePagesSubTree"); 557 _urlParams.put("expandall", "expandAllPagesSubTree"); 558 _urlParams.put("expand", "expandPagesSubTree"); 559 _urlParams.put("hideerror", "hideErrors"); 560 _urlParams.put("hideevent", "hideEvents"); 561 _urlParams.put("hideinfo", "hideInformation"); 562 _urlParams.put("hidewarning", "hideWarnings"); 563 _urlParams.put("showerror", "showErrors"); 564 _urlParams.put("showevent", "showEvents"); 565 _urlParams.put("showinfo", "showInformation"); 566 _urlParams.put("showwarning", "showWarnings"); 567 _urlParams.put("sort", "sortSiteMap"); 568 } 569 570 static private JahiaSiteMapService singletonInstance = null; 571 private HashMap _treeSiteMapViewHelper = new HashMap (); 573 private HashMap _flatSiteMapViewHelper = new HashMap (); 575 private HashMap _siteMapMethods = new HashMap (); 577 private HashMap _urlParams = new HashMap (); 580 581 private static org.apache.log4j.Logger logger = 582 org.apache.log4j.Logger.getLogger(JahiaSiteMapService.class); 583 584 590 public void removeSessionSiteMap(String sessionID) { 591 logger.debug("Remove session " + sessionID + " from site map view helper"); 592 String sessionPart = ":" + sessionID + ":"; 593 Set contextKeys = _treeSiteMapViewHelper.keySet(); 594 Iterator it = contextKeys.iterator(); 595 while (it.hasNext()) { 596 String context = (String )it.next(); 597 if (context.indexOf(sessionPart) != -1) { 598 _treeSiteMapViewHelper.remove(context); 599 contextKeys = _treeSiteMapViewHelper.keySet(); 601 it = contextKeys.iterator(); 602 } 603 } 604 contextKeys = _flatSiteMapViewHelper.keySet(); 605 it = contextKeys.iterator(); 606 while (it.hasNext()) { 607 String context = (String )it.next(); 608 if (context.indexOf(sessionPart) != -1) { 609 _flatSiteMapViewHelper.remove(context); 610 contextKeys = _flatSiteMapViewHelper.keySet(); 612 it = contextKeys.iterator(); 613 } 614 } 615 } 616 617 624 public void removeSessionPageSiteMap(JahiaUser user, ContentPage page, 625 String sessionID) 626 { 627 logger.debug("Remove session " + sessionID + " page from site map view helper"); 628 String contextPart = user.getUserKey() + ":" + sessionID + ":" + page.getID() + ":"; 629 Set contextKeys = _treeSiteMapViewHelper.keySet(); 630 Iterator it = contextKeys.iterator(); 631 while (it.hasNext()) { 632 String context = (String )it.next(); 633 if (context.indexOf(contextPart) != -1) { 634 _treeSiteMapViewHelper.remove(context); 635 contextKeys = _treeSiteMapViewHelper.keySet(); 637 it = contextKeys.iterator(); 638 } 639 } 640 contextKeys = _flatSiteMapViewHelper.keySet(); 641 it = contextKeys.iterator(); 642 while (it.hasNext()) { 643 String context = (String )it.next(); 644 if (context.indexOf(contextPart) != -1) { 645 _flatSiteMapViewHelper.remove(context); 646 contextKeys = _flatSiteMapViewHelper.keySet(); 648 it = contextKeys.iterator(); 649 } 650 } 651 } 652 } 653 | Popular Tags |