[FEAT] add decorator @CollectionItemNotNull

This commit is contained in:
Edouard DUPIN 2025-01-25 14:40:07 +01:00
parent 3f15d560ed
commit 461aece7a0
3 changed files with 47 additions and 0 deletions

View File

@ -85,6 +85,14 @@ public class AnnotationTools {
return tmp;
}
public static CollectionItemNotNull getCollectionItemNotNull(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(CollectionItemNotNull.class);
if (annotation.length == 0) {
return null;
}
return (CollectionItemNotNull) annotation[0];
}
public static boolean getSchemaReadOnly(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Schema.class);
if (annotation.length == 0) {

View File

@ -0,0 +1,12 @@
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;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CollectionItemNotNull {
}

View File

@ -14,6 +14,7 @@ import java.util.UUID;
import java.util.regex.Pattern;
import org.kar.archidata.annotation.AnnotationTools;
import org.kar.archidata.annotation.CollectionItemNotNull;
import org.kar.archidata.annotation.DataJson;
import org.kar.archidata.dataAccess.DBAccess;
import org.kar.archidata.dataAccess.DataAccess;
@ -542,8 +543,34 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} else {
checkerInstance.check(ioDb, baseName + '.' + fieldName, tmpData, null, options);
final CollectionItemNotNull collectionNotNull = AnnotationTools.getCollectionItemNotNull(field);
if (collectionNotNull != null) {
if (!Collection.class.isAssignableFrom(field.getType())) {
throw new DataAccessException(
"Request @CollectionItemNotNull on a non collection field: '" + fieldName + "'");
}
add(fieldName,
(
final DBAccess ioDb,
final String baseName,
final T data,
final List<String> modifiedValue,
final QueryOptions options) -> {
final Object tmpData = field.get(data);
if (tmpData == null) {
return;
}
final Collection<?> tmpCollection = (Collection<?>) tmpData;
final Object[] elements = tmpCollection.toArray();
for (int iii = 0; iii < elements.length; iii++) {
if (elements[iii] == null) {
throw new InputException(baseName + fieldName + '[' + iii + ']',
"This collection can not conatain NULL item");
}
}
});
}
});
}
// keep this is last ==> take more time...