KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/collectors/CmsPriorityDateResourceComparator.java,v $
3  * Date : $Date: 2005/07/07 16:25:27 $
4  * Version: $Revision: 1.12 $
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.CmsResource;
37 import org.opencms.main.CmsException;
38
39 import java.util.Comparator JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /**
45  * Comparator for sorting resource objects based on priority and date.<p>
46  *
47  * Serves as {@link java.util.Comparator} for resources and as comparator key for the resource
48  * at the same time. Uses lazy initializing of comparator keys in a resource.<p>
49  *
50  * @author Alexander Kandzior
51  * @author Andreas Zahner
52  *
53  * @version $Revision: 1.12 $
54  *
55  * @since 6.0.0
56  */

57 public class CmsPriorityDateResourceComparator implements Comparator JavaDoc {
58
59     /** The name of the date property to read. */
60     public static final String JavaDoc PROPERTY_DATE = "collector.date";
61
62     /** The date sort order. */
63     private boolean m_asc;
64
65     /** The current OpenCms user context. */
66     private CmsObject m_cms;
67
68     /** The date of this comparator key. */
69     private long m_date;
70
71     /** The interal map of comparator keys. */
72     private Map JavaDoc m_keys;
73
74     /** The priority of this comparator key. */
75     private int m_priority;
76
77     /**
78      * Creates a new instance of this comparator key.<p>
79      *
80      * @param cms the current OpenCms user context
81      * @param asc if true, the date sort order is ascending, otherwise descending
82      */

83     public CmsPriorityDateResourceComparator(CmsObject cms, boolean asc) {
84
85         m_cms = cms;
86         m_asc = asc;
87         m_keys = new HashMap JavaDoc();
88     }
89
90     /**
91      * Creates a new instance of this comparator key.<p>
92      *
93      * @param resource the resource to create the key for
94      * @param cms the current OpenCms user context
95      *
96      * @return a new instance of this comparatoy key
97      */

98     private static CmsPriorityDateResourceComparator create(CmsResource resource, CmsObject cms) {
99
100         CmsPriorityDateResourceComparator result = new CmsPriorityDateResourceComparator(null, false);
101         result.init(resource, cms);
102         return result;
103     }
104
105     /**
106      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
107      */

108     public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
109
110         if ((arg0 == arg1) || !(arg0 instanceof CmsResource) || !(arg1 instanceof CmsResource)) {
111             return 0;
112         }
113
114         CmsResource res0 = (CmsResource)arg0;
115         CmsResource res1 = (CmsResource)arg1;
116
117         CmsPriorityDateResourceComparator key0 = (CmsPriorityDateResourceComparator)m_keys.get(res0.getStructureId());
118         CmsPriorityDateResourceComparator key1 = (CmsPriorityDateResourceComparator)m_keys.get(res1.getStructureId());
119
120         if (key0 == null) {
121             // initialize key if null
122
key0 = CmsPriorityDateResourceComparator.create(res0, m_cms);
123             m_keys.put(res0.getStructureId(), key0);
124         }
125         if (key1 == null) {
126             // initialize key if null
127
key1 = CmsPriorityDateResourceComparator.create(res1, m_cms);
128             m_keys.put(res1.getStructureId(), key1);
129         }
130
131         // check priority
132
if (key0.getPriority() > key1.getPriority()) {
133             return -1;
134         }
135
136         if (key0.getPriority() < key1.getPriority()) {
137             return 1;
138         }
139
140         if (m_asc) {
141             // sort in ascending order
142
if (key0.getDate() > key1.getDate()) {
143                 return 1;
144             }
145             if (key0.getDate() < key1.getDate()) {
146                 return -1;
147             }
148         } else {
149             // sort in descending order
150
if (key0.getDate() > key1.getDate()) {
151                 return -1;
152             }
153             if (key0.getDate() < key1.getDate()) {
154                 return 1;
155             }
156         }
157
158         return 0;
159     }
160
161     /**
162      * Returns the date of this resource comparator key.<p>
163      *
164      * @return the date of this resource comparator key
165      */

166     public long getDate() {
167
168         return m_date;
169     }
170
171     /**
172      * Returns the priority of this resource comparator key.<p>
173      *
174      * @return the priority of this resource comparator key
175      */

176     public int getPriority() {
177
178         return m_priority;
179     }
180
181     /**
182      * Initializes the comparator key based on the member variables.<p>
183      *
184      * @param resource the resource to use
185      * @param cms the current OpenCms user contxt
186      */

187     private void init(CmsResource resource, CmsObject cms) {
188
189         List JavaDoc properties;
190
191         try {
192             properties = cms.readPropertyObjects(resource, false);
193         } catch (CmsException e) {
194             m_priority = 0;
195             m_date = 0;
196             return;
197         }
198
199         try {
200             m_priority = Integer.parseInt(CmsProperty.get(CmsPriorityResourceCollector.PROPERTY_PRIORITY, properties).getValue());
201         } catch (NumberFormatException JavaDoc e) {
202             m_priority = CmsPriorityResourceCollector.PRIORITY_STANDARD;
203         }
204
205         try {
206             m_date = Long.parseLong(CmsProperty.get(PROPERTY_DATE, properties).getValue());
207         } catch (NumberFormatException JavaDoc e) {
208             m_date = 0;
209         }
210     }
211
212 }
Popular Tags