1 14 41 42 package org.jahia.data.viewhelper.sitemap; 43 44 import java.io.Serializable ; 45 import java.util.ArrayList ; 46 import java.util.Enumeration ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import java.util.Vector ; 51 52 import org.jahia.exceptions.JahiaException; 53 import org.jahia.params.ParamBean; 54 import org.jahia.services.fields.ContentField; 55 import org.jahia.services.pages.ContentPage; 56 import org.jahia.services.pages.JahiaPageBaseService; 57 import org.jahia.services.pages.PageInfoInterface; 58 import org.jahia.services.usermanager.JahiaUser; 59 import org.jahia.services.version.EntryLoadRequest; 60 61 73 public abstract class SiteMapViewHelper implements Serializable { 74 75 public static final int DISPLAY_ALL_LEVEL = Integer.MAX_VALUE; 76 public static final int DEFAULT_LEVEL = 2; 77 78 public static final int TREE_VIEW = 0; 79 public static final int FLAT_VIEW = 1; 80 public static final int SEARCH_VIEW = 2; 81 82 94 public SiteMapViewHelper(JahiaUser user, ContentPage startPage, 95 int pageInfosFlag, String languageCode, 96 int defaultMaxLevel) { 97 initialize(user, startPage, pageInfosFlag, languageCode, 98 defaultMaxLevel, true, null, null); 99 } 100 101 114 public SiteMapViewHelper(JahiaUser user, ContentPage startPage, 115 int pageInfosFlag, String languageCode, 116 int defaultMaxLevel, PagesFilter pagesFilter, 117 ParamBean jParams) { 118 initialize(user, startPage, pageInfosFlag, languageCode, 119 defaultMaxLevel, true, pagesFilter, jParams); 120 } 121 122 135 public SiteMapViewHelper(JahiaUser user, ContentPage startPage, 136 int pageInfosFlag, String languageCode, 137 int defaultMaxLevel, boolean directPageOnly) { 138 initialize(user, startPage, pageInfosFlag, languageCode, 139 defaultMaxLevel, directPageOnly, null, null); 140 } 141 142 156 public SiteMapViewHelper(JahiaUser user, ContentPage startPage, 157 int pageInfosFlag, String languageCode, 158 int defaultMaxLevel, boolean directPagesOnly, 159 PagesFilter pagesFilter, ParamBean jParams) { 160 initialize(user, startPage, pageInfosFlag, languageCode, 161 defaultMaxLevel, directPagesOnly, pagesFilter, jParams); 162 } 163 164 public SiteMapViewHelper(JahiaUser user, List startPages, 165 int pageInfosFlag, String languageCode, int defaultMaxLevel, 166 boolean directPagesOnly, PagesFilter pagesFilter, ParamBean jParams) { 167 initialize(user, startPages, pageInfosFlag, languageCode, 168 defaultMaxLevel, directPagesOnly, pagesFilter, jParams); 169 } 170 171 185 private void initialize (JahiaUser user, ContentPage startPage, 186 int pageInfosFlag, String languageCode, 187 int defaultMaxLevel, boolean directPageOnly, 188 PagesFilter pagesFilter, ParamBean jParams){ 189 List startPages = new ArrayList (); 190 startPages.add(startPage); 191 initialize(user, startPages, pageInfosFlag, languageCode, 192 defaultMaxLevel, directPageOnly, pagesFilter, jParams); 193 } 194 195 private void initialize (JahiaUser user, List startPages, 196 int pageInfosFlag, String languageCode, 197 int defaultMaxLevel, boolean directPagesOnly, 198 PagesFilter pagesFilter, ParamBean jParams){ 199 _user = user; 200 _pageInfosFlag = pageInfosFlag; 201 _languageCode = languageCode; 202 _maxLevel = 0; 203 _defaultMaxLevel = defaultMaxLevel; 204 _directPageOnly = directPagesOnly; 205 _pagesFilter = pagesFilter; 206 _loadRequest = createEntryLoadRequest(pageInfosFlag, languageCode); 207 208 for (Iterator pageIterator = startPages.iterator(); pageIterator 209 .hasNext();) { 210 ContentPage processedPage = (ContentPage) pageIterator.next(); 211 List childPages = getPageChilds(processedPage); 212 213 int pageID = processedPage.getID(); 214 ContentPage contentPage = lookupContentPage(pageID); 215 216 if (_pagesFilter != null) 217 childPages = _pagesFilter.filterChildren(contentPage, 218 childPages, jParams); 219 220 if (contentPage != null) { 221 if (_pagesFilter == null 222 || !_pagesFilter.filterForDisplay(contentPage, jParams)) { 223 if (contentPage.getPageType(_loadRequest) == PageInfoInterface.TYPE_LINK){ 224 contentPage = lookupContentPage(contentPage.getPageLinkID(_loadRequest)); 225 pageID = contentPage.getID(); 226 } 227 _jahiaPageSiteMap 230 .add(new PageSiteMap( 231 pageID, 232 _currentLevel, 233 childPages.size() > 0, 234 0, 235 false, 236 contentPage 237 .getTitles(ContentPage.LAST_UPDATED_TITLES), 238 0, _defaultMaxLevel)); 239 } 240 } 241 242 getAllPageChilds(processedPage, childPages.iterator(), jParams); 243 } 244 } 245 250 public int getMaxLevel() { 251 return _maxLevel; 252 } 253 254 268 public boolean isAvailableLanguageCode(int pageInfosFlag, String languageCode) { 269 HashMap availableLanguageCode = (HashMap )_availableLanguageCodeMap.get(new Integer (pageInfosFlag)); 270 if (availableLanguageCode == null) { 271 availableLanguageCode = new HashMap (); 272 } 273 if (!availableLanguageCode.containsKey(languageCode)) { 275 int i; for (i = 0; i < _jahiaPageSiteMap.size(); i++) { 277 PageSiteMap pageSiteMap = (PageSiteMap)_jahiaPageSiteMap.get(i); 278 ContentPage contentPage = lookupContentPage(pageSiteMap.getPageID()); 279 if (contentPage != null && contentPage.hasEntries(pageInfosFlag, languageCode)) { 280 availableLanguageCode.put(languageCode, "yes"); 281 break; } 283 } 284 if (i == _jahiaPageSiteMap.size()) { 286 availableLanguageCode.put(languageCode, "no"); 287 } 288 } 289 _availableLanguageCodeMap.put(new Integer (pageInfosFlag), availableLanguageCode); 290 return "yes".equals(availableLanguageCode.get(languageCode)); 291 } 292 293 public abstract ContentPage getContentPage(int index); 294 295 public abstract int getPageID(int index); 296 297 public abstract int getPageIndex(int pageID); 298 299 public abstract int getParentPageID(int index); 300 301 public abstract int getPageLevel(int index); 302 303 public abstract String getPageTitle(int index, String languageCode); 304 305 public abstract int size(); 306 307 protected ContentPage lookupContentPage(int pageID) { 308 try { 309 return JahiaPageBaseService.getInstance().lookupContentPage(pageID, false); 310 } catch (JahiaException je) { 311 logger.debug("Cannot recover the page with ID " + pageID); 312 return null; 313 } 314 } 315 316 protected ArrayList getJahiaPageSiteMap() { 317 return _jahiaPageSiteMap; 318 } 319 320 private void getAllPageChilds(ContentPage nodePage, Iterator childPagesIterator, ParamBean jParams) { 321 _currentLevel++; 322 if (_currentLevel > _maxLevel) { 323 _maxLevel = _currentLevel; 324 } 325 while (childPagesIterator.hasNext()) { 326 ContentPage childPage = (ContentPage)childPagesIterator.next(); 327 List nextChildPages = getPageChilds(childPage); 328 int pageID = childPage.getID(); 329 ContentPage contentPage = lookupContentPage(pageID); 330 if (contentPage == null) { 331 break; 332 } 333 if ( this._pageInfosFlag<ContentPage.ARCHIVED_PAGE_INFOS 335 && contentPage.hasEntries(ContentPage.ARCHIVED_PAGE_INFOS) && 336 !(contentPage.hasEntries(ContentPage.ACTIVE_PAGE_INFOS | ContentPage.STAGING_PAGE_INFOS))) { 337 break; 338 } 339 340 if (_pagesFilter != null) 341 nextChildPages = _pagesFilter.filterChildren(contentPage, nextChildPages, jParams); 342 343 int sameParentID = contentPage.hasSameParentID(); 344 if ( ((_pageInfosFlag & ContentPage.STAGING_PAGE_INFOS) != 0) || 345 ((_pageInfosFlag & ContentPage.ARCHIVED_PAGE_INFOS) != 0) ){ 346 if ( (sameParentID == ContentPage.SAME_PARENT) ) { 348 if (_pagesFilter == null || !_pagesFilter.filterForDisplay(contentPage, jParams)){ 349 if (contentPage.getPageType(_loadRequest) == PageInfoInterface.TYPE_LINK){ 350 contentPage = lookupContentPage(contentPage.getPageLinkID(_loadRequest)); 351 pageID = contentPage.getID(); 352 } 353 PageSiteMap pageSiteMap = new PageSiteMap(pageID, _currentLevel, 354 nextChildPages.size() > 0, nodePage.getID(), 355 !childPagesIterator.hasNext(), contentPage.getTitles(ContentPage.LAST_UPDATED_TITLES), 356 _currentLevel, _defaultMaxLevel); 357 _jahiaPageSiteMap.add(pageSiteMap); 358 } 359 getAllPageChilds(childPage, nextChildPages.iterator(), jParams); 360 } else { 361 int parentId = contentPage.getParentID(EntryLoadRequest.STAGED); 364 if ( parentId == nodePage.getID() ){ 365 if (_pagesFilter == null || !_pagesFilter.filterForDisplay(contentPage, jParams)){ 366 if (contentPage.getPageType(_loadRequest) == PageInfoInterface.TYPE_LINK){ 367 contentPage = lookupContentPage(contentPage.getPageLinkID(_loadRequest)); 368 pageID = contentPage.getID(); 369 } 370 PageSiteMap pageSiteMap = new PageSiteMap(pageID, _currentLevel, 371 nextChildPages.size() > 0, nodePage.getID(), 372 !childPagesIterator.hasNext(), contentPage.getTitles(ContentPage.LAST_UPDATED_TITLES), 373 _currentLevel, _defaultMaxLevel); 374 _jahiaPageSiteMap.add(pageSiteMap); 375 } 376 getAllPageChilds(childPage, nextChildPages.iterator(), jParams); 377 } else if (_pagesFilter == null || !_pagesFilter.filterForDisplay(contentPage, jParams)){ 378 if (contentPage.getPageType(_loadRequest) == PageInfoInterface.TYPE_LINK){ 379 contentPage = lookupContentPage(contentPage.getPageLinkID(_loadRequest)); 380 pageID = contentPage.getID(); 381 } 382 PageSiteMap pageSiteMap = new PageSiteMap(pageID, 383 _currentLevel, 384 false, nodePage.getID(), 385 !childPagesIterator.hasNext(), 386 contentPage. 387 getTitles(ContentPage.LAST_UPDATED_TITLES), 388 _currentLevel, _defaultMaxLevel); 389 _jahiaPageSiteMap.add(pageSiteMap); 390 } 391 } 392 } else { 393 if ( (sameParentID == ContentPage.SAME_PARENT) || 395 sameParentID == nodePage.getID()) { 396 if (_pagesFilter == null || !_pagesFilter.filterForDisplay(contentPage, jParams)){ 397 if (contentPage.getPageType(_loadRequest) == PageInfoInterface.TYPE_LINK){ 398 contentPage = lookupContentPage(contentPage.getPageLinkID(_loadRequest)); 399 pageID = contentPage.getID(); 400 } 401 PageSiteMap pageSiteMap = new PageSiteMap(pageID, 402 _currentLevel, 403 nextChildPages.size() > 0, nodePage.getID(), 404 !childPagesIterator.hasNext(), 405 contentPage.getTitles(ContentPage.LAST_UPDATED_TITLES), 406 _currentLevel, _defaultMaxLevel); 407 _jahiaPageSiteMap.add(pageSiteMap); 408 } 409 getAllPageChilds(childPage, nextChildPages.iterator(), jParams); 410 } 411 else if (_pagesFilter == null || !_pagesFilter.filterForDisplay(contentPage, jParams)){ 412 if (contentPage.getPageType(_loadRequest) == PageInfoInterface.TYPE_LINK){ 413 contentPage = lookupContentPage(contentPage.getPageLinkID(_loadRequest)); 414 pageID = contentPage.getID(); 415 } 416 PageSiteMap pageSiteMap = new PageSiteMap(pageID, 418 _currentLevel, 419 false, nodePage.getID(), 420 !childPagesIterator.hasNext(), 421 contentPage.getTitles(ContentPage.LAST_UPDATED_TITLES), 422 _currentLevel, _defaultMaxLevel); 423 _jahiaPageSiteMap.add(pageSiteMap); 424 } 425 } 426 } 427 _currentLevel--; 428 429 } 430 431 private List getPageChilds(ContentPage page) { 432 List pageChildsList = new ArrayList (); 433 try { 434 Enumeration pageChildsEnum = page.getContentPageChilds(_user,_pageInfosFlag,_languageCode,_directPageOnly); 435 if ( pageChildsEnum == null ){ 436 pageChildsEnum = new Vector ().elements(); 437 } 438 if (_pageInfosFlag == (ContentPage.ACTIVE_PAGE_INFOS | ContentPage.STAGING_PAGE_INFOS)) { 439 while (pageChildsEnum.hasMoreElements()) { 440 pageChildsList.add(pageChildsEnum.nextElement()); 441 } 442 return pageChildsList; } 444 445 448 while (pageChildsEnum.hasMoreElements()) { 449 ContentPage contentPage = (ContentPage)pageChildsEnum.nextElement(); 450 if (_languageCode != null) { 451 455 459 if (!contentPage.hasEntries(_pageInfosFlag)) { 460 continue; } 462 } else { 463 if ((_pageInfosFlag == (ContentPage.ACTIVE_PAGE_INFOS | ContentPage.STAGING_PAGE_INFOS)) 464 && !contentPage.hasEntries(_pageInfosFlag)) { 465 continue; } 467 } 468 pageChildsList.add(contentPage); 469 } 470 } catch (JahiaException je) { 471 logger.debug("Unable to find '" + page.getID() + "' child pages"); 472 } 473 return pageChildsList; 474 } 475 476 private EntryLoadRequest createEntryLoadRequest(int pageInfosFlag, String languageCode) { 477 EntryLoadRequest loadRequest = null; 478 ArrayList langs = new ArrayList (); 479 langs.add( 480 org.jahia.utils.LanguageCodeConverters.languageCodeToLocale( 481 ContentField.SHARED_LANGUAGE)); 482 if (languageCode != null) { 483 langs.add( 484 org.jahia.utils.LanguageCodeConverters.languageCodeToLocale( 485 languageCode)); 486 } 487 if ( (pageInfosFlag & ContentPage.STAGING_PAGE_INFOS) != 0) { 488 loadRequest = 489 new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 0, 490 langs); 491 loadRequest.setWithMarkedForDeletion(true); 492 } else if ( (pageInfosFlag & ContentPage.ACTIVE_PAGE_INFOS) != 0) { 493 loadRequest = 494 new EntryLoadRequest(EntryLoadRequest.ACTIVE_WORKFLOW_STATE, 0, 495 langs); 496 loadRequest.setWithMarkedForDeletion(true); 497 } else if ( (pageInfosFlag & ContentPage.ARCHIVED_PAGE_INFOS) != 0) { 498 loadRequest = 499 new EntryLoadRequest(EntryLoadRequest.VERSIONED_WORKFLOW_STATE, 500 0, langs); 501 loadRequest.setWithDeleted(true); 502 loadRequest.setWithMarkedForDeletion(true); 503 } 504 return loadRequest; 505 } 506 507 private int _currentLevel = 0; 508 private int _maxLevel; private int _defaultMaxLevel; 510 private JahiaUser _user; 511 private int _pageInfosFlag; 512 private String _languageCode; 513 private boolean _directPageOnly = true; 514 515 private PagesFilter _pagesFilter; 517 private EntryLoadRequest _loadRequest = null; 518 519 private HashMap _availableLanguageCodeMap = new HashMap (); 521 522 private ArrayList _jahiaPageSiteMap = new ArrayList (8); 523 524 private static org.apache.log4j.Logger logger = 525 org.apache.log4j.Logger.getLogger(SiteMapViewHelper.class); 526 527 } 528 | Popular Tags |