KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/directedit/A_CmsDirectEditProvider.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.configuration.CmsConfigurationException;
35 import org.opencms.db.CmsUserSettings;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsResourceFilter;
39 import org.opencms.i18n.CmsMessages;
40 import org.opencms.lock.CmsLock;
41 import org.opencms.main.CmsLog;
42 import org.opencms.main.OpenCms;
43 import org.opencms.security.CmsPermissionSet;
44 import org.opencms.util.CmsStringUtil;
45 import org.opencms.workplace.editors.Messages;
46
47 import java.io.IOException JavaDoc;
48 import java.util.Collections JavaDoc;
49 import java.util.Map JavaDoc;
50 import java.util.Random JavaDoc;
51 import java.util.TreeMap JavaDoc;
52
53 import javax.servlet.jsp.JspException JavaDoc;
54 import javax.servlet.jsp.PageContext JavaDoc;
55
56 import org.apache.commons.logging.Log;
57
58 /**
59  * Basic functions for direct edit providers.<p>
60  *
61  * @author Alexander Kandzior
62  *
63  * @version $Revision: 1.2 $
64  *
65  * @since 6.2.3
66  */

67 public abstract class A_CmsDirectEditProvider implements I_CmsDirectEditProvider {
68
69     /** Default direct edit include file URI for post 6.2.3 direct edit providers. */
70     protected static final String JavaDoc INCLUDE_FILE_DEFAULT = "/system/workplace/editors/direct_edit_include.txt";
71
72     /** The log object for this class. */
73     private static final Log LOG = CmsLog.getLog(A_CmsDirectEditProvider.class);
74
75     /** The current users OpenCms context. */
76     protected CmsObject m_cms;
77
78     /** The parameters form the configuration. */
79     protected Map JavaDoc m_configurationParameters;
80
81     /** The editor button style to use. */
82     protected int m_editButtonStyle;
83
84     /** Value of the "fileName" parameter. */
85     protected String JavaDoc m_fileName;
86
87     /** Used to access the editor messages. */
88     protected CmsMessages m_messages;
89
90     /** Indicates which direct edit mode is used. */
91     protected CmsDirectEditMode m_mode;
92
93     /** Used to generate the edit id's. */
94     protected Random JavaDoc m_rnd;
95
96     /**
97      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
98      */

99     public void addConfigurationParameter(String JavaDoc paramName, String JavaDoc paramValue) {
100
101         if (m_configurationParameters == null) {
102             m_configurationParameters = new TreeMap JavaDoc();
103         }
104         m_configurationParameters.put(paramName, paramValue);
105     }
106
107     /**
108      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
109      */

110     public Map JavaDoc getConfiguration() {
111
112         if (m_configurationParameters == null) {
113             // configuration parameters are usually null if the constructor has been used
114
return Collections.EMPTY_MAP;
115         }
116         // this implementation ensures that this is an unmodifiable Map in #initConfiguration()
117
return m_configurationParameters;
118     }
119
120     /**
121      * Calculates the direct edit resource information for the given VFS resource.<p>
122      *
123      * This includes the direct edit permissions.
124      * If the permissions are not {@link CmsDirectEditPermissions#INACTIVE}, then the resource and lock
125      * information is also included in the result.<p>
126      *
127      * @param resourceName the name of the VFS resource to get the direct edit info for
128      *
129      * @return the direct edit resource information for the given VFS resource
130      */

131     public CmsDirectEditResourceInfo getResourceInfo(String JavaDoc resourceName) {
132
133         try {
134             // first check some simple preconditions for direct edit
135
if (m_cms.getRequestContext().currentProject().isOnlineProject()) {
136                 // don't show direct edit button in online project
137
return CmsDirectEditResourceInfo.INACTIVE;
138             }
139             if (CmsResource.getName(resourceName).startsWith(org.opencms.workplace.CmsWorkplace.TEMP_FILE_PREFIX)) {
140                 // don't show direct edit button on temporary file
141
return CmsDirectEditResourceInfo.INACTIVE;
142             }
143             if (!m_cms.isInsideCurrentProject(resourceName)) {
144                 // don't show direct edit button on files not belonging to the current project
145
return CmsDirectEditResourceInfo.INACTIVE;
146             }
147             // read the target resource
148
CmsResource resource = m_cms.readResource(resourceName, CmsResourceFilter.ALL);
149             if (!OpenCms.getResourceManager().getResourceType(resource.getTypeId()).isDirectEditable()) {
150                 // don't show direct edit button for non-editable resources
151
return CmsDirectEditResourceInfo.INACTIVE;
152             }
153             // check the resource lock
154
CmsLock lock = m_cms.getLock(resource);
155             boolean unlocked = lock.isNullLock();
156             boolean ownedInProject = (lock.getProjectId() == m_cms.getRequestContext().currentProject().getId())
157                 && (lock.getUserId().equals(m_cms.getRequestContext().currentUser().getId()));
158
159             boolean locked = !(unlocked || ownedInProject);
160             // check the users permissions on the resource
161
if (m_cms.hasPermissions(
162                 resource,
163                 CmsPermissionSet.ACCESS_WRITE,
164                 false,
165                 CmsResourceFilter.IGNORE_EXPIRATION)) {
166                 // only if write permissions are granted the resource may be direct editable
167
if (locked) {
168                     // a locked resource must be shown as "disabled"
169
return new CmsDirectEditResourceInfo(CmsDirectEditPermissions.DISABLED, resource, lock);
170                 }
171                 // if we have write permission and the resource is not locked then direct edit is enabled
172
return new CmsDirectEditResourceInfo(CmsDirectEditPermissions.ENABLED, resource, lock);
173             }
174         } catch (Exception JavaDoc e) {
175             // all exceptions: don't mix up the result HTML, always return INACTIVE mode
176
if (LOG.isWarnEnabled()) {
177                 LOG.warn(Messages.get().getBundle().key(Messages.LOG_CALC_EDIT_MODE_FAILED_1, resourceName), e);
178             }
179         }
180         // otherwise the resource is not direct editable
181
return CmsDirectEditResourceInfo.INACTIVE;
182     }
183
184     /**
185      * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#init(org.opencms.file.CmsObject, org.opencms.workplace.editors.directedit.CmsDirectEditMode, java.lang.String)
186      */

187     public void init(CmsObject cms, CmsDirectEditMode mode, String JavaDoc fileName) {
188
189         m_cms = cms;
190         m_fileName = fileName;
191         if (CmsStringUtil.isEmpty(m_fileName)) {
192             m_fileName = INCLUDE_FILE_DEFAULT;
193         }
194         m_mode = mode != null ? mode : CmsDirectEditMode.AUTO;
195
196         m_rnd = new Random JavaDoc();
197         CmsUserSettings settings = new CmsUserSettings(cms);
198         m_messages = new CmsMessages(Messages.get().getBundleName(), settings.getLocale());
199         m_editButtonStyle = settings.getEditorButtonStyle();
200     }
201
202     /**
203      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
204      */

205     public void initConfiguration() throws CmsConfigurationException {
206
207         // we need a Map with a defined order of keys for serializing the configuration
208
if (m_configurationParameters == null) {
209             // suppress the compiler warning, this is never true
210
m_configurationParameters = Collections.EMPTY_MAP;
211         } else {
212             m_configurationParameters = Collections.unmodifiableMap(m_configurationParameters);
213         }
214         if (m_configurationParameters == null) {
215             // suppress the compiler warning, this is never true
216
throw new CmsConfigurationException(null);
217         }
218     }
219
220     /**
221      * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#isManual(org.opencms.workplace.editors.directedit.CmsDirectEditMode)
222      */

223     public boolean isManual(CmsDirectEditMode mode) {
224
225         return (mode == CmsDirectEditMode.MANUAL)
226             || ((m_mode == CmsDirectEditMode.MANUAL) && (mode == CmsDirectEditMode.TRUE));
227     }
228
229     /**
230      * Returns the given link resolved according to the OpenCms context and export rules.<p>
231      *
232      * @param target the link target to resolve
233      *
234      * @return the given link resolved according to the OpenCms context and export rules
235      */

236     protected String JavaDoc getLink(String JavaDoc target) {
237
238         return OpenCms.getLinkManager().substituteLink(m_cms, target);
239     }
240
241     /**
242      * Returns the next random edit id.<p>
243      *
244      * Random edit id's are used because to separate multiple direct edit buttons on one page.<p>
245      *
246      * @return the next random edit id
247      */

248     protected String JavaDoc getNextDirectEditId() {
249
250         return "ocms_".concat(String.valueOf(m_rnd.nextInt(1000000)));
251     }
252
253     /**
254      * Prints the given content string to the given page context.<p>
255      *
256      * Does not print anything if the content is <code>null</code>.<p>
257      *
258      * @param context the JSP page context to print the content to
259      * @param content the content to print
260      *
261      * @throws JspException in case the content could not be written to the page conext
262      */

263     protected void print(PageContext JavaDoc context, String JavaDoc content) throws JspException JavaDoc {
264
265         if (content != null) {
266             try {
267                 context.getOut().print(content);
268             } catch (IOException JavaDoc e) {
269                 throw new JspException JavaDoc(e);
270             }
271         }
272     }
273 }
Popular Tags