KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Feb 4 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.util.Arrays JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.List JavaDoc;
47
48 import org.jahia.params.ParamBean;
49 import org.jahia.services.pages.ContentPage;
50 import org.jahia.services.usermanager.JahiaUser;
51
52 /**
53  * <p>The Jahia Shared Modification is: Jahia View Helper</p>
54  *
55  * <p>Description:
56  * Create a flat tree from Jahia page tree structure destinated to display a site
57  * map.
58  * </p>
59  * <p>Copyright: MAP (Jahia Solutions Sàrl 2002)</p>
60  * <p>Company: Jahia Solutions Sàrl</p>
61  * @author MAP
62  * @version 1.0
63  */

64 public class FlatSiteMapViewHelper extends SiteMapViewHelper {
65
66     // Flat site map sort flag.
67
public static final int ORDER_AS_IS = -1;
68     public static final int ORDER_BY_PAGE_TITLE = 0;
69     public static final int ORDER_BY_PAGE_ID = 1;
70     public static final int ORDER_BY_PAGE_LEVEL = 2;
71     public static final int ASCENDING_ORDER = 0;
72     public static final int DESCENDING_ORDER = 1;
73
74     /**
75      * Create a view helper on a entire Jahia site map restricted to the actual
76      * logged user.
77      *
78      * @param user The actual user logged in Jahia.
79      * @param startPage The start page for site map.
80      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
81      * @param defaultMaxLevel Site map expansion default level max.
82      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
83      * ARCHIVED_PAGE_INFOS constants.
84      * @param languageCode Get the page with the specified language code.
85      */

86     public FlatSiteMapViewHelper(JahiaUser user, ContentPage startPage,
87                                  int pageInfosFlag, String JavaDoc languageCode,
88                                  int defaultMaxLevel) {
89         super(user, startPage, pageInfosFlag, languageCode, defaultMaxLevel);
90         // Initialize flat site map
91
_sortedPageSiteMap = new PageSiteMap[getJahiaPageSiteMap().size()];
92         getJahiaPageSiteMap().toArray(_sortedPageSiteMap);
93         // Set the default sort.
94
sortSiteMap(languageCode, ORDER_BY_PAGE_TITLE, ASCENDING_ORDER);
95     }
96
97     /**
98      * Create a view helper on a entire Jahia site map restricted to the actual
99      * logged user.
100      *
101      * @param user The actual user logged in Jahia.
102      * @param startPage The start page for site map.
103      * @param pageInfosFlag Kind of page infos desired. This parameter can associate
104      * @param defaultMaxLevel Site map expansion default level max.
105      * the ContentPage ACTIVE_PAGE_INFOS, STAGING_PAGE_INFOS and
106      * ARCHIVED_PAGE_INFOS constants.
107      * @param languageCode Get the page with the specified language code.
108      * @param jParams The ParamBean.
109      */

110     public FlatSiteMapViewHelper(JahiaUser user, ContentPage startPage,
111                                  int pageInfosFlag, String JavaDoc languageCode,
112                                  int defaultMaxLevel, PagesFilter pagesFilter,
113                                  ParamBean jParams) {
114         super(user, startPage, pageInfosFlag, languageCode, defaultMaxLevel, pagesFilter, jParams);
115         // Initialize flat site map
116
_sortedPageSiteMap = new PageSiteMap[getJahiaPageSiteMap().size()];
117         getJahiaPageSiteMap().toArray(_sortedPageSiteMap);
118         // Set the default sort.
119
sortSiteMap(languageCode, ORDER_BY_PAGE_TITLE, ASCENDING_ORDER);
120         
121     }
122     
123     public FlatSiteMapViewHelper(JahiaUser user, List JavaDoc startPages,
124             int pageInfosFlag, String JavaDoc languageCode,
125             int defaultMaxLevel, PagesFilter pagesFilter,
126             ParamBean jParams) {
127         super(user, startPages, pageInfosFlag, languageCode, defaultMaxLevel, true, pagesFilter, jParams);
128         // Initialize flat site map
129
_sortedPageSiteMap = new PageSiteMap[getJahiaPageSiteMap().size()];
130         getJahiaPageSiteMap().toArray(_sortedPageSiteMap);
131         // Set the default sort.
132
sortSiteMap(languageCode, ORDER_BY_PAGE_TITLE, ASCENDING_ORDER);
133     }
134
135     /**
136      * Sort the site map by given criteriums such as page title, page ID, etc...
137      * @param languageCode The language code in which the sort should be made.
138      * @param orderBy The sort key. One of ORDER_AS_IS, ORDER_BY_PAGE_TITLE,
139      * ORDER_BY_PAGE_ID, ORDER_BY_PAGE_LEVEL.
140      * @param sortOrder One of ASCENDING_ORDER or DESCENDING_ORDER order.
141      */

142     public void sortSiteMap(String JavaDoc languageCode, int orderBy, int sortOrder) {
143         if (languageCode == null) {
144             languageCode = "en"; /** @todo : Set a better default language. */
145         }
146         _sortOrder = sortOrder;
147         SiteMapComparator siteMapComparator = new SiteMapComparator(languageCode, orderBy, sortOrder);
148         Arrays.sort(_sortedPageSiteMap, siteMapComparator);
149     }
150
151     /**
152      * Return the last site map ASCENDING_ORDER or DESCENDING_ORDER order.
153      * @return The sort order.
154      */

155     public int getSortOrder() {
156         return _sortOrder;
157     }
158
159     /**
160      * Get a Jahia page content indexed by the sorted flat site map.
161      * @param index The sorted flat site map index.
162      * @return The Jahia page content object.
163      */

164     public ContentPage getContentPage(int index) {
165         PageSiteMap pageSiteMap = _sortedPageSiteMap[index];
166         return pageSiteMap.isDisplayable() ? lookupContentPage(pageSiteMap.getPageID()) : null;
167     }
168
169     /**
170      * Get a page title in a given language indexed by the sorted flat site map.
171      * @param index The sorted flat site map index.
172      * @param languageCode The language code in which the title should be displayed.
173      * @return The page title in a given language indexed by the sorted flat site map.
174      */

175     public String JavaDoc getPageTitle(int index, String JavaDoc languageCode) {
176         return _sortedPageSiteMap[index].getPageTitle(languageCode);
177     }
178
179     /**
180      * Get the page ID indexed by the sorted flat site map.
181      * @param index The sorted flat site map index.
182      * @return The page ID indexed by the sorted flat site map.
183      */

184     public int getPageID(int index) {
185         return _sortedPageSiteMap[index].getPageID();
186     }
187
188     /**
189      * Return the index of a given pageID, -1 if not found
190      *
191      * @param pageID
192      * @return
193      */

194     public int getPageIndex(int pageID){
195         PageSiteMap pageSiteMap = null;
196         for ( int i=0 ; i<_sortedPageSiteMap.length ; i++ ){
197             pageSiteMap = _sortedPageSiteMap[i];
198             if ( pageSiteMap.getPageID() == pageID ){
199                 return i;
200             }
201         }
202         return -1;
203     }
204
205     /**
206      * Get the page level indexed by the sorted flat site map. The there is two
207      * same page level the page title is then used to sorted the level.
208      * @param index The sorted flat site map index.
209      * @return The page level indexed by the sorted flat site map index.
210      */

211     public int getPageLevel(int index) {
212         return _sortedPageSiteMap[index].getPageLevel();
213     }
214
215     /**
216      * Get the parent page ID indexed by the sorted flat site map.
217      * @param index The sorted flat site map index.
218      * @return The parent page ID indexed by the sorted flat site map index.
219      */

220     public int getParentPageID(int index) {
221         return _sortedPageSiteMap[index].getParentPageID();
222     }
223
224     /**
225      * Return the size of the sorted array corresponding to the number of page
226      * included in the site map.
227      * @return The flat site map size.
228      */

229     public int size() {
230         return _sortedPageSiteMap.length;
231     }
232
233     private int _currentLevel = 0;
234     private int _defaultMaxLevel;
235     private int _maxLevel; // Contain the max page level in the tree
236
private HashMap JavaDoc _availableLanguageCodeMap = new HashMap JavaDoc();
237     private JahiaUser _user;
238     private int _pageInfosFlag;
239     private String JavaDoc _languageCode;
240
241     // Sort flags for flat site map.
242
private int _sortOrder;
243     private int _orderBy;
244     private PageSiteMap[] _sortedPageSiteMap;
245
246     private static org.apache.log4j.Logger logger =
247             org.apache.log4j.Logger.getLogger(SiteMapViewHelper.class);
248
249 }
250
Popular Tags