diff --git a/src/org/kar/archidata/dataAccess/options/CheckFunctionInterface.java b/src/org/kar/archidata/dataAccess/options/CheckFunctionInterface.java index 6c8940a..4f2685c 100644 --- a/src/org/kar/archidata/dataAccess/options/CheckFunctionInterface.java +++ b/src/org/kar/archidata/dataAccess/options/CheckFunctionInterface.java @@ -10,9 +10,9 @@ public interface CheckFunctionInterface { /** This function implementation is design to check if the updated class is valid of not for insertion * @param baseName NAme of the object to be precise with the use of what fail. * @param data The object that might be injected. - * @param filterValue List of fields that might be check. If null, then all column must be checked. + * @param modifiedValue List of fields that might be check. If null, then all column must be checked. * @throws Exception Exception is generate if the data are incorrect. */ - void check(final String baseName, Object data, List filterValue, final QueryOptions options) + void check(final String baseName, Object data, List modifiedValue, final QueryOptions options) throws Exception; default void checkAll(final String baseName, final Object data, final QueryOptions options) throws Exception { diff --git a/src/org/kar/archidata/dataAccess/options/CheckJPA.java b/src/org/kar/archidata/dataAccess/options/CheckJPA.java index 06ce78d..e46b07b 100644 --- a/src/org/kar/archidata/dataAccess/options/CheckJPA.java +++ b/src/org/kar/archidata/dataAccess/options/CheckJPA.java @@ -35,10 +35,13 @@ public class CheckJPA implements CheckFunctionInterface { /** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */ public interface CheckInterface { /** This function implementation is design to check if the updated class is valid of not for insertion + * @param baseName Base of the name input that is displayed in exception generated. * @param data The object that might be injected. - * @param filterValue List of fields that might be check. If null, then all column must be checked. + * @param modifiedValue List of fields that modification is requested. + * @param options Some query option that the checker can need to generate basic check. * @throws Exception Exception is generate if the data are incorrect. */ - void check(final String baseName, final K data, final QueryOptions options) throws Exception; + void check(final String baseName, final K data, List modifiedValue, final QueryOptions options) + throws Exception; } protected Map>> checking = null; @@ -67,128 +70,182 @@ public class CheckJPA implements CheckFunctionInterface { for (final Field field : this.clazz.getFields()) { final String fieldName = field.getName(); // AnnotationTools.getFieldName(field); if (AnnotationTools.isPrimaryKey(field)) { - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - throw new InputException(baseName + fieldName, - "This is a '@Id' (primaryKey) ==> can not be change"); - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + throw new InputException(baseName + fieldName, + "This is a '@Id' (primaryKey) ==> can not be change"); + }); } if (AnnotationTools.getConstraintsNotNull(field)) { - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - if (field.get(data) == null) { - throw new InputException(baseName + fieldName, "Can not be null"); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + if (field.get(data) == null) { + throw new InputException(baseName + fieldName, "Can not be null"); + } + }); } if (AnnotationTools.isCreatedAtField(field) || AnnotationTools.isUpdateAtField(field)) { - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - throw new InputException(baseName + fieldName, "It is forbidden to change this field"); - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + throw new InputException(baseName + fieldName, "It is forbidden to change this field"); + }); } final Class type = field.getType(); if (type == Long.class || type == long.class) { final Long maxValue = AnnotationTools.getConstraintsMax(field); if (maxValue != null) { - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final Long elemTyped = (Long) elem; - if (elemTyped > maxValue) { - throw new InputException(baseName + fieldName, "Value too height max: " + maxValue); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final Long elemTyped = (Long) elem; + if (elemTyped > maxValue) { + throw new InputException(baseName + fieldName, + "Value too height max: " + maxValue); + } + }); } final Long minValue = AnnotationTools.getConstraintsMin(field); if (minValue != null) { - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final Long elemTyped = (Long) elem; - if (elemTyped < minValue) { - throw new InputException(baseName + fieldName, "Value too Low min: " + minValue); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final Long elemTyped = (Long) elem; + if (elemTyped < minValue) { + throw new InputException(baseName + fieldName, + "Value too Low min: " + minValue); + } + }); } final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field); if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) { - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final List condCheckers = options.get(ConditionChecker.class); - final Condition conditionCheck = condCheckers.isEmpty() ? null - : condCheckers.get(0).toCondition(); - final long count = DataAccess.count(annotationManyToOne.targetEntity(), elem, - conditionCheck); - if (count == 0) { - throw new InputException(baseName + fieldName, - "Foreign element does not exist in the DB:" + elem); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final List condCheckers = options.get(ConditionChecker.class); + final Condition conditionCheck = condCheckers.isEmpty() ? null + : condCheckers.get(0).toCondition(); + final long count = DataAccess.count(annotationManyToOne.targetEntity(), elem, + conditionCheck); + if (count == 0) { + throw new InputException(baseName + fieldName, + "Foreign element does not exist in the DB:" + elem); + } + }); } } else if (type == Integer.class || type == int.class) { final Long maxValueRoot = AnnotationTools.getConstraintsMax(field); if (maxValueRoot != null) { final int maxValue = maxValueRoot.intValue(); - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final Integer elemTyped = (Integer) elem; - if (elemTyped > maxValue) { - throw new InputException(baseName + fieldName, "Value too height max: " + maxValue); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final Integer elemTyped = (Integer) elem; + if (elemTyped > maxValue) { + throw new InputException(baseName + fieldName, + "Value too height max: " + maxValue); + } + }); } final Long minValueRoot = AnnotationTools.getConstraintsMin(field); if (minValueRoot != null) { final int minValue = minValueRoot.intValue(); - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final Integer elemTyped = (Integer) elem; - if (elemTyped < minValue) { - throw new InputException(baseName + fieldName, "Value too Low min: " + minValue); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final Integer elemTyped = (Integer) elem; + if (elemTyped < minValue) { + throw new InputException(baseName + fieldName, + "Value too Low min: " + minValue); + } + }); } final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field); if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) { - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final long count = DataAccess.count(annotationManyToOne.targetEntity(), elem); - if (count == 0) { - throw new InputException(baseName + fieldName, - "Foreign element does not exist in the DB:" + elem); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final long count = DataAccess.count(annotationManyToOne.targetEntity(), elem); + if (count == 0) { + throw new InputException(baseName + fieldName, + "Foreign element does not exist in the DB:" + elem); + } + }); } } else if (type == UUID.class) { final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field); if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) { - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final long count = DataAccess.count(annotationManyToOne.targetEntity(), elem); - if (count == 0) { - throw new InputException(baseName + fieldName, - "Foreign element does not exist in the DB:" + elem); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final long count = DataAccess.count(annotationManyToOne.targetEntity(), elem); + if (count == 0) { + throw new InputException(baseName + fieldName, + "Foreign element does not exist in the DB:" + elem); + } + }); } } else if (type == Boolean.class || type == boolean.class) { @@ -196,59 +253,83 @@ public class CheckJPA implements CheckFunctionInterface { final Long maxValueRoot = AnnotationTools.getConstraintsMax(field); if (maxValueRoot != null) { final float maxValue = maxValueRoot.floatValue(); - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final Float elemTyped = (Float) elem; - if (elemTyped > maxValue) { - throw new InputException(baseName + fieldName, "Value too height max: " + maxValue); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final Float elemTyped = (Float) elem; + if (elemTyped > maxValue) { + throw new InputException(baseName + fieldName, + "Value too height max: " + maxValue); + } + }); } final Long minValueRoot = AnnotationTools.getConstraintsMin(field); if (minValueRoot != null) { final float minValue = minValueRoot.floatValue(); - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final Float elemTyped = (Float) elem; - if (elemTyped < minValue) { - throw new InputException(baseName + fieldName, "Value too Low min: " + minValue); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final Float elemTyped = (Float) elem; + if (elemTyped < minValue) { + throw new InputException(baseName + fieldName, + "Value too Low min: " + minValue); + } + }); } } else if (type == Double.class || type == double.class) { final Long maxValueRoot = AnnotationTools.getConstraintsMax(field); if (maxValueRoot != null) { final double maxValue = maxValueRoot.doubleValue(); - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final Double elemTyped = (Double) elem; - if (elemTyped > maxValue) { - throw new InputException(baseName + fieldName, "Value too height max: " + maxValue); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final Double elemTyped = (Double) elem; + if (elemTyped > maxValue) { + throw new InputException(baseName + fieldName, + "Value too height max: " + maxValue); + } + }); } final Long minValueRoot = AnnotationTools.getConstraintsMin(field); if (minValueRoot != null) { final double minValue = minValueRoot.doubleValue(); - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final Double elemTyped = (Double) elem; - if (elemTyped < minValue) { - throw new InputException(baseName + fieldName, "Value too Low min: " + minValue); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final Double elemTyped = (Double) elem; + if (elemTyped < minValue) { + throw new InputException(baseName + fieldName, + "Value too Low min: " + minValue); + } + }); } } else if (type == Date.class || type == Timestamp.class) { @@ -259,51 +340,66 @@ public class CheckJPA implements CheckFunctionInterface { } else if (type == String.class) { final int maxSizeString = AnnotationTools.getLimitSize(field); if (maxSizeString > 0) { - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final String elemTyped = (String) elem; - if (elemTyped.length() > maxSizeString) { - throw new InputException(baseName + fieldName, - "Too long size must be <= " + maxSizeString); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final String elemTyped = (String) elem; + if (elemTyped.length() > maxSizeString) { + throw new InputException(baseName + fieldName, + "Too long size must be <= " + maxSizeString); + } + }); } final Size limitSize = AnnotationTools.getConstraintsSize(field); if (limitSize != null) { - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final String elemTyped = (String) elem; - if (elemTyped.length() > limitSize.max()) { - throw new InputException(baseName + fieldName, - "Too long size (constraints) must be <= " + limitSize.max()); - } - if (elemTyped.length() < limitSize.min()) { - throw new InputException(baseName + fieldName, - "Too small size (constraints) must be >= " + limitSize.min()); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final String elemTyped = (String) elem; + if (elemTyped.length() > limitSize.max()) { + throw new InputException(baseName + fieldName, + "Too long size (constraints) must be <= " + limitSize.max()); + } + if (elemTyped.length() < limitSize.min()) { + throw new InputException(baseName + fieldName, + "Too small size (constraints) must be >= " + limitSize.min()); + } + }); } final String patternString = AnnotationTools.getConstraintsPattern(field); if (patternString != null) { final Pattern pattern = Pattern.compile(patternString); - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final String elemTyped = (String) elem; - if (!pattern.matcher(elemTyped).find()) { - throw new InputException(baseName + fieldName, - "does not match the required pattern (constraints) must be '" + patternString - + "'"); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final String elemTyped = (String) elem; + if (!pattern.matcher(elemTyped).find()) { + throw new InputException(baseName + fieldName, + "does not match the required pattern (constraints) must be '" + + patternString + "'"); + } + }); } } else if (type == JsonValue.class) { final DataJson jsonAnnotation = AnnotationTools.getDataJson(field); @@ -311,9 +407,14 @@ public class CheckJPA implements CheckFunctionInterface { // Here if we have an error it crash at start and no new instance after creation... final CheckFunctionInterface instance = jsonAnnotation.checker().getDeclaredConstructor() .newInstance(); - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - instance.checkAll(baseName + fieldName + ".", field.get(data), options); - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + instance.checkAll(baseName + fieldName + ".", field.get(data), options); + }); } } else if (type.isEnum()) { // nothing to do. @@ -321,21 +422,26 @@ public class CheckJPA implements CheckFunctionInterface { // keep this is last ==> take more time... if (AnnotationTools.isUnique(field)) { // Create the request ... - add(fieldName, (final String baseName, final T data, final QueryOptions options) -> { - final List condCheckers = options.get(ConditionChecker.class); - Object other = null; - if (condCheckers.isEmpty()) { - other = DataAccess.getWhere(this.clazz, - new Condition(new QueryCondition(fieldName, "==", field.get(data)))); - } else { - other = DataAccess.getWhere(this.clazz, - new Condition(new QueryCondition(fieldName, "==", field.get(data))), - condCheckers.get(0).toCondition()); - } - if (other != null) { - throw new InputException(baseName + fieldName, "Name already exist in the DB"); - } - }); + add(fieldName, + ( + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final List condCheckers = options.get(ConditionChecker.class); + Object other = null; + if (condCheckers.isEmpty()) { + other = DataAccess.getWhere(this.clazz, + new Condition(new QueryCondition(fieldName, "==", field.get(data)))); + } else { + other = DataAccess.getWhere(this.clazz, + new Condition(new QueryCondition(fieldName, "==", field.get(data))), + condCheckers.get(0).toCondition()); + } + if (other != null) { + throw new InputException(baseName + fieldName, "Name already exist in the DB"); + } + }); } } @@ -349,7 +455,7 @@ public class CheckJPA implements CheckFunctionInterface { public void check( final String baseName, final Object data, - final List filterValue, + final List modifiedValue, final QueryOptions options) throws Exception { if (this.checking == null) { initialize(); @@ -359,19 +465,20 @@ public class CheckJPA implements CheckFunctionInterface { } @SuppressWarnings("unchecked") final T dataCasted = (T) data; - for (final String filter : filterValue) { + for (final String filter : modifiedValue) { final List> actions = this.checking.get(filter); if (actions == null) { continue; } for (final CheckInterface action : actions) { - action.check(baseName, dataCasted, options); + action.check(baseName, dataCasted, modifiedValue, options); } } - checkTyped(dataCasted, filterValue, options); + checkTyped(dataCasted, modifiedValue, options); } - public void checkTyped(final T data, final List filterValue, final QueryOptions options) throws Exception { + public void checkTyped(final T data, final List modifiedValue, final QueryOptions options) + throws Exception { // nothing to do ... } }