KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > editors > directedit > CmsDirectEditJspIncludeProvider


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/directedit/CmsDirectEditJspIncludeProvider.java,v $
3  * Date : $Date: 2006/10/26 12:25:34 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2005 Alkacon Software (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, 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.directedit;
33
34 import org.opencms.db.CmsUserSettings;
35 import org.opencms.file.CmsObject;
36 import org.opencms.flex.CmsFlexController;
37 import org.opencms.flex.CmsFlexResponse;
38 import org.opencms.i18n.CmsEncoder;
39 import org.opencms.jsp.CmsJspTagInclude;
40 import org.opencms.loader.I_CmsResourceLoader;
41 import org.opencms.util.CmsStringUtil;
42
43 import java.io.IOException JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Map JavaDoc;
46
47 import javax.servlet.ServletException JavaDoc;
48 import javax.servlet.ServletRequest JavaDoc;
49 import javax.servlet.ServletResponse JavaDoc;
50 import javax.servlet.jsp.JspException JavaDoc;
51 import javax.servlet.jsp.PageContext JavaDoc;
52
53 /**
54  * Direct edit provider that uses the same JSP include based logic that has been
55  * the default before the 6.2.3 release.<p>
56  *
57  * Even though placing the HTML of the direct edit buttons appears to be more "flexible" at first,
58  * there is a large overhead invloved using this provider as compared to an implementation
59  * like {@link CmsDirectEditDefaultProvider}. For every direct edit button on a page,
60  * a JSP include is processed <i>twice</i> using this provider,
61  * one include for the opening and one for the closing HTML. A JSP include is a costly operation, which means
62  * the performance of a website is be impacted if many content managers work on the system that makes great
63  * use of direct edit with a lot of elements on a page. In order to avoid this performance impact,
64  * OpenCms since version 6.2.3 uses the {@link CmsDirectEditDefaultProvider} by default.<p>
65  *
66  * This provider DOES NOT support {@link CmsDirectEditMode#MANUAL} mode.<p>
67  *
68  * @author Alexander Kandzior
69  *
70  * @version $Revision: 1.2 $
71  *
72  * @since 6.2.3
73  */

74 public class CmsDirectEditJspIncludeProvider extends A_CmsDirectEditProvider implements I_CmsDirectEditProvider {
75
76     /** Prefix for direct edit end elements, used on JPS pages that supply the direct edit html. */
77     public static final String JavaDoc DIRECT_EDIT_AREA_END = "end_directedit";
78
79     /** Prefix for direct edit start elements, used on JPS pages that supply the direct edit html. */
80     public static final String JavaDoc DIRECT_EDIT_AREA_START = "start_directedit";
81
82     /** Default direct edit include file URI. */
83     public static final String JavaDoc DIRECT_EDIT_INCLUDE_FILE_URI_DEFAULT = "/system/workplace/editors/direct_edit.jsp";
84
85     /** Element name for direct edit includes. */
86     public static final String JavaDoc DIRECT_EDIT_INCLUDES = "directedit_includes";
87
88     /** Key to identify the edit button style, used on JPS pages that supply the direct edit html. */
89     public static final String JavaDoc DIRECT_EDIT_PARAM_BUTTONSTYLE = "__directEditButtonStyle";
90
91     /** Key to identify the edit element, used on JPS pages that supply the direct edit html. */
92     public static final String JavaDoc DIRECT_EDIT_PARAM_ELEMENT = "__directEditElement";
93
94     /** Key to identify the edit language, used on JPS pages that supply the direct edit html. */
95     public static final String JavaDoc DIRECT_EDIT_PARAM_LOCALE = "__directEditLocale";
96
97     /** Key to identify the link to use for the "new" button (if enabled). */
98     public static final String JavaDoc DIRECT_EDIT_PARAM_NEWLINK = "__directEditNewLink";
99
100     /** Key to identify additional direct edit options, used e.g. to control which direct edit buttons are displayed */
101     public static final String JavaDoc DIRECT_EDIT_PARAM_OPTIONS = "__directEditOptions";
102
103     /** Key to identify the edit target, used on JPS pages that supply the direct edit html. */
104     public static final String JavaDoc DIRECT_EDIT_PARAM_TARGET = "__directEditTarget";
105
106     /** The last direct edit element. */
107     protected String JavaDoc m_editElement;
108
109     /** The last direct edit target. */
110     protected String JavaDoc m_editTarget;
111
112     /** The last calculated direct edit permissions. */
113     protected String JavaDoc m_permissions;
114
115     /**
116      * Includes the "direct edit" element that adds HTML for the editable area to
117      * the output page.<p>
118      *
119      * @param context the current JSP page context
120      * @param jspIncludeFile the VFS path of the JSP that contains the direct edit HTML fragments
121      * @param element the editor element to include
122      * @param editTarget the direct edit target
123      * @param editElement the direct edit element
124      * @param editOptions the direct edit options
125      * @param editPermissions the direct edit permissions
126      * @param createLink the direct edit create link
127      *
128      * @throws JspException in case something goes wrong
129      *
130      * @return the direct edit permissions
131      */

132     public static String JavaDoc includeDirectEditElement(
133         PageContext JavaDoc context,
134         String JavaDoc jspIncludeFile,
135         String JavaDoc element,
136         String JavaDoc editTarget,
137         String JavaDoc editElement,
138         String JavaDoc editOptions,
139         String JavaDoc editPermissions,
140         String JavaDoc createLink) throws JspException JavaDoc {
141
142         if (editPermissions == null) {
143             // we do not have direct edit permissions
144
return null;
145         }
146
147         ServletRequest JavaDoc req = context.getRequest();
148         ServletResponse JavaDoc res = context.getResponse();
149         CmsFlexController controller = CmsFlexController.getController(req);
150
151         // append "direct edit" permissions to element
152
element = element + "_" + editPermissions;
153
154         // set request parameters required by the included direct edit JSP
155
Map JavaDoc parameterMap = new HashMap JavaDoc();
156         CmsJspTagInclude.addParameter(parameterMap, I_CmsResourceLoader.PARAMETER_ELEMENT, element, true);
157         CmsJspTagInclude.addParameter(parameterMap, DIRECT_EDIT_PARAM_TARGET, editTarget, true);
158         CmsJspTagInclude.addParameter(
159             parameterMap,
160             DIRECT_EDIT_PARAM_LOCALE,
161             controller.getCmsObject().getRequestContext().getLocale().toString(),
162             true);
163         CmsUserSettings settings = new CmsUserSettings(controller.getCmsObject());
164         CmsJspTagInclude.addParameter(
165             parameterMap,
166             DIRECT_EDIT_PARAM_BUTTONSTYLE,
167             String.valueOf(settings.getDirectEditButtonStyle()),
168             true);
169         if (editElement != null) {
170             CmsJspTagInclude.addParameter(parameterMap, DIRECT_EDIT_PARAM_ELEMENT, editElement, true);
171         }
172         if (editOptions != null) {
173             CmsJspTagInclude.addParameter(parameterMap, DIRECT_EDIT_PARAM_OPTIONS, editOptions, true);
174         }
175         if (createLink != null) {
176             CmsJspTagInclude.addParameter(parameterMap, DIRECT_EDIT_PARAM_NEWLINK, CmsEncoder.encode(createLink), true);
177         }
178
179         // save old parameters from current request
180
Map JavaDoc oldParameterMap = controller.getCurrentRequest().getParameterMap();
181
182         try {
183
184             controller.getCurrentRequest().addParameterMap(parameterMap);
185             context.getOut().print(CmsFlexResponse.FLEX_CACHE_DELIMITER);
186             controller.getCurrentResponse().addToIncludeList(jspIncludeFile, parameterMap);
187             controller.getCurrentRequest().getRequestDispatcher(jspIncludeFile).include(req, res);
188
189         } catch (ServletException JavaDoc e) {
190
191             Throwable JavaDoc t;
192             if (e.getRootCause() != null) {
193                 t = e.getRootCause();
194             } else {
195                 t = e;
196             }
197             t = controller.setThrowable(t, jspIncludeFile);
198             throw new JspException JavaDoc(t);
199         } catch (IOException JavaDoc e) {
200
201             Throwable JavaDoc t = controller.setThrowable(e, jspIncludeFile);
202             throw new JspException JavaDoc(t);
203         } finally {
204
205             // restore old parameter map (if required)
206
if (oldParameterMap != null) {
207                 controller.getCurrentRequest().setParameterMap(oldParameterMap);
208             }
209         }
210
211         return editPermissions;
212     }
213
214     /**
215      * @see org.opencms.workplace.editors.directedit.A_CmsDirectEditProvider#init(org.opencms.file.CmsObject, org.opencms.workplace.editors.directedit.CmsDirectEditMode, java.lang.String)
216      */

217     public void init(CmsObject cms, CmsDirectEditMode mode, String JavaDoc fileName) {
218
219         m_cms = cms;
220         m_fileName = fileName;
221         if (CmsStringUtil.isEmpty(m_fileName)) {
222             m_fileName = DIRECT_EDIT_INCLUDE_FILE_URI_DEFAULT;
223         }
224         m_mode = mode != null ? mode : CmsDirectEditMode.AUTO;
225     }
226
227     /**
228      * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#insertDirectEditEnd(javax.servlet.jsp.PageContext)
229      */

230     public void insertDirectEditEnd(PageContext JavaDoc context) throws JspException JavaDoc {
231
232         if (m_editTarget != null) {
233             // otherwise no valid direct edit element has been opened
234
includeDirectEditElement(
235                 context,
236                 m_fileName,
237                 DIRECT_EDIT_AREA_END,
238                 m_editTarget,
239                 m_editElement,
240                 null,
241                 m_permissions,
242                 null);
243             m_editTarget = null;
244             m_permissions = null;
245             m_editElement = null;
246         }
247     }
248
249     /**
250      * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#insertDirectEditIncludes(javax.servlet.jsp.PageContext, org.opencms.workplace.editors.directedit.CmsDirectEditParams)
251      */

252     public void insertDirectEditIncludes(PageContext JavaDoc context, CmsDirectEditParams params) throws JspException JavaDoc {
253
254         try {
255             CmsJspTagInclude.includeTagAction(
256                 context,
257                 m_fileName,
258                 DIRECT_EDIT_INCLUDES,
259                 false,
260                 null,
261                 context.getRequest(),
262                 context.getResponse());
263         } catch (Throwable JavaDoc t) {
264             // should never happen
265
throw new JspException JavaDoc(t);
266         }
267     }
268
269     /**
270      * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#insertDirectEditStart(javax.servlet.jsp.PageContext, org.opencms.workplace.editors.directedit.CmsDirectEditParams)
271      */

272     public boolean insertDirectEditStart(PageContext JavaDoc context, CmsDirectEditParams params) throws JspException JavaDoc {
273
274         String JavaDoc result = null;
275         CmsDirectEditPermissions permissions = getResourceInfo(params.getResourceName()).getPermissions();
276         if (permissions.getPermission() > 0) {
277             // permission to direct edit is granted
278
m_permissions = permissions.toString();
279             m_editTarget = params.getResourceName();
280             m_editElement = params.getElement();
281
282             result = includeDirectEditElement(
283                 context,
284                 m_fileName,
285                 DIRECT_EDIT_AREA_START,
286                 m_editTarget,
287                 m_editElement,
288                 params.getButtonSelection().toString(),
289                 m_permissions,
290                 params.getLinkForNew());
291
292         } else {
293             // no direct edit permissions
294
m_editTarget = null;
295             m_permissions = null;
296             m_editElement = null;
297         }
298         return result != null;
299     }
300
301     /**
302      * Returns <code>false</code> because the JSP include provider does not support manual button placement.<p>
303      *
304      * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#isManual(org.opencms.workplace.editors.directedit.CmsDirectEditMode)
305      */

306     public boolean isManual(CmsDirectEditMode mode) {
307
308         return false;
309     }
310
311     /**
312      * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#newInstance()
313      */

314     public I_CmsDirectEditProvider newInstance() {
315
316         CmsDirectEditJspIncludeProvider result = new CmsDirectEditJspIncludeProvider();
317         result.m_configurationParameters = m_configurationParameters;
318         return result;
319     }
320 }
Popular Tags