001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace.editors.directedit;
029
030import org.opencms.configuration.CmsConfigurationException;
031import org.opencms.configuration.CmsParameterConfiguration;
032import org.opencms.db.CmsUserSettings;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsResource;
035import org.opencms.file.CmsResourceFilter;
036import org.opencms.flex.CmsFlexController;
037import org.opencms.gwt.shared.I_CmsContentLoadCollectorInfo;
038import org.opencms.i18n.CmsMessages;
039import org.opencms.lock.CmsLock;
040import org.opencms.main.CmsLog;
041import org.opencms.main.OpenCms;
042import org.opencms.security.CmsPermissionSet;
043import org.opencms.util.CmsStringUtil;
044
045import java.io.IOException;
046import java.util.Random;
047
048import javax.servlet.ServletRequest;
049import javax.servlet.jsp.JspException;
050import javax.servlet.jsp.PageContext;
051
052import org.apache.commons.logging.Log;
053
054/**
055 * Basic functions for direct edit providers.<p>
056 *
057 * @since 6.2.3
058 */
059public abstract class A_CmsDirectEditProvider implements I_CmsDirectEditProvider {
060
061    /** Default direct edit include file URI for post 6.2.3 direct edit providers. */
062    protected static final String INCLUDE_FILE_DEFAULT = "/system/workplace/editors/direct_edit_include.txt";
063
064    /** The log object for this class. */
065    private static final Log LOG = CmsLog.getLog(A_CmsDirectEditProvider.class);
066
067    /** The current users OpenCms context. */
068    protected CmsObject m_cms;
069
070    /** The parameters form the configuration. */
071    protected CmsParameterConfiguration m_configurationParameters;
072
073    /** The editor button style to use. */
074    protected int m_editButtonStyle;
075
076    /** Value of the "fileName" parameter. */
077    protected String m_fileName;
078
079    /** Used to access the editor messages. */
080    protected CmsMessages m_messages;
081
082    /** Indicates which direct edit mode is used. */
083    protected CmsDirectEditMode m_mode;
084
085    /** Used to generate the edit id's. */
086    protected Random m_rnd;
087
088    /**
089     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
090     */
091    public void addConfigurationParameter(String paramName, String paramValue) {
092
093        if (m_configurationParameters == null) {
094            m_configurationParameters = new CmsParameterConfiguration();
095        }
096        m_configurationParameters.add(paramName, paramValue);
097    }
098
099    /**
100     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
101     */
102    public CmsParameterConfiguration getConfiguration() {
103
104        // this implementation ensures that this is an unmodifiable Map in #initConfiguration()
105        return m_configurationParameters;
106    }
107
108    /**
109     * Calculates the direct edit resource information for the given VFS resource.<p>
110     *
111     * This includes the direct edit permissions.
112     * If the permissions are not {@link CmsDirectEditPermissions#INACTIVE}, then the resource and lock
113     * information is also included in the result.<p>
114     *
115     * @param params the direct edit parameters
116     * @param resourceName the name of the VFS resource to get the direct edit info for
117     *
118     * @return the direct edit resource information for the given VFS resource
119     */
120    public CmsDirectEditResourceInfo getResourceInfo(CmsDirectEditParams params, String resourceName) {
121
122        try {
123            // first check some simple preconditions for direct edit
124            if (m_cms.getRequestContext().getCurrentProject().isOnlineProject()) {
125                // don't show direct edit button in online project
126                return CmsDirectEditResourceInfo.INACTIVE;
127            }
128            if (CmsResource.isTemporaryFileName(resourceName)) {
129                // don't show direct edit button on a temporary file
130                return CmsDirectEditResourceInfo.INACTIVE;
131            }
132            if (!m_cms.isInsideCurrentProject(resourceName)) {
133                // don't show direct edit button on files not belonging to the current project
134                return CmsDirectEditResourceInfo.INACTIVE;
135            }
136            // read the target resource
137            CmsResource resource = m_cms.readResource(resourceName, CmsResourceFilter.ALL);
138            if (!OpenCms.getResourceManager().getResourceType(resource.getTypeId()).isDirectEditable()
139                && !resource.isFolder()) {
140                // don't show direct edit button for non-editable resources
141                return CmsDirectEditResourceInfo.INACTIVE;
142            }
143            // check the resource lock
144            CmsLock lock = m_cms.getLock(resource);
145            boolean locked = !(lock.isUnlocked()
146                || lock.isOwnedInProjectBy(
147                    m_cms.getRequestContext().getCurrentUser(),
148                    m_cms.getRequestContext().getCurrentProject()));
149            // check the users permissions on the resource
150            if (m_cms.hasPermissions(
151                resource,
152                CmsPermissionSet.ACCESS_WRITE,
153                false,
154                CmsResourceFilter.IGNORE_EXPIRATION)) {
155                // only if write permissions are granted the resource may be direct editable
156                if (locked) {
157                    // a locked resource must be shown as "disabled"
158                    return new CmsDirectEditResourceInfo(CmsDirectEditPermissions.DISABLED, resource, lock);
159                }
160                // if we have write permission and the resource is not locked then direct edit is enabled
161                return new CmsDirectEditResourceInfo(CmsDirectEditPermissions.ENABLED, resource, lock);
162            }
163        } catch (Exception e) {
164            // all exceptions: don't mix up the result HTML, always return INACTIVE mode
165            if (LOG.isWarnEnabled()) {
166                LOG.warn(
167                    org.opencms.workplace.editors.Messages.get().getBundle().key(
168                        org.opencms.workplace.editors.Messages.LOG_CALC_EDIT_MODE_FAILED_1,
169                        resourceName),
170                    e);
171            }
172        }
173        // otherwise the resource is not direct editable
174        return CmsDirectEditResourceInfo.INACTIVE;
175    }
176
177    /**
178     * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#init(org.opencms.file.CmsObject, org.opencms.workplace.editors.directedit.CmsDirectEditMode, java.lang.String)
179     */
180    public void init(CmsObject cms, CmsDirectEditMode mode, String fileName) {
181
182        m_cms = cms;
183        m_fileName = fileName;
184        if (CmsStringUtil.isEmpty(m_fileName)) {
185            m_fileName = INCLUDE_FILE_DEFAULT;
186        }
187        m_mode = mode != null ? mode : CmsDirectEditMode.AUTO;
188
189        m_rnd = new Random();
190        CmsUserSettings settings = new CmsUserSettings(cms);
191        m_messages = new CmsMessages(
192            org.opencms.workplace.editors.Messages.get().getBundleName(),
193            settings.getLocale());
194        m_editButtonStyle = settings.getEditorButtonStyle();
195    }
196
197    /**
198     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
199     */
200    public void initConfiguration() throws CmsConfigurationException {
201
202        // we need a Map with a defined order of keys for serializing the configuration
203        if (m_configurationParameters == null) {
204            m_configurationParameters = new CmsParameterConfiguration();
205        }
206        m_configurationParameters = CmsParameterConfiguration.unmodifiableVersion(m_configurationParameters);
207        if (m_configurationParameters == null) {
208            // suppress the compiler warning, this is never true
209            throw new CmsConfigurationException(null);
210        }
211    }
212
213    /**
214     * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#insertDirectEditEmptyList(javax.servlet.jsp.PageContext, org.opencms.workplace.editors.directedit.CmsDirectEditParams)
215     */
216    public void insertDirectEditEmptyList(PageContext context, CmsDirectEditParams params) throws JspException {
217
218        insertDirectEditStart(context, params);
219        ServletRequest req = context.getRequest();
220        CmsFlexController controller = CmsFlexController.getController(req);
221        CmsObject cms = controller.getCmsObject();
222        print(
223            context,
224            "<div style=\"minHeight:24px\">"
225                + Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms)).key(
226                    Messages.GUI_CLICK_TO_ADD_ELEMENT_TO_EMPTY_LIST_0)
227                + "</div>");
228        insertDirectEditEnd(context);
229    }
230
231    /**
232     * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#insertDirectEditListMetadata(javax.servlet.jsp.PageContext, org.opencms.gwt.shared.I_CmsContentLoadCollectorInfo)
233     */
234    @SuppressWarnings("unused")
235    public void insertDirectEditListMetadata(PageContext context, I_CmsContentLoadCollectorInfo info)
236    throws JspException {
237
238        // do nothing by default
239    }
240
241    /**
242     * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#isManual(org.opencms.workplace.editors.directedit.CmsDirectEditMode)
243     */
244    public boolean isManual(CmsDirectEditMode mode) {
245
246        return (mode == CmsDirectEditMode.MANUAL)
247            || ((m_mode == CmsDirectEditMode.MANUAL) && (mode == CmsDirectEditMode.TRUE));
248    }
249
250    /**
251     * Returns the given link resolved according to the OpenCms context and export rules.<p>
252     *
253     * @param target the link target to resolve
254     *
255     * @return the given link resolved according to the OpenCms context and export rules
256     */
257    protected String getLink(String target) {
258
259        return OpenCms.getLinkManager().substituteLink(m_cms, target);
260    }
261
262    /**
263     * Returns the next random edit id.<p>
264     *
265     * Random edit id's are used because to separate multiple direct edit buttons on one page.<p>
266     *
267     * @return the next random edit id
268     */
269    protected String getNextDirectEditId() {
270
271        return "ocms_".concat(String.valueOf(m_rnd.nextInt(1000000)));
272    }
273
274    /**
275     * Prints the given content string to the given page context.<p>
276     *
277     * Does not print anything if the content is <code>null</code>.<p>
278     *
279     * @param context the JSP page context to print the content to
280     * @param content the content to print
281     *
282     * @throws JspException in case the content could not be written to the page conext
283     */
284    protected void print(PageContext context, String content) throws JspException {
285
286        if (content != null) {
287            try {
288                context.getOut().print(content);
289            } catch (IOException e) {
290                throw new JspException(e);
291            }
292        }
293    }
294}