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.HashMap; 019import java.util.LinkedHashMap; 020import java.util.List; 021import java.util.Map; 022import java.util.Optional; 023 024import org.apache.avalon.framework.component.Component; 025import org.apache.avalon.framework.configuration.Configuration; 026import org.apache.avalon.framework.configuration.ConfigurationException; 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.avalon.framework.service.Serviceable; 030 031import org.ametys.runtime.i18n.I18nizableText; 032import org.ametys.runtime.model.ElementDefinition; 033import org.ametys.runtime.model.ModelHelper; 034import org.ametys.runtime.model.disableconditions.DisableConditionsEvaluator; 035import org.ametys.runtime.model.type.ElementType; 036import org.ametys.runtime.model.type.xml.XMLElementType; 037import org.ametys.runtime.parameter.ValidationResult; 038import org.ametys.runtime.plugin.component.AbstractLogEnabled; 039 040/** 041 * Helper to read data for users and groups 042 */ 043public class ReadUsersAndGroupsDataHelper extends AbstractLogEnabled implements Component, Serviceable 044{ 045 /** The avalon role */ 046 public static final String ROLE = ReadUsersAndGroupsDataHelper.class.getName(); 047 048 /** The disable condition evaluator */ 049 protected DisableConditionsEvaluator<Void> _disableConditionsEvaluator; 050 051 @SuppressWarnings("unchecked") 052 public void service(ServiceManager manager) throws ServiceException 053 { 054 _disableConditionsEvaluator = (DisableConditionsEvaluator<Void>) manager.lookup(UsersAndGroupsDataDisableConditionsEvaluator.ROLE); 055 } 056 057 /** 058 * Read data from the given {@link Configuration} and validate the values 059 * @param configuration the configuration 060 * @param prefix the definition's prefix to use 061 * @param definitions the definitions of the data to retrieve 062 * @param allErrors a map to store all validation errors on read data 063 * @return the values, indexed by name 064 * @throws ConfigurationException if an error occurs while reading a value or if the value is not valid 065 */ 066 public Map<String, Object> readAndValidateXMLData(Configuration configuration, String prefix, Map<String, ? extends ElementDefinition> definitions, Map<String, List<I18nizableText>> allErrors) throws ConfigurationException 067 { 068 Map<String, Object> values = new LinkedHashMap<>(); 069 for (String dataName : definitions.keySet()) 070 { 071 ElementDefinition definition = definitions.get(dataName); 072 ElementType type = definition.getType(); 073 if (type instanceof XMLElementType xmlElementType) 074 { 075 Object value = xmlElementType.read(configuration, dataName); 076 values.put(prefix + dataName, value); 077 } 078 } 079 080 Map<String, Object> contextualParametersForDisableConditions = new HashMap<>(); 081 contextualParametersForDisableConditions.put(UsersAndGroupsDataDisableConditionsEvaluator.DEFINITIONS_PARAMETER_KEY, definitions); 082 contextualParametersForDisableConditions.put(UsersAndGroupsDataDisableConditionsEvaluator.PREFIX_PARAMETER_KEY, prefix); 083 Map<String, Object> xmlValues = new LinkedHashMap<>(); 084 for (String dataName : definitions.keySet()) 085 { 086 ElementDefinition definition = definitions.get(dataName); 087 boolean isDisabled = _disableConditionsEvaluator.evaluateDisableConditions(definition, definition.getName(), Optional.empty(), values, null, contextualParametersForDisableConditions); 088 089 Object value = values.get(prefix + dataName); 090 if (!isDisabled) 091 { 092 ValidationResult validationResult = ModelHelper.validateValue(definition, value); 093 if (!validationResult.hasErrors()) 094 { 095 xmlValues.put(dataName, value); 096 } 097 else 098 { 099 allErrors.put(dataName, validationResult.getErrors()); 100 } 101 } 102 else 103 { 104 xmlValues.put(dataName, value); 105 } 106 } 107 108 return xmlValues; 109 } 110}