KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Mar 11 2003 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.Comparator JavaDoc;
45
46 /**
47  * <p>Title: Informations site map page</p>
48  * <p>Description: Implements the 'Comparator' interface to permit the flat site map
49  * sort.
50  * Private class, should be used with the site map view helper.
51  * </p>
52  * <p>Copyright: MAP (Jahia Solutions Sàrl 2002)</p>
53  * <p>Company: Jahia Solutions Sàrl</p>
54  * @author MAP
55  * @version 1.0
56  */

57 final class SiteMapComparator implements Comparator JavaDoc {
58
59     public SiteMapComparator(String JavaDoc languageCode, int orderBy, int sortOrder) {
60         this.languageCode = languageCode;
61         this.sortOrder = sortOrder;
62         this.orderBy = orderBy;
63     }
64
65     /**
66      * Compare the page site map to another page site map regarding the kind of
67      * order and the sort order defined by the method 'setSortOrder', 'setOrderBy'.
68      * @param object The site map object to compare.
69      * @return A negative integer, zero, or a positive integer as this object is
70      * less than, equal to, or greater than the specified object.
71      */

72     public int compare(Object JavaDoc object1, Object JavaDoc object2) {
73         PageSiteMap pageSiteMap1 = (PageSiteMap)object1;
74         PageSiteMap pageSiteMap2 = (PageSiteMap)object2;
75         if (orderBy == FlatSiteMapViewHelper.ORDER_BY_PAGE_TITLE) {
76             String JavaDoc pageTitle1 = pageSiteMap1.getPageTitle(languageCode).toLowerCase();
77             String JavaDoc pageTitle2 = pageSiteMap2.getPageTitle(languageCode).toLowerCase();
78             return sortOrder == FlatSiteMapViewHelper.ASCENDING_ORDER ?
79                                pageTitle1.compareTo(pageTitle2) :
80                                pageTitle2.compareTo(pageTitle1);
81         } else if (orderBy == FlatSiteMapViewHelper.ORDER_BY_PAGE_ID) {
82             return sortOrder == FlatSiteMapViewHelper.ASCENDING_ORDER ?
83                                    pageSiteMap1.getPageID() - pageSiteMap2.getPageID() :
84                                    pageSiteMap2.getPageID() - pageSiteMap1.getPageID();
85         } else if (orderBy == FlatSiteMapViewHelper.ORDER_BY_PAGE_LEVEL) {
86             String JavaDoc pageTitle1 = pageSiteMap1.getPageTitle(languageCode).toLowerCase();
87             String JavaDoc pageTitle2 = pageSiteMap2.getPageTitle(languageCode).toLowerCase();
88             boolean diff = pageSiteMap2.getPageLevel() - pageSiteMap1.getPageLevel() == 0; // Equality test case
89
return sortOrder == FlatSiteMapViewHelper.ASCENDING_ORDER ?
90                                diff ?
91                                    pageTitle1.compareTo(pageTitle2) :
92                                    pageSiteMap1.getPageLevel() - pageSiteMap2.getPageLevel()
93                     :
94                                diff ?
95                                    pageTitle2.compareTo(pageTitle1) :
96                                    pageSiteMap2.getPageLevel() - pageSiteMap1.getPageLevel();
97         }
98         // Let the order as is per default.
99
return 0;
100     }
101
102     // Comparison parameters
103
private String JavaDoc languageCode;
104     private int orderBy;
105     private int sortOrder;
106
107     private static org.apache.log4j.Logger logger =
108             org.apache.log4j.Logger.getLogger(PageSiteMap.class);
109 }
110
Popular Tags