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 resourceName the name of the VFS resource to get the direct edit info for
116     *
117     * @return the direct edit resource information for the given VFS resource
118     */
119    public CmsDirectEditResourceInfo getResourceInfo(String resourceName) {
120
121        try {
122            // first check some simple preconditions for direct edit
123            if (m_cms.getRequestContext().getCurrentProject().isOnlineProject()) {
124                // don't show direct edit button in online project
125                return CmsDirectEditResourceInfo.INACTIVE;
126            }
127            if (CmsResource.isTemporaryFileName(resourceName)) {
128                // don't show direct edit button on a temporary file
129                return CmsDirectEditResourceInfo.INACTIVE;
130            }
131            if (!m_cms.isInsideCurrentProject(resourceName)) {
132                // don't show direct edit button on files not belonging to the current project
133                return CmsDirectEditResourceInfo.INACTIVE;
134            }
135            // read the target resource
136            CmsResource resource = m_cms.readResource(resourceName, CmsResourceFilter.ALL);
137            if (!OpenCms.getResourceManager().getResourceType(resource.getTypeId()).isDirectEditable()
138                && !resource.isFolder()) {
139                // don't show direct edit button for non-editable resources
140                return CmsDirectEditResourceInfo.INACTIVE;
141            }
142            // check the resource lock
143            CmsLock lock = m_cms.getLock(resource);
144            boolean locked = !(lock.isUnlocked()
145                || lock.isOwnedInProjectBy(
146                    m_cms.getRequestContext().getCurrentUser(),
147                    m_cms.getRequestContext().getCurrentProject()));
148            // check the users permissions on the resource
149            if (m_cms.hasPermissions(
150                resource,
151                CmsPermissionSet.ACCESS_WRITE,
152                false,
153                CmsResourceFilter.IGNORE_EXPIRATION)) {
154                // only if write permissions are granted the resource may be direct editable
155                if (locked) {
156                    // a locked resource must be shown as "disabled"
157                    return new CmsDirectEditResourceInfo(CmsDirectEditPermissions.DISABLED, resource, lock);
158                }
159                // if we have write permission and the resource is not locked then direct edit is enabled
160                return new CmsDirectEditResourceInfo(CmsDirectEditPermissions.ENABLED, resource, lock);
161            }
162        } catch (Exception e) {
163            // all exceptions: don't mix up the result HTML, always return INACTIVE mode
164            if (LOG.isWarnEnabled()) {
165                LOG.warn(
166                    org.opencms.workplace.editors.Messages.get().getBundle().key(
167                        org.opencms.workplace.editors.Messages.LOG_CALC_EDIT_MODE_FAILED_1,
168                        resourceName),
169                    e);
170            }
171        }
172        // otherwise the resource is not direct editable
173        return CmsDirectEditResourceInfo.INACTIVE;
174    }
175
176    /**
177     * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#init(org.opencms.file.CmsObject, org.opencms.workplace.editors.directedit.CmsDirectEditMode, java.lang.String)
178     */
179    public void init(CmsObject cms, CmsDirectEditMode mode, String fileName) {
180
181        m_cms = cms;
182        m_fileName = fileName;
183        if (CmsStringUtil.isEmpty(m_fileName)) {
184            m_fileName = INCLUDE_FILE_DEFAULT;
185        }
186        m_mode = mode != null ? mode : CmsDirectEditMode.AUTO;
187
188        m_rnd = new Random();
189        CmsUserSettings settings = new CmsUserSettings(cms);
190        m_messages = new CmsMessages(
191            org.opencms.workplace.editors.Messages.get().getBundleName(),
192            settings.getLocale());
193        m_editButtonStyle = settings.getEditorButtonStyle();
194    }
195
196    /**
197     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
198     */
199    public void initConfiguration() throws CmsConfigurationException {
200
201        // we need a Map with a defined order of keys for serializing the configuration
202        if (m_configurationParameters == null) {
203            m_configurationParameters = new CmsParameterConfiguration();
204        }
205        m_configurationParameters = CmsParameterConfiguration.unmodifiableVersion(m_configurationParameters);
206        if (m_configurationParameters == null) {
207            // suppress the compiler warning, this is never true
208            throw new CmsConfigurationException(null);
209        }
210    }
211
212    /**
213     * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#insertDirectEditEmptyList(javax.servlet.jsp.PageContext, org.opencms.workplace.editors.directedit.CmsDirectEditParams)
214     */
215    public void insertDirectEditEmptyList(PageContext context, CmsDirectEditParams params) throws JspException {
216
217        insertDirectEditStart(context, params);
218        ServletRequest req = context.getRequest();
219        CmsFlexController controller = CmsFlexController.getController(req);
220        CmsObject cms = controller.getCmsObject();
221        print(
222            context,
223            "<div style=\"minHeight:24px\">"
224                + Messages.get().getBundle(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms)).key(
225                    Messages.GUI_CLICK_TO_ADD_ELEMENT_TO_EMPTY_LIST_0)
226                + "</div>");
227        insertDirectEditEnd(context);
228    }
229
230    /**
231     * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#insertDirectEditListMetadata(javax.servlet.jsp.PageContext, org.opencms.gwt.shared.I_CmsContentLoadCollectorInfo)
232     */
233    @SuppressWarnings("unused")
234    public void insertDirectEditListMetadata(PageContext context, I_CmsContentLoadCollectorInfo info)
235    throws JspException {
236
237        // do nothing by default
238    }
239
240    /**
241     * @see org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider#isManual(org.opencms.workplace.editors.directedit.CmsDirectEditMode)
242     */
243    public boolean isManual(CmsDirectEditMode mode) {
244
245        return (mode == CmsDirectEditMode.MANUAL)
246            || ((m_mode == CmsDirectEditMode.MANUAL) && (mode == CmsDirectEditMode.TRUE));
247    }
248
249    /**
250     * Returns the given link resolved according to the OpenCms context and export rules.<p>
251     *
252     * @param target the link target to resolve
253     *
254     * @return the given link resolved according to the OpenCms context and export rules
255     */
256    protected String getLink(String target) {
257
258        return OpenCms.getLinkManager().substituteLink(m_cms, target);
259    }
260
261    /**
262     * Returns the next random edit id.<p>
263     *
264     * Random edit id's are used because to separate multiple direct edit buttons on one page.<p>
265     *
266     * @return the next random edit id
267     */
268    protected String getNextDirectEditId() {
269
270        return "ocms_".concat(String.valueOf(m_rnd.nextInt(1000000)));
271    }
272
273    /**
274     * Prints the given content string to the given page context.<p>
275     *
276     * Does not print anything if the content is <code>null</code>.<p>
277     *
278     * @param context the JSP page context to print the content to
279     * @param content the content to print
280     *
281     * @throws JspException in case the content could not be written to the page conext
282     */
283    protected void print(PageContext context, String content) throws JspException {
284
285        if (content != null) {
286            try {
287                context.getOut().print(content);
288            } catch (IOException e) {
289                throw new JspException(e);
290            }
291        }
292    }
293}