KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > CmsTemplateBean


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/CmsTemplateBean.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.43 $
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.frontend.templateone;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsFolder;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsPropertyDefinition;
38 import org.opencms.file.CmsResource;
39 import org.opencms.file.CmsResourceFilter;
40 import org.opencms.i18n.CmsMessages;
41 import org.opencms.jsp.util.CmsTemplateContentListItem;
42 import org.opencms.loader.CmsLoaderException;
43 import org.opencms.main.CmsException;
44 import org.opencms.main.CmsLog;
45 import org.opencms.main.OpenCms;
46 import org.opencms.util.CmsStringUtil;
47 import org.opencms.workplace.CmsWorkplace;
48 import org.opencms.xml.content.CmsXmlContent;
49 import org.opencms.xml.content.CmsXmlContentFactory;
50
51 import java.io.IOException JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.Collections JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.List JavaDoc;
56 import java.util.Locale JavaDoc;
57 import java.util.Map JavaDoc;
58
59 import javax.servlet.http.HttpServletRequest JavaDoc;
60 import javax.servlet.http.HttpServletResponse JavaDoc;
61 import javax.servlet.jsp.JspException JavaDoc;
62 import javax.servlet.jsp.JspWriter JavaDoc;
63 import javax.servlet.jsp.PageContext JavaDoc;
64
65 import org.apache.commons.logging.Log;
66
67 /**
68  * Provides methods to create the HTML for the frontend output in the main JSP template one.<p>
69  *
70  * @author Andreas Zahner
71  *
72  * @version $Revision: 1.43 $
73  *
74  * @since 6.0.0
75  */

76 public class CmsTemplateBean extends CmsTemplateBase {
77
78     /** File name of the website area configuration file. */
79     public static final String JavaDoc FILE_CONFIG_COMMON = "configuration_common";
80
81     /** File name of the CSS style sheet to use. */
82     public static final String JavaDoc FILE_CSS = "style.css";
83
84     /** File name of the accessible CSS style sheet to use. */
85     public static final String JavaDoc FILE_CSS_ACCESSIBLE = "style_accessible.css";
86
87     /** File name of the head links configuration file. */
88     public static final String JavaDoc FILE_LINKS_HEAD = "configuration_links";
89
90     /** Folder path to the included JSP elements. */
91     public static final String JavaDoc FOLDER_ELEMENTS = "../elements/";
92
93     /** Name of the resource bundle containing the localized messages. */
94     public static final String JavaDoc MESSAGE_BUNDLE = "templateone";
95
96     /** Name of the frontend module in OpenCms. */
97     public static final String JavaDoc MODULE_NAME = "org.opencms.frontend.templateone";
98
99     /** Request parameter name to show the accessible version of a page. */
100     public static final String JavaDoc PARAM_ACCESSIBLE = "accessible";
101
102     /** Request parameter name to show the common version of a page. */
103     public static final String JavaDoc PARAM_COMMON = "common";
104
105     /** Request parameter name for the help page URI. */
106     public static final String JavaDoc PARAM_HELPURI = "helpuri";
107
108     /** Request parameter name to determine the displayed version of a page. */
109     public static final String JavaDoc PARAM_LAYOUT = "layout";
110
111     /** Request parameter name for the login page URI. */
112     public static final String JavaDoc PARAM_LOGINURI = "loginuri";
113
114     /** Request parameter name to determine the part of a JSP element to include. */
115     public static final String JavaDoc PARAM_PART = "part";
116
117     /** Request parameter name to show the print version of a page. */
118     public static final String JavaDoc PARAM_PRINT = "print";
119
120     /** Request parameter name for the current site. */
121     public static final String JavaDoc PARAM_SITE = "site";
122
123     /** Request parameter name for the uri. */
124     public static final String JavaDoc PARAM_URI = "uri";
125
126     /** Name of the property key to set the configuration path for the template. */
127     public static final String JavaDoc PROPERTY_CONFIGPATH = "style_main_configpath";
128
129     /** Name of the property key to set the extension module for the template. */
130     public static final String JavaDoc PROPERTY_EXTENSIONMODULE = "style_main_extensionmodule";
131
132     /** Name of the property key to set the head default link. */
133     public static final String JavaDoc PROPERTY_HEAD_DEFAULTLINK = "style_head_links_defaultlink";
134
135     /** Name of the property key to set the head element URI. */
136     public static final String JavaDoc PROPERTY_HEAD_ELEMENTURI = "style_head_elementuri";
137
138     /** Name of the property key to set the head image height. */
139     public static final String JavaDoc PROPERTY_HEAD_IMGHEIGHT = "style_head_img_height";
140
141     /** Name of the property key to set the head image link. */
142     public static final String JavaDoc PROPERTY_HEAD_IMGLINK = "style_head_img_link";
143
144     /** Name of the property key to set the head image uri. */
145     public static final String JavaDoc PROPERTY_HEAD_IMGURI = "style_head_img_uri";
146
147     /** Name of the property key to set the path to the layout file for the center layout. */
148     public static final String JavaDoc PROPERTY_LAYOUT_CENTER = "layout.center";
149
150     /** Name of the property key to set the path to the layout file for the right layout. */
151     public static final String JavaDoc PROPERTY_LAYOUT_RIGHT = "layout.right";
152
153     /** Name of the property key to set the left navigation include element uri. */
154     public static final String JavaDoc PROPERTY_NAVLEFT_ELEMENTURI = "style_navleft_element_uri";
155
156     /** Name of the property key to set the resource path for the template. */
157     public static final String JavaDoc PROPERTY_RESOURCEPATH = "style_main_resourcepath";
158
159     /** Name of the property key to determine if the head navigation row is shown. */
160     public static final String JavaDoc PROPERTY_SHOW_HEADNAV = "style_show_head_nav";
161
162     /** Name of the property key to determine if the left navigation is shown. */
163     public static final String JavaDoc PROPERTY_SHOW_NAVLEFT = "style_show_navleft";
164
165     /** Name of the property key to show the head image row. */
166     public static final String JavaDoc PROPERTY_SHOWHEADIMAGE = "style_show_head_img";
167
168     /** Name of the property key to show the head links row. */
169     public static final String JavaDoc PROPERTY_SHOWHEADLINKS = "style_show_head_links";
170
171     /** Name of the property key to set the side element uri. */
172     public static final String JavaDoc PROPERTY_SIDE_URI = "style_side_uri";
173
174     /** Name of the property key to set the start folder for navigation and search results. */
175     public static final String JavaDoc PROPERTY_STARTFOLDER = "style_main_startfolder";
176
177     /** Property value "none" for overriding certain properties. */
178     public static final String JavaDoc PROPERTY_VALUE_NONE = CmsTemplateContentListItem.PROPERTY_VALUE_NONE;
179
180     /** Resource type name for the microsite folders specifying a configurable subsite. */
181     public static final String JavaDoc RESOURCE_TYPE_MICROSITE_NAME = "microsite";
182
183     /** The log object for this class. */
184     private static final Log LOG = CmsLog.getLog(CmsTemplateBean.class);
185
186     /** Stores the global website area configuration. */
187     private CmsXmlContent m_globalConfiguration;
188
189     /** The current layout to parse. */
190     private String JavaDoc m_layout;
191
192     /** The default values for building lists of XMLContents. */
193     private Map JavaDoc m_listDefaults;
194
195     /** Stores the localized resource Strings. */
196     private CmsMessages m_messages;
197
198     /** Stores all properties for the requested resource. */
199     private Map JavaDoc m_properties;
200
201     /** Stores the substituted path to the modules resources. */
202     private String JavaDoc m_resPath;
203
204     /** Flag determining if the accessible version of the page should be shown. */
205     private boolean m_showAccessibleVersion;
206
207     /** Flag determining if the head dhtml navigation should be shown. */
208     private boolean m_showHeadNavigation;
209
210     /** Flag determining if the print version should be shown. */
211     private boolean m_showPrintVersion;
212
213     /** Stores the path to the start folder for navigation and search. */
214     private String JavaDoc m_startFolder;
215
216     /**
217      * Empty constructor, required for every JavaBean.<p>
218      */

219     public CmsTemplateBean() {
220
221         super();
222     }
223
224     /**
225      * Constructor, with parameters.<p>
226      *
227      * Use this constructor for the template.<p>
228      *
229      * @param context the JSP page context object
230      * @param req the JSP request
231      * @param res the JSP response
232      */

233     public CmsTemplateBean(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
234
235         super();
236         init(context, req, res);
237     }
238
239     /**
240      * Returns the initialized xmlcontent configuration file.<p>
241      *
242      * @param fileName the absolute path to the configuration file
243      * @param cms the CmsObject to access the VFS
244      * @return the initialized xmlcontent configuration file
245      */

246     protected static CmsXmlContent getConfigurationFile(String JavaDoc fileName, CmsObject cms) {
247
248         CmsXmlContent configuration = null;
249         try {
250             CmsFile configFile = cms.readFile(fileName, CmsResourceFilter.IGNORE_EXPIRATION);
251             configuration = CmsXmlContentFactory.unmarshal(cms, configFile);
252         } catch (Exception JavaDoc e) {
253             // problem getting properties, log error
254
if (LOG.isInfoEnabled()) {
255                 LOG.info(Messages.get().getBundle().key(
256                     Messages.LOG_XMLCONTEN_CONFIG_NOT_FOUND_2,
257                     fileName,
258                     cms.getRequestContext().getUri()));
259             }
260         }
261         return configuration;
262     }
263
264     /**
265      * Builds the main html output for the template foot and includes all subelements depending on the settings.<p>
266      *
267      * @throws IOException if writing the output fails
268      * @throws JspException if including an element fails
269      */

270     public void buildHtmlBodyEnd() throws IOException JavaDoc, JspException JavaDoc {
271
272         m_properties.put(PARAM_LAYOUT, getLayout());
273         // close content column
274
JspWriter JavaDoc out = getJspContext().getOut();
275         String JavaDoc elementName = FOLDER_ELEMENTS + "body_end.jsp";
276         out.print(getTemplateParts().includePart(elementName, "1", getLayout(), this));
277         if (!showPrintVersion()) {
278             // build the side info box
279
include(getExtensionModuleFileUri("elements/info_side.jsp"), null, m_properties);
280         }
281
282         // close main content row
283
out.print(getTemplateParts().includePart(elementName, "2", getLayout(), this));
284
285         if (!showPrintVersion()) {
286             // build the foot links row
287
getProperties().put(PARAM_HELPURI, getConfigurationValue("help.uri", PROPERTY_VALUE_NONE));
288             getProperties().put(PARAM_LOGINURI, getConfigurationValue("login.uri", PROPERTY_VALUE_NONE));
289             include(FOLDER_ELEMENTS + "foot_links.jsp", null, m_properties);
290             boolean showMenus = Boolean.valueOf(getConfigurationValue("headnav.menus", CmsStringUtil.TRUE)).booleanValue();
291             if (showHeadNavigation() && showMenus) {
292                 // create the head navigation dhtml menus
293
if (getProperties().get(CmsTemplateNavigation.PARAM_HEADNAV_MENUDEPTH) == null) {
294                     getProperties().put(PARAM_SITE, getRequestContext().getSiteRoot());
295                     getProperties().put(CmsTemplateNavigation.PARAM_STARTFOLDER, getStartFolder());
296                     getProperties().put(CmsTemplateNavigation.PARAM_HEADNAV_FOLDER, getNavigationStartFolder());
297                     getProperties().put(
298                         CmsTemplateNavigation.PARAM_HEADNAV_MENUDEPTH,
299                         getConfigurationValue("headnav.menudepth", "1"));
300                     getProperties().put(
301                         CmsTemplateNavigation.PARAM_SHOWMENUS,
302                         getConfigurationValue("headnav.menus", CmsStringUtil.TRUE));
303                     getProperties().put(
304                         CmsTemplateNavigation.PARAM_HEADNAV_MANUAL,
305                         getConfigurationValue("headnav.manual", CmsStringUtil.FALSE));
306                 }
307                 include(FOLDER_ELEMENTS + "nav_head_menus.jsp", null, m_properties);
308             }
309         } else {
310             // include the page information
311
m_properties.put(CmsTemplateBean.PARAM_URI, getRequestContext().getUri());
312             include(CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/pages/imprint.html", "content", m_properties);
313         }
314
315         // close body and html
316
out.print(getTemplateParts().includePart(elementName, "3", getLayout(), this));
317     }
318
319     /**
320      * Returns the name of the website area specified in the common configuration file.<p>
321      *
322      * If the name is not found, the title of the start folder is shown.<p>
323      *
324      * @return the name of the website area
325      */

326     public String JavaDoc getAreaName() {
327
328         String JavaDoc name = getConfigurationValue("area.name", null);
329         if (name == null) {
330             String JavaDoc startFolder = getStartFolder();
331             name = property(CmsPropertyDefinition.PROPERTY_TITLE, startFolder, CmsResource.getName(startFolder));
332         }
333         return name;
334     }
335
336     /**
337      * Returns the template configuration path in the OpenCms VFS.<p>
338      *
339      * @return the template configuration path
340      */

341     public String JavaDoc getConfigPath() {
342
343         return property(PROPERTY_CONFIGPATH, "search", "/");
344     }
345
346     /**
347      * Returns the common configuration properties for the current web site area.<p>
348      *
349      * @return the common configuration properties
350      */

351     public CmsXmlContent getConfiguration() {
352
353         if (m_globalConfiguration == null) {
354             m_globalConfiguration = getConfigurationFile(getConfigPath() + FILE_CONFIG_COMMON, getCmsObject());
355         }
356         return m_globalConfiguration;
357     }
358
359     /**
360      * Returns the value for the specified property key name from the configuration.<p>
361      *
362      * @param key the property key name to look up
363      * @return the value for the specified property key name
364      */

365     public String JavaDoc getConfigurationValue(String JavaDoc key) {
366
367         return getConfigurationValue(key, null);
368     }
369
370     /**
371      * Returns the value for the specified property key name from the configuration.<p>
372      *
373      * Returns the default value argument if the property is not found.<p>
374      *
375      * @param key the property key name to look up
376      * @param defaultValue a default value
377      * @return the value for the specified property key name
378      */

379     public String JavaDoc getConfigurationValue(String JavaDoc key, String JavaDoc defaultValue) {
380
381         String JavaDoc value = null;
382         try {
383             value = getConfiguration().getStringValue(null, key, getRequestContext().getLocale());
384         } catch (Exception JavaDoc e) {
385             // log error in debug mode
386
if (LOG.isDebugEnabled()) {
387                 LOG.debug(e.getMessage(), e);
388             }
389         }
390         if (CmsStringUtil.isEmpty(value)) {
391             value = defaultValue;
392         }
393         return value;
394     }
395
396     /**
397      * Returns the "Description" property value of the requested uri.<p>
398      *
399      * @param defaultValue the default value used when the property was not found
400      * @return the "Description" property value of the requested uri
401      */

402     public String JavaDoc getDescription(String JavaDoc defaultValue) {
403
404         if (defaultValue == null) {
405             defaultValue = "";
406         }
407         return property(CmsPropertyDefinition.PROPERTY_DESCRIPTION, "search", defaultValue);
408     }
409
410     /**
411      * Returns the "content encoding" property value of the requested uri.<p>
412      *
413      * @return the "content encoding" property value of the requested uri
414      */

415     public String JavaDoc getEncoding() {
416
417         return property(
418             CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING,
419             "search",
420             OpenCms.getSystemInfo().getDefaultEncoding());
421     }
422
423     /**
424      * Returns the file Uri of a module element to include.<p>
425      *
426      * Checks the presence of an extension module and if the requested element is available.<p>
427      *
428      * @param relFilePath the path of the element relative to the module folder, e.g. "elements/myelement.jsp"
429      * @return the absolute uri of the module element to include
430      */

431     public String JavaDoc getExtensionModuleFileUri(String JavaDoc relFilePath) {
432
433         String JavaDoc configModule = property(
434             PROPERTY_EXTENSIONMODULE,
435             CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME,
436             PROPERTY_VALUE_NONE);
437         if (LOG.isDebugEnabled()) {
438             LOG.debug(Messages.get().getBundle().key(Messages.LOG_EXT_MODULE_PROP_VALUE_1, configModule));
439         }
440         if (!PROPERTY_VALUE_NONE.equals(configModule)) {
441             // extension module name found, check presence of file
442
String JavaDoc fileName = CmsWorkplace.VFS_PATH_MODULES + configModule + "/" + relFilePath;
443             try {
444                 getCmsObject().readResource(fileName);
445                 return fileName;
446             } catch (CmsException e) {
447                 // file not found in extension module, use default file from template one module
448
if (LOG.isDebugEnabled()) {
449                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_EXT_MODULE_FILE_NOT_FOUND_1, fileName));
450                 }
451             }
452         }
453         return CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/" + relFilePath;
454     }
455
456     /**
457      * Returns the URI of the editable head element or "none" if no element is specified.<p>
458      *
459      * @return the URI of the editable head element or "none" if no element is specified
460      */

461     public String JavaDoc getHeadElementUri() {
462
463         String JavaDoc elemUri = (String JavaDoc)getProperties().get(PROPERTY_HEAD_ELEMENTURI);
464         if (CmsStringUtil.isEmptyOrWhitespaceOnly(elemUri)) {
465             elemUri = PROPERTY_VALUE_NONE;
466         }
467         return elemUri;
468     }
469
470     /**
471      * Returns the list of the head links.<p>
472      *
473      * @return the list of head link objects
474      */

475     public List JavaDoc getHeadLinks() {
476
477         List JavaDoc links = new ArrayList JavaDoc(10);
478         // read links to display from configuration file
479
String JavaDoc configFile = getConfigPath() + FILE_LINKS_HEAD;
480         CmsXmlContent configuration = getConfigurationFile(configFile, getCmsObject());
481         // this flag determines if the main link should be shown
482
boolean showMainLink = true;
483         if (configuration != null) {
484             // configuration found, create link list
485
Locale JavaDoc locale = getRequestContext().getLocale();
486             int count = configuration.getIndexCount("Headlink", locale);
487             for (int i = 1; i <= count; i++) {
488                 String JavaDoc prefix = "Headlink[" + i + "]";
489                 try {
490                     String JavaDoc url = configuration.getStringValue(null, prefix + "/link.url", locale);
491                     String JavaDoc text = configuration.getStringValue(null, prefix + "/link.text", locale);
492                     String JavaDoc target = configuration.getStringValue(null, prefix + "/link.target", locale);
493                     if (CmsStringUtil.isEmpty(url) || CmsStringUtil.isEmpty(text)) {
494                         // values for link found in configuration file are not complete, stop loop
495
break;
496                     }
497                     if (url.startsWith("/")) {
498                         // internal link
499
url = link(url);
500                     }
501                     CmsTemplateLink link = new CmsTemplateLink(url, text, target, i);
502                     links.add(link);
503                 } catch (Exception JavaDoc e) {
504                     // log error in debug mode
505
if (LOG.isDebugEnabled()) {
506                         LOG.debug(e.getMessage(), e);
507                     }
508                 }
509                 if (i == 10) {
510                     // all 10 links were specified, do not show main link
511
showMainLink = false;
512                 }
513             }
514         }
515         if (showMainLink) {
516             // less than 10 links defined, main link should be shown
517
String JavaDoc defaultLink = (String JavaDoc)m_properties.get(PROPERTY_HEAD_DEFAULTLINK);
518             if (defaultLink != null && !PROPERTY_VALUE_NONE.equals(defaultLink)) {
519                 String JavaDoc url = defaultLink;
520                 String JavaDoc text = defaultLink;
521                 String JavaDoc target = "";
522                 int sepIndex = defaultLink.indexOf("|");
523                 try {
524                     if (sepIndex != -1) {
525                         url = defaultLink.substring(0, sepIndex);
526                         text = defaultLink.substring(sepIndex + 1);
527                         sepIndex = text.indexOf("|");
528                         if (sepIndex != -1) {
529                             target = text.substring(sepIndex + 1);
530                             text = text.substring(0, sepIndex);
531                         }
532                     }
533                     if (url.startsWith("/")) {
534                         url = link(url);
535                     }
536                     // try to get localized version for found text
537
text = keyDefault(text, text);
538                 } catch (Exception JavaDoc e) {
539                     // problem extracting information from property
540
if (LOG.isErrorEnabled()) {
541                         LOG.error(e);
542                     }
543                 }
544
545                 CmsTemplateLink link = new CmsTemplateLink(url, text, target, 0);
546                 links.add(link);
547                 // sort the list to show the main link first
548
Collections.sort(links);
549             }
550         }
551         return links;
552     }
553
554     /**
555      * Returns the "Keywords" property value of the requested uri.<p>
556      *
557      * @param defaultValue the default value used when the property was not found
558      * @return the "Keywords" property value of the requested uri
559      */

560     public String JavaDoc getKeywords(String JavaDoc defaultValue) {
561
562         if (defaultValue == null) {
563             defaultValue = "";
564         }
565         return property(CmsPropertyDefinition.PROPERTY_KEYWORDS, "search", defaultValue);
566     }
567
568     /**
569      * Returns the current layout to generate from the template.<p>
570      *
571      * @return the current layout
572      */

573     public String JavaDoc getLayout() {
574
575         return m_layout;
576     }
577
578     /**
579      * Returns the left navigation include element uri property value.<p>
580      *
581      * @return the left navigation include element uri property value
582      */

583     public String JavaDoc getLeftNavigationElementUri() {
584
585         return property(PROPERTY_NAVLEFT_ELEMENTURI, "search", PROPERTY_VALUE_NONE);
586     }
587
588     /**
589      * Returns the start folder for the navigation elements.<p>
590      *
591      * @return the start folder for the navigation elements
592      */

593     public String JavaDoc getNavigationStartFolder() {
594
595         String JavaDoc startFolder = getConfigurationValue("navigation.startfolder", null);
596         if (startFolder == null) {
597             return getStartFolder();
598         } else {
599             startFolder = getRequestContext().removeSiteRoot(startFolder);
600         }
601         return startFolder;
602     }
603
604     /**
605      * Returns the Map of found resource properties.<p>
606      *
607      * @return the Map of found resource properties
608      */

609     public Map JavaDoc getProperties() {
610
611         return m_properties;
612     }
613
614     /**
615      * Returns the substituted path to the modules resource folder.<p>
616      *
617      * @return the substituted path to the modules resource folder
618      */

619     public String JavaDoc getResourcePath() {
620
621         if (m_resPath == null) {
622             // resource path has not yet been determined, get it now
623
m_resPath = property(PROPERTY_RESOURCEPATH, "search", "na");
624             if ("na".equals(m_resPath)) {
625                 // no property set, use default module resources
626
m_resPath = CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/resources/";
627             }
628             m_resPath = link(m_resPath);
629         }
630         return m_resPath;
631     }
632
633     /**
634      * Returns the search index name to use, depending on the current site.<p>
635      *
636      * Returns "[Projectname] project (VFS)" for the default site and the site root as index name for other sites.<p>
637      *
638      * @return the search index name to use, depending on the current site
639      */

640     public String JavaDoc getSearchIndexName() {
641
642         String JavaDoc currentSite = getRequestContext().getSiteRoot();
643         if (currentSite.indexOf("sites/default") == -1) {
644             // return site root as index name
645
return currentSite;
646         } else {
647             // return default index
648
return getRequestContext().currentProject().getName() + " project (VFS)";
649         }
650     }
651
652     /**
653      * Returns the start folder for navigation and search results.<p>
654      *
655      * @return the start folder for navigation and search results
656      */

657     public String JavaDoc getStartFolder() {
658
659         if (m_startFolder == null) {
660             // start folder has not yet been determined, so try to get it
661
int folderTypeId = -1;
662             try {
663                 folderTypeId = OpenCms.getResourceManager().getResourceType(RESOURCE_TYPE_MICROSITE_NAME).getTypeId();
664             } catch (CmsLoaderException e) {
665                 // resource type could not be determined
666
if (LOG.isErrorEnabled()) {
667                     LOG.error(Messages.get().getBundle().key(Messages.LOG_MICROSITE_FOLDER_NOT_FOUND_0));
668                 }
669             }
670             m_startFolder = "/";
671             try {
672                 CmsFolder startFolder = getCmsObject().readAncestor(getRequestContext().getUri(), folderTypeId);
673                 if (startFolder != null) {
674                     m_startFolder = getCmsObject().getRequestContext().removeSiteRoot(startFolder.getRootPath());
675                 }
676             } catch (CmsException e) {
677                 // no matching start folder found
678
if (LOG.isErrorEnabled()) {
679                     LOG.error(Messages.get().getBundle().key(Messages.LOG_MICROSITE_READ_START_FOLDER_0));
680                 }
681             }
682         }
683         return m_startFolder;
684     }
685
686     /**
687      * Returns the URI of the CSS style sheet configuration file.<p>
688      *
689      * @return the URI of the CSS style sheet configuration file
690      */

691     public String JavaDoc getStyleSheetConfigUri() {
692
693         String JavaDoc confUri = property(CmsTemplateStyleSheet.PROPERTY_CONFIGFILE, "search", "");
694         if ("".equals(confUri)) {
695             // property not set, try to get default configuration file
696
confUri = getConfigPath() + CmsTemplateStyleSheet.FILENAME_CONFIGFILE;
697         }
698         return confUri;
699     }
700
701     /**
702      * Returns the substituted URI of the CSS style sheet to use in the template.<p>
703      *
704      * @return the substituted URI of the CSS style sheet
705      */

706     public String JavaDoc getStyleSheetUri() {
707
708         String JavaDoc fileName = FILE_CSS;
709         if (showAccessibleVersion()) {
710             // use accessible CSS version
711
fileName = FILE_CSS_ACCESSIBLE;
712         }
713         // generate substituted style sheet URI
714
return link(CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/resources/" + fileName);
715     }
716
717     /**
718      * Returns the substituted URI of the CSS style sheet to use in the template.<p>
719      *
720      * @param resPath the resource path
721      * @param config the uri to the configuration settings
722      * @param site the site
723      * @param locale the locale
724      *
725      * @return the substituted URI of the CSS style sheet
726      */

727     public String JavaDoc getStyleSheetUri(String JavaDoc resPath, String JavaDoc config, String JavaDoc site, Locale JavaDoc locale) {
728
729         StringBuffer JavaDoc result = new StringBuffer JavaDoc(16);
730
731         // create absolute path to VFS module resource folder
732
result.append(CmsWorkplace.VFS_PATH_MODULES);
733         result.append(MODULE_NAME);
734         result.append("/resources/");
735
736         if (showAccessibleVersion()) {
737             // use accessible CSS version
738
result.append(FILE_CSS_ACCESSIBLE);
739         } else {
740             // use common CSS version
741
result.append(FILE_CSS);
742         }
743
744         // append the request parameters for the style sheet
745
result.append("?respath=");
746         result.append(resPath);
747         result.append("&config=");
748         result.append(config);
749         result.append("&site=");
750         result.append(site);
751         result.append("&__locale=");
752         result.append(locale);
753
754         // generate substituted style sheet URI
755
return link(result.toString());
756     }
757
758     /**
759      * Returns the template parts instance to use for including static JSP elements.<p>
760      *
761      * These element parts depend on the current Locale, the layout to display and the project.<p>
762      *
763      * @return the template parts instance
764      */

765     public CmsTemplateParts getTemplateParts() {
766
767         return CmsTemplateParts.getInstance();
768     }
769
770     /**
771      * Returns the "Title" property value of the requested uri.<p>
772      *
773      * @param defaultValue the default value used when the property was not found
774      * @return the "Title" property value of the requested uri
775      */

776     public String JavaDoc getTitle(String JavaDoc defaultValue) {
777
778         if (defaultValue == null) {
779             defaultValue = "";
780         }
781         return property(CmsPropertyDefinition.PROPERTY_TITLE, "search", defaultValue);
782     }
783
784     /**
785      * Includes the page elements if they are present.<p>
786      *
787      * The page elements are layouted in a 4 row, 2 columns layout.
788      * If one element of a single row is missing, the present element
789      * will be spanned across the 2 columns.<p>
790      *
791      * @throws IOException if writing the output fails
792      * @throws JspException if including an element fails
793      */

794     public void includeElements() throws IOException JavaDoc, JspException JavaDoc {
795
796         // check if elements to show are present
797
boolean elementsPresent = template("text1,text2,text3,text4,text5,text6,text7,text8", false);
798
799         // get the list(s) of content items to display between elements
800
String JavaDoc configFile = (String JavaDoc)getProperties().get(PROPERTY_LAYOUT_CENTER);
801         List JavaDoc contents = new ArrayList JavaDoc();
802         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(configFile) && !PROPERTY_VALUE_NONE.equals(configFile)) {
803             // get the content list(s) for the center area
804
contents = getContentListItems(configFile, CmsTemplateContentListItem.DISPLAYAREA_CENTER);
805         }
806         // determine if list can show page links
807
int size = contents.size();
808         boolean showPageLinks = true;
809         if (size > 1) {
810             // more than one list is displayed, do not show page links on lists
811
showPageLinks = false;
812         }
813
814         if (elementsPresent || size > 0) {
815             // at least one element or list is present, create writer
816
JspWriter JavaDoc out = getJspContext().getOut();
817             String JavaDoc elementName = FOLDER_ELEMENTS + "elements.jsp";
818             // create start part (common layout only)
819
out.print(getTemplateParts().includePart(elementName, "start", getLayout(), this));
820
821             // calculate start point for content lists
822
int startPoint = 3 - size;
823             int listIndex = 0;
824             for (int i = 0; i < 4; i++) {
825                 int elementIndex = (i * 2) + 1;
826                 // include the element row
827
includeContentRow("text" + (elementIndex), "text" + (elementIndex + 1), out);
828                 if ((listIndex < size) && (i >= startPoint)) {
829                     // include the list item
830
CmsTemplateContentListItem item = (CmsTemplateContentListItem)contents.get(listIndex);
831                     out.print(getTemplateParts().includePart(elementName, "column_start", getLayout(), this));
832                     out.print(getTemplateParts().includePart(elementName, "element_start_1", getLayout(), this));
833                     item.includeListItem(this, showPageLinks);
834                     out.print(getTemplateParts().includePart(elementName, "element_end", getLayout(), this));
835                     out.print(getTemplateParts().includePart(elementName, "column_end", getLayout(), this));
836                     listIndex++;
837                 }
838             }
839             // create end part (common layout only)
840
out.print(getTemplateParts().includePart(elementName, "end", getLayout(), this));
841         }
842     }
843
844     /**
845      * Includes page elements useable on popup pages ("popuphead", "popupfoot").<p>
846      *
847      * @param element the element to display from the target
848      * @param title the title for the popup page
849      * @throws JspException if including the element fails
850      */

851     public void includePopup(String JavaDoc element, String JavaDoc title) throws JspException JavaDoc {
852
853         Map JavaDoc properties = new HashMap JavaDoc();
854         if ("popuphead".equals(element)) {
855             // include the head part, put necessary information to Map
856
properties.put("stylesheeturi", getStyleSheetUri());
857             properties.put("resourcepath", getResourcePath());
858             properties.put("title", title);
859             properties.put(CmsTemplateStyleSheet.PARAM_CONFIGFILE, getStyleSheetConfigUri());
860         }
861         // include the element
862
include(CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/pages/popup_includes.jsp", element, properties);
863     }
864
865     /**
866      * Includes center list elements if defined.<p>
867      *
868      * @throws JspException if including an element fails
869      */

870     public void includeRightLists() throws JspException JavaDoc {
871
872         String JavaDoc configFile = (String JavaDoc)getProperties().get(PROPERTY_LAYOUT_RIGHT);
873         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(configFile) && !PROPERTY_VALUE_NONE.equals(configFile)) {
874             List JavaDoc contents = getContentListItems(configFile, CmsTemplateContentListItem.DISPLAYAREA_RIGHT);
875             int size = contents.size();
876             for (int i = 0; i < size; i++) {
877                 CmsTemplateContentListItem item = (CmsTemplateContentListItem)contents.get(i);
878                 item.includeListItem(this, false);
879             }
880         }
881     }
882
883     /**
884      * Initialize this bean with the current page context, request and response.<p>
885      *
886      * It is required to call one of the init() methods before you can use the
887      * instance of this bean.
888      *
889      * @param context the JSP page context object
890      * @param req the JSP request
891      * @param res the JSP response
892      */

893     public void init(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
894
895         // call initialization of super class
896
super.init(context, req, res);
897         String JavaDoc className = getClass().getName();
898         if (className.endsWith("Bean")) {
899             // do some additional stuff if this is not called by a super class
900
m_properties = properties("search");
901             // check which page version to display
902
initPageVersion();
903             // check if the head navigation should be shown
904
m_showHeadNavigation = !showPrintVersion()
905                 && Boolean.valueOf(property(PROPERTY_SHOW_HEADNAV, "search", CmsStringUtil.TRUE)).booleanValue();
906         }
907     }
908
909     /**
910      * Gets the localized resource string for a given message key.<p>
911      *
912      * If the key was not found in the bundle, the return value is
913      * <code>"??? " + keyName + " ???"</code>. This will also be returned
914      * if the bundle was not properly initialized first.
915      *
916      * @param keyName the key for the desired string
917      * @return the resource string for the given key
918      *
919      * @see CmsMessages#key(String)
920      */

921     public String JavaDoc key(String JavaDoc keyName) {
922
923         return messages().key(keyName);
924     }
925
926     /**
927      * Returns the localized resource string for a given message key.<p>
928      *
929      * If the key was not found in the bundle, the provided default value
930      * is returned.<p>
931      *
932      * @param keyName the key for the desired string
933      * @param defaultValue the default value in case the key does not exist in the bundle
934      * @return the resource string for the given key it it exists, or the given default if not
935      *
936      * @see CmsMessages#keyDefault(String, String)
937      */

938     public String JavaDoc keyDefault(String JavaDoc keyName, String JavaDoc defaultValue) {
939
940         return messages().keyDefault(keyName, defaultValue);
941     }
942
943     /**
944      * Returns the initialized CmsMessages object to use on the JSP template.<p>
945      *
946      * @return the initialized CmsMessages object
947      */

948     public CmsMessages messages() {
949
950         if (m_messages == null) {
951             messages(MESSAGE_BUNDLE);
952         }
953         return m_messages;
954     }
955
956     /**
957      * Returns the initialized CmsMessages object to use on the JSP template.<p>
958      *
959      * @param bundleName the name of the ResourceBundle to use
960      * @return the initialized CmsMessages object
961      */

962     public CmsMessages messages(String JavaDoc bundleName) {
963
964         if (m_messages == null) {
965             m_messages = new CmsMessages(bundleName, getRequestContext().getLocale());
966         }
967         return m_messages;
968     }
969
970     /**
971      * Puts the information needed to build the navigation elements to the property map.<p>
972      */

973     public void putNavigationProperties() {
974
975         // fill property Map with necessary parameters for included navigation elements
976
getProperties().put(PARAM_SITE, getRequestContext().getSiteRoot());
977         getProperties().put(PARAM_ACCESSIBLE, "" + showAccessibleVersion());
978         getProperties().put(CmsTemplateNavigation.PARAM_RESPATH, getResourcePath());
979         getProperties().put(CmsTemplateNavigation.PARAM_STARTFOLDER, getStartFolder());
980         getProperties().put(CmsTemplateNavigation.PARAM_HEADNAV_FOLDER, getNavigationStartFolder());
981         getProperties().put(
982             CmsTemplateNavigation.PARAM_HEADNAV_IMAGES,
983             getConfigurationValue("headnav.images", CmsStringUtil.FALSE));
984         getProperties().put(
985             CmsTemplateNavigation.PARAM_HEADNAV_MANUAL,
986             getConfigurationValue("headnav.manual", CmsStringUtil.FALSE));
987         getProperties().put(
988             CmsTemplateNavigation.PARAM_HEADNAV_MARKCURRENT,
989             getConfigurationValue("headnav.markcurrent", CmsStringUtil.FALSE));
990         getProperties().put(
991             CmsTemplateNavigation.PARAM_HEADNAV_MENUDEPTH,
992             getConfigurationValue("headnav.menudepth", "1"));
993         getProperties().put(
994             CmsTemplateNavigation.PARAM_HEADNAV_MENUCLICK,
995             getConfigurationValue("headnav.menuclick", CmsStringUtil.FALSE));
996         getProperties().put(
997             CmsTemplateNavigation.PARAM_SHOWMENUS,
998             getConfigurationValue("headnav.menus", CmsStringUtil.TRUE));
999         getProperties().put(
1000            CmsTemplateNavigation.PARAM_NAVLEFT_SHOWSELECTED,
1001            getConfigurationValue("navleft.showselected", CmsStringUtil.FALSE));
1002        getProperties().put(CmsTemplateNavigation.PARAM_NAVLEFT_SHOWTREE, "" + showLeftNavigation());
1003        getProperties().put(CmsTemplateNavigation.PARAM_NAVLEFT_ELEMENTURI, getLeftNavigationElementUri());
1004    }
1005
1006    /**
1007     * Sets the current layout to generate from the template.<p>
1008     *
1009     * @param layout the current layout to generate
1010     */

1011    public void setLayout(String JavaDoc layout) {
1012
1013        m_layout = layout;
1014    }
1015
1016    /**
1017     * Returns if the accessible version of the page should be shown.<p>
1018     *
1019     * @return true if the accessible version should be shown, otherwise false
1020     */

1021    public boolean showAccessibleVersion() {
1022
1023        return m_showAccessibleVersion;
1024    }
1025
1026    /**
1027     * Returns if the head image should be shown.<p>
1028     *
1029     * @return true if the head image should be shown, otherwise false
1030     */

1031    public boolean showHeadImage() {
1032
1033        return Boolean.valueOf(property(PROPERTY_SHOWHEADIMAGE, "search", CmsStringUtil.TRUE)).booleanValue();
1034    }
1035
1036    /**
1037     * Returns if the head links should be shown.<p>
1038     *
1039     * @return true if the head links should be shown, otherwise false
1040     */

1041    public boolean showHeadLinks() {
1042
1043        return Boolean.valueOf(property(PROPERTY_SHOWHEADLINKS, "search", CmsStringUtil.TRUE)).booleanValue();
1044    }
1045
1046    /**
1047     * Returns if the head navigation menu should be shown.<p>
1048     *
1049     * @return true if the head navigation menu should be shown, otherwise false
1050     */

1051    public boolean showHeadNavigation() {
1052
1053        return m_showHeadNavigation;
1054    }
1055
1056    /**
1057     * Returns if the left navigation menu should be shown.<p>
1058     *
1059     * @return true if the left navigation menu should be shown, otherwise false
1060     */

1061    public boolean showLeftNavigation() {
1062
1063        return Boolean.valueOf(property(PROPERTY_SHOW_NAVLEFT, "search", CmsStringUtil.TRUE)).booleanValue();
1064    }
1065
1066    /**
1067     * Returns if the print version of the page should be created.<p>
1068     *
1069     * @return true if the print version should be created, otherwise false
1070     */

1071    public boolean showPrintVersion() {
1072
1073        return m_showPrintVersion;
1074    }
1075
1076    /**
1077     * Returns a list of content list item objects which are needed to display lists on the side and in the center of the page.<p>
1078     *
1079     * @param configFile the configuration file uri from which to read the properties from
1080     * @param displayArea the area where to build the lists (left, center or right)
1081     * @return a list of content list items
1082     */

1083    private List JavaDoc getContentListItems(String JavaDoc configFile, String JavaDoc displayArea) {
1084
1085        List JavaDoc result = new ArrayList JavaDoc();
1086        // read all properties of configuration file
1087
Map JavaDoc properties = properties(configFile);
1088        int i = 1;
1089        boolean cont = true;
1090        do {
1091            // create a list item
1092
CmsTemplateContentListItem item = CmsTemplateContentListItem.newInstance(
1093                getListDefaults(),
1094                properties,
1095                getStartFolder(),
1096                displayArea,
1097                i);
1098            if (item == null) {
1099                // no item created, stop loop
1100
cont = false;
1101            } else {
1102                // add created item to result
1103
result.add(item);
1104                i++;
1105            }
1106        } while (cont);
1107        return result;
1108    }
1109
1110    /**
1111     * Gets the default list configuration values from the workplace messages.<p>
1112     *
1113     * @return the default list configuration values from the workplace messages
1114     */

1115    private Map JavaDoc getListDefaults() {
1116
1117        if (m_listDefaults == null) {
1118            CmsMessages messages = OpenCms.getWorkplaceManager().getMessages(getRequestContext().getLocale());
1119            m_listDefaults = CmsTemplateContentListItem.getDefaultValuesFromMessages(messages);
1120        }
1121        return m_listDefaults;
1122    }
1123
1124    /**
1125     * Includes one row of page elements.<p>
1126     *
1127     * Builds a row with one element spanning the row or
1128     * with two elements in different cells depending on the
1129     * presence of the elements.<p>
1130     *
1131     * @param elementLeft the name of the left element to show
1132     * @param elementRight the name of the right element to show
1133     * @param out the Writer to write the output to
1134     * @throws IOException if writing the output fails
1135     * @throws JspException if including an element fails
1136     */

1137    private void includeContentRow(String JavaDoc elementLeft, String JavaDoc elementRight, JspWriter JavaDoc out)
1138    throws IOException JavaDoc, JspException JavaDoc {
1139
1140        if (template(elementLeft + "," + elementRight, false)) {
1141            // at least one element is present, create row (common layout only)
1142
String JavaDoc elementName = FOLDER_ELEMENTS + "elements.jsp";
1143            out.print(getTemplateParts().includePart(elementName, "column_start", getLayout(), this));
1144
1145            if (template(elementLeft, true)) {
1146                // left element is present
1147
if (template(elementRight, true)) {
1148                    // right element is present, too
1149
out.print(getTemplateParts().includePart(elementName, "element_start_2", getLayout(), this));
1150                } else {
1151                    // no right element
1152
out.print(getTemplateParts().includePart(elementName, "element_start_1", getLayout(), this));
1153                }
1154                include(null, elementLeft, true);
1155                out.print(getTemplateParts().includePart(elementName, "element_end", getLayout(), this));
1156            }
1157            if (template(elementRight, true)) {
1158                // right element is present
1159
if (template(elementLeft, true)) {
1160                    // left element is present, too
1161
out.print(getTemplateParts().includePart(elementName, "element_start_2", getLayout(), this));
1162                } else {
1163                    // no left element
1164
out.print(getTemplateParts().includePart(elementName, "element_start_1", getLayout(), this));
1165                }
1166                include(null, elementRight, true);
1167                out.print(getTemplateParts().includePart(elementName, "element_end", getLayout(), this));
1168            }
1169            // close row (common layout only)
1170
out.print(getTemplateParts().includePart(elementName, "column_end", getLayout(), this));
1171        }
1172    }
1173
1174    /**
1175     * Initializes the page layout parameters to determine which version to show.<p>
1176     *
1177     * It is possible to display the common version, an accessible version and
1178     * a print version of a page.<p>
1179     */

1180    private void initPageVersion() {
1181
1182        // check if the print version should be shown
1183
m_showPrintVersion = Boolean.valueOf(getRequest().getParameter(PARAM_PRINT)).booleanValue();
1184        if (!showPrintVersion()) {
1185            // check if the accessible page layout should be used
1186
String JavaDoc param = getRequest().getParameter(PARAM_ACCESSIBLE);
1187            if (CmsStringUtil.isNotEmpty(param)) {
1188                m_showAccessibleVersion = Boolean.valueOf(param).booleanValue();
1189            } else {
1190                m_showAccessibleVersion = getConfigurationValue("layout.version", PARAM_COMMON).equals(PARAM_ACCESSIBLE);
1191            }
1192            if (showAccessibleVersion()) {
1193                setLayout(PARAM_ACCESSIBLE);
1194            } else {
1195                setLayout(PARAM_COMMON);
1196            }
1197        } else {
1198            setLayout(PARAM_PRINT);
1199        }
1200    }
1201}
Popular Tags