Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe Commits: 757dd93a by Tony Chemit at 2020-07-06T08:53:15+02:00 Revoir la gestion des validateurs taille-poids d'espèces - Closes #1573 (report v8) - - - - - 4 changed files: - services-validation/pom.xml - services-validation/src/main/java/fr/ird/observe/validation/validators/AbstractSpeciesFieldDtoValidator.java - services-validation/src/main/java/fr/ird/observe/validation/validators/SpeciesLengthFieldDtoValidator.java - services-validation/src/main/java/fr/ird/observe/validation/validators/SpeciesWeightFieldDtoValidator.java Changes: ===================================== services-validation/pom.xml ===================================== @@ -101,10 +101,6 @@ <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> </dependency> - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-lang3</artifactId> - </dependency> <dependency> <groupId>com.google.guava</groupId> ===================================== services-validation/src/main/java/fr/ird/observe/validation/validators/AbstractSpeciesFieldDtoValidator.java ===================================== @@ -27,9 +27,6 @@ import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport; import fr.ird.observe.dto.referential.common.SpeciesDto; import fr.ird.observe.dto.referential.common.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; import java.util.Objects; @@ -61,88 +58,56 @@ import java.util.Objects; */ @SuppressWarnings("unused") 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; - 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; - } - } + protected abstract Float getBoundMin(SpeciesDto species); - return answer; - } + protected abstract Float getBoundMax(SpeciesDto species); @Override public void validate(Object object) throws ValidationException { @@ -156,9 +121,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); @@ -167,54 +131,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); - - if (log.isDebugEnabled()) { - log.debug("Bound : " + bound); - log.debug("Ratio to validate : " + ratio); - log.debug("Bound with ratio : " + boundWithRatio); - } + computedBound = bound.applyRatio(ratio); + log.debug(String.format("Bound with ratio : %s", computedBound)); - boolean valid = validateBound(data, boundWithRatio); + boolean valid = computedBound.validate(data); if (!valid) { @@ -222,61 +178,117 @@ public abstract class AbstractSpeciesFieldDtoValidator extends FieldValidatorSup } } + 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 void setEnable(String enable) { + this.enable = Boolean.parseBoolean(Objects.requireNonNull(enable)); + } + public Float getMin() { - return Numbers.roundThreeDigits(boundWithRatio.getMin()); + return computedBound == null ? null : Numbers.roundThreeDigits(computedBound.getMin()); } public Float getMax() { - return Numbers.roundThreeDigits(boundWithRatio.getMax()); + return computedBound == null ? null : Numbers.roundThreeDigits(computedBound.getMax()); } - private Bound getBound(SpeciesDto species) { + private Boolean getValidationLengthWeightEnable(Object object) throws ValidationException { + if (validationLengthWeightEnable == null) { + validationLengthWeightEnable = (Boolean) getFieldValue("validationLengthWeightEnable", object); + } + return validationLengthWeightEnable; + } - Float min = getBoundMin(species); - Float max = getBoundMax(species); + private boolean shouldValidate(Object object) throws ValidationException { + boolean shouldValidate = shouldValidateFromEnable(object); + if (!shouldValidate) { + log.debug("Skip speed validation from 'validationLengthWeightEnable'"); + } else { + shouldValidate = shouldValidateFromExpression(object); - 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; + if (!shouldValidate) { + log.debug("Skip speed validation from 'expression'"); + } } - return new Bound(min, max); + return shouldValidate; } - private boolean validateBound(Float value, Bound bound) { - if (value == null) { + 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; + } - // valeur non définie - return true; + 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; + } } - boolean valid; + return answer; + } - float min = bound.getMin(); - float max = bound.getMax(); + private Bound getBound(SpeciesDto species) { + + Float min = getBoundMin(species); + Float max = getBoundMax(species); - valid = min <= value && value <= max; - return valid; + 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; ===================================== services-validation/src/main/java/fr/ird/observe/validation/validators/SpeciesLengthFieldDtoValidator.java ===================================== @@ -33,13 +33,13 @@ import fr.ird.observe.dto.referential.common.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 ===================================== services-validation/src/main/java/fr/ird/observe/validation/validators/SpeciesWeightFieldDtoValidator.java ===================================== @@ -33,13 +33,13 @@ import fr.ird.observe.dto.referential.common.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/757dd93a10d19e99f7984ad2a9... -- View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/commit/757dd93a10d19e99f7984ad2a9... You're receiving this email because of your account on gitlab.com.
participants (1)
-
Tony CHEMIT