KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsEditorActionDefault.java,v $
3  * Date : $Date: 2006/03/27 14:52:49 $
4  * Version: $Revision: 1.19 $
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.CmsObject;
35 import org.opencms.file.CmsProject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsResourceFilter;
38 import org.opencms.file.types.CmsResourceTypeXmlPage;
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.util.CmsStringUtil;
47 import org.opencms.util.CmsUUID;
48 import org.opencms.workplace.CmsDialog;
49 import org.opencms.workplace.CmsFrameset;
50 import org.opencms.workplace.CmsWorkplace;
51 import org.opencms.xml.I_CmsXmlDocument;
52 import org.opencms.xml.page.CmsXmlPageFactory;
53
54 import java.io.IOException JavaDoc;
55 import java.util.List JavaDoc;
56 import java.util.Locale JavaDoc;
57
58 import javax.servlet.ServletRequest JavaDoc;
59 import javax.servlet.jsp.JspException JavaDoc;
60
61 import org.apache.commons.logging.Log;
62
63 /**
64  * Provides a method to perform a user defined action when editing a page.<p>
65  *
66  * @author Andreas Zahner
67  *
68  * @version $Revision: 1.19 $
69  *
70  * @since 6.0.0
71  */

72 public class CmsEditorActionDefault implements I_CmsEditorActionHandler {
73
74     /** The log object for this class. */
75     private static final Log LOG = CmsLog.getLog(CmsEditorActionDefault.class);
76
77     /**
78      * Default constructor needed for editor action handler implementation.<p>
79      */

80     public CmsEditorActionDefault() {
81
82         // empty constructor
83
}
84
85     /**
86      * @see org.opencms.workplace.editors.I_CmsEditorActionHandler#editorAction(org.opencms.workplace.editors.CmsEditor, org.opencms.jsp.CmsJspActionElement)
87      */

88     public void editorAction(CmsEditor editor, CmsJspActionElement jsp) throws IOException JavaDoc, JspException JavaDoc {
89
90         // save the edited content
91
editor.actionSave();
92         // delete temporary file and unlock resource in direct edit mode
93
editor.actionClear(true);
94         // create the publish link to redirect to
95
String JavaDoc publishLink = jsp.link(CmsWorkplace.PATH_DIALOGS + "publishresource.jsp");
96         // define the parameters which are necessary for publishing the resource
97
StringBuffer JavaDoc params = new StringBuffer JavaDoc(64);
98         params.append("?resource=");
99         params.append(editor.getParamResource());
100         params.append("&action=");
101         params.append(CmsDialog.DIALOG_CONFIRMED);
102         params.append("&directpublish=true&publishsiblings=true");
103         params.append("&title=");
104         params.append(CmsEncoder.escapeWBlanks(editor.key(Messages.GUI_MESSAGEBOX_TITLE_PUBLISHRESOURCE_0)
105             + ": "
106             + editor.getParamResource(), CmsEncoder.ENCODING_UTF_8));
107         params.append("&").append(CmsDialog.PARAM_REDIRECT).append("=").append(CmsStringUtil.TRUE);
108         params.append("&closelink=");
109         if (Boolean.valueOf(editor.getParamDirectedit()).booleanValue()) {
110             String JavaDoc linkTarget;
111             if (!"".equals(editor.getParamBacklink())) {
112                 linkTarget = jsp.link(editor.getParamBacklink());
113             } else {
114                 linkTarget = jsp.link(editor.getParamResource());
115             }
116             // append the parameters and the report "ok" button action to the link
117
publishLink += params.toString() + CmsEncoder.escapeWBlanks(linkTarget, CmsEncoder.ENCODING_UTF_8);
118         } else {
119             // append the parameters and the report "ok" button action to the link
120
publishLink += params.toString()
121                 + CmsEncoder.escapeWBlanks(jsp.link(CmsFrameset.JSP_WORKPLACE_URI), CmsEncoder.ENCODING_UTF_8);
122
123         }
124         // redirect to the publish dialog with all necessary parameters
125
jsp.getResponse().sendRedirect(publishLink);
126     }
127
128     /**
129      * @see org.opencms.workplace.editors.I_CmsEditorActionHandler#getButtonName()
130      */

131     public String JavaDoc getButtonName() {
132
133         return Messages.GUI_EXPLORER_CONTEXT_PUBLISH_0;
134     }
135
136     /**
137      * @see org.opencms.workplace.editors.I_CmsEditorActionHandler#getButtonUrl(CmsJspActionElement, java.lang.String)
138      */

139     public String JavaDoc getButtonUrl(CmsJspActionElement jsp, String JavaDoc resourceName) {
140
141         // get the button image
142
String JavaDoc button = CmsWorkplace.VFS_PATH_RESOURCES + "buttons/publish.png";
143         if (!isButtonActive(jsp, resourceName)) {
144             // show disabled button if not active
145
button = CmsWorkplace.VFS_PATH_RESOURCES + "buttons/publish_in.png";
146         }
147         return jsp.link(button);
148     }
149
150     /**
151      * @see org.opencms.workplace.editors.I_CmsEditorActionHandler#getEditMode(org.opencms.file.CmsObject, java.lang.String, java.lang.String, javax.servlet.ServletRequest)
152      */

153     public String JavaDoc getEditMode(CmsObject cmsObject, String JavaDoc filename, String JavaDoc element, ServletRequest JavaDoc req) {
154
155         try {
156
157             CmsResource resource = cmsObject.readResource(filename, CmsResourceFilter.ALL);
158             int currentProject = cmsObject.getRequestContext().currentProject().getId();
159             CmsUUID userId = cmsObject.getRequestContext().currentUser().getId();
160             CmsLock lock = cmsObject.getLock(filename);
161             boolean locked = !(lock.isNullLock() || (lock.getUserId().equals(userId) && lock.getProjectId() == currentProject));
162
163             if (currentProject == CmsProject.ONLINE_PROJECT_ID) {
164                 // don't render direct edit button in online project
165
return null;
166             } else if (!OpenCms.getResourceManager().getResourceType(resource.getTypeId()).isDirectEditable()) {
167                 // don't render direct edit button for non-editable resources
168
return null;
169             } else if (CmsResource.getName(filename).startsWith(org.opencms.workplace.CmsWorkplace.TEMP_FILE_PREFIX)) {
170                 // don't show direct edit button on temporary file
171
return DIRECT_EDIT_MODE_INACTIVE;
172             } else if (!cmsObject.isInsideCurrentProject(filename)) {
173                 // don't show direct edit button on files not belonging to the current project
174
return DIRECT_EDIT_MODE_INACTIVE;
175             } else if (!cmsObject.hasPermissions(
176                 resource,
177                 CmsPermissionSet.ACCESS_WRITE,
178                 false,
179                 CmsResourceFilter.IGNORE_EXPIRATION)) {
180                 // don't show direct edit button on files without write permissions
181
if (locked) {
182                     return DIRECT_EDIT_MODE_DISABLED;
183                 } else {
184                     return DIRECT_EDIT_MODE_INACTIVE;
185                 }
186             } else if (locked) {
187                 return DIRECT_EDIT_MODE_DISABLED;
188             }
189
190             if ((element != null) && (resource.getTypeId() == CmsResourceTypeXmlPage.getStaticTypeId())) {
191                 // check if the desired element is available (in case of xml page)
192
I_CmsXmlDocument document = CmsXmlPageFactory.unmarshal(cmsObject, filename, req);
193                 List JavaDoc locales = document.getLocales();
194                 Locale JavaDoc locale;
195                 if ((locales == null) || (locales.size() == 0)) {
196                     locale = (Locale JavaDoc)OpenCms.getLocaleManager().getDefaultLocales(cmsObject, filename).get(0);
197                 } else {
198                     locale = OpenCms.getLocaleManager().getBestMatchingLocale(
199                         null,
200                         OpenCms.getLocaleManager().getDefaultLocales(cmsObject, filename),
201                         locales);
202                 }
203                 if (!document.hasValue(element, locale) || !document.isEnabled(element, locale)) {
204                     return DIRECT_EDIT_MODE_INACTIVE;
205                 }
206             }
207
208             // otherwise the resource is editable
209
return DIRECT_EDIT_MODE_ENABLED;
210
211         } catch (CmsException e) {
212             if (LOG.isWarnEnabled()) {
213                 LOG.warn(Messages.get().getBundle().key(Messages.LOG_CALC_EDIT_MODE_FAILED_1, filename), e);
214             }
215             // something went wrong - so the resource seems not to be editable
216
return DIRECT_EDIT_MODE_INACTIVE;
217         }
218     }
219
220     /**
221      * @see org.opencms.workplace.editors.I_CmsEditorActionHandler#isButtonActive(CmsJspActionElement, java.lang.String)
222      */

223     public boolean isButtonActive(CmsJspActionElement jsp, String JavaDoc resourceName) {
224
225         return jsp.getCmsObject().hasPublishPermissions(resourceName);
226     }
227
228 }
229
Popular Tags