KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/collectors/CmsPriorityResourceCollector.java,v $
3  * Date : $Date: 2005/07/07 16:25:27 $
4  * Version: $Revision: 1.14 $
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.CmsDataAccessException;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsProject;
37 import org.opencms.file.CmsProperty;
38 import org.opencms.file.CmsResource;
39 import org.opencms.file.CmsResourceFilter;
40 import org.opencms.main.CmsException;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.Arrays JavaDoc;
44 import java.util.Collections JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.List JavaDoc;
47
48 /**
49  * A collector to fetch sorted XML contents in a folder or subtree based on their priority
50  * and date or title values.<p>
51  *
52  * The date or title information has to be stored as property for each resource.<p>
53  *
54  * @author Andreas Zahner
55  *
56  * @version $Revision: 1.14 $
57  *
58  * @since 6.0.0
59  */

60 public class CmsPriorityResourceCollector extends A_CmsResourceCollector {
61
62     /** The standard priority value if no value was set on resource. */
63     public static final int PRIORITY_STANDARD = 3;
64
65     /** The name of the channel property to read. */
66     public static final String JavaDoc PROPERTY_CHANNEL = "collector.channel";
67
68     /** The name of the priority property to read. */
69     public static final String JavaDoc PROPERTY_PRIORITY = "collector.priority";
70
71     /** Static array of the collectors implemented by this class. */
72     private static final String JavaDoc[] COLLECTORS = {
73         "allInFolderPriorityDateAsc",
74         "allInSubTreePriorityDateAsc",
75         "allInFolderPriorityDateDesc",
76         "allInSubTreePriorityDateDesc",
77         "allInFolderPriorityTitleDesc",
78         "allInSubTreePriorityTitleDesc",
79         "allMappedToUriPriorityDateAsc",
80         "allMappedToUriPriorityDateDesc"};
81
82     /** Array list for fast collector name lookup. */
83     private static final List JavaDoc COLLECTORS_LIST = Collections.unmodifiableList(Arrays.asList(COLLECTORS));
84
85     /**
86      * @see org.opencms.file.collectors.I_CmsResourceCollector#getCollectorNames()
87      */

88     public List JavaDoc getCollectorNames() {
89
90         return COLLECTORS_LIST;
91     }
92
93     /**
94      * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateLink(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
95      */

96     public String JavaDoc getCreateLink(CmsObject cms, String JavaDoc collectorName, String JavaDoc param) throws CmsException {
97
98         // if action is not set, use default action
99
if (collectorName == null) {
100             collectorName = COLLECTORS[1];
101         }
102
103         switch (COLLECTORS_LIST.indexOf(collectorName)) {
104             case 0:
105             case 2:
106             case 4:
107                 // "allInFolderPriorityDateAsc", "allInFolderPriorityDateDesc" or "allInFolderPriorityTitleDesc"
108
return getCreateInFolder(cms, param);
109             case 1:
110             case 3:
111             case 5:
112             case 6:
113             case 7:
114                 // "allInSubTreePriorityDateAsc", "allInSubTreePriorityDateDesc" or "allInSubTreePriorityTitleDesc"
115
// "allMappedToUriPriorityDateAsc", "allMappedToUriPriorityDateDesc"
116
return null;
117             default:
118                 throw new CmsDataAccessException(Messages.get().container(
119                     Messages.ERR_COLLECTOR_NAME_INVALID_1,
120                     collectorName));
121         }
122     }
123
124     /**
125      * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateParam(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
126      */

127     public String JavaDoc getCreateParam(CmsObject cms, String JavaDoc collectorName, String JavaDoc param) throws CmsDataAccessException {
128
129         // if action is not set, use default action
130
if (collectorName == null) {
131             collectorName = COLLECTORS[1];
132         }
133
134         switch (COLLECTORS_LIST.indexOf(collectorName)) {
135             case 0:
136             case 2:
137             case 4:
138                 // "allInFolderPriorityDateAsc", "allInFolderPriorityDateDesc" or "allInFolderPriorityTitleDesc"
139
return param;
140             case 1:
141             case 3:
142             case 5:
143             case 6:
144             case 7:
145                 // "allInSubTreePriorityDateAsc", "allInSubTreePriorityDateDesc" or "allInSubTreePriorityTitleDesc"
146
// "allMappedToUriPriorityDateAsc", "allMappedToUriPriorityDateDesc"
147
return null;
148             default:
149                 throw new CmsDataAccessException(Messages.get().container(
150                     Messages.ERR_COLLECTOR_NAME_INVALID_1,
151                     collectorName));
152         }
153     }
154
155     /**
156      * @see org.opencms.file.collectors.I_CmsResourceCollector#getResults(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
157      */

158     public List JavaDoc getResults(CmsObject cms, String JavaDoc collectorName, String JavaDoc param)
159     throws CmsException, CmsDataAccessException {
160
161         // if action is not set use default
162
if (collectorName == null) {
163             collectorName = COLLECTORS[0];
164         }
165
166         switch (COLLECTORS_LIST.indexOf(collectorName)) {
167
168             case 0:
169                 // "allInFolderPriorityDateAsc"
170
return allInFolderPriorityDate(cms, param, false, true);
171             case 1:
172                 // "allInSubTreePriorityDateAsc"
173
return allInFolderPriorityDate(cms, param, true, true);
174             case 2:
175                 // "allInFolderPriorityDateDesc"
176
return allInFolderPriorityDate(cms, param, false, false);
177             case 3:
178                 // "allInSubTreePriorityDateDesc"
179
return allInFolderPriorityDate(cms, param, true, false);
180             case 4:
181                 // "allInFolderPriorityTitleDesc"
182
return allInFolderPriorityTitle(cms, param, false);
183             case 5:
184                 // "allInSubTreePriorityTitleDesc"
185
return allInFolderPriorityTitle(cms, param, true);
186             case 6:
187                 // "allMappedToUriPriorityDateAsc"
188
return allMappedToUriPriorityDate(cms, param, true);
189             case 7:
190                 // "allMappedToUriPriorityDateDesc"
191
return allMappedToUriPriorityDate(cms, param, false);
192             default:
193                 throw new CmsDataAccessException(Messages.get().container(
194                     Messages.ERR_COLLECTOR_NAME_INVALID_1,
195                     collectorName));
196         }
197
198     }
199
200     /**
201      * Returns a list of all resource in a specified folder sorted by priority, then date ascending or descending.<p>
202      *
203      * @param cms the current OpenCms user context
204      * @param param the folder name to use
205      * @param tree if true, look in folder and all child folders, if false, look only in given folder
206      * @param asc if true, the date sort order is ascending, otherwise descending
207      *
208      * @return all resources in the folder matching the given criteria
209      *
210      * @throws CmsException if something goes wrong
211      */

212     protected List JavaDoc allInFolderPriorityDate(CmsObject cms, String JavaDoc param, boolean tree, boolean asc) throws CmsException {
213
214         CmsCollectorData data = new CmsCollectorData(param);
215         String JavaDoc foldername = CmsResource.getFolderPath(data.getFileName());
216
217         CmsResourceFilter filter = CmsResourceFilter.DEFAULT.addRequireType(data.getType()).addExcludeFlags(
218             CmsResource.FLAG_TEMPFILE);
219         List JavaDoc result = cms.readResources(foldername, filter, tree);
220
221         // create priority comparator to use to sort the resources
222
CmsPriorityDateResourceComparator comparator = new CmsPriorityDateResourceComparator(cms, asc);
223         Collections.sort(result, comparator);
224
225         return shrinkToFit(result, data.getCount());
226     }
227
228     /**
229      * Returns a list of all resource in a specified folder sorted by priority descending, then Title ascending.<p>
230      *
231      * @param cms the current OpenCms user context
232      * @param param the folder name to use
233      * @param tree if true, look in folder and all child folders, if false, look only in given folder
234      *
235      * @return all resources in the folder matching the given criteria
236      *
237      * @throws CmsException if something goes wrong
238      */

239     protected List JavaDoc allInFolderPriorityTitle(CmsObject cms, String JavaDoc param, boolean tree) throws CmsException {
240
241         CmsCollectorData data = new CmsCollectorData(param);
242         String JavaDoc foldername = CmsResource.getFolderPath(data.getFileName());
243
244         CmsResourceFilter filter = CmsResourceFilter.DEFAULT.addRequireType(data.getType()).addExcludeFlags(
245             CmsResource.FLAG_TEMPFILE);
246         List JavaDoc result = cms.readResources(foldername, filter, tree);
247
248         // create priority comparator to use to sort the resources
249
CmsPriorityTitleResourceComparator comparator = new CmsPriorityTitleResourceComparator(cms);
250         Collections.sort(result, comparator);
251
252         return shrinkToFit(result, data.getCount());
253     }
254
255     /**
256      * Returns a list of all resource from specified folder that have been mapped to
257      * the currently requested uri, sorted by priority, then date ascending or descending.<p>
258      *
259      * @param cms the current OpenCms user context
260      * @param param the folder name to use
261      * @param asc if true, the date sort order is ascending, otherwise descending
262      *
263      * @return all resources in the folder matching the given criteria
264      *
265      * @throws CmsException if something goes wrong
266      */

267     protected List JavaDoc allMappedToUriPriorityDate(CmsObject cms, String JavaDoc param, boolean asc) throws CmsException {
268
269         CmsCollectorData data = new CmsCollectorData(param);
270         String JavaDoc foldername = CmsResource.getFolderPath(data.getFileName());
271
272         CmsResourceFilter filter = CmsResourceFilter.DEFAULT.addRequireType(data.getType()).addExcludeFlags(
273             CmsResource.FLAG_TEMPFILE);
274
275         List JavaDoc result = cms.readResources(foldername, filter, true);
276         List JavaDoc mapped = new ArrayList JavaDoc();
277
278         // sort out the resources mapped to the current page
279
Iterator JavaDoc i = result.iterator();
280         while (i.hasNext()) {
281             CmsResource res = (CmsResource)i.next();
282             // read all properties - reason: comparator will do this later anyway, so we just prefill the cache
283
CmsProperty prop = cms.readPropertyObject(res, PROPERTY_CHANNEL, false);
284             if (!prop.isNullProperty()) {
285                 if (CmsProject.isInsideProject(prop.getValueList(), cms.getRequestContext().getSiteRoot()
286                     + cms.getRequestContext().getUri())) {
287                     mapped.add(res);
288                 }
289             }
290         }
291
292         if (mapped.isEmpty()) {
293             // nothing was mapped, no need for further processing
294
return mapped;
295         }
296
297         // create priority comparator to use to sort the resources
298
CmsPriorityDateResourceComparator comparator = new CmsPriorityDateResourceComparator(cms, asc);
299         Collections.sort(mapped, comparator);
300
301         return shrinkToFit(mapped, data.getCount());
302     }
303 }
Popular Tags