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.file.types;
029
030import org.opencms.ade.contenteditor.CmsContentService;
031import org.opencms.db.CmsSecurityManager;
032import org.opencms.file.CmsFile;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsResource;
035import org.opencms.main.CmsException;
036import org.opencms.main.OpenCms;
037import org.opencms.util.CmsWaitHandle;
038
039/**
040 * Resource type class for the second version of dynamic functions.<p>
041 */
042public class CmsResourceTypeFunctionConfig extends CmsResourceTypeXmlAdeConfiguration {
043
044    /** The path of the JSP used for rendering v2 functions. */
045    public static final String FORMATTER_PATH = "/system/modules/org.opencms.base/formatters/function.jsp";
046
047    /** The type name. */
048    public static final String TYPE_NAME = "function_config";
049
050    /** The serial version id. */
051    private static final long serialVersionUID = -2378978201570511075L;
052
053    /**
054     * Returns the static type name of this (default) resource type.<p>
055     *
056     * @return the static type name of this (default) resource type
057     */
058    public static String getStaticTypeName() {
059
060        return TYPE_NAME;
061    }
062
063    /**
064     * Checks if a resource has this type.<p>
065     *
066     * @param res the resource to check
067     * @return true if the resource is a V2 dynamic function
068     */
069    public static boolean isFunction(CmsResource res) {
070
071        return OpenCms.getResourceManager().matchResourceType(TYPE_NAME, res.getTypeId());
072    }
073
074    /**
075     * @see org.opencms.file.types.CmsResourceTypeXmlContent#getCachePropertyDefault()
076     */
077    @Override
078    public String getCachePropertyDefault() {
079
080        return null;
081    }
082
083    /**
084     * @see org.opencms.file.types.CmsResourceTypeXmlContent#writeFile(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsFile)
085     *
086     * After writing the file, this method waits until the formatter configuration is update the next time.
087     */
088    @Override
089    public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource) throws CmsException {
090
091        String savingStr = (String)cms.getRequestContext().getAttribute(CmsContentService.ATTR_EDITOR_SAVING);
092        CmsFile file = super.writeFile(cms, securityManager, resource);
093        // Formatter configuration cache updates are asynchronous, but to be able to reload a container page
094        // element in the page editor directly after editing it and having it reflect the changes made by the user
095        // requires that we wait on a wait handle for the formatter cache.
096
097        if (Boolean.valueOf(savingStr).booleanValue()) {
098            CmsWaitHandle waitHandle = OpenCms.getADEManager().addFormatterCacheWaitHandle(false);
099            waitHandle.enter(10000);
100        }
101        return file;
102    }
103
104}