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.core.util;
017
018import java.util.LinkedHashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.runtime.model.DefinitionAndValue;
028import org.ametys.runtime.model.ElementDefinition;
029import org.ametys.runtime.model.ModelHelper;
030import org.ametys.runtime.model.type.ElementType;
031import org.ametys.runtime.model.type.xml.XMLElementType;
032import org.ametys.runtime.plugin.component.AbstractLogEnabled;
033
034/**
035 * Helper to read XML data
036 */
037public class ReadXMLDataHelper extends AbstractLogEnabled implements Component
038{
039    /** The avalon role */
040    public static final String ROLE = ReadXMLDataHelper.class.getName();
041    
042    /**
043     * Read data from the given {@link Configuration} and validate the values
044     * @param configuration the configuration
045     * @param prefix the defintion's prefix to use
046     * @param definitions the definitions of the data to retrieve
047     * @param allErrors a map to store all validation errors on read data
048     * @return the values, indexed by name
049     * @throws ConfigurationException if an error occurs while reading a value or if the value is not valid
050     */
051    public Map<String, Object> readAndValidateXMLData(Configuration configuration, String prefix, Map<String, ? extends ElementDefinition> definitions, Map<String, List<I18nizableText>> allErrors) throws ConfigurationException
052    {
053        Map<String, DefinitionAndValue> definitionAndValues = new LinkedHashMap<>();
054        for (String dataName : definitions.keySet())
055        {
056            ElementDefinition definition = definitions.get(dataName);
057            ElementType type = definition.getType();
058            if (type instanceof XMLElementType xmlElementType)
059            {
060                Object value = xmlElementType.read(configuration, dataName);
061                definitionAndValues.put(prefix + dataName, new DefinitionAndValue<>(null, definition,  value));
062            }
063        }
064        
065        Map<String, Object> xmlValues = new LinkedHashMap<>();
066        for (String dataName : definitions.keySet())
067        {
068            ElementDefinition definition = definitions.get(dataName);
069            boolean isDisabled = ModelHelper.evaluateDisableConditions(definition.getDisableConditions(), definitionAndValues, getLogger());
070            
071            Object value = definitionAndValues.get(prefix + dataName).getValue();
072            if (!isDisabled)
073            {
074                List<I18nizableText> errors = ModelHelper.validateValue(definition, value);
075                if (errors.isEmpty())
076                {
077                    xmlValues.put(dataName, value);
078                }
079                else
080                {
081                    allErrors.put(dataName, errors);
082                }
083            }
084            else
085            {
086                xmlValues.put(dataName, value);
087            }
088        }
089        
090        return xmlValues;
091    }
092}