KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > sitemap > JahiaSiteMapService


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  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JSCL 1.0/GPL 2.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia Services.
30  *
31  * Portions created by the Initial Developer are Copyright (C) 2002 by the
32  * Initial Developer. All Rights Reserved.
33  *
34  * Contributor(s):
35  * Oct 22 2002 Jahia Solutions S�rl: MAP Initial release.
36  *
37  * Alternatively, the contents of this file may be used under the terms of the
38  * GNU General Public License Version 2 or later (the "GPL"), in which case
39  * the provisions of the GPL are applicable instead of those above. If you
40  * wish to allow use of your version of this file only under the terms of the
41  * GPL, and not to allow others to use your version of this file under the
42  * terms of the JSCL, indicate your decision by deleting the provisions above
43  * and replace them with the notice and other provisions required by the GPL.
44  * If you do not delete the provisions above, a recipient may use your version
45  * of this file under the terms of any one of the JSCL or the GPL.
46  *
47  * ----- END LICENSE BLOCK -----
48  */

49
50 package org.jahia.services.sitemap;
51
52 import java.lang.reflect.InvocationTargetException JavaDoc;
53 import java.lang.reflect.Method JavaDoc;
54 import java.util.ArrayList JavaDoc;
55 import java.util.HashMap JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.List JavaDoc;
58 import java.util.Set JavaDoc;
59 import java.util.StringTokenizer JavaDoc;
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 /**
78  * This class create and manage a site map helper that can be imported to a
79  * view file such as JSP.
80  *
81  * <p>Title: </p>
82  * <p>Description: </p>
83  * <p>Copyright: MAP (Jahia Solutions S�rl 2002)</p>
84  * <p>Company: Jahia Solutions S�rl</p>
85  * @author MAP
86  * @version 1.0
87  */

88 public class JahiaSiteMapService extends JahiaInitializableService {
89
90     private boolean cacheSiteMap = true;
91
92     /**
93      * The pages filter parameter
94      */

95     public static final String JavaDoc PAGES_FILTER = "pagesFilter";
96     public static final String JavaDoc FILTER_INIT_PARAMETER = "filterInitParameter";
97     
98     /**
99      * Returns the singleton instance of the service, and creates it if there
100      * wasn't one.
101      * @return a JahiaSiteMapService object that is the singleton instance of
102      * this service.
103      * @throws JahiaException not used for the moment but reserved for later
104      * use.
105      */

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     /**
116      * Return the pagesFilter instance set for this request in the parameters.
117      *
118      * @param jParams Jahia processing context
119      */

120     public static PagesFilter getCurrentPagesFilter(JahiaField theField, ParamBean jParams) throws JahiaException {
121         PagesFilter pagesFilter = null;
122
123         if (jParams != null) {
124             final String JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
158      * Implementation of the JahiaInitializableService. (For future purposes)
159      *
160      * @param jSettings Jahia Private Settings
161      */

162     public void init(SettingsBean jSettings) {
163         logger.debug("Start Jahia Site map Service");
164         Class JavaDoc siteMapViewHelperClass = TreeSiteMapViewHelper.class;
165         Method JavaDoc[] siteMapViewHelperClassMethods = siteMapViewHelperClass.getMethods();
166         for (int i = 0; i < siteMapViewHelperClassMethods.length; i++) {
167             Method JavaDoc aSiteMapViewHelperMethod = siteMapViewHelperClassMethods[i];
168             Class JavaDoc[] paramTypes = aSiteMapViewHelperMethod.getParameterTypes();
169             Class JavaDoc returnType = aSiteMapViewHelperMethod.getReturnType();
170
171             // Filter the methods having one param of type int returning void.
172
if (paramTypes.length == 1 && paramTypes[0] == int.class && returnType == void.class) {
173                 _siteMapMethods.put(aSiteMapViewHelperMethod.getName(), aSiteMapViewHelperMethod);
174             }
175         }
176         // Try to get the 'sortSiteMap' method
177
Class JavaDoc[] cl = { String JavaDoc.class, int.class, int.class };
178         Class JavaDoc flatSiteMapViewHelperClass = FlatSiteMapViewHelper.class;
179         try {
180             Method JavaDoc sortSiteMapMethod = flatSiteMapViewHelperClass.getMethod("sortSiteMap", cl);
181             _siteMapMethods.put(sortSiteMapMethod.getName(), sortSiteMapMethod);
182         } catch (NoSuchMethodException JavaDoc nsme) {}
183         logger.debug(_siteMapMethods.toString());
184     }
185
186     /**
187      * Construct the tree site map view helper if necessary. In the case a same user
188      * acesses from a same page to the site map, a previous stored site map view
189      * helper is returned. Otherwise a new one is created.
190      * The reason of page dependency is to garantize a low memory obstruction.
191      *
192      * @param user The Jahia user context referencing the site map.
193      * @param page The Jahia page context referencing the site map.
194      * @param sessionID The session ID defines another contextual key if the same
195      * user is logged more than one time.
196      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
197      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
198      * ARCHIVED_PAGE_INFOS constants.
199      * @param defaultMaxLevel Maximum page level that should be displayed by default.
200      * @param languageCode Get the page with the specified language code.
201      * @param jParams The ParamBean.
202      * @return The site map view helper.
203      */

204     public SiteMapViewHelper getTreeSiteMapViewHelper(JahiaUser user, ContentPage page,
205             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, int defaultMaxLevel,
206             boolean directPageOnly, PagesFilter pagesFilter, ParamBean jParams) {
207         List JavaDoc pages = new ArrayList JavaDoc();
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 JavaDoc pages,
213             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, int defaultMaxLevel,
214             boolean directPageOnly, PagesFilter pagesFilter, ParamBean jParams) {
215         String JavaDoc pagesFilterClass = (jParams != null && pagesFilter != null ? pagesFilter.getClass().getName() : null);
216         StringBuffer JavaDoc pageIDBuffer = new StringBuffer JavaDoc();
217         for (Iterator JavaDoc i = pages.iterator(); i.hasNext();) {
218             pageIDBuffer.append(((ContentPage)i.next()).getID()).append(":");
219         }
220         String JavaDoc pageIDs = pageIDBuffer.toString();
221         String JavaDoc 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 JavaDoc 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     /**
240      * Construct the tree site map view helper if necessary. In the case a same user
241      * acesses from a same page to the site map, a previous stored site map view
242      * helper is returned. Otherwise a new one is created.
243      * The reason of page dependency is to garantize a low memory obstruction.
244      *
245      * @param user The Jahia user context referencing the site map.
246      * @param page The Jahia page context referencing the site map.
247      * @param sessionID The session ID defines another contextual key if the same
248      * user is logged more than one time.
249      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
250      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
251      * ARCHIVED_PAGE_INFOS constants.
252      * @param defaultMaxLevel Maximum page level that should be displayed by default.
253      * @param languageCode Get the page with the specified language code.
254      * @return The site map view helper.
255      */

256     public SiteMapViewHelper getTreeSiteMapViewHelper(JahiaUser user, ContentPage page,
257             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, int defaultMaxLevel, boolean directPageOnly) {
258               return getTreeSiteMapViewHelper(user,page,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,directPageOnly,null,null); }
259     
260     /**
261      * Construct the tree site map view helper if necessary. In the case a same user
262      * acesses from a same page to the site map, a previous stored site map view
263      * helper is returned. Otherwise a new one is created.
264      * The reason of page dependency is to garantize a low memory obstruction.
265      *
266      * @param user The Jahia user context referencing the site map.
267      * @param page The Jahia page context referencing the site map.
268      * @param sessionID The session ID defines another contextual key if the same
269      * user is logged more than one time.
270      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
271      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
272      * ARCHIVED_PAGE_INFOS constants.
273      * @param defaultMaxLevel Maximum page level that should be displayed by default.
274      * @param languageCode Get the page with the specified language code.
275      * @param jParams The ParamBean.
276      * @return The site map view helper.
277      */

278     public SiteMapViewHelper getTreeSiteMapViewHelper(JahiaUser user, ContentPage page,
279             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc 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 JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, int defaultMaxLevel,
285             PagesFilter pagesFilter, ParamBean jParams) {
286         return getTreeSiteMapViewHelper(user,page,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,true,pagesFilter,jParams);
287     }
288
289     /**
290      * Construct the tree site map view helper if necessary. In the case a same user
291      * acesses from a same page to the site map, a previous stored site map view
292      * helper is returned. Otherwise a new one is created.
293      * The reason of page dependency is to garantize a low memory obstruction.
294      *
295      * @param user The Jahia user context referencing the site map.
296      * @param page The Jahia page context referencing the site map.
297      * @param sessionID The session ID defines another contextual key if the same
298      * user is logged more than one time.
299      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
300      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
301      * ARCHIVED_PAGE_INFOS constants.
302      * @param defaultMaxLevel Maximum page level that should be displayed by default.
303      * @param languageCode Get the page with the specified language code.
304      * @return The site map view helper.
305      */

306     public SiteMapViewHelper getTreeSiteMapViewHelper(JahiaUser user, ContentPage page,
307             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, int defaultMaxLevel) {
308         return getTreeSiteMapViewHelper(user,page,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,true,null,null);
309     }
310
311     /**
312      * Construct the flat site map view helper if necessary. In the case a same user
313      * acesses from a same page to the site map, a previous stored site map view
314      * helper is returned. Otherwise a new one is created.
315      * The reason of page dependency is to garantize a low memory obstruction.
316      *
317      * @param user The Jahia user context referencing the site map.
318      * @param page The Jahia page context referencing the site map.
319      * @param sessionID The session ID defines another contextual key if the same
320      * user is logged more than one time.
321      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
322      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
323      * ARCHIVED_PAGE_INFOS constants.
324      * @param defaultMaxLevel Maximum page level that should be displayed by default.
325      * @param languageCode Get the page with the specified language code.
326      * @return The site map view helper.
327      */

328     public SiteMapViewHelper getFlatSiteMapViewHelper(JahiaUser user, ContentPage page,
329             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, int defaultMaxLevel) {
330         return getFlatSiteMapViewHelper(user,page,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,null,null);
331     }
332
333     /**
334      * Construct the flat site map view helper if necessary. In the case a same user
335      * acesses from a same page to the site map, a previous stored site map view
336      * helper is returned. Otherwise a new one is created.
337      * The reason of page dependency is to garantize a low memory obstruction.
338      *
339      * @param user The Jahia user context referencing the site map.
340      * @param page The Jahia page context referencing the site map.
341      * @param sessionID The session ID defines another contextual key if the same
342      * user is logged more than one time.
343      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
344      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
345      * ARCHIVED_PAGE_INFOS constants.
346      * @param defaultMaxLevel Maximum page level that should be displayed by default.
347      * @param languageCode Get the page with the specified language code.
348      * @param jParams The ParamBean.
349      * @return The site map view helper.
350      */

351     public SiteMapViewHelper getFlatSiteMapViewHelper(JahiaUser user, ContentPage page,
352             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, int defaultMaxLevel,
353             PagesFilter pagesFilter, ParamBean jParams) {
354         List JavaDoc pages = new ArrayList JavaDoc();
355         pages.add(page);
356         return getFlatSiteMapViewHelper(user,pages,sessionID,pageInfosFlag,languageCode,defaultMaxLevel,pagesFilter,jParams);
357     }
358        
359     public SiteMapViewHelper getFlatSiteMapViewHelper(JahiaUser user, List JavaDoc pages,
360             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, int defaultMaxLevel,
361              PagesFilter pagesFilter, ParamBean jParams) {
362         String JavaDoc pagesFilterClass = (jParams != null && pagesFilter != null ? pagesFilter.getClass().getName() : null);
363         StringBuffer JavaDoc pageIDBuffer = new StringBuffer JavaDoc();
364         for (Iterator JavaDoc i = pages.iterator(); i.hasNext();) {
365             pageIDBuffer.append(((ContentPage)i.next()).getID()).append(":");
366         }
367         String JavaDoc pageIDs = pageIDBuffer.toString();
368         String JavaDoc 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 JavaDoc 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     /**
385      * Invalid the site map view helper that should be reinstanciate due
386      * to a Jahia page addition.
387      * Called from org.jahia.data.viewhelper.sitemap.SiteMapEventListener
388      */

389     public void resetSiteMap() {
390         logger.debug("Reset site map view helper");
391         _treeSiteMapViewHelper.clear();
392         _flatSiteMapViewHelper.clear();
393     }
394
395     /**
396      * Remove the sitemap corresponding to the Jahia user.
397      * Called from org.jahia.data.viewhelper.sitemap.SiteMapEventListener
398      *
399      * @param userKey The user object to remove from hash map.
400      */

401     public void removeUserSiteMap(String JavaDoc userKey) {
402         logger.debug("Remove user " + userKey + " from site map view helper");
403         Set JavaDoc contextKeys = _treeSiteMapViewHelper.keySet();
404         Iterator JavaDoc it = contextKeys.iterator();
405         while (it.hasNext()) {
406             String JavaDoc context = (String JavaDoc)it.next();
407             if (context.indexOf(userKey) != -1) {
408                 _treeSiteMapViewHelper.remove(context);
409                 // Reinit the iterator to avoid Concurrent Modification Exception.
410
contextKeys = _treeSiteMapViewHelper.keySet();
411                 it = contextKeys.iterator();
412             }
413         }
414         contextKeys = _flatSiteMapViewHelper.keySet();
415         it = contextKeys.iterator();
416         while (it.hasNext()) {
417             String JavaDoc context = (String JavaDoc)it.next();
418             if (context.indexOf(userKey) != -1) {
419                 _flatSiteMapViewHelper.remove(context);
420                 // Reinit the iterator to avoid Concurrent Modification Exception.
421
contextKeys = _flatSiteMapViewHelper.keySet();
422                 it = contextKeys.iterator();
423             }
424         }
425     }
426
427     /**
428      * Call the appropriate tree site map view helper method in a specified context.
429      * @param user The logged user who use the site map.
430      * @param page The page from where the site map is used.
431      * @param sessionID The session ID defines another contextual key if the same
432      * user is logged more than one time.
433      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
434      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
435      * ARCHIVED_PAGE_INFOS constants.
436      * @param languageCode Get the page with the specified language code.
437      * @param siteMapParam The site map method parameters.
438      */

439     public void invokeTreeSiteMapViewHelperMethod(JahiaUser user, ContentPage page,
440             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, String JavaDoc siteMapParam) {
441         invokeTreeSiteMapViewHelperMethod(user, page, sessionID, pageInfosFlag, languageCode, siteMapParam, true, null,null);
442     }
443
444     /**
445      * Call the appropriate tree site map view helper method in a specified context.
446      * @param user The logged user who use the site map.
447      * @param page The page from where the site map is used.
448      * @param sessionID The session ID defines another contextual key if the same
449      * user is logged more than one time.
450      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
451      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
452      * ARCHIVED_PAGE_INFOS constants.
453      * @param languageCode Get the page with the specified language code.
454      * @param siteMapParam The site map method parameters.
455      * @param jParams The ParamBean.
456      */

457     public void invokeTreeSiteMapViewHelperMethod(JahiaUser user, ContentPage page,
458             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, String JavaDoc 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 JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, String JavaDoc siteMapParam,
464             boolean directPageOnly, PagesFilter pagesFilter, ParamBean jParams) {
465         List JavaDoc pages = new ArrayList JavaDoc();
466         pages.add(page);
467         invokeTreeSiteMapViewHelperMethod(user, pages, sessionID, pageInfosFlag, languageCode, siteMapParam, directPageOnly, pagesFilter,jParams);
468     }
469     public void invokeTreeSiteMapViewHelperMethod(JahiaUser user, List JavaDoc pages,
470             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, String JavaDoc siteMapParam,
471             boolean directPageOnly, PagesFilter pagesFilter, ParamBean jParams) {
472         StringTokenizer JavaDoc siteMapParamTokens = new StringTokenizer JavaDoc(siteMapParam, "|");
473         String JavaDoc methodName = (String JavaDoc)_urlParams.get(siteMapParamTokens.nextToken());
474         Method JavaDoc method = (Method JavaDoc)_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 JavaDoc[] args = { new Integer JavaDoc(siteMapParamTokens.nextToken()) };
483                 method.invoke(siteMapViewHelper, args);
484             }
485         } catch(InvocationTargetException JavaDoc ite) {
486             logger.debug("Cannot invoke method " + methodName, ite);
487         } catch(IllegalAccessException JavaDoc iae) {
488             logger.debug("Bad access to method " + methodName, iae);
489         }
490     }
491
492     /**
493      * Call the appropriate flat site map view helper method in a specified context.
494      * @param user The logged user who use the site map.
495      * @param page The page from where the site map is used.
496      * @param sessionID The session ID defines another contextual key if the same
497      * user is logged more than one time.
498      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
499      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
500      * ARCHIVED_PAGE_INFOS constants.
501      * @param languageCode Get the page with the specified language code.
502      * @param siteMapParam The site map method parameters.
503      */

504     public void invokeFlatSiteMapViewHelperMethod(JahiaUser user, ContentPage page,
505             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, String JavaDoc siteMapParam) {
506         invokeFlatSiteMapViewHelperMethod(user, page, sessionID, pageInfosFlag, languageCode, siteMapParam, null, null);
507     }
508
509     /**
510      * Call the appropriate flat site map view helper method in a specified context.
511      * @param user The logged user who use the site map.
512      * @param page The page from where the site map is used.
513      * @param sessionID The session ID defines another contextual key if the same
514      * user is logged more than one time.
515      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
516      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
517      * ARCHIVED_PAGE_INFOS constants.
518      * @param languageCode Get the page with the specified language code.
519      * @param siteMapParam The site map method parameters.
520      * @param jParams The ParamBean.
521      */

522     public void invokeFlatSiteMapViewHelperMethod(JahiaUser user, ContentPage page,
523             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, String JavaDoc siteMapParam,
524             PagesFilter pagesFilter, ParamBean jParams) {
525         List JavaDoc pages = new ArrayList JavaDoc();
526         pages.add(page);
527         invokeFlatSiteMapViewHelperMethod(user, pages, sessionID, pageInfosFlag, languageCode, siteMapParam, pagesFilter, jParams);
528     }
529     public void invokeFlatSiteMapViewHelperMethod(JahiaUser user, List JavaDoc pages,
530             String JavaDoc sessionID, int pageInfosFlag, String JavaDoc languageCode, String JavaDoc siteMapParam,
531             PagesFilter pagesFilter, ParamBean jParams) {
532         StringTokenizer JavaDoc siteMapParamTokens = new StringTokenizer JavaDoc(siteMapParam, "|");
533         String JavaDoc methodName = (String JavaDoc)_urlParams.get(siteMapParamTokens.nextToken());
534         Method JavaDoc method = (Method JavaDoc)_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 JavaDoc[] args = { new String JavaDoc(siteMapParamTokens.nextToken()),
543                                   new Integer JavaDoc(siteMapParamTokens.nextToken()),
544                                   new Integer JavaDoc(siteMapParamTokens.nextToken()) };
545                 method.invoke(siteMapViewHelper, args);
546             }
547         } catch(InvocationTargetException JavaDoc ite) {
548             logger.debug("Cannot invoke method " + methodName, ite);
549         } catch(IllegalAccessException JavaDoc 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     // Store the tree site map view helper for a given context.
572
private HashMap JavaDoc _treeSiteMapViewHelper = new HashMap JavaDoc();
573     // Store the flat site map view helper for a given context.
574
private HashMap JavaDoc _flatSiteMapViewHelper = new HashMap JavaDoc();
575     // Store the parameters key corresponding to method pointers
576
private HashMap JavaDoc _siteMapMethods = new HashMap JavaDoc();
577     // To avoid a long URL parameter this hash map store the match between the
578
// site map view helper method to call and a parameter.
579
private HashMap JavaDoc _urlParams = new HashMap JavaDoc();
580
581     private static org.apache.log4j.Logger logger =
582             org.apache.log4j.Logger.getLogger(JahiaSiteMapService.class);
583             
584   /**
585    * Remove the sitemap corresponding to the Jahia user.
586    * Called from org.jahia.data.viewhelper.sitemap.SiteMapEventListener
587    *
588    * @param sessionID The users session id
589    */

590   public void removeSessionSiteMap(String JavaDoc sessionID) {
591       logger.debug("Remove session " + sessionID + " from site map view helper");
592       String JavaDoc sessionPart = ":" + sessionID + ":";
593       Set JavaDoc contextKeys = _treeSiteMapViewHelper.keySet();
594       Iterator JavaDoc it = contextKeys.iterator();
595       while (it.hasNext()) {
596           String JavaDoc context = (String JavaDoc)it.next();
597           if (context.indexOf(sessionPart) != -1) {
598               _treeSiteMapViewHelper.remove(context);
599               // Reinit the iterator to avoid Concurrent Modification Exception.
600
contextKeys = _treeSiteMapViewHelper.keySet();
601               it = contextKeys.iterator();
602           }
603       }
604       contextKeys = _flatSiteMapViewHelper.keySet();
605       it = contextKeys.iterator();
606       while (it.hasNext()) {
607           String JavaDoc context = (String JavaDoc)it.next();
608           if (context.indexOf(sessionPart) != -1) {
609               _flatSiteMapViewHelper.remove(context);
610               // Reinit the iterator to avoid Concurrent Modification Exception.
611
contextKeys = _flatSiteMapViewHelper.keySet();
612               it = contextKeys.iterator();
613           }
614       }
615   }
616   
617   /**
618    * Remove page information from session site map
619    *
620    * @param user
621    * @param page
622    * @param sessionID
623    */

624   public void removeSessionPageSiteMap(JahiaUser user, ContentPage page,
625     String JavaDoc sessionID)
626   {
627     logger.debug("Remove session " + sessionID + " page from site map view helper");
628     String JavaDoc contextPart = user.getUserKey() + ":" + sessionID + ":" + page.getID() + ":";
629     Set JavaDoc contextKeys = _treeSiteMapViewHelper.keySet();
630     Iterator JavaDoc it = contextKeys.iterator();
631     while (it.hasNext()) {
632         String JavaDoc context = (String JavaDoc)it.next();
633         if (context.indexOf(contextPart) != -1) {
634             _treeSiteMapViewHelper.remove(context);
635             // Reinit the iterator to avoid Concurrent Modification Exception.
636
contextKeys = _treeSiteMapViewHelper.keySet();
637             it = contextKeys.iterator();
638         }
639     }
640     contextKeys = _flatSiteMapViewHelper.keySet();
641     it = contextKeys.iterator();
642     while (it.hasNext()) {
643         String JavaDoc context = (String JavaDoc)it.next();
644         if (context.indexOf(contextPart) != -1) {
645             _flatSiteMapViewHelper.remove(context);
646             // Reinit the iterator to avoid Concurrent Modification Exception.
647
contextKeys = _flatSiteMapViewHelper.keySet();
648             it = contextKeys.iterator();
649         }
650     }
651   }
652 }
653
Popular Tags