KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > CmsWorkplace


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/CmsWorkplace.java,v $
3  * Date : $Date: 2006/09/22 15:17:02 $
4  * Version: $Revision: 1.161 $
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.workplace;
33
34 import org.opencms.db.CmsDbEntryNotFoundException;
35 import org.opencms.db.CmsUserSettings;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsProject;
38 import org.opencms.file.CmsRequestContext;
39 import org.opencms.file.CmsResource;
40 import org.opencms.file.CmsResourceFilter;
41 import org.opencms.file.CmsUser;
42 import org.opencms.file.types.I_CmsResourceType;
43 import org.opencms.i18n.CmsEncoder;
44 import org.opencms.i18n.CmsMessages;
45 import org.opencms.i18n.CmsMultiMessages;
46 import org.opencms.jsp.CmsJspActionElement;
47 import org.opencms.lock.CmsLock;
48 import org.opencms.main.CmsBroadcast;
49 import org.opencms.main.CmsException;
50 import org.opencms.main.CmsLog;
51 import org.opencms.main.OpenCms;
52 import org.opencms.security.CmsPermissionSet;
53 import org.opencms.site.CmsSite;
54 import org.opencms.site.CmsSiteManager;
55 import org.opencms.util.CmsMacroResolver;
56 import org.opencms.util.CmsRequestUtil;
57 import org.opencms.util.CmsStringUtil;
58 import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
59 import org.opencms.workplace.help.CmsHelpTemplateBean;
60
61 import java.io.IOException JavaDoc;
62 import java.lang.reflect.InvocationTargetException JavaDoc;
63 import java.lang.reflect.Method JavaDoc;
64 import java.util.ArrayList JavaDoc;
65 import java.util.Collection JavaDoc;
66 import java.util.HashMap JavaDoc;
67 import java.util.Iterator JavaDoc;
68 import java.util.List JavaDoc;
69 import java.util.Locale JavaDoc;
70 import java.util.Map JavaDoc;
71
72 import javax.servlet.ServletException JavaDoc;
73 import javax.servlet.http.HttpServletRequest JavaDoc;
74 import javax.servlet.http.HttpServletResponse JavaDoc;
75 import javax.servlet.http.HttpSession JavaDoc;
76 import javax.servlet.jsp.PageContext JavaDoc;
77
78 import org.apache.commons.collections.Buffer;
79 import org.apache.commons.logging.Log;
80
81 /**
82  * Master class for the JSP based workplace which provides default methods and
83  * session handling for all JSP workplace classes.<p>
84  *
85  * @author Alexander Kandzior
86  *
87  * @version $Revision: 1.161 $
88  *
89  * @since 6.0.0
90  */

91 public abstract class CmsWorkplace {
92
93     /** The debug flag. */
94     public static final boolean DEBUG = false;
95
96     /** Parameter for the default locale. */
97     public static final Locale JavaDoc DEFAULT_LOCALE = Locale.ENGLISH;
98
99     /** Parameter for the default language. */
100     public static final String JavaDoc DEFAULT_LANGUAGE = DEFAULT_LOCALE.getLanguage();
101
102     /** Constant for the JSP explorer filelist file. */
103     public static final String JavaDoc FILE_EXPLORER_FILELIST = CmsWorkplace.VFS_PATH_WORKPLACE
104         + "views/explorer/explorer_files.jsp";
105
106     /** Helper variable to deliver the html end part. */
107     public static final int HTML_END = 1;
108
109     /** Helper variable to deliver the html start part. */
110     public static final int HTML_START = 0;
111
112     /** The request parameter for the workplace project selection. */
113     public static final String JavaDoc PARAM_WP_EXPLORER_RESOURCE = "wpExplorerResource";
114
115     /** The request parameter for the workplace project selection. */
116     public static final String JavaDoc PARAM_WP_PROJECT = "wpProject";
117
118     /** The request parameter for the workplace site selection. */
119     public static final String JavaDoc PARAM_WP_SITE = "wpSite";
120
121     /** Path to system folder. */
122     public static final String JavaDoc VFS_PATH_SYSTEM = "/system/";
123
124     /** Path to the workplace. */
125     public static final String JavaDoc VFS_PATH_WORKPLACE = VFS_PATH_SYSTEM + "workplace/";
126
127     /** Constant for the JSP dialogs path. */
128     public static final String JavaDoc PATH_DIALOGS = VFS_PATH_WORKPLACE + "commons/";
129
130     /** Constant for the JSP workplace path. */
131     public static final String JavaDoc PATH_WORKPLACE = VFS_PATH_WORKPLACE;
132
133     /** Path to exported system image folder. */
134     public static final String JavaDoc RFS_PATH_RESOURCES = "/resources/";
135
136     /** Prefix for temporary files in the VFS. */
137     public static final String JavaDoc TEMP_FILE_PREFIX = "~";
138
139     /** Directory name of content default_bodies folder. */
140     public static final String JavaDoc VFS_DIR_DEFAULTBODIES = "default_bodies/";
141
142     /** Directory name of content templates folder. */
143     public static final String JavaDoc VFS_DIR_TEMPLATES = "templates/";
144
145     /** Path to commons. */
146     public static final String JavaDoc VFS_PATH_COMMONS = VFS_PATH_WORKPLACE + "commons/";
147
148     /** Path to the workplace editors. */
149     public static final String JavaDoc VFS_PATH_EDITORS = VFS_PATH_WORKPLACE + "editors/";
150
151     /** Path to the galleries. */
152     public static final String JavaDoc VFS_PATH_GALLERIES = VFS_PATH_SYSTEM + "galleries/";
153
154     /** Path to locales. */
155     public static final String JavaDoc VFS_PATH_LOCALES = VFS_PATH_WORKPLACE + "locales/";
156
157     /** Path to modules folder. */
158     public static final String JavaDoc VFS_PATH_MODULES = VFS_PATH_SYSTEM + "modules/";
159
160     /** Path to system image folder. */
161     public static final String JavaDoc VFS_PATH_RESOURCES = VFS_PATH_WORKPLACE + "resources/";
162
163     /** Path to workplace views. */
164     public static final String JavaDoc VFS_PATH_VIEWS = VFS_PATH_WORKPLACE + "views/";
165
166     /** Constant for the JSP common files (e.g. error page) path. */
167     public static final String JavaDoc DIALOG_PATH_COMMON = PATH_DIALOGS + "includes/";
168
169     /** Constant for the JSP common close dialog page. */
170     public static final String JavaDoc FILE_DIALOG_CLOSE = DIALOG_PATH_COMMON + "closedialog.jsp";
171
172     /** Constant for the JSP common confirmation dialog. */
173     public static final String JavaDoc FILE_DIALOG_SCREEN_CONFIRM = DIALOG_PATH_COMMON + "confirmation.jsp";
174
175     /** Constant for the JSP common error dialog. */
176     public static final String JavaDoc FILE_DIALOG_SCREEN_ERROR = DIALOG_PATH_COMMON + "error.jsp";
177
178     /** Constant for the JSP common error dialog. */
179     public static final String JavaDoc FILE_DIALOG_SCREEN_ERRORPAGE = DIALOG_PATH_COMMON + "errorpage.jsp";
180
181     /** Constant for the JSP common wait screen. */
182     public static final String JavaDoc FILE_DIALOG_SCREEN_WAIT = DIALOG_PATH_COMMON + "wait.jsp";
183
184     /** Constant for the JSP common report page. */
185     public static final String JavaDoc FILE_REPORT_OUTPUT = DIALOG_PATH_COMMON + "report.jsp";
186
187     /** Key name for the request attribute to indicate a multipart request was already parsed. */
188     protected static final String JavaDoc REQUEST_ATTRIBUTE_MULTIPART = "__CmsWorkplace.MULTIPART";
189
190     /** Key name for the request attribute to reload the folder tree view. */
191     protected static final String JavaDoc REQUEST_ATTRIBUTE_RELOADTREE = "__CmsWorkplace.RELOADTREE";
192
193     /** Key name for the session workplace class. */
194     protected static final String JavaDoc SESSION_WORKPLACE_CLASS = "__CmsWorkplace.WORKPLACE_CLASS";
195
196     /** The log object for this class. */
197     private static final Log LOG = CmsLog.getLog(CmsWorkplace.class);
198
199     /** The link to the explorer file list (cached for performance reasons). */
200     private static String JavaDoc m_file_explorer_filelist;
201
202     /** The URI to the skin resources (cached for performance reasons). */
203     private static String JavaDoc m_skinUri;
204
205     /** The URI to the stylesheet resources (cached for performance reasons). */
206     private static String JavaDoc m_styleUri;
207
208     /** The current users OpenCms context. */
209     private CmsObject m_cms;
210
211     /** Helper variable to store the id of the current project. */
212     private int m_currentProjectId = -1;
213
214     /** Flag for indicating that request forwarded was. */
215     private boolean m_forwarded;
216
217     /** The current JSP action element. */
218     private CmsJspActionElement m_jsp;
219
220     /** The macro resolver, this is cached to avoid multiple instance generation. */
221     private CmsMacroResolver m_macroResolver;
222
223     /** The currently used message bundle. */
224     private CmsMultiMessages m_messages;
225
226     /** The list of multi part file items (if available). */
227     private List JavaDoc m_multiPartFileItems;
228
229     /** The map of parameters read from the current request. */
230     private Map JavaDoc m_parameterMap;
231
232     /** The current resource URI. */
233     private String JavaDoc m_resourceUri;
234
235     /** The current OpenCms users http session. */
236     private HttpSession JavaDoc m_session;
237
238     /** The current OpenCms users workplace settings. */
239     private CmsWorkplaceSettings m_settings;
240
241     /**
242      * Public constructor.<p>
243      *
244      * @param jsp the initialized JSP context
245      */

246     public CmsWorkplace(CmsJspActionElement jsp) {
247
248         initWorkplaceMembers(jsp);
249     }
250
251     /**
252      * Public constructor with JSP variables.<p>
253      *
254      * @param context the JSP page context
255      * @param req the JSP request
256      * @param res the JSP response
257      */

258     public CmsWorkplace(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
259
260         this(new CmsJspActionElement(context, req, res));
261     }
262
263     /**
264      * Generates a html select box out of the provided values.<p>
265      *
266      * @param parameters a string that will be inserted into the initial select tag,
267      * if null no parameters will be inserted
268      * @param options the options
269      * @param values the option values, if null the select will have no value attributes
270      * @param selected the index of the pre-selected option, if -1 no option is pre-selected
271      * @param useLineFeed if true, adds some formatting "\n" to the output String
272      * @return a String representing a html select box
273      */

274     public static String JavaDoc buildSelect(String JavaDoc parameters, List JavaDoc options, List JavaDoc values, int selected, boolean useLineFeed) {
275
276         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
277         result.append("<select ");
278         if (parameters != null) {
279             result.append(parameters);
280         }
281         result.append(">");
282         if (useLineFeed) {
283             result.append("\n");
284         }
285         int length = options.size();
286         String JavaDoc value = null;
287         for (int i = 0; i < length; i++) {
288             if (values != null) {
289                 try {
290                     value = (String JavaDoc)values.get(i);
291                 } catch (Exception JavaDoc e) {
292                     // can usually be ignored
293
if (LOG.isInfoEnabled()) {
294                         LOG.info(e.getLocalizedMessage());
295                     }
296                     // lists are not properly initialized, just don't use the value
297
value = null;
298                 }
299             }
300             if (value == null) {
301                 result.append("<option");
302                 if (i == selected) {
303                     result.append(" selected=\"selected\"");
304                 }
305                 result.append(">");
306                 result.append(options.get(i));
307                 result.append("</option>");
308                 if (useLineFeed) {
309                     result.append("\n");
310                 }
311             } else {
312                 result.append("<option value=\"");
313                 result.append(value);
314                 result.append("\"");
315                 if (i == selected) {
316                     result.append(" selected=\"selected\"");
317                 }
318                 result.append(">");
319                 result.append(options.get(i));
320                 result.append("</option>");
321                 if (useLineFeed) {
322                     result.append("\n");
323                 }
324             }
325         }
326         result.append("</select>");
327         if (useLineFeed) {
328             result.append("\n");
329         }
330         return result.toString();
331     }
332
333     /**
334      * Returns the full Workplace resource path to the selected resource.<p>
335      *
336      * @param resourceName the name of the resource to get the resource path for
337      *
338      * @return the full Workplace resource path to the selected resource
339      */

340     public static String JavaDoc getResourceUri(String JavaDoc resourceName) {
341
342         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
343         result.append(getSkinUri());
344         result.append(resourceName);
345         return result.toString();
346     }
347
348     /**
349      * Returns the path to the skin resources.<p>
350      *
351      * @return the path to the skin resources
352      */

353     public static String JavaDoc getSkinUri() {
354
355         if (m_skinUri == null) {
356             m_skinUri = OpenCms.getSystemInfo().getContextPath() + RFS_PATH_RESOURCES;
357         }
358         return m_skinUri;
359     }
360
361     /**
362      * Returns the path to the cascading stylesheets.<p>
363      *
364      * @param jsp the JSP context
365      * @return the path to the cascading stylesheets
366      */

367     public static String JavaDoc getStyleUri(CmsJspActionElement jsp) {
368
369         if (m_styleUri == null) {
370
371             CmsProject project = jsp.getCmsObject().getRequestContext().currentProject();
372             try {
373                 jsp.getCmsObject().getRequestContext().setCurrentProject(
374                     jsp.getCmsObject().readProject(CmsProject.ONLINE_PROJECT_ID));
375                 m_styleUri = jsp.link("/system/workplace/commons/style/");
376             } catch (CmsException e) {
377                 LOG.error(e.getLocalizedMessage());
378             } finally {
379                 jsp.getCmsObject().getRequestContext().setCurrentProject(project);
380             }
381         }
382         return m_styleUri;
383     }
384
385     /**
386      * Returns the path to the cascading stylesheets.<p>
387      *
388      * @param jsp the JSP context
389      * @param filename the name of the stylesheet
390      * @return the path to the cascading stylesheets
391      */

392     public static String JavaDoc getStyleUri(CmsJspActionElement jsp, String JavaDoc filename) {
393
394         if (m_styleUri == null) {
395
396             CmsProject project = jsp.getCmsObject().getRequestContext().currentProject();
397             try {
398                 jsp.getCmsObject().getRequestContext().setCurrentProject(
399                     jsp.getCmsObject().readProject(CmsProject.ONLINE_PROJECT_ID));
400                 m_styleUri = jsp.link("/system/workplace/commons/style/");
401             } catch (CmsException e) {
402                 // ins log schreiben
403
} finally {
404                 jsp.getCmsObject().getRequestContext().setCurrentProject(project);
405             }
406         }
407         return m_styleUri + filename;
408     }
409
410     /**
411      * Updates the user settings in the given workplace settings for the current user, reading the user settings
412      * from the database if required.<p>
413      *
414      * @param cms the cms object for the current user
415      * @param settings the workplace settings to update (if <code>null</code> a new instance is created)
416      * @param update flag indicating if settings are only updated (user preferences)
417      *
418      * @return the current users workplace settings
419      *
420      * @see #initWorkplaceSettings(CmsObject, CmsWorkplaceSettings, boolean)
421      */

422     public static CmsWorkplaceSettings initUserSettings(CmsObject cms, CmsWorkplaceSettings settings, boolean update) {
423
424         if (settings == null) {
425             settings = new CmsWorkplaceSettings();
426         }
427
428         // save current workplace user & user settings object
429
CmsUser user;
430         if (update) {
431             try {
432                 // read the user from db to get the latest user information if required
433
user = cms.readUser(cms.getRequestContext().currentUser().getId());
434             } catch (CmsException e) {
435                 // can usually be ignored
436
if (LOG.isInfoEnabled()) {
437                     LOG.info(e.getLocalizedMessage());
438                 }
439                 user = cms.getRequestContext().currentUser();
440             }
441         } else {
442             user = cms.getRequestContext().currentUser();
443         }
444         // store the user and it's settings in the Workplace settings
445
settings.setUser(user);
446         settings.setUserSettings(new CmsUserSettings(cms, user));
447
448         // return the result settings
449
return settings;
450     }
451
452     /**
453      * Updates the given workplace settings, also re-initializing
454      * the state of the Workplace to the users preferences (for example setting the startup site and project).
455      *
456      * The user settings will also be updated by calling <code>{@link #initUserSettings(CmsObject, CmsWorkplaceSettings, boolean)}</code>
457      * before updating the workplace project, selected site etc.<p>
458      *
459      * @param cms the cms object for the current user
460      * @param settings the workplace settings to update (if <code>null</code> a new instance is created)
461      * @param update flag indicating if settings are only updated (user preferences)
462      *
463      * @return the current users initialized workplace settings
464      *
465      * @see #initUserSettings(CmsObject, CmsWorkplaceSettings, boolean)
466      */

467     public static synchronized CmsWorkplaceSettings initWorkplaceSettings(
468         CmsObject cms,
469         CmsWorkplaceSettings settings,
470         boolean update) {
471
472         // init the workplace user settings
473
settings = initUserSettings(cms, settings, update);
474
475         // save current project
476
settings.setProject(cms.getRequestContext().currentProject().getId());
477
478         // switch to users preferred site
479
String JavaDoc siteRoot = settings.getUserSettings().getStartSite();
480         if (siteRoot.endsWith("/")) {
481             // remove trailing slash
482
siteRoot = siteRoot.substring(0, siteRoot.length() - 1);
483         }
484         if (CmsStringUtil.isNotEmpty(siteRoot) && (CmsSiteManager.getSite(siteRoot) == null)) {
485             // this is not the root site and the site is not in the list
486
siteRoot = OpenCms.getWorkplaceManager().getDefaultUserSettings().getStartSite();
487             if (siteRoot.endsWith("/")) {
488                 // remove trailing slash
489
siteRoot = siteRoot.substring(0, siteRoot.length() - 1);
490             }
491         }
492         boolean access = false;
493         CmsResource res = null;
494         try {
495             // check access to the site
496
res = cms.readResource("/");
497             access = cms.hasPermissions(res, CmsPermissionSet.ACCESS_VIEW);
498         } catch (CmsException e) {
499             // error reading site root, in this case we will use a readable default
500
if (LOG.isInfoEnabled()) {
501                 LOG.info(e.getLocalizedMessage(), e);
502             }
503
504         }
505         if ((res == null) || !access) {
506             List JavaDoc sites = CmsSiteManager.getAvailableSites(cms, true);
507             if (sites.size() > 0) {
508                 siteRoot = ((CmsSite)sites.get(0)).getSiteRoot();
509                 cms.getRequestContext().setSiteRoot(siteRoot);
510             }
511         }
512         // set the current site
513
settings.setSite(siteRoot);
514
515         // set the preferred folder to display
516
settings.setExplorerResource(settings.getUserSettings().getStartFolder());
517
518         // get the default view from the user settings
519
settings.setViewUri(OpenCms.getLinkManager().substituteLink(cms, settings.getUserSettings().getStartView()));
520
521         // save the editable resource types for the current user
522
settings.setResourceTypes(initWorkplaceResourceTypes(cms));
523
524         return settings;
525     }
526
527     /**
528      * Stores the settings in the given session.<p>
529      *
530      * @param session the session to store the settings in
531      * @param settings the settings
532      */

533     static void storeSettings(HttpSession JavaDoc session, CmsWorkplaceSettings settings) {
534
535         // save the workplace settings in the session
536
session.setAttribute(CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS, settings);
537     }
538
539     /**
540      * Initializes a Map with all editable resource types for the current user.<p>
541      *
542      * @param cms the CmsObject
543      * @return all editable resource types in a map with the resource type id as key value
544      */

545     private static Map JavaDoc initWorkplaceResourceTypes(CmsObject cms) {
546
547         Map JavaDoc resourceTypes = new HashMap JavaDoc();
548         List JavaDoc allResTypes = OpenCms.getResourceManager().getResourceTypes();
549         for (int i = 0; i < allResTypes.size(); i++) {
550             // loop through all types and check which types can be displayed and edited for the user
551
I_CmsResourceType type = (I_CmsResourceType)allResTypes.get(i);
552             // get the settings for the resource type
553
CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(type.getTypeName());
554             if (settings != null) {
555                 // determine if this resource type is editable for the current user
556
CmsPermissionSet permissions = settings.getAccess().getPermissions(cms);
557                 if (permissions.requiresWritePermission()) {
558                     // user is allowed to edit this resource type
559
resourceTypes.put(new Integer JavaDoc(type.getTypeId()), type);
560                 }
561             }
562         }
563         return resourceTypes;
564     }
565
566     /**
567      * Returns all parameters of the current workplace class
568      * as hidden field tags that can be inserted in a form.<p>
569      *
570      * @return all parameters of the current workplace class
571      * as hidden field tags that can be inserted in a html form
572      */

573     public String JavaDoc allParamsAsHidden() {
574
575         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
576         Map JavaDoc params = allParamValues();
577         Iterator JavaDoc i = params.keySet().iterator();
578         while (i.hasNext()) {
579             String JavaDoc param = (String JavaDoc)i.next();
580             Object JavaDoc value = params.get(param);
581             result.append("<input type=\"hidden\" name=\"");
582             result.append(param);
583             result.append("\" value=\"");
584             String JavaDoc encoded = CmsEncoder.encode(value.toString(), getCms().getRequestContext().getEncoding());
585             result.append(encoded);
586             result.append("\">\n");
587         }
588         return result.toString();
589     }
590
591     /**
592      * Returns all present request parameters as String.<p>
593      *
594      * The String is formatted as a parameter String ("param1=val1&param2=val2") with UTF-8 encoded values.<p>
595      *
596      * @return all present request parameters as String
597      */

598     public String JavaDoc allParamsAsRequest() {
599
600         StringBuffer JavaDoc retValue = new StringBuffer JavaDoc(512);
601         HttpServletRequest JavaDoc request = getJsp().getRequest();
602         Iterator JavaDoc paramNames = request.getParameterMap().keySet().iterator();
603         while (paramNames.hasNext()) {
604             String JavaDoc paramName = (String JavaDoc)paramNames.next();
605             String JavaDoc paramValue = request.getParameter(paramName);
606             retValue.append(paramName + "=" + CmsEncoder.encode(paramValue, getCms().getRequestContext().getEncoding()));
607             if (paramNames.hasNext()) {
608                 retValue.append("&");
609             }
610         }
611         return retValue.toString();
612     }
613
614     /**
615      * Builds the end html of the body.<p>
616      *
617      * @return the end html of the body
618      */

619     public String JavaDoc bodyEnd() {
620
621         return pageBody(HTML_END, null, null);
622     }
623
624     /**
625      * Builds the start html of the body.<p>
626      *
627      * @param className optional class attribute to add to the body tag
628      * @return the start html of the body
629      */

630     public String JavaDoc bodyStart(String JavaDoc className) {
631
632         return pageBody(HTML_START, className, null);
633     }
634
635     /**
636      * Builds the start html of the body.<p>
637      *
638      * @param className optional class attribute to add to the body tag
639      * @param parameters optional parameters to add to the body tag
640      * @return the start html of the body
641      */

642     public String JavaDoc bodyStart(String JavaDoc className, String JavaDoc parameters) {
643
644         return pageBody(HTML_START, className, parameters);
645     }
646
647     /**
648      * Generates a html select box out of the provided values.<p>
649      *
650      * @param parameters a string that will be inserted into the initial select tag,
651      * if null no parameters will be inserted
652      * @param options the options
653      * @param values the option values, if null the select will have no value attributes
654      * @param selected the index of the pre-selected option, if -1 no option is pre-selected
655      * @return a formatted html String representing a html select box
656      */

657     public String JavaDoc buildSelect(String JavaDoc parameters, List JavaDoc options, List JavaDoc values, int selected) {
658
659         return buildSelect(parameters, options, values, selected, true);
660     }
661
662     /**
663      * Generates a button for the OpenCms workplace.<p>
664      *
665      * @param href the href link for the button, if none is given the button will be disabled
666      * @param target the href link target for the button, if none is given the target will be same window
667      * @param image the image name for the button, skin path will be automattically added as prefix
668      * @param label the label for the text of the button
669      * @param type 0: image only (default), 1: image and text, 2: text only
670      *
671      * @return a button for the OpenCms workplace
672      */

673     public String JavaDoc button(String JavaDoc href, String JavaDoc target, String JavaDoc image, String JavaDoc label, int type) {
674
675         return button(href, target, image, label, type, getSkinUri() + "buttons/");
676     }
677
678     /**
679      * Generates a button for the OpenCms workplace.<p>
680      *
681      * @param href the href link for the button, if none is given the button will be disabled
682      * @param target the href link target for the button, if none is given the target will be same window
683      * @param image the image name for the button, skin path will be automattically added as prefix
684      * @param label the label for the text of the button
685      * @param type 0: image only (default), 1: image and text, 2: text only
686      * @param imagePath the path to the image
687      *
688      * @return a button for the OpenCms workplace
689      */

690     public String JavaDoc button(String JavaDoc href, String JavaDoc target, String JavaDoc image, String JavaDoc label, int type, String JavaDoc imagePath) {
691
692         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
693
694         String JavaDoc anchorStart = "<a HREF=\"";
695         if (href != null && href.toLowerCase().startsWith("javascript:")) {
696             anchorStart = "<a HREF=\"#\" onclick=\"";
697         }
698
699         result.append("<td style=\"vertical-align: top;\">");
700         switch (type) {
701             case 1:
702                 // image and text
703
if (href != null) {
704                     result.append(anchorStart);
705                     result.append(href);
706                     result.append("\" class=\"button\"");
707                     if (target != null) {
708                         result.append(" target=\"");
709                         result.append(target);
710                         result.append("\"");
711                     }
712                     result.append(">");
713                 }
714                 result.append("<span unselectable=\"on\" ");
715                 if (href != null) {
716                     result.append("class=\"norm\" onmouseover=\"className='over'\" onmouseout=\"className='norm'\" onmousedown=\"className='push'\" onmouseup=\"className='over'\"");
717                 } else {
718                     result.append("class=\"disabled\"");
719                 }
720                 result.append("><span unselectable=\"on\" class=\"combobutton\" ");
721                 result.append("style=\"background-image: url('");
722                 result.append(imagePath);
723                 result.append(image);
724                 if (image != null && image.indexOf('.') == -1) {
725                     // append default suffix for button images
726
result.append(".png");
727                 }
728                 result.append("');\">");
729                 result.append(shortKey(label));
730                 result.append("</span></span>");
731                 if (href != null) {
732                     result.append("</a>");
733                 }
734                 break;
735
736             case 2:
737                 // text only
738
if (href != null) {
739                     result.append(anchorStart);
740                     result.append(href);
741                     result.append("\" class=\"button\"");
742                     if (target != null) {
743                         result.append(" target=\"");
744                         result.append(target);
745                         result.append("\"");
746                     }
747                     result.append(">");
748                 }
749                 result.append("<span unselectable=\"on\" ");
750                 if (href != null) {
751                     result.append("class=\"norm\" onmouseover=\"className='over'\" onmouseout=\"className='norm'\" onmousedown=\"className='push'\" onmouseup=\"className='over'\"");
752                 } else {
753                     result.append("class=\"disabled\"");
754                 }
755                 result.append("><span unselectable=\"on\" class=\"txtbutton\">");
756                 result.append(shortKey(label));
757                 result.append("</span></span>");
758                 if (href != null) {
759                     result.append("</a>");
760                 }
761                 break;
762
763             default:
764                 // only image
765
if (href != null) {
766                     result.append(anchorStart);
767                     result.append(href);
768                     result.append("\" class=\"button\"");
769                     if (target != null) {
770                         result.append(" target=\"");
771                         result.append(target);
772                         result.append("\"");
773                     }
774                     result.append(" title=\"");
775                     result.append(key(label));
776                     result.append("\">");
777                 }
778                 result.append("<span unselectable=\"on\" ");
779                 if (href != null) {
780                     result.append("class=\"norm\" onmouseover=\"className='over'\" onmouseout=\"className='norm'\" onmousedown=\"className='push'\" onmouseup=\"className='over'\"");
781                 } else {
782                     result.append("class=\"disabled\"");
783                 }
784                 result.append("><img class=\"button\" SRC=\"");
785                 result.append(imagePath);
786                 result.append(image);
787                 if (image != null && image.indexOf('.') == -1) {
788                     // append default suffix for button images
789
result.append(".png");
790                 }
791                 result.append("\" alt=\"");
792                 result.append(key(label));
793                 result.append("\">");
794                 result.append("</span>");
795                 if (href != null) {
796                     result.append("</a>");
797                 }
798                 break;
799         }
800         result.append("</td>\n");
801         return result.toString();
802     }
803
804     /**
805      * Returns the html for a button bar.<p>
806      *
807      * @param segment the HTML segment (START / END)
808      *
809      * @return a button bar html start / end segment
810      */

811     public String JavaDoc buttonBar(int segment) {
812
813         return buttonBar(segment, null);
814     }
815
816     /**
817      * Returns the html for a button bar.<p>
818      *
819      * @param segment the HTML segment (START / END)
820      * @param attributes optional attributes for the table tag
821      *
822      * @return a button bar html start / end segment
823      */

824     public String JavaDoc buttonBar(int segment, String JavaDoc attributes) {
825
826         if (segment == HTML_START) {
827             String JavaDoc result = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"";
828             if (attributes != null) {
829                 result += " " + attributes;
830             }
831             return result + "><tr>\n";
832         } else {
833             return "</tr></table>";
834         }
835     }
836
837     /**
838      * Generates a horizontal button bar separator line with maximum width.<p>
839      *
840      * @return a horizontal button bar separator line
841      */

842     public String JavaDoc buttonBarHorizontalLine() {
843
844         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
845         result.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"maxwidth\">\n");
846         result.append("<tr>\n");
847         result.append("\t<td class=\"horseparator\" ><img SRC=\"");
848         result.append(getSkinUri());
849         result.append("tree/empty.gif\" border=\"0\" width=\"1\" height=\"1\" alt=\"\"></td>\n");
850         result.append("</tr>\n");
851         result.append("</table>\n");
852         return result.toString();
853     }
854
855     /**
856      * Generates a button bar label.<p>
857      *
858      * @param label the label to show
859      *
860      * @return a button bar label
861      */

862     public String JavaDoc buttonBarLabel(String JavaDoc label) {
863
864         return buttonBarLabel(label, "norm");
865     }
866
867     /**
868      * Generates a button bar label.<p>
869      *
870      * @param label the label to show
871      * @param className the css class name for the formatting
872      *
873      * @return a button bar label
874      */

875     public String JavaDoc buttonBarLabel(String JavaDoc label, String JavaDoc className) {
876
877         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
878         result.append("<td><span class=\"");
879         result.append(className);
880         result.append("\"><span unselectable=\"on\" class=\"txtbutton\">");
881         result.append(key(label));
882         result.append("</span></span></td>\n");
883         return result.toString();
884     }
885
886     /**
887      * Generates a variable button bar separator line.<p>
888      *
889      * @param leftPixel the amount of pixel left to the line
890      * @param rightPixel the amount of pixel right to the line
891      * @param className the css class name for the formatting
892      *
893      * @return a variable button bar separator line
894      */

895     public String JavaDoc buttonBarLine(int leftPixel, int rightPixel, String JavaDoc className) {
896
897         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
898         if (leftPixel > 0) {
899             result.append(buttonBarLineSpacer(leftPixel));
900         }
901         result.append("<td><span class=\"");
902         result.append(className);
903         result.append("\"></span></td>\n");
904         if (rightPixel > 0) {
905             result.append(buttonBarLineSpacer(rightPixel));
906         }
907         return result.toString();
908     }
909
910     /**
911      * Generates a variable button bar separator line spacer.<p>
912      *
913      * @param pixel the amount of pixel space
914      *
915      * @return a variable button bar separator line spacer
916      */

917     public String JavaDoc buttonBarLineSpacer(int pixel) {
918
919         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
920         result.append("<td><span class=\"norm\"><span unselectable=\"on\" class=\"txtbutton\" style=\"padding-right: 0px; padding-left: ");
921         result.append(pixel);
922         result.append("px;\"></span></span></td>\n");
923         return result.toString();
924     }
925
926     /**
927      * Generates a button bar separator.<p>
928      *
929      * @param leftPixel the amount of pixel left to the separator
930      * @param rightPixel the amount of pixel right to the separator
931      *
932      * @return a button bar separator
933      */

934     public String JavaDoc buttonBarSeparator(int leftPixel, int rightPixel) {
935
936         return buttonBarLine(leftPixel, rightPixel, "separator");
937     }
938
939     /**
940      * Returns the html for an invisible spacer between button bar contents like buttons, labels, etc.<p>
941      *
942      * @param width the width of the invisible spacer
943      * @return the html for the invisible spacer
944      */

945     public String JavaDoc buttonBarSpacer(int width) {
946
947         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
948         result.append("<td><span class=\"norm\"><span unselectable=\"on\" class=\"txtbutton\" style=\"width: ");
949         result.append(width);
950         result.append("px;\"></span></span></td>\n");
951         return result.toString();
952     }
953
954     /**
955      * Generates a button bar starter tab.<p>
956      *
957      * @param leftPixel the amount of pixel left to the starter
958      * @param rightPixel the amount of pixel right to the starter
959      *
960      * @return a button bar starter tab
961      */

962     public String JavaDoc buttonBarStartTab(int leftPixel, int rightPixel) {
963
964         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
965         result.append(buttonBarLineSpacer(leftPixel));
966         result.append("<td><span class=\"starttab\"><span style=\"width:1px; height:1px\"></span></span></td>\n");
967         result.append(buttonBarLineSpacer(rightPixel));
968         return result.toString();
969     }
970
971     /**
972      * Checks the lock state of the resource and locks it if the autolock feature is enabled.<p>
973      *
974      * @param resource the resource name which is checked
975      * @throws CmsException if reading or locking the resource fails
976      */

977     public void checkLock(String JavaDoc resource) throws CmsException {
978
979         checkLock(resource, org.opencms.lock.CmsLock.COMMON);
980     }
981
982     /**
983      * Checks the lock state of the resource and locks it if the autolock feature is enabled.<p>
984      *
985      * @param resource the resource name which is checked
986      * @param mode flag indicating the mode (temporary or common) of a lock
987      * @throws CmsException if reading or locking the resource fails
988      */

989     public void checkLock(String JavaDoc resource, int mode) throws CmsException {
990
991         CmsResource res = getCms().readResource(resource, CmsResourceFilter.ALL);
992         CmsLock lock = getCms().getLock(res);
993         if (OpenCms.getWorkplaceManager().autoLockResources()) {
994             // autolock is enabled, check the lock state of the resource
995
if (lock.isNullLock()) {
996                 // resource is not locked, lock it automatically
997
getCms().lockResource(resource, mode);
998             } else if (!lock.getUserId().equals(getCms().getRequestContext().currentUser().getId())) {
999                 throw new CmsException(Messages.get().container(Messages.ERR_WORKPLACE_LOCK_RESOURCE_1, resource));
1000            }
1001        } else {
1002            if (lock.isNullLock()
1003                || (!lock.isNullLock() && !lock.getUserId().equals(getCms().getRequestContext().currentUser().getId()))) {
1004                throw new CmsException(Messages.get().container(Messages.ERR_WORKPLACE_LOCK_RESOURCE_1, resource));
1005            }
1006        }
1007    }
1008
1009    /**
1010     * First sets site and project in the workplace settings, then fills all class parameter values from the data
1011     * provided in the current request.<p>
1012     *
1013     * @param settings the workplace settings
1014     * @param request the current request
1015     */

1016    public void fillParamValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
1017
1018        initSettings(settings, request);
1019        fillParamValues(request);
1020    }
1021
1022    /**
1023     * Fills all class parameter values from the data provided in the current request.<p>
1024     *
1025     * All methods that start with "setParam" are possible candidates to be
1026     * automatically filled. The remaining part of the method name is converted
1027     * to lower case. Then a parameter of this name is searched in the request parameters.
1028     * If the parameter is found, the "setParam" method is automatically invoked
1029     * by reflection with the value of the parameter.<p>
1030     *
1031     * @param request the current JSP request
1032     */

1033    public void fillParamValues(HttpServletRequest JavaDoc request) {
1034
1035        m_parameterMap = null;
1036        // ensure a multipart request is pared only once (for "forward" screnarios with reports)
1037
if (null == request.getAttribute(REQUEST_ATTRIBUTE_MULTIPART)) {
1038            // check if this is a multipart request
1039
m_multiPartFileItems = CmsRequestUtil.readMultipartFileItems(request);
1040            if (m_multiPartFileItems != null) {
1041                // this was indeed a multipart form request
1042
m_parameterMap = CmsRequestUtil.readParameterMapFromMultiPart(
1043                    getCms().getRequestContext().getEncoding(),
1044                    m_multiPartFileItems);
1045                request.setAttribute(REQUEST_ATTRIBUTE_MULTIPART, Boolean.TRUE);
1046            }
1047        }
1048        if (m_parameterMap == null) {
1049            // the request was a "normal" request
1050
m_parameterMap = request.getParameterMap();
1051        }
1052
1053        List JavaDoc methods = paramSetMethods();
1054        Iterator JavaDoc i = methods.iterator();
1055        while (i.hasNext()) {
1056            Method JavaDoc m = (Method JavaDoc)i.next();
1057            String JavaDoc name = m.getName().substring(8).toLowerCase();
1058            String JavaDoc[] values = (String JavaDoc[])m_parameterMap.get(name);
1059            String JavaDoc value = null;
1060            if (values != null) {
1061                // get the parameter value from the map
1062
value = values[0];
1063            }
1064            if (CmsStringUtil.isEmpty(value)) {
1065                value = null;
1066            }
1067            value = decodeParamValue(name, value);
1068            try {
1069                if (LOG.isDebugEnabled() && (value != null)) {
1070                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_SET_PARAM_2, m.getName(), value));
1071                }
1072                m.invoke(this, new Object JavaDoc[] {value});
1073            } catch (InvocationTargetException JavaDoc ite) {
1074                // can usually be ignored
1075
if (LOG.isInfoEnabled()) {
1076                    LOG.info(ite.getLocalizedMessage());
1077                }
1078            } catch (IllegalAccessException JavaDoc eae) {
1079                // can usually be ignored
1080
if (LOG.isInfoEnabled()) {
1081                    LOG.info(eae.getLocalizedMessage());
1082                }
1083            }
1084        }
1085    }
1086
1087    /**
1088     * Returns the message String for the broadcast message alert of the workplace.<p>
1089     *
1090     * Caution: returns the pure message String (not escaped) or null, if no message is pending.<p>
1091     *
1092     * @return the message String for the broadcast message alert of the workplace
1093     */

1094    public String JavaDoc getBroadcastMessageString() {
1095
1096        String JavaDoc sessionId = getSession().getId();
1097        Buffer messageQueue = OpenCms.getSessionManager().getBroadcastQueue(sessionId);
1098        if (!messageQueue.isEmpty()) {
1099            // create message String
1100
StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
1101            // the user has pending messages, display them all
1102
while (!messageQueue.isEmpty()) {
1103                CmsBroadcast message = (CmsBroadcast)messageQueue.remove();
1104                result.append('[');
1105                result.append(getMessages().getDateTime(message.getSendTime()));
1106                result.append("] ");
1107                result.append(key(Messages.GUI_LABEL_BROADCASTMESSAGEFROM_0));
1108                result.append(' ');
1109                result.append(message.getUser().getName());
1110                result.append(":\n");
1111                result.append(message.getMessage());
1112                result.append("\n\n");
1113            }
1114            return result.toString();
1115        }
1116        // no message pending, return null
1117
return null;
1118    }
1119
1120    /**
1121     * Returns the initialized cms object for the current user.<p>
1122     *
1123     * @return the initialized cms object for the current user
1124     */

1125    public CmsObject getCms() {
1126
1127        return m_cms;
1128    }
1129
1130    /**
1131     * Returns the current workplace encoding.<p>
1132     *
1133     * @return the current workplace encoding
1134     */

1135    public String JavaDoc getEncoding() {
1136
1137        return OpenCms.getWorkplaceManager().getEncoding();
1138    }
1139
1140    /**
1141     * Returns the uri (including context path) to the explorer file list.<p>
1142     *
1143     * @return the uri (including context path) to the explorer file list
1144     */

1145    public String JavaDoc getExplorerFileListFullUri() {
1146
1147        if (m_file_explorer_filelist != null) {
1148            return m_file_explorer_filelist;
1149        }
1150        synchronized (this) {
1151            m_file_explorer_filelist = OpenCms.getLinkManager().substituteLink(getCms(), FILE_EXPLORER_FILELIST);
1152        }
1153        return m_file_explorer_filelist;
1154    }
1155
1156    /**
1157     * Returns the html for the frame name and source and stores this information in the workplace settings.<p>
1158     *
1159     * @param frameName the name of the frame
1160     * @param uri the absolute path of the frame
1161     * @return the html for the frame name and source
1162     */

1163    public String JavaDoc getFrameSource(String JavaDoc frameName, String JavaDoc uri) {
1164
1165        String JavaDoc frameString = "name=\"" + frameName + "\" SRC=\"" + uri + "\"";
1166        int paramIndex = uri.indexOf("?");
1167        if (paramIndex != -1) {
1168            // remove request parameters from URI before putting it to Map
1169
uri = uri.substring(0, uri.indexOf("?"));
1170        }
1171        getSettings().getFrameUris().put(frameName, uri);
1172        return frameString;
1173    }
1174
1175    /**
1176     * Returns the JSP action element.<p>
1177     *
1178     * @return the JSP action element
1179     */

1180    public CmsJspActionElement getJsp() {
1181
1182        return m_jsp;
1183    }
1184
1185    /**
1186     * Returns the current users workplace locale settings.<p>
1187     *
1188     * @return the current users workplace locale setting
1189     */

1190    public Locale JavaDoc getLocale() {
1191
1192        return m_settings.getUserSettings().getLocale();
1193    }
1194
1195    /**
1196     * Returns the current used macro resolver instance.<p>
1197     *
1198     * @return the macro resolver
1199     */

1200    public CmsMacroResolver getMacroResolver() {
1201
1202        if (m_macroResolver == null) {
1203            // create a new macro resolver "with everything we got"
1204
m_macroResolver = CmsMacroResolver.newInstance()
1205            // initialize resolver with the objects available
1206
.setCmsObject(m_cms).setMessages(getMessages()).setJspPageContext(
1207                    (m_jsp == null) ? null : m_jsp.getJspContext());
1208        }
1209        return m_macroResolver;
1210    }
1211
1212    /**
1213     * Returns the current used message object.<p>
1214     *
1215     * @return the current used message object
1216     */

1217    public CmsMessages getMessages() {
1218
1219        return m_messages;
1220    }
1221
1222    /**
1223     * Returns a list of FileItem instances parsed from the request, in the order that they were transmitted.<p>
1224     *
1225     * This list is automatically initialized from the createParameterMapFromMultiPart(HttpServletRequest) method.<p>
1226     *
1227     * @return list of FileItem instances parsed from the request, in the order that they were transmitted
1228     */

1229    public List JavaDoc getMultiPartFileItems() {
1230
1231        return m_multiPartFileItems;
1232    }
1233
1234    /**
1235     * Returns the path to the workplace static resources.<p>
1236     *
1237     * Workplaces static resources are images, css files etc.
1238     * These are exported during the installation of OpenCms,
1239     * and are usually only read from this exported location to
1240     * avoid the overhaead of accessing the database later.<p>
1241     *
1242     * @return the path to the workplace static resources
1243     */

1244    public String JavaDoc getResourceUri() {
1245
1246        if (m_resourceUri == null) {
1247            m_resourceUri = OpenCms.getSystemInfo().getContextPath() + CmsWorkplace.RFS_PATH_RESOURCES;
1248        }
1249        return m_resourceUri;
1250    }
1251
1252    /**
1253     * Returns the current user http session.<p>
1254     *
1255     * @return the current user http session
1256     */

1257    public HttpSession JavaDoc getSession() {
1258
1259        return m_session;
1260    }
1261
1262    /**
1263     * Returns the current users workplace settings.<p>
1264     *
1265     * @return the current users workplace settings
1266     */

1267    public CmsWorkplaceSettings getSettings() {
1268
1269        return m_settings;
1270    }
1271
1272    /**
1273     * Returns the path to the cascading stylesheets.<p>
1274     *
1275     * @param filename the name of the stylesheet
1276     * @return the path to the cascading stylesheets
1277     */

1278    public String JavaDoc getStyleUri(String JavaDoc filename) {
1279
1280        return getStyleUri(getJsp(), filename);
1281    }
1282
1283    /**
1284     * Builds the end html of the page.<p>
1285     *
1286     * @return the end html of the page
1287     */

1288    public String JavaDoc htmlEnd() {
1289
1290        return pageHtml(HTML_END, null);
1291    }
1292
1293    /**
1294     * Builds the start html of the page, including setting of DOCTYPE and
1295     * inserting a header with the content-type.<p>
1296     *
1297     * @param title the content for the title tag
1298     * @return the start html of the page
1299     */

1300    public String JavaDoc htmlStart(String JavaDoc title) {
1301
1302        return pageHtml(HTML_START, title);
1303    }
1304
1305    /**
1306     * Sets site and project in the workplace settings with the request values of parameters
1307     * <code>{@link CmsWorkplace#PARAM_WP_SITE}</code> and <code>{@link CmsWorkplace#PARAM_WP_PROJECT}</code>.<p>
1308     *
1309     * @param settings the workplace settings
1310     * @param request the current request
1311     *
1312     * @return true, if a reload of the main body frame is required
1313     */

1314    public boolean initSettings(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
1315
1316        // check if the user requested a project change
1317
String JavaDoc project = request.getParameter(PARAM_WP_PROJECT);
1318        boolean reloadRequired = false;
1319        if (project != null) {
1320            reloadRequired = true;
1321            try {
1322                getCms().readProject(Integer.parseInt(project));
1323            } catch (Exception JavaDoc e) {
1324                // project not found, set online project
1325
project = String.valueOf(CmsProject.ONLINE_PROJECT_ID);
1326            }
1327            try {
1328                m_cms.getRequestContext().setCurrentProject(getCms().readProject(Integer.parseInt(project)));
1329            } catch (Exception JavaDoc e) {
1330                if (LOG.isInfoEnabled()) {
1331                    LOG.info(e);
1332                }
1333            }
1334            settings.setProject(Integer.parseInt(project));
1335        }
1336
1337        // check if the user requested a site change
1338
String JavaDoc site = request.getParameter(PARAM_WP_SITE);
1339        if (site != null) {
1340            reloadRequired = true;
1341            m_cms.getRequestContext().setSiteRoot(site);
1342            settings.setSite(site);
1343        }
1344
1345        // check which resource was requested
1346
String JavaDoc explorerResource = request.getParameter(PARAM_WP_EXPLORER_RESOURCE);
1347        if (explorerResource != null) {
1348            reloadRequired = true;
1349            settings.setExplorerResource(explorerResource);
1350        }
1351
1352        return reloadRequired;
1353    }
1354
1355    /**
1356     * Returns the forwarded flag.<p>
1357     *
1358     * @return the forwarded flag
1359     */

1360    public boolean isForwarded() {
1361
1362        return m_forwarded;
1363    }
1364
1365    /**
1366     * Returns true if the online help for the users current workplace language is installed.<p>
1367     *
1368     * @return true if the online help for the users current workplace language is installed
1369     */

1370    public boolean isHelpEnabled() {
1371
1372        return getCms().existsResource(
1373            resolveMacros(CmsHelpTemplateBean.PATH_HELP),
1374            CmsResourceFilter.IGNORE_EXPIRATION);
1375    }
1376
1377    /**
1378     * Returns true if the currently processed element is an included sub element.<p>
1379     *
1380     * @return true if the currently processed element is an included sub element
1381     */

1382    public boolean isSubElement() {
1383
1384        return !getJsp().getRequestContext().getUri().equals(getJsp().info("opencms.request.element.uri"));
1385    }
1386
1387    /**
1388     * Returns the localized resource string for a given message key,
1389     * checking the workplace default resources and all module bundles.<p>
1390     *
1391     * If the key was not found, the return value is
1392     * <code>"??? " + keyName + " ???"</code>.<p>
1393     *
1394     * If the key starts with <code>"help."</code> and is not found,
1395     * the value <code>"index.html"</code> is returned.<p>
1396     *
1397     * @param keyName the key for the desired string
1398     * @return the resource string for the given key
1399     *
1400     * @see CmsMessages#key(String)
1401     */

1402    public String JavaDoc key(String JavaDoc keyName) {
1403
1404        return getMessages().key(keyName);
1405    }
1406
1407    /**
1408     * Returns the localized resource string for a given message key,
1409     * with the provided replacement parameters.<p>
1410     *
1411     * If the key was found in the bundle, it will be formatted using
1412     * a <code>{@link java.text.MessageFormat}</code> using the provided parameters.<p>
1413     *
1414     * If the key was not found in the bundle, the return value is
1415     * <code>"??? " + keyName + " ???"</code>. This will also be returned
1416     * if the bundle was not properly initialized first.
1417     *
1418     * @param keyName the key for the desired string
1419     * @param params the parameters to use for formatting
1420     * @return the resource string for the given key
1421     *
1422     * @see CmsMessages#key(String)
1423     */

1424    public String JavaDoc key(String JavaDoc keyName, Object JavaDoc[] params) {
1425
1426        return getMessages().key(keyName, params);
1427    }
1428
1429    /**
1430     * Returns the localized resource string for the given message key,
1431     * checking the workplace default resources and all module bundles.<p>
1432     *
1433     * If the key was not found, the provided default value
1434     * is returned.<p>
1435     *
1436     * @param keyName the key for the desired string
1437     * @param defaultValue the default value in case the key does not exist in the bundle
1438     * @return the resource string for the given key it it exists, or the given default if not
1439     *
1440     * @see CmsMessages#keyDefault(String, String)
1441     */

1442    public String JavaDoc keyDefault(String JavaDoc keyName, String JavaDoc defaultValue) {
1443
1444        return getMessages().keyDefault(keyName, defaultValue);
1445    }
1446
1447    /**
1448     * Returns the empty String "" if the provided value is null, otherwise just returns
1449     * the provided value.<p>
1450     *
1451     * Use this method in forms if a getParamXXX method is used, but a String (not null)
1452     * is required.
1453     *
1454     * @param value the String to check
1455     * @return the empty String "" if the provided value is null, otherwise just returns
1456     * the provided value
1457     */

1458    public String JavaDoc nullToEmpty(String JavaDoc value) {
1459
1460        if (value != null) {
1461            return value;
1462        }
1463        return "";
1464    }
1465
1466    /**
1467     * Builds the html of the body.<p>
1468     *
1469     * @param segment the HTML segment (START / END)
1470     * @param className optional class attribute to add to the body tag
1471     * @param parameters optional parameters to add to the body tag
1472     * @return the html of the body
1473     */

1474    public String JavaDoc pageBody(int segment, String JavaDoc className, String JavaDoc parameters) {
1475
1476        if (segment == HTML_START) {
1477            StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
1478            result.append("</head>\n<body unselectable=\"on\"");
1479            if (getSettings().isViewAdministration()) {
1480                if (className == null || "dialog".equals(className)) {
1481                    className = "dialogadmin";
1482                }
1483                if (parameters == null) {
1484                    result.append(" onLoad=\"window.top.body.admin_head.location.href='");
1485                    result.append(getJsp().link(CmsWorkplace.VFS_PATH_WORKPLACE + "action/administration_head.html"));
1486                    result.append("';\"");
1487                }
1488            }
1489            if (className != null) {
1490                result.append(" class=\"");
1491                result.append(className);
1492                result.append("\"");
1493            }
1494            if (CmsStringUtil.isNotEmpty(parameters)) {
1495                result.append(" ");
1496                result.append(parameters);
1497            }
1498            result.append(">\n");
1499            return result.toString();
1500        } else {
1501            return "</body>";
1502        }
1503    }
1504
1505    /**
1506     * Returns the default html for a workplace page, including setting of DOCTYPE and
1507     * inserting a header with the content-type.<p>
1508     *
1509     * @param segment the HTML segment (START / END)
1510     * @param title the title of the page, if null no title tag is inserted
1511     * @return the default html for a workplace page
1512     */

1513    public String JavaDoc pageHtml(int segment, String JavaDoc title) {
1514
1515        return pageHtmlStyle(segment, title, null);
1516    }
1517
1518    /**
1519     * Returns the default html for a workplace page, including setting of DOCTYPE and
1520     * inserting a header with the content-type, allowing the selection of an individual style sheet.<p>
1521     *
1522     * @param segment the HTML segment (START / END)
1523     * @param title the title of the page, if null no title tag is inserted
1524     * @param stylesheet the used style sheet, if null the default stylesheet 'workplace.css' is inserted
1525     * @return the default html for a workplace page
1526     */

1527    public String JavaDoc pageHtmlStyle(int segment, String JavaDoc title, String JavaDoc stylesheet) {
1528
1529        if (segment == HTML_START) {
1530            StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
1531            result.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n");
1532            result.append("<html>\n<head>\n");
1533            if (title != null) {
1534                result.append("<title>");
1535                result.append(title);
1536                result.append("</title>\n");
1537            }
1538            result.append("<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=");
1539            result.append(getEncoding());
1540            result.append("\">\n");
1541            result.append("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"");
1542
1543            result.append(getStyleUri(getJsp(), stylesheet == null ? "workplace.css" : stylesheet));
1544            result.append("\">\n");
1545            return result.toString();
1546        } else {
1547            return "</html>";
1548        }
1549    }
1550
1551    /**
1552     * Returns all initialized parameters of the current workplace class
1553     * as hidden field tags that can be inserted in a form.<p>
1554     *
1555     * @return all initialized parameters of the current workplace class
1556     * as hidden field tags that can be inserted in a html form
1557     */

1558    public String JavaDoc paramsAsHidden() {
1559
1560        return paramsAsHidden(null);
1561    }
1562
1563    /**
1564     * Returns all initialized parameters of the current workplace class
1565     * that are not in the given exclusion list as hidden field tags that can be inserted in a form.<p>
1566     *
1567     * @param excludes the parameters to exclude
1568     *
1569     * @return all initialized parameters of the current workplace class
1570     * that are not in the given exclusion list as hidden field tags that can be inserted in a form
1571     */

1572    public String JavaDoc paramsAsHidden(Collection JavaDoc excludes) {
1573
1574        StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
1575        Map JavaDoc params = paramValues();
1576        Iterator JavaDoc i = params.keySet().iterator();
1577        while (i.hasNext()) {
1578            String JavaDoc param = (String JavaDoc)i.next();
1579            if ((excludes == null) || (!excludes.contains(param))) {
1580                Object JavaDoc value = params.get(param);
1581                result.append("<input type=\"hidden\" name=\"");
1582                result.append(param);
1583                result.append("\" value=\"");
1584                String JavaDoc encoded = CmsEncoder.encode(value.toString(), getCms().getRequestContext().getEncoding());
1585                result.append(encoded);
1586                result.append("\">\n");
1587            }
1588        }
1589        return result.toString();
1590    }
1591
1592    /**
1593     * Returns all initialized parameters of the current workplace class in the
1594     * form of a parameter map, i.e. the values are arrays.<p>
1595     *
1596     * @return all initialized parameters of the current workplace class in the
1597     * form of a parameter map
1598     */

1599    public Map JavaDoc paramsAsParameterMap() {
1600
1601        return CmsRequestUtil.createParameterMap(paramValues());
1602    }
1603
1604    /**
1605     * Returns all initialized parameters of the current workplace class
1606     * as request parameters, i.e. in the form <code>key1=value1&key2=value2</code> etc.
1607     *
1608     * @return all initialized parameters of the current workplace class
1609     * as request parameters
1610     */

1611    public String JavaDoc paramsAsRequest() {
1612
1613        StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
1614        Map JavaDoc params = paramValues();
1615        Iterator JavaDoc i = params.keySet().iterator();
1616        while (i.hasNext()) {
1617            String JavaDoc param = (String JavaDoc)i.next();
1618            Object JavaDoc value = params.get(param);
1619            result.append(param);
1620            result.append("=");
1621            result.append(CmsEncoder.encode(value.toString(), getCms().getRequestContext().getEncoding()));
1622            if (i.hasNext()) {
1623                result.append("&");
1624            }
1625        }
1626        return result.toString();
1627    }
1628
1629    /**
1630     * Resolves the macros in the given String and replaces them by their localized keys.<p>
1631     *
1632     * The following macro contexts are available in the Workplace:<ul>
1633     * <li>Macros based on the current users OpenCms context (obtained from the current <code>{@link CmsObject}</code>).</li>
1634     * <li>Localized key macros (obtained from the current <code>{@link CmsMessages}</code>).</li>
1635     * <li>Macros from the current JSP page context (obtained by <code>{@link #getJsp()}</code>).</li>
1636     * </ul>
1637     *
1638     * @param input the input String containing the macros
1639     * @return the resolved String
1640     *
1641     * @see CmsMacroResolver#resolveMacros(String)
1642     */

1643    public String JavaDoc resolveMacros(String JavaDoc input) {
1644
1645        // resolve the macros
1646
return getMacroResolver().resolveMacros(input);
1647    }
1648
1649    /**
1650     * Sends a http redirect to the specified URI in the OpenCms VFS.<p>
1651     *
1652     * @param location the location the response is redirected to
1653     * @throws IOException in case redirection fails
1654     */

1655    public void sendCmsRedirect(String JavaDoc location) throws IOException JavaDoc {
1656
1657        // IBM Websphere patch: use forward here
1658
int todo = 0;
1659        getJsp().getResponse().sendRedirect(OpenCms.getSystemInfo().getOpenCmsContext() + location);
1660    }
1661
1662    /**
1663     * Forwards to the specified location in the OpenCms VFS.<p>
1664     *
1665     * @param location the location the response is redirected to
1666     * @param params the map of parameters to use for the forwarded request
1667     *
1668     * @throws IOException in case the forward fails
1669     * @throws ServletException in case the forward fails
1670     */

1671    public void sendForward(String JavaDoc location, Map JavaDoc params) throws IOException JavaDoc, ServletException JavaDoc {
1672
1673        setForwarded(true);
1674        // params must be arrays of String, ensure this is the case
1675
params = CmsRequestUtil.createParameterMap(params);
1676        CmsRequestUtil.forwardRequest(getJsp().link(location), params, getJsp().getRequest(), getJsp().getResponse());
1677    }
1678
1679    /**
1680     * Sets the forwarded flag.<p>
1681     *
1682     * @param forwarded the forwarded flag to set
1683     */

1684    public void setForwarded(boolean forwarded) {
1685
1686        m_forwarded = forwarded;
1687    }
1688
1689    /**
1690     * Get a localized short key value for the workplace.<p>
1691     *
1692     * @param keyName name of the key
1693     * @return a localized short key value
1694     */

1695    public String JavaDoc shortKey(String JavaDoc keyName) {
1696
1697        String JavaDoc value = keyDefault(keyName + CmsMessages.KEY_SHORT_SUFFIX, (String JavaDoc)null);
1698        if (value == null) {
1699            // short key value not found, return "long" key value
1700
return key(keyName);
1701        }
1702        return value;
1703    }
1704
1705    /**
1706     * Auxiliary method for initialization of messages.<p>
1707     *
1708     * @param messages the {@link CmsMessages} to add
1709     */

1710    protected void addMessages(CmsMessages messages) {
1711
1712        if (messages != null) {
1713            m_messages.addMessages(messages);
1714        }
1715    }
1716
1717    /**
1718     * Auxiliary method for initialization of messages.<p>
1719     *
1720     * @param bundleName the resource bundle name to add
1721     */

1722    protected void addMessages(String JavaDoc bundleName) {
1723
1724        addMessages(new CmsMessages(bundleName, getLocale()));
1725    }
1726
1727    /**
1728     * Returns the values of all parameter methods of this workplace class instance.<p>
1729     *
1730     * @return the values of all parameter methods of this workplace class instance
1731     */

1732    protected Map JavaDoc allParamValues() {
1733
1734        List JavaDoc methods = paramGetMethods();
1735        Map JavaDoc map = new HashMap JavaDoc(methods.size());
1736        Iterator JavaDoc i = methods.iterator();
1737        while (i.hasNext()) {
1738            Method JavaDoc m = (Method JavaDoc)i.next();
1739            Object JavaDoc o = null;
1740            try {
1741                o = m.invoke(this, new Object JavaDoc[0]);
1742            } catch (InvocationTargetException JavaDoc ite) {
1743                // can usually be ignored
1744
if (LOG.isInfoEnabled()) {
1745                    LOG.info(ite);
1746                }
1747            } catch (IllegalAccessException JavaDoc eae) {
1748                // can usually be ignored
1749
if (LOG.isInfoEnabled()) {
1750                    LOG.info(eae);
1751                }
1752            }
1753            if (o == null) {
1754                o = "";
1755            }
1756            map.put(m.getName().substring(8).toLowerCase(), o);
1757        }
1758        return map;
1759    }
1760
1761    /**
1762     * Decodes an individual parameter value.<p>
1763     *
1764     * In special cases some parameters might require a different-from-default
1765     * encoding. This is the case if the content of the parameter was
1766     * encoded using the JavaScript encodeURIComponent() method on the client,
1767     * which always encodes in UTF-8.<p>
1768     *
1769     * @param paramName the name of the parameter
1770     * @param paramValue the unencoded value of the parameter
1771     *
1772     * @return the encoded value of the parameter
1773     */

1774    protected String JavaDoc decodeParamValue(String JavaDoc paramName, String JavaDoc paramValue) {
1775
1776        if ((paramName != null) && (paramValue != null)) {
1777            return CmsEncoder.decode(paramValue, getCms().getRequestContext().getEncoding());
1778        } else {
1779            return null;
1780        }
1781    }
1782
1783    /**
1784     * Returns the map of parameters read from the current request.<p>
1785     *
1786     * This method will also handle parameters from forms
1787     * of type <code>multipart/form-data</code>.<p>
1788     *
1789     * @return the map of parameters read from the current request
1790     */

1791    protected Map JavaDoc getParameterMap() {
1792
1793        return m_parameterMap;
1794    }
1795
1796    /**
1797     * Initializes the message object.<p>
1798     *
1799     * By default the {@link CmsWorkplaceMessages} are initialized.<p>
1800     *
1801     * You SHOULD override this method for setting the bundles you really need,
1802     * using the <code>{@link #addMessages(CmsMessages)}</code> or <code>{@link #addMessages(String)}</code> method.<p>
1803     */

1804    protected void initMessages() {
1805
1806        // no bundles are added by default as all core bundles are added as part of the WorkplaceModuleMessages
1807
}
1808
1809    /**
1810     * Initializes this workplace class instance.<p>
1811     *
1812     * This method can be used in case there a workplace class was generated using
1813     * {@link Class#forName(java.lang.String)} to initialize the class members.<p>
1814     *
1815     * @param jsp the initialized JSP context
1816     */

1817    protected void initWorkplaceMembers(CmsJspActionElement jsp) {
1818
1819        if (jsp != null) {
1820            m_jsp = jsp;
1821            m_cms = m_jsp.getCmsObject();
1822            m_session = m_jsp.getRequest().getSession();
1823
1824            // get / create the workplace settings
1825
m_settings = (CmsWorkplaceSettings)m_session.getAttribute(CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS);
1826
1827            if (m_settings == null) {
1828                // create the settings object
1829
m_settings = new CmsWorkplaceSettings();
1830                m_settings = initWorkplaceSettings(m_cms, m_settings, false);
1831                storeSettings(m_session, m_settings);
1832            }
1833
1834            // initialize messages
1835
CmsMessages messages = OpenCms.getWorkplaceManager().getMessages(getLocale());
1836            // generate a new multi messages object and add the messages from the workplace
1837
m_messages = new CmsMultiMessages(getLocale());
1838            m_messages.addMessages(messages);
1839            initMessages();
1840
1841            // check request for changes in the workplace settings
1842
initWorkplaceRequestValues(m_settings, m_jsp.getRequest());
1843
1844            // set cms context accordingly
1845
initWorkplaceCmsContext(m_settings, m_cms);
1846        }
1847    }
1848
1849    /**
1850     * Analyzes the request for workplace parameters and adjusts the workplace
1851     * settings accordingly.<p>
1852     *
1853     * @param settings the workplace settings
1854     * @param request the current request
1855     */

1856    protected abstract void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request);
1857
1858    /**
1859     * Returns the values of all parameter methods of this workplace class instance.<p>
1860     *
1861     * @return the values of all parameter methods of this workplace class instance
1862     */

1863    protected Map JavaDoc paramValues() {
1864
1865        List JavaDoc methods = paramGetMethods();
1866        Map JavaDoc map = new HashMap JavaDoc(methods.size());
1867        Iterator JavaDoc i = methods.iterator();
1868        while (i.hasNext()) {
1869            Method JavaDoc m = (Method JavaDoc)i.next();
1870            Object JavaDoc o = null;
1871            try {
1872                o = m.invoke(this, new Object JavaDoc[0]);
1873            } catch (InvocationTargetException JavaDoc ite) {
1874                // can usually be ignored
1875
if (LOG.isInfoEnabled()) {
1876                    LOG.info(ite.getLocalizedMessage());
1877                }
1878            } catch (IllegalAccessException JavaDoc eae) {
1879                // can usually be ignored
1880
if (LOG.isInfoEnabled()) {
1881                    LOG.info(eae.getLocalizedMessage());
1882                }
1883            }
1884            if (o != null) {
1885                map.put(m.getName().substring(8).toLowerCase(), o);
1886            }
1887        }
1888        return map;
1889    }
1890
1891    /**
1892     * Helper method to change back from the temporary project to the current project.<p>
1893     *
1894     * @throws CmsException if switching back fails
1895     */

1896    protected void switchToCurrentProject() throws CmsException {
1897
1898        if (m_currentProjectId != -1) {
1899            // switch back to the current users project
1900
getCms().getRequestContext().setCurrentProject(getCms().readProject(m_currentProjectId));
1901        }
1902    }
1903
1904    /**
1905     * Helper method to change the current project to the temporary file project.<p>
1906     *
1907     * The id of the old project is stored in a member variable to switch back.<p>
1908     *
1909     * @return the id of the tempfileproject
1910     * @throws CmsException if getting the tempfileproject id fails
1911     */

1912    protected int switchToTempProject() throws CmsException {
1913
1914        // store the current project id in member variable
1915
m_currentProjectId = getSettings().getProject();
1916        int tempProjectId = OpenCms.getWorkplaceManager().getTempFileProjectId();
1917        getCms().getRequestContext().setCurrentProject(getCms().readProject(tempProjectId));
1918        return tempProjectId;
1919    }
1920
1921    /**
1922     * Sets the cms request context and other cms related settings to the
1923     * values stored int the workplace settings.<p>
1924     *
1925     * @param settings the workplace settings
1926     * @param cms the current cms object
1927     */

1928    private void initWorkplaceCmsContext(CmsWorkplaceSettings settings, CmsObject cms) {
1929
1930        CmsRequestContext reqCont = cms.getRequestContext();
1931
1932        // check project setting
1933
if (settings.getProject() != reqCont.currentProject().getId()) {
1934            try {
1935                reqCont.setCurrentProject(cms.readProject(settings.getProject()));
1936            } catch (CmsDbEntryNotFoundException e) {
1937                try {
1938                    // project not found, set current project and settings to online project
1939
reqCont.setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
1940                    settings.setProject(CmsProject.ONLINE_PROJECT_ID);
1941                } catch (CmsException ex) {
1942                    // log error
1943
if (LOG.isInfoEnabled()) {
1944                        LOG.info(ex.getLocalizedMessage());
1945                    }
1946                }
1947            } catch (CmsException e1) {
1948                if (LOG.isInfoEnabled()) {
1949                    LOG.info(e1.getLocalizedMessage());
1950                }
1951            }
1952        }
1953
1954        // check site setting
1955
if (!(settings.getSite().equals(reqCont.getSiteRoot()))) {
1956            // site was switched, set new site root
1957
reqCont.setSiteRoot(settings.getSite());
1958            // removed setting explorer resource to "/" to get the stored folder
1959
}
1960    }
1961
1962    /**
1963     * Returns a list of all methods of the current class instance that
1964     * start with "getParam" and have no parameters.<p>
1965     *
1966     * @return a list of all methods of the current class instance that
1967     * start with "getParam" and have no parameters
1968     */

1969    private List JavaDoc paramGetMethods() {
1970
1971        List JavaDoc list = new ArrayList JavaDoc();
1972        Method JavaDoc[] methods = this.getClass().getMethods();
1973        int length = methods.length;
1974        for (int i = 0; i < length; i++) {
1975            Method JavaDoc method = methods[i];
1976            if (method.getName().startsWith("getParam") && (method.getParameterTypes().length == 0)) {
1977                if (DEBUG) {
1978                    System.err.println("getMethod: " + method.getName());
1979                }
1980                list.add(method);
1981            }
1982        }
1983        return list;
1984    }
1985
1986    /**
1987     * Returns a list of all methods of the current class instance that
1988     * start with "setParam" and have exactly one String parameter.<p>
1989     *
1990     * @return a list of all methods of the current class instance that
1991     * start with "setParam" and have exactly one String parameter
1992     */

1993    private List JavaDoc paramSetMethods() {
1994
1995        List JavaDoc list = new ArrayList JavaDoc();
1996        Method JavaDoc[] methods = this.getClass().getMethods();
1997        int length = methods.length;
1998        for (int i = 0; i < length; i++) {
1999            Method JavaDoc method = methods[i];
2000            if (method.getName().startsWith("setParam")
2001                && (method.getParameterTypes().length == 1)
2002                && (method.getParameterTypes()[0].equals(java.lang.String JavaDoc.class))) {
2003                if (DEBUG) {
2004                    System.err.println("setMethod: " + method.getName());
2005                }
2006                list.add(method);
2007            }
2008        }
2009        return list;
2010    }
2011}
Popular Tags