001/*
002 *  Copyright 2018 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.runtime.config;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020import org.apache.avalon.framework.service.ServiceManager;
021
022import org.ametys.runtime.model.CategorizedElementDefinitionParser;
023import org.ametys.runtime.model.ElementDefinition;
024import org.ametys.runtime.model.ElementDefinitionParser;
025import org.ametys.runtime.model.Model;
026import org.ametys.runtime.model.ModelItemGroup;
027
028/**
029 * Parser for configuration parameter wrappers
030 */
031public class ConfigParameterDefinitionWrapperParser extends CategorizedElementDefinitionParser
032{
033    /**
034     * Creates a configuration parameter definition parser.
035     * @param parser the element definition parser
036     */
037    public ConfigParameterDefinitionWrapperParser(ElementDefinitionParser parser)
038    {
039        super(parser);
040    }
041    
042    @Override
043    public ConfigParameterDefinitionWrapper parse(ServiceManager serviceManager, String pluginName, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException
044    {
045        ConfigParameterDefinitionWrapper wrapper = (ConfigParameterDefinitionWrapper) super.parse(serviceManager, pluginName, definitionConfig, model, parent);
046        
047        wrapper.setGroupSwitch(_isGroupSwitch(definitionConfig));
048        _setGroupSwitchDefaultValue(definitionConfig, wrapper.getDefinition());
049        
050        return wrapper;
051    }
052    
053    @Override
054    protected ConfigParameterDefinitionWrapper _createElementDefinitionWrapper()
055    {
056        return new ConfigParameterDefinitionWrapper();
057        
058    }
059    
060    @SuppressWarnings("unchecked")
061    private void _setGroupSwitchDefaultValue(Configuration definitionConfig, ElementDefinition definition)
062    {
063        Object defaultValue = definition.getDefaultValue();
064        if (_isGroupSwitch(definitionConfig) && defaultValue == null)
065        {
066            definition.setDefaultValue(Boolean.FALSE);
067        }
068    }
069    
070    private boolean _isGroupSwitch(Configuration definitionConfig)
071    {
072        return definitionConfig.getAttributeAsBoolean("group-switch", false);
073    }
074}