001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsPreEditorAction.java,v $
003: * Date : $Date: 2008-02-27 12:05:23 $
004: * Version: $Revision: 1.4 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.editors;
033:
034: import org.opencms.file.CmsResource;
035: import org.opencms.file.CmsResourceFilter;
036: import org.opencms.file.types.I_CmsResourceType;
037: import org.opencms.i18n.CmsEncoder;
038: import org.opencms.jsp.CmsJspActionElement;
039: import org.opencms.main.CmsLog;
040: import org.opencms.main.OpenCms;
041: import org.opencms.util.CmsRequestUtil;
042: import org.opencms.util.CmsStringUtil;
043: import org.opencms.workplace.CmsDialog;
044: import org.opencms.workplace.CmsWorkplace;
045:
046: import java.util.Map;
047:
048: import javax.servlet.http.HttpServletRequest;
049: import javax.servlet.http.HttpServletResponse;
050: import javax.servlet.jsp.PageContext;
051:
052: import org.apache.commons.logging.Log;
053:
054: /**
055: * Handles the actions that should be performed before opening the editor frameset.<p>
056: *
057: * For each resource type, a pre editor action class can be defined that is triggered in the workplace JSP
058: * <code>/system/workplace/editors/editor.jsp</code> before the editor is initially opened.
059: * If an action was performed, be sure to use the static method {@link #sendForwardToEditor(CmsDialog, Map)}
060: * to open the editor after the action.<p>
061: *
062: * @author Andreas Zahner
063: *
064: * @version $Revision: 1.4 $
065: *
066: * @since 6.5.4
067: */
068: public class CmsPreEditorAction extends CmsDialog {
069:
070: /** The log object for this class. */
071: private static final Log LOG = CmsLog
072: .getLog(CmsPreEditorAction.class);
073:
074: /** The original request parameters passed to the editor. */
075: private String m_originalParams;
076:
077: /**
078: * Public constructor with JSP action element.<p>
079: *
080: * @param jsp an initialized JSP action element
081: */
082: public CmsPreEditorAction(CmsJspActionElement jsp) {
083:
084: super (jsp);
085: }
086:
087: /**
088: * Public constructor with JSP variables.<p>
089: *
090: * @param context the JSP page context
091: * @param req the JSP request
092: * @param res the JSP response
093: */
094: public CmsPreEditorAction(PageContext context,
095: HttpServletRequest req, HttpServletResponse res) {
096:
097: super (context, req, res);
098: }
099:
100: /**
101: * Returns if the dialog is currently running in pre editor action mode,
102: * depending on the presence of the original request parameters.<p>
103: * @param dialog the dialog instance currently used
104: * @return true if the dialog is currently running in pre editor action mode, otherwise false
105: */
106: public static boolean isPreEditorMode(CmsDialog dialog) {
107:
108: return CmsStringUtil
109: .isNotEmpty(dialog.getParamOriginalParams());
110: }
111:
112: /**
113: * Forwards to the editor and opens it after the action was performed.<p>
114: *
115: * @param dialog the dialog instance forwarding to the editor
116: */
117: public static void sendForwardToEditor(CmsDialog dialog) {
118:
119: sendForwardToEditor(dialog, null);
120: }
121:
122: /**
123: * Forwards to the editor and opens it after the action was performed.<p>
124: *
125: * @param dialog the dialog instance forwarding to the editor
126: * @param additionalParams eventual additional request parameters for the editor to use
127: */
128: public static void sendForwardToEditor(CmsDialog dialog,
129: Map additionalParams) {
130:
131: // create the Map of original request parameters
132: Map params = CmsRequestUtil.createParameterMap(dialog
133: .getParamOriginalParams());
134: // put the parameter indicating that the pre editor action was executed
135: params.put(PARAM_PREACTIONDONE, CmsStringUtil.TRUE);
136: if (additionalParams != null) {
137: // put the additional parameters to the Map
138: params.putAll(additionalParams);
139: }
140: try {
141: // now forward to the editor frameset
142: dialog.sendForward(CmsWorkplace.VFS_PATH_EDITORS
143: + "editor.jsp", params);
144: } catch (Exception e) {
145: // error forwarding, log the exception as error
146: if (LOG.isErrorEnabled()) {
147: LOG.error(e.getLocalizedMessage(), e);
148: }
149: }
150: }
151:
152: /**
153: * Returns if an action has to be performed before opening the editor depending on the resource to edit
154: * and request parameter values.<p>
155: *
156: * @return true if an action has to be performed, then the editor frameset is not generated
157: */
158: public boolean doPreAction() {
159:
160: String resourceName = getParamResource();
161: try {
162: boolean preActionDone = Boolean.valueOf(
163: getParamPreActionDone()).booleanValue();
164: if (!preActionDone) {
165: // pre editor action not executed yet now check if a pre action class is given for the resource type
166: CmsResource resource = getCms().readResource(
167: resourceName, CmsResourceFilter.ALL);
168: I_CmsResourceType type = OpenCms.getResourceManager()
169: .getResourceType(resource.getTypeId());
170: I_CmsPreEditorActionDefinition preAction = OpenCms
171: .getWorkplaceManager()
172: .getPreEditorConditionDefinition(type);
173: if (preAction != null) {
174: return preAction.doPreAction(resource, this ,
175: getOriginalParams());
176: }
177: }
178: } catch (Exception e) {
179: // log error
180: if (LOG.isErrorEnabled()) {
181: LOG.error(e.getLocalizedMessage(), e);
182: }
183: }
184: // nothing to be done as pre action, open the editor
185: return false;
186: }
187:
188: /**
189: * Returns the original request parameters for the editor to pass to the pre editor action dialog.<p>
190: *
191: * @return the original request parameters for the editor
192: */
193: public String getOriginalParams() {
194:
195: if (m_originalParams == null) {
196: m_originalParams = CmsEncoder.decode(CmsRequestUtil
197: .encodeParams(getJsp().getRequest()));
198: }
199: return m_originalParams;
200: }
201:
202: }
|