001/*
002 *  Copyright 2023 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.contenttype;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020import org.apache.commons.lang3.StringUtils;
021
022import org.ametys.runtime.config.Config;
023import org.ametys.runtime.config.ConfigManager;
024import org.ametys.runtime.model.ModelItem;
025import org.ametys.runtime.model.type.ModelItemTypeConstants;
026
027/**
028 * Content type overrides, based on a configuration parameter of type boolean.
029 * The overrides of these extensions are active only if the configuration parameter value is <code>true</code>
030 */
031public class ConfigBasedContentTypeOverrides extends StaticContentTypeOverrides
032{
033    @Override
034    public void configure(Configuration configuration) throws ConfigurationException
035    {
036        Configuration configParamConfiguration = configuration.getChild("config-param");
037        String configParamId = configParamConfiguration.getValue();
038        
039        ConfigManager configManager = ConfigManager.getInstance();
040        boolean isActive;
041        if (configManager.hasModelItem(configParamId))
042        {
043            ModelItem configParam = ConfigManager.getInstance().getModelItem(configParamId);
044            if (!ModelItemTypeConstants.BOOLEAN_TYPE_ID.equals(configParam.getType().getId()))
045            {
046                throw new ConfigurationException("Unable to override content types according the the configuration parameter '" + configParamId + "'. This parameter is not a boolean", configParamConfiguration);
047            }
048            
049            isActive = Config.getInstance().getValue(configParamId);
050        }
051        else
052        {
053            // If the config parameter does not exist, log a warning but do not throw an exception, it could be on purpose (ex: an excluded feature)
054            getLogger().warn("Unable to add some override configuration for content types '" + StringUtils.join(_overriddenContentTypeIds, ", ") + ". The configuration parameter '" + configParamId + "' is not defined.");
055            isActive = false;
056        }
057        
058        if (isActive)
059        {
060            super.configure(configuration);
061        }
062    }
063}