Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe

Commits:

4 changed files:

Changes:

  • services-validation/pom.xml
    ... ... @@ -101,10 +101,6 @@
    101 101
           <groupId>org.apache.logging.log4j</groupId>
    
    102 102
           <artifactId>log4j-api</artifactId>
    
    103 103
         </dependency>
    
    104
    -    <dependency>
    
    105
    -      <groupId>org.apache.commons</groupId>
    
    106
    -      <artifactId>commons-lang3</artifactId>
    
    107
    -    </dependency>
    
    108 104
     
    
    109 105
         <dependency>
    
    110 106
           <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;
    27 27
     import fr.ird.observe.dto.referential.common.SpeciesDto;
    
    28 28
     import fr.ird.observe.dto.referential.common.SpeciesReference;
    
    29 29
     import io.ultreia.java4all.lang.Numbers;
    
    30
    -import org.apache.commons.lang3.StringUtils;
    
    31
    -import org.apache.logging.log4j.LogManager;
    
    32
    -import org.apache.logging.log4j.Logger;
    
    33 30
     
    
    34 31
     import java.util.Objects;
    
    35 32
     
    
    ... ... @@ -61,88 +58,56 @@ import java.util.Objects;
    61 58
      */
    
    62 59
     @SuppressWarnings("unused")
    
    63 60
     public abstract class AbstractSpeciesFieldDtoValidator extends FieldValidatorSupport {
    
    64
    -
    
    65
    -    /** Logger. */
    
    66
    -    private static final Logger log = LogManager.getLogger(AbstractSpeciesFieldDtoValidator.class);
    
    67 61
         /**
    
    68
    -     * Une expression qui si elle est remplie doit être vérifié avant de faire
    
    69
    -     * la validation par borne, si l'expression n'est pas vérifiée, alors
    
    70
    -     * le test sur les borne n'est pas effectué.
    
    62
    +     * Optional expression to enable of not this validator.
    
    63
    +     * <p>
    
    64
    +     * If set to not {@code null}, then will be enable only if evaluation of thi expression is true.
    
    65
    +     * Otherwise will always try to validate.
    
    71 66
          *
    
    67
    +     * @see #shouldValidateFromExpression(Object)
    
    72 68
          * @since 2.3
    
    73 69
          */
    
    74 70
         protected String expression;
    
    71
    +    /**
    
    72
    +     * If set to not {@code null}, then will be enable only if {@link #validationLengthWeightEnable} is set the exact same value.
    
    73
    +     * Otherwise will always try to validate.
    
    74
    +     *
    
    75
    +     * @see #validationLengthWeightEnable
    
    76
    +     * @see #shouldValidateFromEnable(Object)
    
    77
    +     */
    
    75 78
         private Boolean enable;
    
    76
    -    /** le ratio a appliquer sur les bornes définies dans le référentiel */
    
    79
    +
    
    80
    +    /**
    
    81
    +     * Loaded from validation context.
    
    82
    +     * Permits to enable or not this validator.
    
    83
    +     *
    
    84
    +     * @see #enable
    
    85
    +     * @see #shouldValidateFromEnable(Object)
    
    86
    +     */
    
    87
    +    private transient Boolean validationLengthWeightEnable;
    
    88
    +
    
    89
    +    /**
    
    90
    +     * Ration to apply to range of species value.
    
    91
    +     */
    
    77 92
         private Float ratio;
    
    93
    +    /**
    
    94
    +     * Field name where to find species value.
    
    95
    +     */
    
    78 96
         private String speciesField = "species";
    
    97
    +    /**
    
    98
    +     * Bound found from species
    
    99
    +     *
    
    100
    +     * @see SpeciesDto
    
    101
    +     */
    
    79 102
         private Bound bound;
    
    80
    -    private Bound boundWithRatio;
    
    81
    -
    
    82
    -    private String getSpeciesField() {
    
    83
    -        return speciesField;
    
    84
    -    }
    
    85
    -
    
    86
    -    public void setSpeciesField(String speciesField) {
    
    87
    -        this.speciesField = speciesField;
    
    88
    -    }
    
    89
    -
    
    90
    -    public void setRatio(String ratio) {
    
    91
    -        this.ratio = Float.parseFloat(Objects.requireNonNull(ratio));
    
    92
    -    }
    
    93
    -
    
    94
    -    public void setExpression(String expression) {
    
    95
    -        this.expression = expression;
    
    96
    -    }
    
    97
    -
    
    98
    -    public boolean isEnable() {
    
    99
    -        return enable;
    
    100
    -    }
    
    101
    -
    
    102
    -    public void setEnable(String enable) {
    
    103
    -        this.enable = Boolean.parseBoolean(Objects.requireNonNull(enable));
    
    104
    -    }
    
    105
    -
    
    106
    -    protected abstract Float getBoundMin(SpeciesDto referentiel);
    
    107
    -
    
    108
    -    protected abstract Float getBoundMax(SpeciesDto referentiel);
    
    109
    -
    
    110
    -    public Bound getBound() {
    
    111
    -        return bound;
    
    112
    -    }
    
    113
    -
    
    114
    -    private boolean shouldValidate(Object object) throws ValidationException {
    
    115
    -
    
    116
    -        Boolean enable = (Boolean) getFieldValue("validationLengthWeightEnable", object);
    
    117
    -        if (enable != null && this.enable != null) {
    
    118
    -
    
    119
    -            if (!Objects.equals(this.enable, enable)) {
    
    120
    -
    
    121
    -                if (log.isDebugEnabled()) {
    
    122
    -                    log.debug("Skip speed validation");
    
    123
    -                }
    
    124
    -                return false;
    
    125
    -            }
    
    126
    -
    
    127
    -        }
    
    103
    +    /**
    
    104
    +     * Computed bound with {@link #ratio} applied to {@link #bound}.
    
    105
    +     */
    
    106
    +    private Bound computedBound;
    
    128 107
     
    
    129
    -        Boolean answer = true;
    
    130
    -        if (StringUtils.isNotEmpty(expression)) {
    
    131
    -            try {
    
    132
    -                answer = (Boolean) getFieldValue(expression, object);
    
    133
    -            } catch (ValidationException e) {
    
    134
    -                throw e;
    
    135
    -            } catch (Exception e) {
    
    136
    -                // let this pass, but it will be logged right below
    
    137
    -                if (log.isErrorEnabled()) {
    
    138
    -                    log.error("Could not get expression: " + expression);
    
    139
    -                }
    
    140
    -                answer = true;
    
    141
    -            }
    
    142
    -        }
    
    108
    +    protected abstract Float getBoundMin(SpeciesDto species);
    
    143 109
     
    
    144
    -        return answer;
    
    145
    -    }
    
    110
    +    protected abstract Float getBoundMax(SpeciesDto species);
    
    146 111
     
    
    147 112
         @Override
    
    148 113
         public void validate(Object object) throws ValidationException {
    
    ... ... @@ -156,9 +121,8 @@ public abstract class AbstractSpeciesFieldDtoValidator extends FieldValidatorSup
    156 121
                 throw new ValidationException("No parameter 'fieldName' filled");
    
    157 122
             }
    
    158 123
     
    
    159
    -        String speciesFieldName = getSpeciesField();
    
    160
    -        if (speciesFieldName == null) {
    
    161
    -            throw new ValidationException("No parameter 'speciesFieldName' filled");
    
    124
    +        if (speciesField == null) {
    
    125
    +            throw new ValidationException("No parameter 'speciesField' filled");
    
    162 126
             }
    
    163 127
     
    
    164 128
             boolean shouldValidate = shouldValidate(object);
    
    ... ... @@ -167,54 +131,46 @@ public abstract class AbstractSpeciesFieldDtoValidator extends FieldValidatorSup
    167 131
                 return;
    
    168 132
             }
    
    169 133
     
    
    170
    -        // la donnee a valider
    
    134
    +        // data to validate
    
    171 135
             Object value = getFieldValue(fieldName, object);
    
    172 136
             Float data = value == null ? null : Float.valueOf(String.valueOf(value));
    
    173 137
     
    
    174 138
             if (data == null) {
    
    175
    -            // la donnee a valider n'est pas définie
    
    139
    +            // no data found, can not validate
    
    140
    +            log.debug("No data found, skip validate.");
    
    176 141
                 return;
    
    177 142
             }
    
    178 143
     
    
    179
    -        if (log.isDebugEnabled()) {
    
    180
    -            log.debug("data to validate : " + data);
    
    181
    -        }
    
    144
    +        log.debug(String.format("data to validate : %s", data));
    
    182 145
     
    
    183
    -        SpeciesReference speciesRef = (SpeciesReference) getFieldValue(speciesFieldName, object);
    
    146
    +        SpeciesReference speciesRef = (SpeciesReference) getFieldValue(speciesField, object);
    
    184 147
     
    
    185 148
             if (speciesRef == null) {
    
    186 149
     
    
    187
    -            // pas de species trouvée, on ne peut pas valider
    
    150
    +            // no species found, can not validate
    
    151
    +            log.debug("No species found, skip validate.");
    
    188 152
                 return;
    
    189 153
             }
    
    190 154
     
    
    191
    -        if (log.isDebugEnabled()) {
    
    192
    -            log.debug("Species to validate : " + speciesRef);
    
    193
    -        }
    
    155
    +        log.debug(String.format("Species to validate : %s", speciesRef));
    
    194 156
     
    
    195 157
             SpeciesDto speciesDto = (SpeciesDto) stack.findValue("getSpecies(\"" + speciesRef.getId() + "\")");
    
    196 158
     
    
    197 159
             bound = getBound(speciesDto);
    
    198 160
     
    
    199
    -        if (log.isDebugEnabled()) {
    
    200
    -            log.debug("Species Bound to validate : " + bound);
    
    201
    -        }
    
    161
    +        log.debug(String.format("Species Bound to validate : %s", bound));
    
    202 162
     
    
    203 163
             if (bound == null) {
    
    204 164
     
    
    205
    -            // pas de donnée dans le référentiel acceptable
    
    165
    +            // no bound found, can not validate
    
    166
    +            log.debug("No species bound found, skip validate.");
    
    206 167
                 return;
    
    207 168
             }
    
    208 169
     
    
    209
    -        boundWithRatio = bound.applyRatio(ratio);
    
    210
    -
    
    211
    -        if (log.isDebugEnabled()) {
    
    212
    -            log.debug("Bound             : " + bound);
    
    213
    -            log.debug("Ratio to validate : " + ratio);
    
    214
    -            log.debug("Bound with ratio  : " + boundWithRatio);
    
    215
    -        }
    
    170
    +        computedBound = bound.applyRatio(ratio);
    
    171
    +        log.debug(String.format("Bound with ratio  : %s", computedBound));
    
    216 172
     
    
    217
    -        boolean valid = validateBound(data, boundWithRatio);
    
    173
    +        boolean valid = computedBound.validate(data);
    
    218 174
     
    
    219 175
             if (!valid) {
    
    220 176
     
    
    ... ... @@ -222,61 +178,117 @@ public abstract class AbstractSpeciesFieldDtoValidator extends FieldValidatorSup
    222 178
             }
    
    223 179
         }
    
    224 180
     
    
    181
    +    public void setSpeciesField(String speciesField) {
    
    182
    +        this.speciesField = speciesField;
    
    183
    +    }
    
    184
    +
    
    185
    +    public void setRatio(String ratio) {
    
    186
    +        this.ratio = Float.parseFloat(Objects.requireNonNull(ratio));
    
    187
    +    }
    
    188
    +
    
    189
    +    public void setExpression(String expression) {
    
    190
    +        this.expression = expression;
    
    191
    +    }
    
    192
    +
    
    193
    +    public void setEnable(String enable) {
    
    194
    +        this.enable = Boolean.parseBoolean(Objects.requireNonNull(enable));
    
    195
    +    }
    
    196
    +
    
    225 197
         public Float getMin() {
    
    226
    -        return Numbers.roundThreeDigits(boundWithRatio.getMin());
    
    198
    +        return computedBound == null ? null : Numbers.roundThreeDigits(computedBound.getMin());
    
    227 199
         }
    
    228 200
     
    
    229 201
         public Float getMax() {
    
    230
    -        return Numbers.roundThreeDigits(boundWithRatio.getMax());
    
    202
    +        return computedBound == null ? null : Numbers.roundThreeDigits(computedBound.getMax());
    
    231 203
         }
    
    232 204
     
    
    233
    -    private Bound getBound(SpeciesDto species) {
    
    205
    +    private Boolean getValidationLengthWeightEnable(Object object) throws ValidationException {
    
    206
    +        if (validationLengthWeightEnable == null) {
    
    207
    +            validationLengthWeightEnable = (Boolean) getFieldValue("validationLengthWeightEnable", object);
    
    208
    +        }
    
    209
    +        return validationLengthWeightEnable;
    
    210
    +    }
    
    234 211
     
    
    235
    -        Float min = getBoundMin(species);
    
    236
    -        Float max = getBoundMax(species);
    
    212
    +    private boolean shouldValidate(Object object) throws ValidationException {
    
    213
    +        boolean shouldValidate = shouldValidateFromEnable(object);
    
    214
    +        if (!shouldValidate) {
    
    215
    +            log.debug("Skip speed validation from 'validationLengthWeightEnable'");
    
    216
    +        } else {
    
    217
    +            shouldValidate = shouldValidateFromExpression(object);
    
    237 218
     
    
    238
    -        if (min == null || min == 0 || max == null || max == 0) {
    
    239
    -            // l'une des deux borne n'est pas définie, on ne peut pas utiliser
    
    240
    -            // la données
    
    241
    -            return null;
    
    219
    +            if (!shouldValidate) {
    
    220
    +                log.debug("Skip speed validation from 'expression'");
    
    221
    +            }
    
    242 222
             }
    
    243
    -        return new Bound(min, max);
    
    223
    +        return shouldValidate;
    
    244 224
         }
    
    245 225
     
    
    246
    -    private boolean validateBound(Float value, Bound bound) {
    
    247
    -        if (value == null) {
    
    226
    +    private boolean shouldValidateFromEnable(Object object) throws ValidationException {
    
    227
    +        boolean answer = true;
    
    228
    +        if (this.enable != null) {
    
    229
    +            Boolean enable;
    
    230
    +            try {
    
    231
    +                enable = getValidationLengthWeightEnable(object);
    
    232
    +            } catch (ValidationException e) {
    
    233
    +                // let this pass, but it will be logged right below
    
    234
    +                log.error("Could not get expression: validationLengthWeightEnable", e);
    
    235
    +                throw e;
    
    236
    +            }
    
    237
    +            answer = Objects.equals(this.enable, enable);
    
    238
    +        }
    
    239
    +        return answer;
    
    240
    +    }
    
    248 241
     
    
    249
    -            // valeur non définie
    
    250
    -            return true;
    
    242
    +    private boolean shouldValidateFromExpression(Object object) throws ValidationException {
    
    243
    +        boolean answer = true;
    
    244
    +        if (this.expression != null && !this.expression.isEmpty()) {
    
    245
    +            try {
    
    246
    +                Boolean answerObject = (Boolean) getFieldValue(expression, object);
    
    247
    +                answer = answerObject != null && answerObject;
    
    248
    +            } catch (ValidationException e) {
    
    249
    +                // let this pass, but it will be logged right below
    
    250
    +                log.error(String.format("Could not get expression: %s", expression), e);
    
    251
    +                throw e;
    
    252
    +            }
    
    251 253
             }
    
    252
    -        boolean valid;
    
    254
    +        return answer;
    
    255
    +    }
    
    253 256
     
    
    254
    -        float min = bound.getMin();
    
    255
    -        float max = bound.getMax();
    
    257
    +    private Bound getBound(SpeciesDto species) {
    
    258
    +
    
    259
    +        Float min = getBoundMin(species);
    
    260
    +        Float max = getBoundMax(species);
    
    256 261
     
    
    257
    -        valid = min <= value && value <= max;
    
    258
    -        return valid;
    
    262
    +        if (min == null || min == 0 || max == null || max == 0) {
    
    263
    +            // one of range is missing, can not use this bound
    
    264
    +            return null;
    
    265
    +        }
    
    266
    +        return new Bound(min, max);
    
    259 267
         }
    
    260 268
     
    
    261 269
         public static class Bound {
    
    262 270
     
    
    263
    -        private final Float min;
    
    271
    +        private final float min;
    
    264 272
     
    
    265
    -        private final Float max;
    
    273
    +        private final float max;
    
    266 274
     
    
    267
    -        Bound(Float min, Float max) {
    
    275
    +        Bound(float min, float max) {
    
    268 276
                 this.min = min;
    
    269 277
                 this.max = max;
    
    270 278
             }
    
    271 279
     
    
    272
    -        public Float getMin() {
    
    280
    +        public float getMin() {
    
    273 281
                 return min;
    
    274 282
             }
    
    275 283
     
    
    276
    -        public Float getMax() {
    
    284
    +        public float getMax() {
    
    277 285
                 return max;
    
    278 286
             }
    
    279 287
     
    
    288
    +        boolean validate(float value) {
    
    289
    +            return min <= value && value <= max;
    
    290
    +        }
    
    291
    +
    
    280 292
             Bound applyRatio(float ratio) {
    
    281 293
                 float delta = min / 100 * ratio;
    
    282 294
                 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;
    33 33
     public class SpeciesLengthFieldDtoValidator extends AbstractSpeciesFieldDtoValidator {
    
    34 34
     
    
    35 35
         @Override
    
    36
    -    protected Float getBoundMin(SpeciesDto referentiel) {
    
    37
    -        return referentiel.getMinLength();
    
    36
    +    protected Float getBoundMin(SpeciesDto species) {
    
    37
    +        return species.getMinLength();
    
    38 38
         }
    
    39 39
     
    
    40 40
         @Override
    
    41
    -    protected Float getBoundMax(SpeciesDto referentiel) {
    
    42
    -        return referentiel.getMaxLength();
    
    41
    +    protected Float getBoundMax(SpeciesDto species) {
    
    42
    +        return species.getMaxLength();
    
    43 43
         }
    
    44 44
     
    
    45 45
         @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;
    33 33
     public class SpeciesWeightFieldDtoValidator extends AbstractSpeciesFieldDtoValidator {
    
    34 34
     
    
    35 35
         @Override
    
    36
    -    protected Float getBoundMin(SpeciesDto referentiel) {
    
    37
    -        return referentiel.getMinWeight();
    
    36
    +    protected Float getBoundMin(SpeciesDto species) {
    
    37
    +        return species.getMinWeight();
    
    38 38
         }
    
    39 39
     
    
    40 40
         @Override
    
    41
    -    protected Float getBoundMax(SpeciesDto referentiel) {
    
    42
    -        return referentiel.getMaxWeight();
    
    41
    +    protected Float getBoundMax(SpeciesDto species) {
    
    42
    +        return species.getMaxWeight();
    
    43 43
         }
    
    44 44
     
    
    45 45
         @Override