KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > collectors > CmsPriorityTitleResourceComparator


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/collectors/CmsPriorityTitleResourceComparator.java,v $
3  * Date : $Date: 2005/09/14 15:44:58 $
4  * Version: $Revision: 1.15 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.file.collectors;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProperty;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.file.CmsResource;
38 import org.opencms.main.CmsException;
39 import org.opencms.util.CmsStringUtil;
40
41 import java.text.Collator JavaDoc;
42 import java.util.Comparator JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Map JavaDoc;
46
47 /**
48  * Comparator for sorting resource objects based on priority and title.<p>
49  *
50  * Serves as {@link java.util.Comparator} for resources and as comparator key for the resource
51  * at the same time. Uses lazy initializing of comparator keys in a resource.<p>
52  *
53  * @author Andreas Zahner
54  *
55  * @version $Revision: 1.15 $
56  *
57  * @since 6.0.0
58  */

59 public class CmsPriorityTitleResourceComparator implements Comparator JavaDoc {
60
61     /** The current OpenCms user context. */
62     private CmsObject m_cms;
63
64     /** The interal map of comparator keys. */
65     private Map JavaDoc m_keys;
66
67     /** The priority of this comparator key. */
68     private int m_priority;
69
70     /** The title of this comparator key. */
71     private String JavaDoc m_title;
72
73     /**
74      * Creates a new instance of this comparator key.<p>
75      *
76      * @param cms the current OpenCms user context
77      */

78     public CmsPriorityTitleResourceComparator(CmsObject cms) {
79
80         m_cms = cms;
81         m_keys = new HashMap JavaDoc();
82     }
83
84     /**
85      * Creates a new instance of this comparator key.<p>
86      *
87      * @param resource the resource to create the key for
88      * @param cms the current OpenCms user context
89      *
90      * @return a new instance of this comparatoy key
91      */

92     private static CmsPriorityTitleResourceComparator create(CmsResource resource, CmsObject cms) {
93
94         CmsPriorityTitleResourceComparator result = new CmsPriorityTitleResourceComparator(null);
95         result.init(resource, cms);
96         return result;
97     }
98
99     /**
100      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
101      */

102     public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
103
104         if ((arg0 == arg1) || !(arg0 instanceof CmsResource) || !(arg1 instanceof CmsResource)) {
105             return 0;
106         }
107
108         CmsResource res0 = (CmsResource)arg0;
109         CmsResource res1 = (CmsResource)arg1;
110
111         CmsPriorityTitleResourceComparator key0 = (CmsPriorityTitleResourceComparator)m_keys.get(res0.getStructureId());
112         CmsPriorityTitleResourceComparator key1 = (CmsPriorityTitleResourceComparator)m_keys.get(res1.getStructureId());
113
114         if (key0 == null) {
115             // initialize key if null
116
key0 = CmsPriorityTitleResourceComparator.create(res0, m_cms);
117             m_keys.put(res0.getStructureId(), key0);
118         }
119         if (key1 == null) {
120             // initialize key if null
121
key1 = CmsPriorityTitleResourceComparator.create(res1, m_cms);
122             m_keys.put(res1.getStructureId(), key1);
123         }
124
125         if (key0.getPriority() > key1.getPriority()) {
126             return -1;
127         }
128
129         if (key0.getPriority() < key1.getPriority()) {
130             return 1;
131         }
132         
133         // sort by title property depending on the locale
134
Collator JavaDoc collator = Collator.getInstance(m_cms.getRequestContext().getLocale());
135         return collator.compare(key0.getTitle(), key1.getTitle());
136     }
137
138     /**
139      * Returns the priority of this resource comparator key.<p>
140      *
141      * @return the priority of this resource comparator key
142      */

143     public int getPriority() {
144
145         return m_priority;
146     }
147
148     /**
149      * Returns the title of this resource comparator key.<p>
150      *
151      * @return the title of this resource comparator key
152      */

153     public String JavaDoc getTitle() {
154
155         return m_title;
156     }
157
158     /**
159      * Initializes the comparator key based on the member variables.<p>
160      *
161      * @param resource the resource to use
162      * @param cms the current OpenCms user contxt
163      */

164     private void init(CmsResource resource, CmsObject cms) {
165
166         List JavaDoc properties;
167
168         try {
169             properties = cms.readPropertyObjects(resource, false);
170         } catch (CmsException e) {
171             m_priority = 0;
172             m_title = "";
173             return;
174         }
175
176         try {
177             m_priority = Integer.parseInt(CmsProperty.get(CmsPriorityResourceCollector.PROPERTY_PRIORITY, properties).getValue());
178         } catch (NumberFormatException JavaDoc e) {
179             m_priority = CmsPriorityResourceCollector.PRIORITY_STANDARD;
180         }
181
182         m_title = CmsProperty.get(CmsPropertyDefinition.PROPERTY_TITLE, properties).getValue();
183         if (CmsStringUtil.isEmpty(m_title)) {
184             m_title = "";
185         }
186     }
187 }
Popular Tags