[FEAT,API] remove checker from JsonData and creadte @CheckerAnnotation
This commit is contained in:
parent
d028eb2261
commit
b5fcc3e20c
@ -189,6 +189,14 @@ public class AnnotationTools {
|
||||
return (DataJson) annotation[0];
|
||||
}
|
||||
|
||||
public static Checker[] getCheckers(final Field element) {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Checker.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
return (Checker[]) annotation;
|
||||
}
|
||||
|
||||
public static Long getConstraintsMax(final Field element) {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Max.class);
|
||||
if (annotation.length == 0) {
|
||||
|
14
src/org/kar/archidata/annotation/Checker.java
Normal file
14
src/org/kar/archidata/annotation/Checker.java
Normal file
@ -0,0 +1,14 @@
|
||||
package org.kar.archidata.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.kar.archidata.dataAccess.options.CheckFunctionInterface;
|
||||
|
||||
@Target({ ElementType.TYPE, ElementType.FIELD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Checker {
|
||||
Class<? extends CheckFunctionInterface> value();
|
||||
}
|
@ -5,13 +5,8 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.kar.archidata.dataAccess.options.CheckFunctionInterface;
|
||||
import org.kar.archidata.dataAccess.options.CheckFunctionVoid;
|
||||
|
||||
@Target({ ElementType.TYPE, ElementType.FIELD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface DataJson {
|
||||
Class<? extends CheckFunctionInterface> checker() default CheckFunctionVoid.class;
|
||||
|
||||
Class<?> targetEntity() default Void.class;
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import org.kar.archidata.dataAccess.QueryOptions;
|
||||
/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */
|
||||
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 baseName Name of the object to be precise with the use of what fail.
|
||||
* @param data The object that might be injected.
|
||||
* @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. */
|
||||
|
@ -16,10 +16,10 @@ import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.annotation.Checker;
|
||||
import org.kar.archidata.annotation.CollectionItemNotNull;
|
||||
import org.kar.archidata.annotation.CollectionItemUnique;
|
||||
import org.kar.archidata.annotation.CollectionNotEmpty;
|
||||
import org.kar.archidata.annotation.DataJson;
|
||||
import org.kar.archidata.dataAccess.DBAccess;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.kar.archidata.dataAccess.QueryCondition;
|
||||
@ -29,8 +29,6 @@ import org.kar.archidata.exception.InputException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
@ -458,28 +456,16 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (type == JsonValue.class) {
|
||||
final DataJson jsonAnnotation = AnnotationTools.getDataJson(field);
|
||||
if (jsonAnnotation != null && jsonAnnotation.checker() != CheckFunctionVoid.class) {
|
||||
// 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 DBAccess ioDb,
|
||||
final String baseName,
|
||||
final T data,
|
||||
final List<String> modifiedValue,
|
||||
final QueryOptions options) -> {
|
||||
instance.checkAll(ioDb, baseName + fieldName + ".", field.get(data), options);
|
||||
});
|
||||
}
|
||||
} else if (type.isEnum()) {
|
||||
// nothing to do.
|
||||
}
|
||||
final DataJson dataJson = AnnotationTools.getDataJson(field);
|
||||
if (dataJson != null && dataJson.checker() != null) {
|
||||
final CheckFunctionInterface checkerInstance = dataJson.checker().getDeclaredConstructor()
|
||||
final Checker[] checkers = AnnotationTools.getCheckers(field);
|
||||
if (checkers != null) {
|
||||
for (final Checker checker : checkers) {
|
||||
if (checker == null || checker.value() == CheckFunctionVoid.class) {
|
||||
continue;
|
||||
}
|
||||
final CheckFunctionInterface checkerInstance = checker.value().getDeclaredConstructor()
|
||||
.newInstance();
|
||||
if (Collection.class.isAssignableFrom(field.getType())) {
|
||||
add(fieldName,
|
||||
@ -522,6 +508,7 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
final CollectionItemUnique collectionUnique = AnnotationTools.getCollectionItemUnique(field);
|
||||
if (collectionUnique != null) {
|
||||
if (!Collection.class.isAssignableFrom(field.getType())) {
|
||||
|
@ -56,7 +56,6 @@ public class RESTApi {
|
||||
"Fail to get the data [" + httpResponse.statusCode() + "] " + httpResponse.body());
|
||||
}
|
||||
}
|
||||
//return this.mapper.readValue(httpResponse.body(), new TypeReference<List<T>>() {});
|
||||
return this.mapper.readValue(httpResponse.body(),
|
||||
this.mapper.getTypeFactory().constructCollectionType(List.class, clazz));
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package test.kar.archidata.dataAccess.model;
|
||||
|
||||
import org.kar.archidata.annotation.Checker;
|
||||
import org.kar.archidata.annotation.DataJson;
|
||||
import org.kar.archidata.dataAccess.options.CheckJPA;
|
||||
|
||||
@ -11,6 +12,7 @@ public class DataWithSubJson {
|
||||
}
|
||||
}
|
||||
|
||||
@DataJson(checker = DataInJson.DataInJsonChecker.class)
|
||||
@DataJson()
|
||||
@Checker(DataInJson.DataInJsonChecker.class)
|
||||
public DataInJson dataSerialized;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user