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.ade.contenteditor;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsLog;
032import org.opencms.widgets.A_CmsFormatterWidget;
033import org.opencms.widgets.CmsAddFormatterWidget;
034import org.opencms.xml.content.CmsXmlContent;
035import org.opencms.xml.content.CmsXmlContentValueSequence;
036import org.opencms.xml.types.CmsXmlBooleanValue;
037import org.opencms.xml.types.I_CmsXmlContentValue;
038
039import java.util.Collection;
040import java.util.List;
041import java.util.Locale;
042
043import org.apache.commons.logging.Log;
044
045/** Editor change handler implementation for the formatter selection in the sitemap config.
046 *
047 * If "Remove all formatters" is selected/deselected, the set of formatters that can be added/removed changes.
048 * That means, formerly explicitly added/removed formatters in the sitemap config can not be added/removed anymore.
049 * The respective content nodes must be removed. This is done by that handler.
050 *
051 */
052public class CmsEditorChangeHandlerFormatterSelection extends A_CmsXmlContentEditorChangeHandler {
053
054    /** The logger instance for this class. */
055    private static final Log LOG = CmsLog.getLog(A_CmsFormatterWidget.class);
056
057    /** XPath to the main node of the formatters to remove subnodes. */
058    private static final String REMOVE_PATH = "/RemoveFormatters";
059    /** XPath to the main node of the formatters to add subnodes. */
060    private static final String ADD_PATH = "/AddFormatters";
061    /** XPath to the node of a formatter to add (without number of the sequence). */
062    private static final String ADD_PATH_SINGLE_NODE = ADD_PATH + "/AddFormatter";
063
064    /**
065     * Adjusts the added/removed formatters if "Remove all formatters" is checked/unchecked.
066     *
067     * @see org.opencms.xml.content.I_CmsXmlContentEditorChangeHandler#handleChange(org.opencms.file.CmsObject, org.opencms.xml.content.CmsXmlContent, java.util.Locale, java.util.Collection)
068     */
069    public CmsXmlContent handleChange(
070        final CmsObject cms,
071        final CmsXmlContent content,
072        final Locale locale,
073        final Collection<String> changedPaths) {
074
075        if ((changedPaths.size() != 1)
076            && content.getValue(changedPaths.iterator().next(), locale).getTypeName().equals(
077                CmsXmlBooleanValue.TYPE_NAME)) {
078            LOG.error(
079                Messages.get().container(
080                    Messages.ERROR_CONFIGURATION_EDITOR_CHANGE_HANDLER_FORMATTER_SELECTION_1,
081                    content.getContentDefinition().getSchemaLocation()));
082            return content;
083        }
084
085        String removeAllPath = changedPaths.iterator().next();
086        String removeAllStringValue = content.getStringValue(cms, removeAllPath, locale);
087        boolean removeAll = Boolean.valueOf(removeAllStringValue).booleanValue();
088        if (removeAll && content.hasValue(REMOVE_PATH, locale)) {
089            content.removeValue(REMOVE_PATH, locale, 0);
090        } else if (content.hasValue(ADD_PATH, locale)) {
091            String rootPath = content.getFile().getRootPath();
092            List<String> optionValues = CmsAddFormatterWidget.getSelectOptionValues(cms, rootPath, false);
093            CmsXmlContentValueSequence addSequence = content.getValueSequence(ADD_PATH_SINGLE_NODE, locale);
094            List<I_CmsXmlContentValue> values = addSequence.getValues();
095            boolean removeMainAddNode = true;
096            for (int i = values.size() - 1; i >= 0; i--) {
097                if (optionValues.contains(values.get(i).getStringValue(cms))) {
098                    removeMainAddNode = false;
099                } else {
100                    content.removeValue(ADD_PATH_SINGLE_NODE, locale, i);
101                }
102            }
103            if (removeMainAddNode) {
104                content.removeValue(ADD_PATH, locale, 0);
105            }
106        }
107        return content;
108    }
109
110}