KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > editors > CmsEditor


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsEditor.java,v $
3  * Date : $Date: 2006/11/21 14:16:29 $
4  * Version: $Revision: 1.41 $
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.editors;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsResourceFilter;
39 import org.opencms.i18n.CmsEncoder;
40 import org.opencms.jsp.CmsJspActionElement;
41 import org.opencms.lock.CmsLock;
42 import org.opencms.main.CmsException;
43 import org.opencms.main.CmsLog;
44 import org.opencms.main.OpenCms;
45 import org.opencms.security.CmsPermissionSet;
46 import org.opencms.security.I_CmsPrincipal;
47 import org.opencms.util.CmsStringUtil;
48 import org.opencms.workplace.CmsDialog;
49 import org.opencms.workplace.CmsFrameset;
50 import org.opencms.workplace.CmsWorkplace;
51 import org.opencms.xml.content.CmsXmlContent;
52 import org.opencms.xml.content.CmsXmlContentFactory;
53
54 import java.io.IOException JavaDoc;
55 import java.util.ArrayList JavaDoc;
56 import java.util.HashMap JavaDoc;
57 import java.util.List JavaDoc;
58 import java.util.Locale JavaDoc;
59
60 import javax.servlet.ServletException JavaDoc;
61 import javax.servlet.jsp.JspException JavaDoc;
62
63 import org.apache.commons.logging.Log;
64
65 /**
66  * Provides basic methods for building the file editors of OpenCms.<p>
67  *
68  * The editor classes have to extend this class and implement action methods for common editor actions.<p>
69  *
70  * @author Andreas Zahner
71  *
72  * @version $Revision: 1.41 $
73  *
74  * @since 6.0.0
75  */

76 public abstract class CmsEditor extends CmsDialog {
77
78     /** Value for the action: change the body. */
79     public static final int ACTION_CHANGE_BODY = 124;
80
81     /** Value for the action: delete the current locale. */
82     public static final int ACTION_DELETELOCALE = 140;
83
84     /** Value for the action: exit. */
85     public static final int ACTION_EXIT = 122;
86
87     /** Value for the action: show a preview. */
88     public static final int ACTION_PREVIEW = 126;
89
90     /** Value for the action: save. */
91     public static final int ACTION_SAVE = 121;
92
93     /** Constant value for the customizable action button. */
94     public static final int ACTION_SAVEACTION = 130;
95
96     /** Value for the action: save and exit. */
97     public static final int ACTION_SAVEEXIT = 123;
98
99     /** Value for the action: show the editor. */
100     public static final int ACTION_SHOW = 125;
101
102     /** Value for the action: an error occured. */
103     public static final int ACTION_SHOW_ERRORMESSAGE = 127;
104
105     /** Value for the action parameter: change the element. */
106     public static final String JavaDoc EDITOR_CHANGE_ELEMENT = "changeelement";
107
108     /** Value for the action parameter: cleanup content. */
109     public static final String JavaDoc EDITOR_CLEANUP = "cleanup";
110
111     /** Value for the action parameter: close browser window (accidentally). */
112     public static final String JavaDoc EDITOR_CLOSEBROWSER = "closebrowser";
113
114     /** Value for the action parameter: delete the current locale. */
115     public static final String JavaDoc EDITOR_DELETELOCALE = "deletelocale";
116
117     /** Value for the action parameter: exit editor. */
118     public static final String JavaDoc EDITOR_EXIT = "exit";
119
120     /** Value for the action parameter: show a preview. */
121     public static final String JavaDoc EDITOR_PREVIEW = "preview";
122
123     /** Value for the action parameter: save content. */
124     public static final String JavaDoc EDITOR_SAVE = "save";
125
126     /** Value for the customizable action button. */
127     public static final String JavaDoc EDITOR_SAVEACTION = "saveaction";
128
129     /** Value for the action parameter: save and exit. */
130     public static final String JavaDoc EDITOR_SAVEEXIT = "saveexit";
131
132     /** Value for the action parameter: show the editor. */
133     public static final String JavaDoc EDITOR_SHOW = "show";
134
135     /** Value for the action parameter: an error occured. */
136     public static final String JavaDoc EDITOR_SHOW_ERRORMESSAGE = "error";
137
138     /** Marker for empty locale in locale selection. */
139     public static final String JavaDoc EMPTY_LOCALE = " [-]";
140
141     /** Stores the VFS editor path. */
142     public static final String JavaDoc PATH_EDITORS = PATH_WORKPLACE + "editors/";
143
144     /** Parameter name for the request parameter "backlink". */
145     public static final String JavaDoc PARAM_BACKLINK = "backlink";
146
147     /** Parameter name for the request parameter "content". */
148     public static final String JavaDoc PARAM_CONTENT = "content";
149
150     /** Parameter name for the request parameter "directedit". */
151     public static final String JavaDoc PARAM_DIRECTEDIT = "directedit";
152
153     /** Parameter name for the request parameter "editastext". */
154     public static final String JavaDoc PARAM_EDITASTEXT = "editastext";
155
156     /** Parameter name for the request parameter "editormode". */
157     public static final String JavaDoc PARAM_EDITORMODE = "editormode";
158
159     /** Parameter name for the request parameter "element language". */
160     public static final String JavaDoc PARAM_ELEMENTLANGUAGE = "elementlanguage";
161
162     /** Parameter name for the request parameter "loaddefault". */
163     public static final String JavaDoc PARAM_LOADDEFAULT = "loaddefault";
164
165     /** Parameter name for the request parameter "modified". */
166     public static final String JavaDoc PARAM_MODIFIED = "modified";
167
168     /** Parameter name for the request parameter "old element language". */
169     public static final String JavaDoc PARAM_OLDELEMENTLANGUAGE = "oldelementlanguage";
170
171     /** Parameter name for the request parameter "tempfile". */
172     public static final String JavaDoc PARAM_TEMPFILE = "tempfile";
173
174     /** The log object for this class. */
175     private static final Log LOG = CmsLog.getLog(CmsEditor.class);
176
177     /** The encoding to use (will be read from the file property). */
178     private String JavaDoc m_fileEncoding;
179
180     // some private members for parameter storage
181
private String JavaDoc m_paramBackLink;
182     private String JavaDoc m_paramContent;
183     private String JavaDoc m_paramDirectedit;
184     private String JavaDoc m_paramEditAsText;
185     private String JavaDoc m_paramEditormode;
186     private String JavaDoc m_paramElementlanguage;
187     private String JavaDoc m_paramLoadDefault;
188     private String JavaDoc m_paramModified;
189     private String JavaDoc m_paramOldelementlanguage;
190     private String JavaDoc m_paramTempFile;
191
192     /** Helper variable to store the uri to the editors pictures. */
193     private String JavaDoc m_picsUri;
194
195     /**
196      * Public constructor.<p>
197      *
198      * @param jsp an initialized JSP action element
199      */

200     public CmsEditor(CmsJspActionElement jsp) {
201
202         super(jsp);
203     }
204
205     /**
206      * Unlocks the edited resource when in direct edit mode or when the resource was not modified.<p>
207      *
208      * @param forceUnlock if true, the resource will be unlocked anyway
209      */

210     public abstract void actionClear(boolean forceUnlock);
211
212     /**
213      * Performs the exit editor action.<p>
214      *
215      * @throws CmsException if something goes wrong
216      * @throws IOException if a forward fails
217      * @throws ServletException if a forward fails
218      * @throws JspException if including an element fails
219      */

220     public abstract void actionExit() throws CmsException, IOException JavaDoc, ServletException JavaDoc, JspException JavaDoc;
221
222     /**
223      * Performs the save content action.<p>
224      *
225      * @throws IOException if a redirection fails
226      * @throws JspException if including an element fails
227      */

228     public abstract void actionSave() throws IOException JavaDoc, JspException JavaDoc;
229
230     /**
231      * Builds the html String for the element language selector.<p>
232      *
233      * @param attributes optional attributes for the &lt;select&gt; tag
234      * @param resource the name of the resource to edit
235      * @param selectedLocale the currently selected Locale
236      * @return the html for the element language selectbox
237      */

238     public String JavaDoc buildSelectElementLanguage(String JavaDoc attributes, String JavaDoc resource, Locale JavaDoc selectedLocale) {
239
240         // get locale names based on properties and global settings
241
List JavaDoc locales = OpenCms.getLocaleManager().getAvailableLocales(getCms(), resource);
242         List JavaDoc options = new ArrayList JavaDoc(locales.size());
243         List JavaDoc selectList = new ArrayList JavaDoc(locales.size());
244         int currentIndex = -1;
245
246         String JavaDoc filename = resource;
247
248         //get the locales already used in the resource
249
List JavaDoc contentLocales = new ArrayList JavaDoc();
250         try {
251
252             CmsResource res = getCms().readResource(filename);
253
254             String JavaDoc temporaryFilename = CmsResource.getFolderPath(resource)
255                 + CmsWorkplace.TEMP_FILE_PREFIX
256                 + res.getName();
257             if (getCms().existsResource(temporaryFilename)) {
258                 res = getCms().readResource(temporaryFilename);
259             }
260             CmsFile file = CmsFile.upgrade(res, getCms());
261             CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), file);
262             contentLocales = xmlContent.getLocales();
263         } catch (CmsException e) {
264             // to nothing here in case the resource could not be opened
265
if (LOG.isErrorEnabled()) {
266                 LOG.error(Messages.get().getBundle().key(Messages.LOG_GET_LOCALES_1, filename), e);
267             }
268         }
269
270         for (int counter = 0; counter < locales.size(); counter++) {
271             // create the list of options and values
272
Locale JavaDoc curLocale = (Locale JavaDoc)locales.get(counter);
273             selectList.add(curLocale.toString());
274             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
275             buf.append(curLocale.getDisplayName(getLocale()));
276             if (!contentLocales.contains(curLocale)) {
277                 buf.append(EMPTY_LOCALE);
278             }
279             options.add(buf.toString());
280             if (curLocale.equals(selectedLocale)) {
281                 // set the selected index of the selector
282
currentIndex = counter;
283             }
284         }
285
286         if (currentIndex == -1) {
287             // no matching element language found, use first element language in list
288
if (selectList.size() > 0) {
289                 currentIndex = 0;
290                 setParamElementlanguage((String JavaDoc)selectList.get(0));
291             }
292         }
293
294         return buildSelect(attributes, options, selectList, currentIndex, false);
295     }
296
297     /**
298      * Generates a button for the OpenCms editor.<p>
299      *
300      * @param href the href link for the button, if none is given the button will be disabled
301      * @param target the href link target for the button, if none is given the target will be same window
302      * @param image the image name for the button, skin path will be automattically added as prefix
303      * @param label the label for the text of the button
304      * @param type 0: image only (default), 1: image and text, 2: text only
305      * @param useCustomImage if true, the button has to be placed in the editors "custom pics" folder
306      *
307      * @return a button for the OpenCms editor
308      */

309     public String JavaDoc button(String JavaDoc href, String JavaDoc target, String JavaDoc image, String JavaDoc label, int type, boolean useCustomImage) {
310
311         if (useCustomImage) {
312             // search the picture in the "custom pics" folder
313
return button(href, target, image, label, type, getPicsUri());
314         } else {
315             // search the picture in the common "buttons" folder
316
return button(href, target, image, label, type);
317         }
318     }
319
320     /**
321      * Returns the editor action for a "cancel" button.<p>
322      *
323      * This overwrites the cancel method of the CmsDialog class.<p>
324      *
325      * Always use this value, do not write anything directly in the html page.<p>
326      *
327      * @return the default action for a "cancel" button
328      */

329     public String JavaDoc buttonActionCancel() {
330
331         String JavaDoc target = null;
332         if (Boolean.valueOf(getParamDirectedit()).booleanValue()) {
333             // editor is in direct edit mode
334
if (CmsStringUtil.isNotEmpty(getParamBacklink())) {
335                 // set link to the specified back link target
336
target = getParamBacklink();
337             } else {
338                 // set link to the edited resource
339
target = getParamResource();
340             }
341         } else {
342             // in workplace mode, show explorer view
343
target = OpenCms.getLinkManager().substituteLink(getCms(), CmsFrameset.JSP_WORKPLACE_URI);
344         }
345         return "onclick=\"top.location.href='" + getJsp().link(target) + "';\"";
346     }
347
348     /**
349      * Builds the html to display the special action button for the direct edit mode of the editor.<p>
350      *
351      * @param jsFunction the JavaScript function which will be executed on the mouseup event
352      * @param type 0: image only (default), 1: image and text, 2: text only
353      * @return the html to display the special action button
354      */

355     public String JavaDoc buttonActionDirectEdit(String JavaDoc jsFunction, int type) {
356
357         // get the action class from the OpenCms runtime property
358
I_CmsEditorActionHandler actionClass = OpenCms.getWorkplaceManager().getEditorActionHandler();
359         String JavaDoc url;
360         String JavaDoc name;
361         boolean active = false;
362         if (actionClass != null) {
363             // get button parameters and state from action class
364
url = actionClass.getButtonUrl(getJsp(), getParamResource());
365             name = actionClass.getButtonName();
366             active = actionClass.isButtonActive(getJsp(), getParamResource());
367         } else {
368             // action class not defined, display inactive button
369
url = getSkinUri() + "buttons/publish_in.png";
370             name = Messages.GUI_EXPLORER_CONTEXT_PUBLISH_0;
371         }
372         String JavaDoc image = url.substring(url.lastIndexOf("/") + 1);
373         if (url.endsWith(".gif")) {
374             image = image.substring(0, image.length() - 4);
375         }
376
377         if (active) {
378             // create the link for the button
379
return button("javascript:" + jsFunction, null, image, name, type, url.substring(
380                 0,
381                 url.lastIndexOf("/") + 1));
382         } else {
383             // create the inactive button
384
return button(null, null, image, name, type, url.substring(0, url.lastIndexOf("/") + 1));
385         }
386     }
387
388     /**
389      * @see org.opencms.workplace.CmsWorkplace#checkLock(java.lang.String, int)
390      */

391     public void checkLock(String JavaDoc resource, int mode) throws CmsException {
392
393         CmsResource res = getCms().readResource(resource, CmsResourceFilter.ALL);
394         if (!getCms().getLock(res).isNullLock()) {
395             setParamModified(Boolean.TRUE.toString());
396         }
397         super.checkLock(resource, mode);
398     }
399
400     /**
401      * Generates a button for delete locale.<p>
402      *
403      * @param href the href link for the button, if none is given the button will be disabled
404      * @param target the href link target for the button, if none is given the target will be same window
405      * @param image the image name for the button, skin path will be automattically added as prefix
406      * @param label the label for the text of the button
407      * @param type 0: image only (default), 1: image and text, 2: text only
408      *
409      * @return a button for the OpenCms workplace
410      */

411     public String JavaDoc deleteLocaleButton(String JavaDoc href, String JavaDoc target, String JavaDoc image, String JavaDoc label, int type) {
412
413         String JavaDoc filename = getParamResource();
414
415         try {
416             CmsResource res = getCms().readResource(filename);
417
418             String JavaDoc temporaryFilename = CmsResource.getFolderPath(filename)
419                 + CmsWorkplace.TEMP_FILE_PREFIX
420                 + res.getName();
421             if (getCms().existsResource(temporaryFilename)) {
422                 res = getCms().readResource(temporaryFilename);
423             }
424             CmsFile file = CmsFile.upgrade(res, getCms());
425             CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), file);
426             int locales = xmlContent.getLocales().size();
427             // there are less than 2 locales, so disable the delete locale button
428
if (locales < 2) {
429                 href = null;
430                 target = null;
431                 image += "_in";
432             }
433         } catch (CmsException e) {
434             // to nothing here in case the resource could not be opened
435
if (LOG.isErrorEnabled()) {
436                 LOG.error(Messages.get().getBundle().key(Messages.LOG_GET_LOCALES_1, filename), e);
437             }
438         }
439         return button(href, target, image, label, type, getSkinUri() + "buttons/");
440
441     }
442
443     /**
444      * Returns the instanciated editor display option class from the workplace manager.<p>
445      *
446      * This is a convenience method to be used on editor JSPs.<p>
447      *
448      * @return the instanciated editor display option class
449      */

450     public CmsEditorDisplayOptions getEditorDisplayOptions() {
451
452         return OpenCms.getWorkplaceManager().getEditorDisplayOptions();
453     }
454
455     /**
456      * Returns the URI to the editor resource folder where button images and javascripts are located.<p>
457      *
458      * @return the URI to the editor resource folder
459      */

460     public abstract String JavaDoc getEditorResourceUri();
461
462     /**
463      * Returns the OpenCms request context path.<p>
464      *
465      * This is a convenience method to use in the editor.<p>
466      *
467      * @return the OpenCms request context path
468      */

469     public String JavaDoc getOpenCmsContext() {
470
471         return OpenCms.getSystemInfo().getOpenCmsContext();
472     }
473
474     /**
475      * Returns the back link when closing the editor.<p>
476      *
477      * @return the back link
478      */

479     public String JavaDoc getParamBacklink() {
480
481         if (m_paramBackLink == null) {
482             m_paramBackLink = "";
483         }
484         return m_paramBackLink;
485     }
486
487     /**
488      * Returns the content of the editor.<p>
489      * @return the content of the editor
490      */

491     public String JavaDoc getParamContent() {
492
493         if (m_paramContent == null) {
494             m_paramContent = "";
495         }
496         return m_paramContent;
497     }
498
499     /**
500      * Returns the direct edit flag parameter.<p>
501      *
502      * @return the direct edit flag parameter
503      */

504     public String JavaDoc getParamDirectedit() {
505
506         return m_paramDirectedit;
507     }
508
509     /**
510      * Returns the edit as text parameter.<p>
511      *
512      * @return the edit as text parameter
513      */

514     public String JavaDoc getParamEditastext() {
515
516         return m_paramEditAsText;
517     }
518
519     /**
520      * Returns the editor mode parameter.<p>
521      *
522      * @return the editor mode parameter
523      */

524     public String JavaDoc getParamEditormode() {
525
526         return m_paramEditormode;
527     }
528
529     /**
530      * Returns the current element language.<p>
531      *
532      * @return the current element language
533      */

534     public String JavaDoc getParamElementlanguage() {
535
536         return m_paramElementlanguage;
537     }
538
539     /**
540      * Returns the "loaddefault" parameter to determine if the default editor should be loaded.<p>
541      *
542      * @return the "loaddefault" parameter
543      */

544     public String JavaDoc getParamLoaddefault() {
545
546         return m_paramLoadDefault;
547     }
548
549     /**
550      * Returns the modified parameter indicating if the resource has been saved.<p>
551      *
552      * @return the modified parameter indicating if the resource has been saved
553      */

554     public String JavaDoc getParamModified() {
555
556         return m_paramModified;
557     }
558
559     /**
560      * Returns the old element language.<p>
561      *
562      * @return the old element language
563      */

564     public String JavaDoc getParamOldelementlanguage() {
565
566         return m_paramOldelementlanguage;
567     }
568
569     /**
570      * Returns the name of the temporary file.<p>
571      *
572      * @return the name of the temporary file
573      */

574     public String JavaDoc getParamTempfile() {
575
576         return m_paramTempFile;
577     }
578
579     /**
580      * Returns the path to the images used by this editor.<p>
581      *
582      * @return the path to the images used by this editor
583      */

584     public String JavaDoc getPicsUri() {
585
586         if (m_picsUri == null) {
587             m_picsUri = getEditorResourceUri() + "pics/";
588         }
589         return m_picsUri;
590     }
591
592     /**
593      * Sets the back link when closing the editor.<p>
594      *
595      * @param backLink the back link
596      */

597     public void setParamBacklink(String JavaDoc backLink) {
598
599         m_paramBackLink = backLink;
600     }
601
602     /**
603      * Sets the content of the editor.<p>
604      *
605      * @param content the content of the editor
606      */

607     public void setParamContent(String JavaDoc content) {
608
609         if (content == null) {
610             content = "";
611         }
612         m_paramContent = content;
613     }
614
615     /**
616      * Sets the direct edit flag parameter.<p>
617      *
618      * @param direct the direct edit flag parameter
619      */

620     public void setParamDirectedit(String JavaDoc direct) {
621
622         m_paramDirectedit = direct;
623     }
624
625     /**
626      * Sets the edit as text parameter.<p>
627      *
628      * @param editAsText <code>"true"</code> if the resource should be handled like a text file
629      */

630     public void setParamEditastext(String JavaDoc editAsText) {
631
632         m_paramEditAsText = editAsText;
633     }
634
635     /**
636      * Sets the editor mode parameter.<p>
637      *
638      * @param mode the editor mode parameter
639      */

640     public void setParamEditormode(String JavaDoc mode) {
641
642         m_paramEditormode = mode;
643     }
644
645     /**
646      * Sets the current element language.<p>
647      *
648      * @param elementLanguage the current element language
649      */

650     public void setParamElementlanguage(String JavaDoc elementLanguage) {
651
652         m_paramElementlanguage = elementLanguage;
653     }
654
655     /**
656      * Sets the "loaddefault" parameter to determine if the default editor should be loaded.<p>
657      *
658      * @param loadDefault the "loaddefault" parameter
659      */

660     public void setParamLoaddefault(String JavaDoc loadDefault) {
661
662         m_paramLoadDefault = loadDefault;
663     }
664
665     /**
666      * Sets the modified parameter indicating if the resource has been saved.<p>
667      *
668      * @param modified the modified parameter indicating if the resource has been saved
669      */

670     public void setParamModified(String JavaDoc modified) {
671
672         m_paramModified = modified;
673     }
674
675     /**
676      * Sets the old element language.<p>
677      *
678      * @param oldElementLanguage the old element language
679      */

680     public void setParamOldelementlanguage(String JavaDoc oldElementLanguage) {
681
682         m_paramOldelementlanguage = oldElementLanguage;
683     }
684
685     /**
686      * Sets the name of the temporary file.<p>
687      *
688      * @param fileName the name of the temporary file
689      */

690     public void setParamTempfile(String JavaDoc fileName) {
691
692         m_paramTempFile = fileName;
693     }
694
695     /**
696      * Closes the editor and forwards to the workplace or the resource depending on the editor mode.<p>
697      *
698      * @throws IOException if forwarding fails
699      * @throws ServletException if forwarding fails
700      * @throws JspException if including a JSP fails
701      */

702     protected void actionClose() throws IOException JavaDoc, JspException JavaDoc, ServletException JavaDoc {
703
704         if (Boolean.valueOf(getParamDirectedit()).booleanValue()) {
705             // editor is in direct edit mode
706
if (CmsStringUtil.isNotEmpty(getParamBacklink())) {
707                 // set link to the specified back link target
708
setParamCloseLink(getJsp().link(getParamBacklink()));
709             } else {
710                 // set link to the edited resource
711
setParamCloseLink(getJsp().link(getParamResource()));
712             }
713             // save initialized instance of this class in request attribute for included sub-elements
714
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
715             // load the common JSP close dialog
716
getJsp().include(FILE_DIALOG_CLOSE);
717         } else {
718             // forward to the workplace explorer view
719
sendForward(CmsFrameset.JSP_WORKPLACE_URI, new HashMap JavaDoc());
720         }
721     }
722
723     /**
724      * Writes the content of a temporary file back to the original file.<p>
725      *
726      * @throws CmsException if something goes wrong
727      */

728     protected void commitTempFile() throws CmsException {
729
730         switchToTempProject();
731         CmsFile tempFile;
732         List JavaDoc properties;
733         try {
734             tempFile = getCms().readFile(getParamTempfile(), CmsResourceFilter.ALL);
735             properties = getCms().readPropertyObjects(getParamTempfile(), false);
736         } finally {
737             // make sure the project is reset in case of any exception
738
switchToCurrentProject();
739         }
740         if (getCms().existsResource(getParamResource(), CmsResourceFilter.ALL)) {
741             // update properties of original file first (required if change in encoding occured)
742
getCms().writePropertyObjects(getParamResource(), properties);
743             // now replace the content of the original file
744
CmsFile orgFile = getCms().readFile(getParamResource(), CmsResourceFilter.ALL);
745             orgFile.setContents(tempFile.getContents());
746             getCms().writeFile(orgFile);
747         } else {
748             // original file does not exist, remove visibility permission entries and copy temporary file
749
if (getCms().hasPermissions(tempFile, CmsPermissionSet.ACCESS_CONTROL)) {
750                 getCms().rmacc(
751                     getParamTempfile(),
752                     I_CmsPrincipal.PRINCIPAL_GROUP,
753                     OpenCms.getDefaultUsers().getGroupUsers());
754                 getCms().rmacc(
755                     getParamTempfile(),
756                     I_CmsPrincipal.PRINCIPAL_GROUP,
757                     OpenCms.getDefaultUsers().getGroupProjectmanagers());
758             }
759             getCms().copyResource(getParamTempfile(), getParamResource(), CmsResource.COPY_AS_NEW);
760         }
761         // remove the temporary file flag
762
int flags = getCms().readResource(getParamResource(), CmsResourceFilter.ALL).getFlags();
763         if ((flags & CmsResource.FLAG_TEMPFILE) == CmsResource.FLAG_TEMPFILE) {
764             flags ^= CmsResource.FLAG_TEMPFILE;
765             getCms().chflags(getParamResource(), flags);
766         }
767     }
768
769     /**
770      * Creates a temporary file which is needed while working in an editor with preview option.<p>
771      *
772      * @return the file name of the temporary file
773      * @throws CmsException if something goes wrong
774      */

775     protected String JavaDoc createTempFile() throws CmsException {
776
777         // read the selected file
778
CmsResource file = getCms().readResource(getParamResource(), CmsResourceFilter.ALL);
779
780         // create the filename of the temporary file
781
String JavaDoc temporaryFilename = CmsResource.getFolderPath(getCms().getSitePath(file))
782             + CmsWorkplace.TEMP_FILE_PREFIX
783             + file.getName();
784
785         // check if the temporary file is already present
786
if (getCms().existsResource(temporaryFilename, CmsResourceFilter.ALL)) {
787             // delete old temporary file
788
if (!getCms().getLock(temporaryFilename).equals(CmsLock.getNullLock())) {
789                 // steal lock
790
getCms().changeLock(temporaryFilename);
791             } else {
792                 // lock resource to current user
793
getCms().lockResource(temporaryFilename);
794             }
795             getCms().deleteResource(temporaryFilename, CmsResource.DELETE_PRESERVE_SIBLINGS);
796         }
797
798         // switch to the temporary file project
799
switchToTempProject();
800
801         // copy the file to edit to a temporary file
802
try {
803             getCms().copyResource(getCms().getSitePath(file), temporaryFilename, CmsResource.COPY_AS_NEW);
804             getCms().setDateLastModified(temporaryFilename, System.currentTimeMillis(), false);
805             // set the temporary file flag
806
CmsResource tempFile = getCms().readResource(temporaryFilename, CmsResourceFilter.ALL);
807             int flags = tempFile.getFlags();
808             if ((flags & CmsResource.FLAG_TEMPFILE) == 0) {
809                 flags += CmsResource.FLAG_TEMPFILE;
810             }
811             getCms().chflags(temporaryFilename, flags);
812             // remove eventual release & expiration date from temporary file to make preview in editor work
813
getCms().setDateReleased(temporaryFilename, CmsResource.DATE_RELEASED_DEFAULT, false);
814             getCms().setDateExpired(temporaryFilename, CmsResource.DATE_EXPIRED_DEFAULT, false);
815             // remove visibility permissions for users and projectmanagers on temporary file
816
if (getCms().hasPermissions(tempFile, CmsPermissionSet.ACCESS_CONTROL)) {
817                 getCms().chacc(
818                     temporaryFilename,
819                     I_CmsPrincipal.PRINCIPAL_GROUP,
820                     OpenCms.getDefaultUsers().getGroupUsers(),
821                     "-v");
822                 getCms().chacc(
823                     temporaryFilename,
824                     I_CmsPrincipal.PRINCIPAL_GROUP,
825                     OpenCms.getDefaultUsers().getGroupProjectmanagers(),
826                     "-v");
827             }
828         } catch (CmsException e) {
829             switchToCurrentProject();
830             throw e;
831         }
832
833         // switch back to current project
834
switchToCurrentProject();
835
836         return temporaryFilename;
837     }
838
839     /**
840      * Decodes the given content the same way the client would do it.<p>
841      *
842      * Content is decoded as if it was encoded using the JavaScript
843      * "encodeURIComponent()" function.<p>
844      *
845      * @param content the content to decode
846      * @return the decoded content
847      */

848     protected String JavaDoc decodeContent(String JavaDoc content) {
849
850         return CmsEncoder.unescape(content, CmsEncoder.ENCODING_UTF_8);
851     }
852
853     /**
854      * Decodes an individual parameter value, ensuring the content is always decoded in UTF-8.<p>
855      *
856      * For editors the content is always encoded using the
857      * JavaScript encodeURIComponent() method on the client,
858      * which always encodes in UTF-8.<p>
859      *
860      * @param paramName the name of the parameter
861      * @param paramValue the unencoded value of the parameter
862      * @return the encoded value of the parameter
863      */

864     protected String JavaDoc decodeParamValue(String JavaDoc paramName, String JavaDoc paramValue) {
865
866         if ((paramName != null) && (paramValue != null)) {
867             if (PARAM_CONTENT.equals(paramName)) {
868                 // content will be always encoded in UTF-8 unicode by the editor client
869
return CmsEncoder.decode(paramValue, CmsEncoder.ENCODING_UTF_8);
870             } else if (PARAM_RESOURCE.equals(paramName) || PARAM_TEMPFILE.equals(paramName)) {
871                 String JavaDoc filename = CmsEncoder.decode(paramValue, getCms().getRequestContext().getEncoding());
872                 if (PARAM_TEMPFILE.equals(paramName) || CmsStringUtil.isEmpty(getParamTempfile())) {
873                     // always use value from temp file if it is available
874
setFileEncoding(getFileEncoding(getCms(), filename));
875                 }
876                 return filename;
877             } else {
878                 return CmsEncoder.decode(paramValue, getCms().getRequestContext().getEncoding());
879             }
880         } else {
881             return null;
882         }
883     }
884
885     /**
886      * Deletes a temporary file from the OpenCms VFS, needed when exiting an editor.<p>
887      */

888     protected void deleteTempFile() {
889
890         try {
891             // switch to the temporary file project
892
switchToTempProject();
893             // delete the temporary file
894
getCms().deleteResource(getParamTempfile(), CmsResource.DELETE_PRESERVE_SIBLINGS);
895             // switch back to the current project
896
switchToCurrentProject();
897         } catch (CmsException e) {
898             // should usually never happen
899
if (LOG.isInfoEnabled()) {
900                 LOG.info(e);
901             }
902         }
903     }
904
905     /**
906      * Encodes the given content so that it can be transfered to the client.<p>
907      *
908      * Content is encoded so that it is compatible with the JavaScript
909      * "decodeURIComponent()" function.<p>
910      *
911      * @param content the content to encode
912      * @return the encoded content
913      */

914     protected String JavaDoc encodeContent(String JavaDoc content) {
915
916         return CmsEncoder.escapeWBlanks(content, CmsEncoder.ENCODING_UTF_8);
917     }
918
919     /**
920      * Returns the encoding parameter.<p>
921      *
922      * @return the encoding parameter
923      */

924     protected String JavaDoc getFileEncoding() {
925
926         return m_fileEncoding;
927     }
928
929     /**
930      * Helper method to determine the encoding of the given file in the VFS,
931      * which must be set using the "content-encoding" property.<p>
932      *
933      * @param cms the CmsObject
934      * @param filename the name of the file which is to be checked
935      * @return the encoding for the file
936      */

937     protected String JavaDoc getFileEncoding(CmsObject cms, String JavaDoc filename) {
938
939         try {
940             return cms.readPropertyObject(filename, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, false).getValue(
941                 OpenCms.getSystemInfo().getDefaultEncoding());
942         } catch (CmsException e) {
943             return OpenCms.getSystemInfo().getDefaultEncoding();
944         }
945     }
946
947     /**
948      * Initializes the editor content when openening the editor for the first time.<p>
949      */

950     protected abstract void initContent();
951
952     /**
953      * Sets the encoding parameter.<p>
954      *
955      * @param value the encoding value to set
956      */

957     protected void setFileEncoding(String JavaDoc value) {
958
959         m_fileEncoding = CmsEncoder.lookupEncoding(value, value);
960     }
961
962     /**
963      * Shows the selected error page in case of an exception.<p>
964      *
965      * @param exception the current exception
966      * @throws JspException if inclusion of the error page fails
967      */

968     protected void showErrorPage(Exception JavaDoc exception) throws JspException JavaDoc {
969
970         // reset the action parameter
971
setParamAction("");
972         showErrorPage(this, exception);
973         // save not successful, set cancel action
974
setAction(ACTION_CANCEL);
975         return;
976     }
977
978     /**
979      * Shows the selected error page in case of an exception.<p>
980      *
981      * @param editor initialized instance of the editor class
982      * @param exception the current exception
983      * @throws JspException if inclusion of the error page fails
984      */

985     protected void showErrorPage(Object JavaDoc editor, Exception JavaDoc exception) throws JspException JavaDoc {
986
987         // save initialized instance of the editor class in request attribute for included sub-elements
988
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, editor);
989
990         // reading of file contents failed, show error dialog
991
setAction(ACTION_SHOW_ERRORMESSAGE);
992         setParamTitle(key("title.edit") + ": " + CmsResource.getName(getParamResource()));
993         if (exception != null) {
994             getJsp().getRequest().setAttribute(ATTRIBUTE_THROWABLE, exception);
995             if (CmsLog.getLog(editor).isWarnEnabled()) {
996                 CmsLog.getLog(editor).warn(exception.getLocalizedMessage(), exception);
997             }
998         }
999         // include the common error dialog
1000
getJsp().include(FILE_DIALOG_SCREEN_ERRORPAGE);
1001    }
1002}
Popular Tags