KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > explorer > CmsNewResourceXmlPage


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/explorer/CmsNewResourceXmlPage.java,v $
3  * Date : $Date: 2006/03/27 14:52:30 $
4  * Version: $Revision: 1.23 $
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.explorer;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsFolder;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsProperty;
38 import org.opencms.file.CmsPropertyDefinition;
39 import org.opencms.file.CmsResource;
40 import org.opencms.file.CmsResourceFilter;
41 import org.opencms.file.types.CmsResourceTypeXmlPage;
42 import org.opencms.jsp.CmsJspActionElement;
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.workplace.CmsWorkplaceSettings;
49 import org.opencms.workplace.commons.CmsPropertyAdvanced;
50
51 import java.io.IOException JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.List JavaDoc;
56 import java.util.Map JavaDoc;
57 import java.util.TreeMap JavaDoc;
58
59 import javax.servlet.ServletException JavaDoc;
60 import javax.servlet.http.HttpServletRequest JavaDoc;
61 import javax.servlet.http.HttpServletResponse JavaDoc;
62 import javax.servlet.jsp.JspException JavaDoc;
63 import javax.servlet.jsp.PageContext JavaDoc;
64
65 import org.apache.commons.logging.Log;
66
67 /**
68  * The new resource page dialog handles the creation of an xml page.<p>
69  *
70  * The following files use this class:
71  * <ul>
72  * <li>/commons/newresource_xmlpage.jsp
73  * </ul>
74  * <p>
75  *
76  * @author Andreas Zahner
77  *
78  * @version $Revision: 1.23 $
79  *
80  * @since 6.0.0
81  */

82 public class CmsNewResourceXmlPage extends CmsNewResource {
83
84     /** Request parameter name for the selected body. */
85     public static final String JavaDoc PARAM_BODYFILE = "bodyfile";
86
87     /** Request parameter name for the suffix check. */
88     public static final String JavaDoc PARAM_SUFFIXCHECK = "suffixcheck";
89
90     /** Request parameter name for the selected template. */
91     public static final String JavaDoc PARAM_TEMPLATE = "template";
92
93     /** The log object for this class. */
94     private static final Log LOG = CmsLog.getLog(CmsNewResourceXmlPage.class);
95     private String JavaDoc m_paramBodyFile;
96     private String JavaDoc m_paramDialogMode;
97     private String JavaDoc m_paramSuffixCheck;
98     private String JavaDoc m_paramTemplate;
99
100     /**
101      * Public constructor with JSP action element.<p>
102      *
103      * @param jsp an initialized JSP action element
104      */

105     public CmsNewResourceXmlPage(CmsJspActionElement jsp) {
106
107         super(jsp);
108     }
109
110     /**
111      * Public constructor with JSP variables.<p>
112      *
113      * @param context the JSP page context
114      * @param req the JSP request
115      * @param res the JSP response
116      */

117     public CmsNewResourceXmlPage(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
118
119         this(new CmsJspActionElement(context, req, res));
120     }
121
122     /**
123      * Returns a sorted Map of all available body files of the OpenCms modules.<p>
124      *
125      * @param cms the current cms object
126      * @param currWpPath the current path in the OpenCms workplace
127      * @return a sorted map with the body file title as key and absolute path to the body file as value
128      * @throws CmsException if reading a folder or file fails
129      */

130     public static TreeMap JavaDoc getBodies(CmsObject cms, String JavaDoc currWpPath) throws CmsException {
131
132         return getElements(cms, CmsWorkplace.VFS_DIR_DEFAULTBODIES, currWpPath, true);
133     }
134
135     /**
136      * Returns a sorted Map of all available body files of the OpenCms modules.<p>
137      *
138      * @param cms the current cms object
139      * @param currWpPath the current path in the OpenCms workplace
140      * @param emptyMap flag indicating if it is OK to return a filtered empty Map
141      * @return a sorted map with the body file title as key and absolute path to the body file as value
142      * @throws CmsException if reading a folder or file fails
143      */

144     public static TreeMap JavaDoc getBodies(CmsObject cms, String JavaDoc currWpPath, boolean emptyMap) throws CmsException {
145
146         return getElements(cms, CmsWorkplace.VFS_DIR_DEFAULTBODIES, currWpPath, emptyMap);
147     }
148
149     /**
150      * Returns a sorted Map of all available templates of the OpenCms modules.<p>
151      *
152      * @param cms the current cms object
153      * @param currWpPath the current path in the OpenCms workplace
154      * @return a sorted map with the template title as key and absolute path to the template as value
155      * @throws CmsException if reading a folder or file fails
156      */

157     public static TreeMap JavaDoc getTemplates(CmsObject cms, String JavaDoc currWpPath) throws CmsException {
158
159         return getElements(cms, CmsWorkplace.VFS_DIR_TEMPLATES, currWpPath, true);
160     }
161
162     /**
163      * Returns a sorted Map of all available templates of the OpenCms modules.<p>
164      *
165      * @param cms the current cms object
166      * @param currWpPath the current path in the OpenCms workplace
167      * @param emptyMap flag indicating if it is OK to return a filtered empty Map
168      * @return a sorted map with the template title as key and absolute path to the template as value
169      * @throws CmsException if reading a folder or file fails
170      */

171     public static TreeMap JavaDoc getTemplates(CmsObject cms, String JavaDoc currWpPath, boolean emptyMap) throws CmsException {
172
173         return getElements(cms, CmsWorkplace.VFS_DIR_TEMPLATES, currWpPath, emptyMap);
174     }
175
176     /**
177      * Returns a sorted Map of all available elements in the specified subfolder of the OpenCms modules.<p>
178      *
179      * @param cms the current cms object
180      * @param elementFolder the module subfolder to serach for elements
181      * @param currWpPath the current path in the OpenCms workplace
182      * @param emptyMap flag indicating if it is OK to return a filtered empty Map
183      * @return a sorted map with the element title as key and absolute path to the element as value
184      * @throws CmsException if reading a folder or file fails
185      */

186     protected static TreeMap JavaDoc getElements(CmsObject cms, String JavaDoc elementFolder, String JavaDoc currWpPath, boolean emptyMap)
187     throws CmsException {
188
189         TreeMap JavaDoc elements = new TreeMap JavaDoc();
190         TreeMap JavaDoc allElements = new TreeMap JavaDoc();
191
192         if (CmsStringUtil.isNotEmpty(currWpPath)) {
193             // add site root to current workplace path
194
currWpPath = cms.getRequestContext().addSiteRoot(currWpPath);
195         }
196
197         // get all visible template elements in the module folders
198
List JavaDoc modules = cms.getSubFolders(CmsWorkplace.VFS_PATH_MODULES, CmsResourceFilter.IGNORE_EXPIRATION);
199         for (int i = 0; i < modules.size(); i++) {
200             List JavaDoc moduleTemplateFiles = new ArrayList JavaDoc();
201             String JavaDoc folder = cms.getSitePath((CmsFolder)modules.get(i));
202             try {
203                 moduleTemplateFiles = cms.getFilesInFolder(
204                     folder + elementFolder,
205                     CmsResourceFilter.DEFAULT.addRequireVisible());
206             } catch (CmsException e) {
207                 // folder not available, list will be empty
208
if (LOG.isDebugEnabled()) {
209                     LOG.debug(e.getMessage(), e);
210                 }
211             }
212             for (int j = 0; j < moduleTemplateFiles.size(); j++) {
213                 // get the current template file
214
CmsFile templateFile = (CmsFile)moduleTemplateFiles.get(j);
215                 String JavaDoc title = null;
216                 String JavaDoc folderProp = null;
217                 try {
218                     title = cms.readPropertyObject(
219                         cms.getSitePath(templateFile),
220                         CmsPropertyDefinition.PROPERTY_TITLE,
221                         false).getValue();
222                     folderProp = cms.readPropertyObject(
223                         templateFile,
224                         CmsPropertyDefinition.PROPERTY_FOLDERS_AVAILABLE,
225                         false).getValue();
226                 } catch (CmsException e) {
227                     // property not available, will be null
228
if (LOG.isInfoEnabled()) {
229                         LOG.info(e);
230                     }
231                 }
232
233                 boolean isInFolder = false;
234                 // check template folders property value
235
if (CmsStringUtil.isNotEmpty(currWpPath) && CmsStringUtil.isNotEmpty(folderProp)) {
236                     // property value set on template, check if current workplace path fits
237
List JavaDoc folders = CmsStringUtil.splitAsList(folderProp, DELIM_PROPERTYVALUES);
238                     for (int k = 0; k < folders.size(); k++) {
239                         String JavaDoc checkFolder = (String JavaDoc)folders.get(k);
240                         if (currWpPath.startsWith(checkFolder)) {
241                             isInFolder = true;
242                             break;
243                         }
244                     }
245                 } else {
246                     isInFolder = true;
247                 }
248
249                 if (title == null) {
250                     // no title property found, display the file name
251
title = templateFile.getName();
252                 }
253                 String JavaDoc path = cms.getSitePath(templateFile);
254                 if (isInFolder) {
255                     // element is valid, add it to result
256
elements.put(title, path);
257                 }
258                 // also put element to overall result
259
allElements.put(title, path);
260             }
261         }
262         if (!emptyMap && elements.size() < 1) {
263             // empty Map should not be returned, return all collected elements
264
return allElements;
265         }
266         // return the filtered elements sorted by title
267
return elements;
268     }
269
270     /**
271      * Used to close the current JSP dialog.<p>
272      *
273      * This method overwrites the close dialog method in the super class,
274      * because in case a new folder was created before, after this dialog the tree view has to be refreshed.<p>
275      *
276      * It tries to include the URI stored in the workplace settings.
277      * This URI is determined by the frame name, which has to be set
278      * in the framename parameter.<p>
279      *
280      * @throws JspException if including an element fails
281      */

282     public void actionCloseDialog() throws JspException JavaDoc {
283
284         if (isCreateIndexMode()) {
285             // set the current explorer resource to the new created folder
286
String JavaDoc updateFolder = CmsResource.getParentFolder(getSettings().getExplorerResource());
287             getSettings().setExplorerResource(updateFolder);
288             List JavaDoc folderList = new ArrayList JavaDoc(1);
289             if (updateFolder != null) {
290                 folderList.add(updateFolder);
291             }
292             getJsp().getRequest().setAttribute(REQUEST_ATTRIBUTE_RELOADTREE, folderList);
293         }
294         super.actionCloseDialog();
295     }
296
297     /**
298      * Creates the xml page using the specified resource name.<p>
299      *
300      * @throws JspException if inclusion of error dialog fails
301      */

302     public void actionCreateResource() throws JspException JavaDoc {
303
304         try {
305             // calculate the new resource Title property value
306
String JavaDoc title = computeNewTitleProperty();
307             // create the full resource name
308
String JavaDoc fullResourceName = computeFullResourceName();
309
310             // eventually append ".html" suffix to new file if not present
311
boolean forceSuffix = false;
312             if (CmsStringUtil.isEmpty(getParamSuffixCheck())) {
313                 // backward compatibility: append suffix every time
314
forceSuffix = true;
315             }
316             fullResourceName = appendSuffixHtml(fullResourceName, forceSuffix);
317
318             // get the body file content
319
byte[] bodyFileBytes = null;
320             if (CmsStringUtil.isEmpty(getParamBodyFile())) {
321                 // body file not specified, use empty body
322
bodyFileBytes = ("").getBytes();
323             } else {
324                 // get the specified body file
325
bodyFileBytes = getCms().readFile(getParamBodyFile(), CmsResourceFilter.IGNORE_EXPIRATION).getContents();
326             }
327
328             // create the xml page
329
List JavaDoc properties = new ArrayList JavaDoc(4);
330             // add the template property to the new file
331
properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_TEMPLATE, getParamTemplate(), null));
332             properties.addAll(createResourceProperties(
333                 fullResourceName,
334                 CmsResourceTypeXmlPage.getStaticTypeName(),
335                 title));
336             getCms().createResource(
337                 fullResourceName,
338                 CmsResourceTypeXmlPage.getStaticTypeId(),
339                 bodyFileBytes,
340                 properties);
341
342             // set the resource parameter to full path for property dialog
343
setParamResource(fullResourceName);
344             setResourceCreated(true);
345         } catch (Throwable JavaDoc e) {
346             // error creating folder, show error dialog
347
includeErrorpage(this, e);
348         }
349     }
350
351     /**
352      * Forwards to the property dialog if the resourceeditprops parameter is true.<p>
353      *
354      * If the parameter is not true, the dialog will be closed.<p>
355      *
356      * @throws IOException if forwarding to the property dialog fails
357      * @throws ServletException if forwarding to the property dialog fails
358      * @throws JspException if an inclusion fails
359      */

360     public void actionEditProperties() throws IOException JavaDoc, JspException JavaDoc, ServletException JavaDoc {
361
362         boolean editProps = Boolean.valueOf(getParamNewResourceEditProps()).booleanValue();
363         if (editProps) {
364             // edit properties checkbox checked, forward to property dialog
365
Map JavaDoc params = new HashMap JavaDoc();
366             params.put(PARAM_RESOURCE, getParamResource());
367             if (isCreateIndexMode()) {
368                 params.put(CmsPropertyAdvanced.PARAM_DIALOGMODE, CmsPropertyAdvanced.MODE_WIZARD_INDEXCREATED);
369             } else {
370                 params.put(CmsPropertyAdvanced.PARAM_DIALOGMODE, CmsPropertyAdvanced.MODE_WIZARD);
371             }
372             sendForward(CmsPropertyAdvanced.URI_PROPERTY_DIALOG_HANDLER, params);
373         } else {
374             // edit properties not checked, close the dialog
375
actionCloseDialog();
376         }
377     }
378
379     /**
380      * Builds the html for the page body file select box.<p>
381      *
382      * @param attributes optional attributes for the &lt;select&gt; tag
383      * @return the html for the page body file select box
384      */

385     public String JavaDoc buildSelectBodyFile(String JavaDoc attributes) {
386
387         List JavaDoc options = new ArrayList JavaDoc();
388         List JavaDoc values = new ArrayList JavaDoc();
389         TreeMap JavaDoc bodies = null;
390         try {
391             // get all available body files
392
bodies = getBodies(getCms(), getParamCurrentFolder(), false);
393         } catch (CmsException e) {
394             // can usually be ignored
395
if (LOG.isInfoEnabled()) {
396                 LOG.info(e);
397             }
398         }
399         if (bodies == null) {
400             // no body files found, return empty String
401
return "";
402         } else {
403             // body files found, create option and value lists
404
Iterator JavaDoc i = bodies.keySet().iterator();
405             int counter = 0;
406             while (i.hasNext()) {
407                 String JavaDoc key = (String JavaDoc)i.next();
408                 String JavaDoc path = (String JavaDoc)bodies.get(key);
409
410                 options.add(key);
411                 values.add(path);
412                 counter++;
413             }
414         }
415         return buildSelect(attributes, options, values, -1, false);
416     }
417
418     /**
419      * Builds the html for the page template select box.<p>
420      *
421      * @param attributes optional attributes for the &lt;select&gt; tag
422      * @return the html for the page template select box
423      */

424     public String JavaDoc buildSelectTemplates(String JavaDoc attributes) {
425
426         List JavaDoc options = new ArrayList JavaDoc();
427         List JavaDoc values = new ArrayList JavaDoc();
428         TreeMap JavaDoc templates = null;
429         try {
430             // get all available templates
431
templates = getTemplates(getCms(), getParamCurrentFolder(), false);
432         } catch (CmsException e) {
433             // can usually be ignored
434
if (LOG.isInfoEnabled()) {
435                 LOG.info(e);
436             }
437         }
438         if (templates == null) {
439             // no templates found, return empty String
440
return "";
441         } else {
442             // templates found, create option and value lists
443
Iterator JavaDoc i = templates.keySet().iterator();
444             int counter = 0;
445             while (i.hasNext()) {
446                 String JavaDoc key = (String JavaDoc)i.next();
447                 String JavaDoc path = (String JavaDoc)templates.get(key);
448
449                 options.add(key);
450                 values.add(path);
451                 counter++;
452             }
453         }
454         return buildSelect(attributes, options, values, -1, false);
455     }
456
457     /**
458      * Returns the body file parameter value.<p>
459      *
460      * @return the body file parameter value
461      */

462     public String JavaDoc getParamBodyFile() {
463
464         return m_paramBodyFile;
465     }
466
467     /**
468      * Returns the value of the dialogmode parameter,
469      * or null if this parameter was not provided.<p>
470      *
471      * The dialogmode parameter stores the different modes of the property dialog,
472      * e.g. for displaying other buttons in the new resource wizard.<p>
473      *
474      * @return the value of the usetempfileproject parameter
475      */

476     public String JavaDoc getParamDialogmode() {
477
478         return m_paramDialogMode;
479     }
480
481     /**
482      * Returns the request parameter flag inidicating if the suffix field is present or not.<p>
483      *
484      * @return the request parameter flag inidicating if the suffix field is present or not
485      */

486     public String JavaDoc getParamSuffixCheck() {
487
488         return m_paramSuffixCheck;
489     }
490
491     /**
492      * Returns the template parameter value.<p>
493      *
494      * @return the template parameter value
495      */

496     public String JavaDoc getParamTemplate() {
497
498         return m_paramTemplate;
499     }
500
501     /**
502      * Returns true if the current mode is: create an index page in a newly created folder.<p>
503      *
504      * @return true if we are in wizard mode to create an index page, otherwise false
505      */

506     public boolean isCreateIndexMode() {
507
508         return CmsPropertyAdvanced.MODE_WIZARD_CREATEINDEX.equals(getParamDialogmode());
509     }
510
511     /**
512      * Overrides the super implementation to avoid problems with double reqource input fields.<p>
513      *
514      * @see org.opencms.workplace.CmsWorkplace#paramsAsHidden()
515      */

516     public String JavaDoc paramsAsHidden() {
517
518         String JavaDoc resourceName = getParamResource();
519         // remove resource parameter from hidden params to avoid problems with double input fields in form
520
setParamResource(null);
521         String JavaDoc params = super.paramsAsHidden();
522         // set resource parameter to stored value
523
setParamResource(resourceName);
524         return params;
525     }
526
527     /**
528      * Sets the body file parameter value.<p>
529      *
530      * @param bodyFile the body file parameter value
531      */

532     public void setParamBodyFile(String JavaDoc bodyFile) {
533
534         m_paramBodyFile = bodyFile;
535     }
536
537     /**
538      * Sets the value of the dialogmode parameter.<p>
539      *
540      * @param value the value to set
541      */

542     public void setParamDialogmode(String JavaDoc value) {
543
544         m_paramDialogMode = value;
545     }
546
547     /**
548      * Sets the request parameter flag inidicating if the suffix field is present or not.<p>
549      *
550      * @param paramSuffixCheck he request parameter flag inidicating if the suffix field is present or not
551      */

552     public void setParamSuffixCheck(String JavaDoc paramSuffixCheck) {
553
554         m_paramSuffixCheck = paramSuffixCheck;
555     }
556
557     /**
558      * Sets the template parameter value.<p>
559      *
560      * @param template the template parameter value
561      */

562     public void setParamTemplate(String JavaDoc template) {
563
564         m_paramTemplate = template;
565     }
566
567     /**
568      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
569      */

570     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
571
572         // fill the parameter values in the get/set methods
573
fillParamValues(request);
574         // set the dialog type
575
setParamDialogtype(DIALOG_TYPE);
576         // set the action for the JSP switch
577
if (DIALOG_OK.equals(getParamAction())) {
578             setAction(ACTION_OK);
579         } else if (DIALOG_CANCEL.equals(getParamAction())) {
580             setAction(ACTION_CANCEL);
581         } else {
582             // set resource name if we are in new folder wizard mode
583
setInitialResourceName();
584             setAction(ACTION_DEFAULT);
585             // build title for new resource dialog
586
setParamTitle(key(Messages.GUI_NEWRESOURCE_XMLPAGE_0));
587         }
588     }
589
590     /**
591      * Sets the initial resource name of the new page.<p>
592      *
593      * This is used for the "new" wizard after creating a new folder followed
594      * by the "create index file" procedure.<p>
595      */

596     private void setInitialResourceName() {
597
598         if (isCreateIndexMode()) {
599             // creation of an index file in a new folder, use default file name
600
String JavaDoc defaultFile = "";
601             try {
602                 defaultFile = (String JavaDoc)OpenCms.getDefaultFiles().get(0);
603             } catch (IndexOutOfBoundsException JavaDoc e) {
604                 // list is empty, ignore
605
}
606             if (CmsStringUtil.isEmpty(defaultFile)) {
607                 // make sure that the default file name is not empty
608
defaultFile = "index.html";
609             }
610             setParamResource(defaultFile);
611         } else {
612             setParamResource("");
613         }
614     }
615
616 }
Popular Tags