001/*
002 *  Copyright 2014 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.parameter.AbstractParameterParser;
023import org.ametys.runtime.parameter.Enumerator;
024import org.ametys.runtime.parameter.ParameterHelper;
025import org.ametys.runtime.parameter.ParameterHelper.ParameterType;
026import org.ametys.runtime.parameter.Validator;
027import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
028
029/**
030 * This class parses the configuration parameters to help SAX them later.
031 */
032public class ConfigParameterParser extends AbstractParameterParser<ConfigParameter, ParameterType>
033{
034    /**
035     * The configuration parameter parser constructor.
036     * @param enumeratorManager The manager for enumarators
037     * @param validatorManager The manager for validators
038     */
039    public ConfigParameterParser(ThreadSafeComponentManager<Enumerator> enumeratorManager, ThreadSafeComponentManager<Validator> validatorManager)
040    {
041        super(enumeratorManager, validatorManager);
042    }
043
044    @Override
045    protected ConfigParameter _createParameter(Configuration parameterConfig) throws ConfigurationException
046    {
047        return new ConfigParameter();
048    }
049    
050    @Override
051    protected String _parseId(Configuration parameterConfig) throws ConfigurationException
052    {
053        return parameterConfig.getAttribute("id");
054    }
055    
056    @Override
057    protected ParameterType _parseType(Configuration parameterConfig) throws ConfigurationException
058    {
059        try
060        {
061            return ParameterType.valueOf(parameterConfig.getAttribute("type").toUpperCase());
062        }
063        catch (IllegalArgumentException e)
064        {
065            throw new ConfigurationException("Invalid type", parameterConfig, e);
066        }
067    }
068    
069    @Override
070    protected Object _parseDefaultValue(Configuration parameterConfig, ConfigParameter parameter)
071    {
072        String value;
073        
074        Configuration childNode = parameterConfig.getChild("default-value", false);
075        if (childNode == null)
076        {
077            value = null;
078        }
079        else
080        {
081            value = childNode.getValue("");
082        }
083        
084        return ParameterHelper.castValue(value, parameter.getType());
085    }
086    
087    /**
088     * Parses the disable condition.
089     * @param disableConditionConfiguration the configuration of the disable condition
090     * @return result the parsed disable condition to be converted in JSON
091     * @throws ConfigurationException if an error occurred
092     */
093    protected DisableConditions _parseDisableConditions(Configuration disableConditionConfiguration) throws ConfigurationException
094    {
095        if (disableConditionConfiguration == null)
096        {
097            return null;
098        }
099         
100        DisableConditions conditions = new DisableConditions();
101
102        Configuration[] conditionsConfiguration = disableConditionConfiguration.getChildren();
103        for (Configuration conditionConfiguration : conditionsConfiguration)
104        {
105            String tagName = conditionConfiguration.getName();
106            
107            // Recursive case
108            if (tagName.equals("conditions"))
109            {
110                conditions.getSubConditions().add(_parseDisableConditions(conditionConfiguration));
111            }
112            else if (tagName.equals("condition"))
113            {
114                String id = conditionConfiguration.getAttribute("id");
115                DisableCondition.OPERATOR operator = DisableCondition.OPERATOR.valueOf(conditionConfiguration.getAttribute("operator", "eq").toUpperCase());
116                String value = conditionConfiguration.getValue("");
117                
118                
119                DisableCondition condition = new DisableCondition(id, operator, value);
120                conditions.getConditions().add(condition);
121            }
122        }
123        
124        conditions.setAssociation(DisableConditions.ASSOCIATION_TYPE.valueOf(disableConditionConfiguration.getAttribute("type", "and").toUpperCase()));
125        
126        return conditions;
127    }
128    
129    @Override
130    protected void _additionalParsing(ServiceManager manager, String pluginName, Configuration parameterConfig, String parameterId, ConfigParameter parameter) throws ConfigurationException
131    {
132        super._additionalParsing(manager, pluginName, parameterConfig, parameterId, parameter);
133        
134        parameter.setId(parameterId);
135        parameter.setDisplayCategory(_parseI18nizableText(parameterConfig, pluginName, "category"));
136        parameter.setDisplayGroup(_parseI18nizableText(parameterConfig, pluginName, "group"));
137        parameter.setGroupSwitch(parameterConfig.getAttributeAsBoolean("group-switch", false));
138        parameter.setOrder(parameterConfig.getChild("order").getValueAsLong(0));
139        parameter.setDisableConditions(_parseDisableConditions(parameterConfig.getChild("disable-conditions", false)));
140    }
141}