KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > util > CmsTemplateContentListItem


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/util/CmsTemplateContentListItem.java,v $
3  * Date : $Date: 2006/03/27 14:52:59 $
4  * Version: $Revision: 1.8 $
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.jsp.util;
33
34 import org.opencms.file.types.I_CmsResourceType;
35 import org.opencms.i18n.CmsMessages;
36 import org.opencms.jsp.CmsJspActionElement;
37 import org.opencms.main.OpenCms;
38 import org.opencms.util.CmsMacroResolver;
39 import org.opencms.util.CmsStringUtil;
40
41 import java.util.HashMap JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Map JavaDoc;
44
45 import javax.servlet.jsp.JspException JavaDoc;
46
47 /**
48  * Holds information of a single XMLContent list item, either for the page side elements or the center area.<p>
49  *
50  * Use the newInstance() method with correct arguments to create a fully configured item object for list generation.<p>
51  *
52  * @author Andreas Zahner
53  *
54  * @version $Revision: 1.8 $
55  *
56  * @since 6.0.0
57  */

58 public class CmsTemplateContentListItem {
59
60     // TODO: This is really specific to templateone and should not be in the core package!
61
int todo = 0;
62     
63     /** The display area of the list: center. */
64     public static final String JavaDoc DISPLAYAREA_CENTER = "center";
65     
66     /** The display area of the list: left. */
67     public static final String JavaDoc DISPLAYAREA_LEFT = "left";
68     
69     /** The display area of the list: right. */
70     public static final String JavaDoc DISPLAYAREA_RIGHT = "right";
71
72     /** The possible display areas for the template. */
73     public static final String JavaDoc[] DISPLAYAREAS = {DISPLAYAREA_CENTER, DISPLAYAREA_LEFT, DISPLAYAREA_RIGHT};
74
75     /** The name of the list variation: long. */
76     public static final String JavaDoc LISTVARIATION_LONG = "long";
77     
78     /** The name of the list variation: short. */
79     public static final String JavaDoc LISTVARIATION_SHORT = "short";
80
81     /** The possible list variations for a content type. */
82     public static final String JavaDoc[] LISTVARIATIONS = {"", '.' + LISTVARIATION_LONG, '.' + LISTVARIATION_SHORT};
83
84     /** Macro used in property definition names to resolve the list index. */
85     public static final String JavaDoc MACRO_LISTINDEX = "index";
86     
87     /** Macro used in folder String to represent the microsite folder. */
88     public static final String JavaDoc MACRO_MICROSITEFOLDER = "microsite.folder";
89
90     /** Request parameter name for the collector. */
91     public static final String JavaDoc PARAM_COLLECTOR = "collector";
92     
93     /** Request parameter name for the list count. */
94     public static final String JavaDoc PARAM_COUNT = "count";
95     
96     /** Request parameter name maximum number of elements to show. */
97     public static final String JavaDoc PARAM_ELEMENTCOUNT = "elementcount";
98     
99     /** Request parameter name for the xmlcontent folder. */
100     public static final String JavaDoc PARAM_FOLDER = "folder";
101     
102     /** Request parameter name for the xmlcontent listelement. */
103     public static final String JavaDoc PARAM_LISTELEMENT = "listelement";
104
105     /** Name of the property key to set the element collector. */
106     public static final String JavaDoc PROPERTY_LAYOUT_COLLECTOR = "layout.${" + MACRO_LISTINDEX + "}." + PARAM_COLLECTOR;
107     
108     /** Name of the property key to set the element count. */
109     public static final String JavaDoc PROPERTY_LAYOUT_COUNT = "layout.${" + MACRO_LISTINDEX + "}." + PARAM_COUNT;
110     
111     /** Name of the property key to set the folder holding the contents. */
112     public static final String JavaDoc PROPERTY_LAYOUT_FOLDER = "layout.${" + MACRO_LISTINDEX + "}." + PARAM_FOLDER;
113     
114     /** Name of the property key to set the element type. */
115     public static final String JavaDoc PROPERTY_LAYOUT_TYPE = "layout.${" + MACRO_LISTINDEX + "}.type";
116     
117     /** Name of the property key to set the list variation. */
118     public static final String JavaDoc PROPERTY_LAYOUT_VARIATION = "layout.${" + MACRO_LISTINDEX + "}.variation";
119
120     /** The property value for displaying no list. */
121     public static final String JavaDoc PROPERTY_VALUE_NONE = "none";
122
123     private String JavaDoc m_collector;
124     private int m_count;
125     private String JavaDoc m_displayArea;
126     private String JavaDoc m_folder;
127     private String JavaDoc m_listElement;
128     private String JavaDoc m_type;
129     private String JavaDoc m_variation;
130
131     /**
132      * Creates a Map holding the default values for the list creation from given workplace messages.<p>
133      *
134      * @param messages the localized workplace messages
135      *
136      * @return the default values for the list creation from given workplace messages
137      */

138     public static Map JavaDoc getDefaultValuesFromMessages(CmsMessages messages) {
139
140         HashMap JavaDoc result = new HashMap JavaDoc();
141         List JavaDoc resTypes = OpenCms.getResourceManager().getResourceTypes();
142
143         for (int i = 0; i < resTypes.size(); i++) {
144             I_CmsResourceType type = (I_CmsResourceType)resTypes.get(i);
145             if (type.isAdditionalModuleResourceType()) {
146                 // only look for keys if the resource type is an additional one
147
for (int k = 0; k < DISPLAYAREAS.length; k++) {
148                     // loop over the possible display areas
149
String JavaDoc area = DISPLAYAREAS[k];
150                     String JavaDoc typeName = type.getTypeName();
151                     StringBuffer JavaDoc keyPrefix = new StringBuffer JavaDoc(8);
152                     keyPrefix.append("layout.").append(typeName).append('.').append(area).append('.');
153                     // get collector default
154
String JavaDoc key = keyPrefix + PARAM_COLLECTOR;
155                     String JavaDoc value = messages.key(key);
156                     if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)
157                         || value.startsWith(CmsMessages.UNKNOWN_KEY_EXTENSION)) {
158                         // no default collector found, this indicates no list specifications for this type are present
159
continue;
160                     }
161                     result.put(key, value);
162                     // get the default count
163
key = keyPrefix + PARAM_COUNT;
164                     result.put(key, messages.key(key));
165                     // get the default folder
166
key = keyPrefix + PARAM_FOLDER;
167                     result.put(key, messages.key(key));
168                     // get the possible list element JSPs
169
for (int m = 0; m < LISTVARIATIONS.length; m++) {
170                         key = keyPrefix + PARAM_LISTELEMENT + LISTVARIATIONS[m];
171                         value = messages.key(key);
172                         if (!value.startsWith(CmsMessages.UNKNOWN_KEY_EXTENSION)) {
173                             result.put(key, value);
174                         }
175                     }
176                 }
177             }
178         }
179         return result;
180     }
181
182     /**
183      * Factory method to create a new {@link CmsTemplateContentListItem} instance.<p>
184      *
185      * If no type for the specified index can be found, null is returned.<p>
186      *
187      * @param defaultValues holds the default values for collector, count, list folder, JSP element
188      * @param properties the properties of the layout file to use to get the list information
189      * @param microSiteFolder the folder URI of the current microsite
190      * @param displayArea the area where to build the lists (left, center or right)
191      * @param index the index of the content list item to create
192      *
193      * @return a new instance of a {@link CmsTemplateContentListItem}
194      */

195     public static CmsTemplateContentListItem newInstance(
196         Map JavaDoc defaultValues,
197         Map JavaDoc properties,
198         String JavaDoc microSiteFolder,
199         String JavaDoc displayArea,
200         int index) {
201
202         // initialize a macro resolver to resolve the current index and the microsite folder
203
CmsMacroResolver resolver = CmsMacroResolver.newInstance();
204         resolver.addMacro(MACRO_LISTINDEX, Integer.toString(index));
205         resolver.addMacro(MACRO_MICROSITEFOLDER, microSiteFolder);
206
207         // try to get the list type from the properties
208
String JavaDoc type = (String JavaDoc)properties.get(resolver.resolveMacros(PROPERTY_LAYOUT_TYPE));
209         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(type) && !PROPERTY_VALUE_NONE.equals(type)) {
210             // type is specified, create new instance and fill members
211
CmsTemplateContentListItem listItem = new CmsTemplateContentListItem();
212             listItem.setType(type);
213             // create workplace messages key prefix
214
StringBuffer JavaDoc keyPrefix = new StringBuffer JavaDoc(8);
215             keyPrefix.append("layout.").append(type).append('.').append(displayArea).append('.');
216             // determine the collector name to use
217
String JavaDoc collector = (String JavaDoc)properties.get(resolver.resolveMacros(PROPERTY_LAYOUT_COLLECTOR));
218             if (CmsStringUtil.isEmptyOrWhitespaceOnly(collector)) {
219                 collector = (String JavaDoc)defaultValues.get(keyPrefix + PARAM_COLLECTOR);
220             }
221             listItem.setCollector(collector);
222             // determine the count of contents to display
223
String JavaDoc count = (String JavaDoc)properties.get(resolver.resolveMacros(PROPERTY_LAYOUT_COUNT));
224             String JavaDoc defaultCount = (String JavaDoc)defaultValues.get(keyPrefix + PARAM_COUNT);
225             if (CmsStringUtil.isEmptyOrWhitespaceOnly(count)) {
226                 count = defaultCount;
227             }
228             try {
229                 listItem.setCount(Integer.parseInt(count));
230             } catch (NumberFormatException JavaDoc e) {
231                 // no valid number specified, use default value
232
listItem.setCount(Integer.parseInt(defaultCount));
233             }
234             // determine the folder which holds the contents
235
String JavaDoc folder = (String JavaDoc)properties.get(resolver.resolveMacros(PROPERTY_LAYOUT_FOLDER));
236             if (CmsStringUtil.isEmptyOrWhitespaceOnly(folder)) {
237                 folder = (String JavaDoc)defaultValues.get(keyPrefix + PARAM_FOLDER);
238             }
239             listItem.setFolder(resolver.resolveMacros(folder));
240             // determine the list variation to use
241
String JavaDoc variation = (String JavaDoc)properties.get(resolver.resolveMacros(PROPERTY_LAYOUT_VARIATION));
242             if (CmsStringUtil.isEmptyOrWhitespaceOnly(variation)) {
243                 variation = "";
244             } else {
245                 variation = '.' + variation;
246             }
247             // set the JSP element uri creating the list considering the list variation
248
String JavaDoc listElement = (String JavaDoc)defaultValues.get(keyPrefix + PARAM_LISTELEMENT + variation);
249             listItem.setListElement(listElement);
250             // set the display area of the list
251
listItem.setDisplayArea(displayArea);
252             return listItem;
253         } else {
254             // no type specified, return null
255
return null;
256         }
257     }
258
259     /**
260      * Returns the collector to use for this list.<p>
261      *
262      * @return the collector to use for this list
263      */

264     public String JavaDoc getCollector() {
265
266         return m_collector;
267     }
268
269     /**
270      * Returns the maximum count of entries for elements.<p>
271      *
272      * @return the maximum count of entries for elements
273      */

274     public int getCount() {
275
276         return m_count;
277     }
278
279     /**
280      * Returns the display area for this XMLContent list.<p>
281      *
282      * @return the display area for this XMLContent list
283      */

284     public String JavaDoc getDisplayArea() {
285
286         return m_displayArea;
287     }
288
289     /**
290      * Returns the folder which holds the XMLContent.<p>
291      *
292      * @return the folder which holds the XMLContent
293      */

294     public String JavaDoc getFolder() {
295
296         return m_folder;
297     }
298
299     /**
300      * Returns the list element URI to use to display the list.<p>
301      *
302      * @return the list element URI to use to display the list
303      */

304     public String JavaDoc getListElement() {
305
306         return m_listElement;
307     }
308
309     /**
310      * Returns the XMLContent type.<p>
311      *
312      * @return the XMLContent type
313      */

314     public String JavaDoc getType() {
315
316         return m_type;
317     }
318
319     /**
320      * Returns the list variation.<p>
321      *
322      * @return the list variation
323      */

324     public String JavaDoc getVariation() {
325
326         return m_variation;
327     }
328
329     /**
330      * Includes the list generation JSP and creates the necessary request parameters for the included JSP.<p>
331      *
332      * @param jsp the JSP action element
333      * @param showPageLinks flag to determine if page links should be shown
334      *
335      * @throws JspException if inclusion of the element fails
336      */

337     public void includeListItem(CmsJspActionElement jsp, boolean showPageLinks) throws JspException JavaDoc {
338
339         Map JavaDoc properties = new HashMap JavaDoc(4);
340         properties.put(PARAM_COLLECTOR, getCollector());
341         properties.put(PARAM_COUNT, Integer.toString(getCount()));
342         if (showPageLinks) {
343             properties.put(PARAM_ELEMENTCOUNT, Integer.toString(Integer.MAX_VALUE));
344         } else {
345             properties.put(PARAM_ELEMENTCOUNT, Integer.toString(getCount()));
346         }
347         properties.put(PARAM_FOLDER, getFolder());
348         jsp.include(getListElement(), null, properties);
349     }
350
351     /**
352      * Sets the collector to use for this list.<p>
353      *
354      * @param collector the collector to use for this list
355      */

356     protected void setCollector(String JavaDoc collector) {
357
358         m_collector = collector;
359     }
360
361     /**
362      * Sets the maximum count of entries for elements.<p>
363      *
364      * @param count the maximum count of entries for elements
365      */

366     protected void setCount(int count) {
367
368         m_count = count;
369     }
370
371     /**
372      * Sets the display area for this XMLContent list.<p>
373      *
374      * @param displayArea the display area for this XMLContent list
375      */

376     protected void setDisplayArea(String JavaDoc displayArea) {
377
378         m_displayArea = displayArea;
379     }
380
381     /**
382      * Sets the folder which holds the XMLContent.<p>
383      *
384      * @param contentFolder the folder which holds the XMLContent
385      */

386     protected void setFolder(String JavaDoc contentFolder) {
387
388         m_folder = contentFolder;
389     }
390
391     /**
392      * Sets the list element URI to use to display the list.<p>
393      *
394      * @param listElement the list element URI to use to display the list
395      */

396     protected void setListElement(String JavaDoc listElement) {
397
398         m_listElement = listElement;
399     }
400
401     /**
402      * Sets the XMLContent type.<p>
403      *
404      * @param type the XMLContent type to set
405      */

406     protected void setType(String JavaDoc type) {
407
408         m_type = type;
409     }
410
411     /**
412      * Sets the list variation.<p>
413      *
414      * @param variation the list variation to set
415      */

416     protected void setVariation(String JavaDoc variation) {
417
418         m_variation = variation;
419     }
420 }
Popular Tags