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.cms.contenttype.validation;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023
024import org.ametys.cms.contenttype.ContentValidator;
025import org.ametys.cms.contenttype.MetadataDefinition;
026import org.ametys.cms.contenttype.MetadataType;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.parameter.Errors;
029
030/**
031 * Base {@link ContentValidator} validating the content by comparing one or more couples of numerical values.
032 * For each couple of values, the identified max value should be greater than or equal to the identified min value.
033 * 
034 * The max and min values to compare are identified in validator's configuration by the metadata path as follows:
035 * <min path="path/to/metadata/min"/>
036 * <max path="path/to/metadata/max"/>
037 * 
038 * If the path of metadata can contain one or more repeaters.
039 * 
040 * @param <T> The numeric type tested by the validator.
041 */
042public abstract class AbstractNumberIntervalValidator<T extends Number> extends AbstractIntervalValidator<T>
043{
044    @Override
045    protected String configureMinPath(Configuration configuration) throws ConfigurationException
046    {
047        return configuration.getChild("min").getAttribute("path");
048    }
049    
050    @Override
051    protected String configureMaxPath(Configuration configuration) throws ConfigurationException
052    {
053        return configuration.getChild("max").getAttribute("path");
054    }
055    
056    @Override
057    protected boolean isSupportedType(MetadataType type)
058    {
059        return type == MetadataType.DOUBLE || type == MetadataType.LONG;
060    }
061    
062    @Override
063    protected void addIntervalError(Errors errors, MetadataDefinition minDefinition, MetadataDefinition maxDefinition, T min, T max)
064    {
065        Map<String, I18nizableText> i18nParams = new HashMap<>();
066        i18nParams.put("minValue", new I18nizableText(min.toString()));
067        i18nParams.put("maxValue", new I18nizableText(max.toString()));
068        i18nParams.put("minLabel", minDefinition.getLabel());
069        i18nParams.put("maxLabel", maxDefinition.getLabel());
070        errors.addError(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_VALIDATOR_NUMBER_INTERVAL_ERROR", i18nParams));
071    }
072
073    @Override
074    protected void addErrorEmpty(Errors errors, MetadataDefinition minDefinition, MetadataDefinition maxDefinition, I18nizableText emptyLabel)
075    {
076        Map<String, I18nizableText> i18nParams = new HashMap<>();
077        i18nParams.put("emptyLabel", emptyLabel);
078        i18nParams.put("minLabel", minDefinition.getLabel());
079        i18nParams.put("maxLabel", maxDefinition.getLabel());
080        errors.addError(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_VALIDATOR_NUMBER_INTERVAL_NULL_ERROR", i18nParams));
081    }
082}