KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > viewhelper > sitemap > SiteMapViewHelper


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13

14 /*
15  * ----- BEGIN LICENSE BLOCK -----
16  * Version: JCSL 1.0
17  *
18  * The contents of this file are subject to the Jahia Community Source License
19  * 1.0 or later (the "License"); you may not use this file except in
20  * compliance with the License. You may obtain a copy of the License at
21  * http://www.jahia.org/license
22  *
23  * Software distributed under the License is distributed on an "AS IS" basis,
24  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
25  * for the rights, obligations and limitations governing use of the contents
26  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
27  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
28  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
29  *
30  * The Shared Modifications are Jahia View Helper.
31  *
32  * The Developer of the Shared Modifications is Jahia Solution S�rl.
33  * Portions created by the Initial Developer are Copyright (C) 2002 by the
34  * Initial Developer. All Rights Reserved.
35  *
36  * Contributor(s):
37  * Sep 24 2002 Jahia Solutions S�rl: MAP Initial release.
38  *
39  * ----- END LICENSE BLOCK -----
40  */

41
42 package org.jahia.data.viewhelper.sitemap;
43
44 import java.io.Serializable JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Enumeration JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Vector JavaDoc;
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 /**
62  * <p>The Jahia Shared Modification is: Jahia View Helper</p>
63  *
64  * <p>Description:
65  * Create a flat tree from Jahia page tree structure destinated to display a tree
66  * or a flat site map.
67  * </p>
68  * <p>Copyright: MAP (Jahia Solutions S�rl 2002)</p>
69  * <p>Company: Jahia Solutions S�rl</p>
70  * @author MAP
71  * @version 1.0
72  */

73 public abstract class SiteMapViewHelper implements Serializable JavaDoc {
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     /**
83      * Create a view helper on a entire Jahia site map restricted to the actual
84      * logged user.
85      *
86      * @param user The actual user logged in Jahia.
87      * @param startPage The start page for site map.
88      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
89      * @param defaultMaxLevel Site map expansion default level max.
90      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
91      * ARCHIVED_PAGE_INFOS constants.
92      * @param languageCode Get the page with the specified language code.
93      */

94     public SiteMapViewHelper(JahiaUser user, ContentPage startPage,
95                              int pageInfosFlag, String JavaDoc languageCode,
96                              int defaultMaxLevel) {
97        initialize(user, startPage, pageInfosFlag, languageCode,
98            defaultMaxLevel, true, null, null);
99     }
100
101     /**
102      * Create a view helper on a entire Jahia site map restricted to the actual
103      * logged user.
104      *
105      * @param user The actual user logged in Jahia.
106      * @param startPage The start page for site map.
107      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
108      * @param defaultMaxLevel Site map expansion default level max.
109      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
110      * ARCHIVED_PAGE_INFOS constants.
111      * @param languageCode Get the page with the specified language code.
112      * @param jParams The ParamBean.
113      */

114     public SiteMapViewHelper(JahiaUser user, ContentPage startPage,
115                              int pageInfosFlag, String JavaDoc languageCode,
116                              int defaultMaxLevel, PagesFilter pagesFilter,
117                              ParamBean jParams) {
118        initialize(user, startPage, pageInfosFlag, languageCode,
119            defaultMaxLevel, true, pagesFilter, jParams);
120     }
121
122     /**
123      * Create a view helper on a entire Jahia site map restricted to the actual
124      * logged user.
125      *
126      * @param user The actual user logged in Jahia.
127      * @param startPage The start page for site map.
128      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
129      * @param defaultMaxLevel Site map expansion default level max.
130      * @param directPageOnly
131      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
132      * ARCHIVED_PAGE_INFOS constants.
133      * @param languageCode Get the page with the specified language code.
134      */

135     public SiteMapViewHelper(JahiaUser user, ContentPage startPage,
136                              int pageInfosFlag, String JavaDoc languageCode,
137                              int defaultMaxLevel, boolean directPageOnly) {
138        initialize(user, startPage, pageInfosFlag, languageCode,
139            defaultMaxLevel, directPageOnly, null, null);
140     }
141
142     /**
143      * Create a view helper on a entire Jahia site map restricted to the actual
144      * logged user.
145      *
146      * @param user The actual user logged in Jahia.
147      * @param startPage The start page for site map.
148      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
149      * @param defaultMaxLevel Site map expansion default level max.
150      * @param directPageOnly
151      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
152      * ARCHIVED_PAGE_INFOS constants.
153      * @param languageCode Get the page with the specified language code.
154      * @param jParams The ParamBean.
155      */

156     public SiteMapViewHelper(JahiaUser user, ContentPage startPage,
157                              int pageInfosFlag, String JavaDoc 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 JavaDoc startPages,
165             int pageInfosFlag, String JavaDoc languageCode, int defaultMaxLevel,
166             boolean directPagesOnly, PagesFilter pagesFilter, ParamBean jParams) {
167         initialize(user, startPages, pageInfosFlag, languageCode,
168                 defaultMaxLevel, directPagesOnly, pagesFilter, jParams);
169     }
170     
171     /**
172      * Initialize and create a view helper on a entire Jahia site map restricted to the actual
173      * logged user.
174      *
175      * @param user The actual user logged in Jahia.
176      * @param startPage The start page for site map.
177      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
178      * @param defaultMaxLevel Site map expansion default level max.
179      * @param directPageOnly
180      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
181      * ARCHIVED_PAGE_INFOS constants.
182      * @param languageCode Get the page with the specified language code.
183      * @param jParams The ParamBean.
184      */

185     private void initialize (JahiaUser user, ContentPage startPage,
186             int pageInfosFlag, String JavaDoc languageCode,
187             int defaultMaxLevel, boolean directPageOnly,
188             PagesFilter pagesFilter, ParamBean jParams){
189         List JavaDoc startPages = new ArrayList JavaDoc();
190         startPages.add(startPage);
191         initialize(user, startPages, pageInfosFlag, languageCode,
192                 defaultMaxLevel, directPageOnly, pagesFilter, jParams);
193     }
194     
195     private void initialize (JahiaUser user, List JavaDoc startPages,
196                               int pageInfosFlag, String JavaDoc 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 JavaDoc pageIterator = startPages.iterator(); pageIterator
209                 .hasNext();) {
210             ContentPage processedPage = (ContentPage) pageIterator.next();
211             List JavaDoc 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                     // The parent page parameter is set to 0 to avoid
228
// transmitting the ParamBean
229
_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     /**
246      * Return the maximum page level computed in the 'getAllPageChilds' method
247      * called when this object is created.
248      * @return The maximum level.
249      */

250     public int getMaxLevel() {
251         return _maxLevel;
252     }
253
254     /**
255      * Verify if a language is available in a specified workflow state. This
256      * method is usefull if we should display exactly the number of language
257      * columns in a site map. For example, a anonymous user (guest) does not
258      * need to know all languages defined in the site but in which languages
259      * (exactly) the active pages are available.
260      *
261      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
262      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
263      * ARCHIVED_PAGE_INFOS constants.
264      * @param languageCode Get the page with the specified language code.
265      * @return True if the language code is available in the specified workflow
266      * state.
267      */

268     public boolean isAvailableLanguageCode(int pageInfosFlag, String JavaDoc languageCode) {
269         HashMap JavaDoc availableLanguageCode = (HashMap JavaDoc)_availableLanguageCodeMap.get(new Integer JavaDoc(pageInfosFlag));
270         if (availableLanguageCode == null) {
271             availableLanguageCode = new HashMap JavaDoc();
272         }
273         // If already exists for this page infos, don't recompute it.
274
if (!availableLanguageCode.containsKey(languageCode)) {
275             int i; // Unfortunately we have to look for all pages...
276
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; // ... but when find it, it's sufficiant to stop the loop.
282
}
283             }
284             // If not found, don't reiterate a second time this loop for this language
285
if (i == _jahiaPageSiteMap.size()) {
286                 availableLanguageCode.put(languageCode, "no");
287             }
288         }
289         _availableLanguageCodeMap.put(new Integer JavaDoc(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 JavaDoc getPageTitle(int index, String JavaDoc 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 JavaDoc getJahiaPageSiteMap() {
317         return _jahiaPageSiteMap;
318     }
319
320     private void getAllPageChilds(ContentPage nodePage, Iterator JavaDoc 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 JavaDoc nextChildPages = getPageChilds(childPage);
328             int pageID = childPage.getID();
329             ContentPage contentPage = lookupContentPage(pageID);
330             if (contentPage == null) {
331                 break;
332             }
333             // Stop loop if page has a deleted entry only.
334
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                 // we want staged site map or versioned
347
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                     // we want to get the staging parent and if it is equals to the current node page, then we continue, else
362
// we stop expanding the tree
363
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                 // we want active site map
394
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                     // It is the active page, break down tree construction
417
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 JavaDoc getPageChilds(ContentPage page) {
432         List JavaDoc pageChildsList = new ArrayList JavaDoc();
433         try {
434             Enumeration JavaDoc pageChildsEnum = page.getContentPageChilds(_user,_pageInfosFlag,_languageCode,_directPageOnly);
435             if ( pageChildsEnum == null ){
436                 pageChildsEnum = new Vector JavaDoc().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; // In this case shortcut the next steps for optmization.
443
}
444
445             // Precompute what should contain the site map regarding the page
446
// infos flag and the language code.
447

448             while (pageChildsEnum.hasMoreElements()) {
449                 ContentPage contentPage = (ContentPage)pageChildsEnum.nextElement();
450                 if (_languageCode != null) {
451                     /**
452                      * FIXME : NK, we want to retrieve page child, we don't botter
453                      * le language code isn't it ? that is why I comment this out.
454                      */

455                      /*
456                     if (!contentPage.hasEntries(_pageInfosFlag, _languageCode)) {
457                         continue; // This page does not fill the criterions
458                     }*/

459                     if (!contentPage.hasEntries(_pageInfosFlag)) {
460                         continue; // This page does not fill the criterions
461
}
462                 } else {
463                     if ((_pageInfosFlag == (ContentPage.ACTIVE_PAGE_INFOS | ContentPage.STAGING_PAGE_INFOS))
464                         && !contentPage.hasEntries(_pageInfosFlag)) {
465                         continue; // This page does not fill the criterions
466
}
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 JavaDoc languageCode) {
477         EntryLoadRequest loadRequest = null;
478         ArrayList JavaDoc langs = new ArrayList JavaDoc();
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; // Contain the max page level in the tree
509
private int _defaultMaxLevel;
510     private JahiaUser _user;
511     private int _pageInfosFlag;
512     private String JavaDoc _languageCode;
513     private boolean _directPageOnly = true;
514         
515     // Added by PAP:
516
private PagesFilter _pagesFilter;
517     private EntryLoadRequest _loadRequest = null;
518     
519     // Available language code storage.
520
private HashMap JavaDoc _availableLanguageCodeMap = new HashMap JavaDoc();
521
522     private ArrayList JavaDoc _jahiaPageSiteMap = new ArrayList JavaDoc(8);
523
524     private static org.apache.log4j.Logger logger =
525             org.apache.log4j.Logger.getLogger(SiteMapViewHelper.class);
526
527 }
528
Popular Tags