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