Tony CHEMIT pushed to branch develop-7.x at ultreiaio / ird-observe Commits: 0212a530 by Tony Chemit at 2020-07-06T08:11:04+02:00 Revoir la gestion des validateurs taille-poids d'espèces - Closes #1573 - - - - - 3 changed files: - dto/src/main/java/fr/ird/observe/validation/validators/AbstractSpeciesFieldDtoValidator.java - dto/src/main/java/fr/ird/observe/validation/validators/SpeciesLengthFieldDtoValidator.java - dto/src/main/java/fr/ird/observe/validation/validators/SpeciesWeightFieldDtoValidator.java Changes: ===================================== dto/src/main/java/fr/ird/observe/validation/validators/AbstractSpeciesFieldDtoValidator.java ===================================== @@ -27,7 +27,6 @@ import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport; import fr.ird.observe.dto.referential.SpeciesDto; import fr.ird.observe.dto.referential.SpeciesReference; import io.ultreia.java4all.lang.Numbers; -import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -61,87 +60,57 @@ import java.util.Objects; */ public abstract class AbstractSpeciesFieldDtoValidator extends FieldValidatorSupport { - /** Logger. */ private static final Logger log = LogManager.getLogger(AbstractSpeciesFieldDtoValidator.class); /** - * Une expression qui si elle est remplie doit être vérifié avant de faire - * la validation par borne, si l'expression n'est pas vérifiée, alors - * le test sur les borne n'est pas effectué. + * Optional expression to enable of not this validator. + * <p> + * If set to not {@code null}, then will be enable only if evaluation of thi expression is true. + * Otherwise will always try to validate. * + * @see #shouldValidateFromExpression(Object) * @since 2.3 */ protected String expression; + /** + * If set to not {@code null}, then will be enable only if {@link #validationLengthWeightEnable} is set the exact same value. + * Otherwise will always try to validate. + * + * @see #validationLengthWeightEnable + * @see #shouldValidateFromEnable(Object) + */ private Boolean enable; - /** le ratio a appliquer sur les bornes définies dans le référentiel */ + + /** + * Loaded from validation context. + * Permits to enable or not this validator. + * + * @see #enable + * @see #shouldValidateFromEnable(Object) + */ + private transient Boolean validationLengthWeightEnable; + + /** + * Ration to apply to range of species value. + */ private Float ratio; + /** + * Field name where to find species value. + */ private String speciesField = "species"; + /** + * Bound found from species + * + * @see SpeciesDto + */ private Bound bound; - private Bound boundWithRatio; - - private String getSpeciesField() { - return speciesField; - } - - public void setSpeciesField(String speciesField) { - this.speciesField = speciesField; - } - - public void setRatio(String ratio) { - this.ratio = Float.parseFloat(Objects.requireNonNull(ratio)); - } - - public void setExpression(String expression) { - this.expression = expression; - } - - public boolean isEnable() { - return enable; - } - - public void setEnable(String enable) { - this.enable = Boolean.parseBoolean(Objects.requireNonNull(enable)); - } - - protected abstract Float getBoundMin(SpeciesDto referentiel); - - protected abstract Float getBoundMax(SpeciesDto referentiel); - - public Bound getBound() { - return bound; - } - - private boolean shouldValidate(Object object) throws ValidationException { - - Boolean enable = (Boolean) getFieldValue("validationLengthWeightEnable", object); - if (enable != null && this.enable != null) { - - if (!Objects.equals(this.enable, enable)) { - - if (log.isDebugEnabled()) { - log.debug("Skip speed validation"); - } - return false; - } + /** + * Computed bound with {@link #ratio} applied to {@link #bound}. + */ + private Bound computedBound; - } + protected abstract Float getBoundMin(SpeciesDto species); - Boolean answer = true; - if (StringUtils.isNotEmpty(expression)) { - try { - answer = (Boolean) getFieldValue(expression, object); - } catch (ValidationException e) { - throw e; - } catch (Exception e) { - // let this pass, but it will be logged right below - if (log.isErrorEnabled()) { - log.error("Could not get expression: " + expression); - } - answer = true; - } - } - - return answer; - } + protected abstract Float getBoundMax(SpeciesDto species); @Override public void validate(Object object) throws ValidationException { @@ -155,9 +124,8 @@ public abstract class AbstractSpeciesFieldDtoValidator extends FieldValidatorSup throw new ValidationException("No parameter 'fieldName' filled"); } - String speciesFieldName = getSpeciesField(); - if (speciesFieldName == null) { - throw new ValidationException("No parameter 'speciesFieldName' filled"); + if (speciesField == null) { + throw new ValidationException("No parameter 'speciesField' filled"); } boolean shouldValidate = shouldValidate(object); @@ -166,54 +134,46 @@ public abstract class AbstractSpeciesFieldDtoValidator extends FieldValidatorSup return; } - // la donnee a valider + // data to validate Object value = getFieldValue(fieldName, object); Float data = value == null ? null : Float.valueOf(String.valueOf(value)); if (data == null) { - // la donnee a valider n'est pas définie + // no data found, can not validate + log.debug("No data found, skip validate."); return; } - if (log.isDebugEnabled()) { - log.debug("data to validate : " + data); - } + log.debug(String.format("data to validate : %s", data)); - SpeciesReference speciesRef = (SpeciesReference) getFieldValue(speciesFieldName, object); + SpeciesReference speciesRef = (SpeciesReference) getFieldValue(speciesField, object); if (speciesRef == null) { - // pas de species trouvée, on ne peut pas valider + // no species found, can not validate + log.debug("No species found, skip validate."); return; } - if (log.isDebugEnabled()) { - log.debug("Species to validate : " + speciesRef); - } + log.debug(String.format("Species to validate : %s", speciesRef)); SpeciesDto speciesDto = (SpeciesDto) stack.findValue("getSpecies(\"" + speciesRef.getId() + "\")"); bound = getBound(speciesDto); - if (log.isDebugEnabled()) { - log.debug("Species Bound to validate : " + bound); - } + log.debug(String.format("Species Bound to validate : %s", bound)); if (bound == null) { - // pas de donnée dans le référentiel acceptable + // no bound found, can not validate + log.debug("No species bound found, skip validate."); return; } - boundWithRatio = bound.applyRatio(ratio); + computedBound = bound.applyRatio(ratio); + log.debug(String.format("Bound with ratio : %s", computedBound)); - if (log.isDebugEnabled()) { - log.debug("Bound : " + bound); - log.debug("Ratio to validate : " + ratio); - log.debug("Bound with ratio : " + boundWithRatio); - } - - boolean valid = validateBound(data, boundWithRatio); + boolean valid = computedBound.validate(data); if (!valid) { @@ -221,61 +181,117 @@ public abstract class AbstractSpeciesFieldDtoValidator extends FieldValidatorSup } } - private Bound getBound(SpeciesDto species) { + public void setSpeciesField(String speciesField) { + this.speciesField = speciesField; + } - Float min = getBoundMin(species); - Float max = getBoundMax(species); + public void setRatio(String ratio) { + this.ratio = Float.parseFloat(Objects.requireNonNull(ratio)); + } - if (min == null || min == 0 || max == null || max == 0) { - // l'une des deux borne n'est pas définie, on ne peut pas utiliser - // la données - return null; - } - return new Bound(min, max); + public void setExpression(String expression) { + this.expression = expression; + } + + public void setEnable(String enable) { + this.enable = Boolean.parseBoolean(Objects.requireNonNull(enable)); } - private boolean validateBound(Float value, Bound bound) { - if (value == null) { + public Float getMin() { + return computedBound == null ? null : Numbers.roundThreeDigits(computedBound.getMin()); + } - // valeur non définie - return true; + public Float getMax() { + return computedBound == null ? null : Numbers.roundThreeDigits(computedBound.getMax()); + } + + private Boolean getValidationLengthWeightEnable(Object object) throws ValidationException { + if (validationLengthWeightEnable == null) { + validationLengthWeightEnable = (Boolean) getFieldValue("validationLengthWeightEnable", object); } - boolean valid; + return validationLengthWeightEnable; + } + + private boolean shouldValidate(Object object) throws ValidationException { + boolean shouldValidate = shouldValidateFromEnable(object); + if (!shouldValidate) { + log.debug("Skip speed validation from 'validationLengthWeightEnable'"); + } else { + shouldValidate = shouldValidateFromExpression(object); - float min = bound.getMin(); - float max = bound.getMax(); + if (!shouldValidate) { + log.debug("Skip speed validation from 'expression'"); + } + } + return shouldValidate; + } - valid = min <= value && value <= max; - return valid; + private boolean shouldValidateFromEnable(Object object) throws ValidationException { + boolean answer = true; + if (this.enable != null) { + Boolean enable; + try { + enable = getValidationLengthWeightEnable(object); + } catch (ValidationException e) { + // let this pass, but it will be logged right below + log.error("Could not get expression: validationLengthWeightEnable", e); + throw e; + } + answer = Objects.equals(this.enable, enable); + } + return answer; } - public Float getMin() { - return boundWithRatio == null ? null : Numbers.roundThreeDigits(boundWithRatio.getMin()); + private boolean shouldValidateFromExpression(Object object) throws ValidationException { + boolean answer = true; + if (this.expression != null && !this.expression.isEmpty()) { + try { + Boolean answerObject = (Boolean) getFieldValue(expression, object); + answer = answerObject != null && answerObject; + } catch (ValidationException e) { + // let this pass, but it will be logged right below + log.error(String.format("Could not get expression: %s", expression), e); + throw e; + } + } + return answer; } - public Float getMax() { - return boundWithRatio == null ? null : Numbers.roundThreeDigits(boundWithRatio.getMax()); + private Bound getBound(SpeciesDto species) { + + Float min = getBoundMin(species); + Float max = getBoundMax(species); + + if (min == null || min == 0 || max == null || max == 0) { + // one of range is missing, can not use this bound + return null; + } + return new Bound(min, max); } public static class Bound { - private final Float min; + private final float min; - private final Float max; + private final float max; - Bound(Float min, Float max) { + Bound(float min, float max) { this.min = min; this.max = max; } - public Float getMin() { + public float getMin() { return min; } - public Float getMax() { + public float getMax() { return max; } + boolean validate(float value) { + return min <= value && value <= max; + } + Bound applyRatio(float ratio) { float delta = min / 100 * ratio; float min = this.min - delta; ===================================== dto/src/main/java/fr/ird/observe/validation/validators/SpeciesLengthFieldDtoValidator.java ===================================== @@ -33,13 +33,13 @@ import fr.ird.observe.dto.referential.SpeciesDto; public class SpeciesLengthFieldDtoValidator extends AbstractSpeciesFieldDtoValidator { @Override - protected Float getBoundMin(SpeciesDto referentiel) { - return referentiel.getMinLength(); + protected Float getBoundMin(SpeciesDto species) { + return species.getMinLength(); } @Override - protected Float getBoundMax(SpeciesDto referentiel) { - return referentiel.getMaxLength(); + protected Float getBoundMax(SpeciesDto species) { + return species.getMaxLength(); } @Override ===================================== dto/src/main/java/fr/ird/observe/validation/validators/SpeciesWeightFieldDtoValidator.java ===================================== @@ -33,13 +33,13 @@ import fr.ird.observe.dto.referential.SpeciesDto; public class SpeciesWeightFieldDtoValidator extends AbstractSpeciesFieldDtoValidator { @Override - protected Float getBoundMin(SpeciesDto referentiel) { - return referentiel.getMinWeight(); + protected Float getBoundMin(SpeciesDto species) { + return species.getMinWeight(); } @Override - protected Float getBoundMax(SpeciesDto referentiel) { - return referentiel.getMaxWeight(); + protected Float getBoundMax(SpeciesDto species) { + return species.getMaxWeight(); } @Override View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/commit/0212a530e1f6c9a86713a9d28f... -- View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/commit/0212a530e1f6c9a86713a9d28f... You're receiving this email because of your account on gitlab.com.