KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/collectors/CmsDefaultResourceCollector.java,v $
3  * Date : $Date: 2006/03/27 14:52:50 $
4  * Version: $Revision: 1.11 $
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.CmsResource;
37 import org.opencms.file.CmsResourceFilter;
38 import org.opencms.jsp.CmsJspNavBuilder;
39 import org.opencms.jsp.CmsJspNavElement;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsIllegalArgumentException;
42 import org.opencms.main.CmsLog;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.Arrays JavaDoc;
46 import java.util.Collections JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Map JavaDoc;
50
51 import org.apache.commons.logging.Log;
52
53 /**
54  * A default resource collector to generate some example list of resources from the VFS.<p>
55  *
56  * @author Alexander Kandzior
57  * @author Thomas Weckert
58  *
59  * @version $Revision: 1.11 $
60  *
61  * @since 6.0.0
62  */

63 public class CmsDefaultResourceCollector extends A_CmsResourceCollector {
64
65     /** Static array of the collectors implemented by this class. */
66     private static final String JavaDoc[] COLLECTORS = {
67         "singleFile",
68         "allInFolder",
69         "allInFolderDateReleasedDesc",
70         "allInFolderNavPos",
71         "allInSubTree",
72         "allInSubTreeDateReleasedDesc",
73         "allInSubTreeNavPos"};
74
75     /** Array list for fast collector name lookup. */
76     private static final List JavaDoc COLLECTORS_LIST = Collections.unmodifiableList(Arrays.asList(COLLECTORS));
77
78     /** The log object for this class. */
79     private static final Log LOG = CmsLog.getLog(CmsDefaultResourceCollector.class);
80
81     /**
82      * @see org.opencms.file.collectors.I_CmsResourceCollector#getCollectorNames()
83      */

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

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

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

163     public List JavaDoc getResults(CmsObject cms, String JavaDoc collectorName, String JavaDoc param)
164     throws CmsDataAccessException, CmsException {
165
166         // if action is not set use default
167
if (collectorName == null) {
168             collectorName = COLLECTORS[0];
169         }
170
171         switch (COLLECTORS_LIST.indexOf(collectorName)) {
172             case 0:
173                 // "singleFile"
174
return getSingleFile(cms, param);
175             case 1:
176                 // "allInFolder"
177
return getAllInFolder(cms, param, false);
178             case 2:
179                 // "allInFolderDateReleasedDesc"
180
return allInFolderDateReleasedDesc(cms, param, false);
181             case 3:
182                 // allInFolderNavPos"
183
return allInFolderNavPos(cms, param, false);
184             case 4:
185                 // "allInSubTree"
186
return getAllInFolder(cms, param, true);
187             case 5:
188                 // "allInSubTreeDateReleasedDesc"
189
return allInFolderDateReleasedDesc(cms, param, true);
190             case 6:
191                 // "allInSubTreeNavPos"
192
return allInFolderNavPos(cms, param, true);
193             default:
194                 throw new CmsDataAccessException(Messages.get().container(
195                     Messages.ERR_COLLECTOR_NAME_INVALID_1,
196                     collectorName));
197         }
198     }
199
200     /**
201      * Returns a List of all resources in the folder pointed to by the parameter
202      * sorted by the release date, descending.<p>
203      *
204      * @param cms the current CmsObject
205      * @param param the folder name to use
206      * @param tree if true, look in folder and all child folders, if false, look only in given folder
207      *
208      * @return a List of all resources in the folder pointed to by the parameter
209      * sorted by the release date, descending
210      *
211      * @throws CmsException if something goes wrong
212      */

213     protected List JavaDoc allInFolderDateReleasedDesc(CmsObject cms, String JavaDoc param, boolean tree) throws CmsException {
214
215         CmsCollectorData data = new CmsCollectorData(param);
216         String JavaDoc foldername = CmsResource.getFolderPath(data.getFileName());
217
218         CmsResourceFilter filter = CmsResourceFilter.DEFAULT.addRequireType(data.getType()).addExcludeFlags(
219             CmsResource.FLAG_TEMPFILE);
220         List JavaDoc result = cms.readResources(foldername, filter, tree);
221
222         Collections.sort(result, CmsResource.COMPARE_DATE_RELEASED);
223
224         return shrinkToFit(result, data.getCount());
225     }
226
227     /**
228      * Collects all resources in a folder (or subtree) sorted by the NavPos property.<p>
229      *
230      * @param cms the current user's Cms object
231      * @param param the collector's parameter(s)
232      * @param readSubTree if true, collects all resources in the subtree
233      * @return a List of Cms resources found by the collector
234      * @throws CmsException if something goes wrong
235      *
236      */

237     protected List JavaDoc allInFolderNavPos(CmsObject cms, String JavaDoc param, boolean readSubTree) throws CmsException {
238
239         CmsCollectorData data = new CmsCollectorData(param);
240         String JavaDoc foldername = CmsResource.getFolderPath(data.getFileName());
241
242         CmsResourceFilter filter = CmsResourceFilter.DEFAULT.addRequireType(data.getType()).addExcludeFlags(
243             CmsResource.FLAG_TEMPFILE);
244         List JavaDoc foundResources = cms.readResources(foldername, filter, readSubTree);
245
246         // the Cms resources are saved in a map keyed by their nav elements
247
// to save time sorting the resources by the value of their NavPos property
248
Map JavaDoc navElementMap = new HashMap JavaDoc();
249         for (int i = 0, n = foundResources.size(); i < n; i++) {
250
251             CmsResource resource = (CmsResource)foundResources.get(i);
252             CmsJspNavElement navElement = CmsJspNavBuilder.getNavigationForResource(cms, cms.getSitePath(resource));
253
254             // check if the resource has the NavPos property set or not
255
if (navElement != null && navElement.getNavPosition() != Float.MAX_VALUE) {
256                 navElementMap.put(navElement, resource);
257             } else if (LOG.isInfoEnabled()) {
258                 // printing a log messages makes it a little easier to indentify
259
// resources having not the NavPos property set
260
LOG.info(Messages.get().getBundle().key(Messages.LOG_RESOURCE_WITHOUT_NAVPROP_1, navElement.getResourceName()));
261             }
262         }
263
264         List JavaDoc result = null;
265         if (navElementMap.size() == foundResources.size()) {
266             // all found resources have the NavPos property set
267
// sort the nav. elements, and pull the found Cms resources
268
// from the map in the correct order into a list
269
List JavaDoc navElementList = new ArrayList JavaDoc(navElementMap.keySet());
270             result = new ArrayList JavaDoc();
271
272             Collections.sort(navElementList);
273             for (int i = 0, n = navElementList.size(); i < n; i++) {
274
275                 CmsJspNavElement navElement = (CmsJspNavElement)navElementList.get(i);
276                 result.add(navElementMap.get(navElement));
277             }
278         } else {
279             // not all found resources have the NavPos property set
280
// sort the resources by release date as usual
281
result = foundResources;
282             Collections.sort(result, CmsResource.COMPARE_DATE_RELEASED);
283         }
284
285         return shrinkToFit(result, data.getCount());
286     }
287
288     /**
289      * Returns all resources in the folder pointed to by the parameter.<p>
290      *
291      * @param cms the current OpenCms user context
292      * @param param the folder name to use
293      * @param tree if true, look in folder and all child folders, if false, look only in given folder
294      *
295      * @return all resources in the folder matching the given criteria
296      *
297      * @throws CmsException if something goes wrong
298      * @throws CmsIllegalArgumentException if the given param argument is not a link to a single file
299      *
300      */

301     protected List JavaDoc getAllInFolder(CmsObject cms, String JavaDoc param, boolean tree)
302     throws CmsException, CmsIllegalArgumentException {
303
304         CmsCollectorData data = new CmsCollectorData(param);
305         String JavaDoc foldername = CmsResource.getFolderPath(data.getFileName());
306
307         CmsResourceFilter filter = CmsResourceFilter.DEFAULT.addRequireType(data.getType()).addExcludeFlags(
308             CmsResource.FLAG_TEMPFILE);
309         List JavaDoc result = cms.readResources(foldername, filter, tree);
310
311         Collections.sort(result, CmsResource.COMPARE_ROOT_PATH);
312         Collections.reverse(result);
313
314         return shrinkToFit(result, data.getCount());
315     }
316
317     /**
318      * Returns a List containing the resources pointed to by the parameter.<p>
319      *
320      * @param cms the current CmsObject
321      * @param param the name of the file to load
322      *
323      * @return a List containing the resources pointed to by the parameter
324      *
325      * @throws CmsException if something goes wrong
326      */

327     protected List JavaDoc getSingleFile(CmsObject cms, String JavaDoc param) throws CmsException {
328
329         if ((param == null) || (cms == null)) {
330             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_COLLECTOR_PARAM_SINGLE_FILE_0));
331         }
332
333         // create a list and return it
334
ArrayList JavaDoc result = new ArrayList JavaDoc(1);
335         result.add(cms.readFile(param));
336         return result;
337     }
338 }
Popular Tags