[FEAT] add decorator CollectionNotEmpty
This commit is contained in:
parent
6974adbfdf
commit
d011b3a587
@ -101,6 +101,14 @@ public class AnnotationTools {
|
||||
return (CollectionItemUnique) annotation[0];
|
||||
}
|
||||
|
||||
public static CollectionNotEmpty getCollectionNotEmpty(final Field element) {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(CollectionNotEmpty.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
return (CollectionNotEmpty) annotation[0];
|
||||
}
|
||||
|
||||
public static boolean getSchemaReadOnly(final Field element) {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Schema.class);
|
||||
if (annotation.length == 0) {
|
||||
|
12
src/org/kar/archidata/annotation/CollectionNotEmpty.java
Normal file
12
src/org/kar/archidata/annotation/CollectionNotEmpty.java
Normal 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 CollectionNotEmpty {
|
||||
|
||||
}
|
@ -18,6 +18,7 @@ import java.util.regex.Pattern;
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
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;
|
||||
@ -574,6 +575,27 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
}
|
||||
});
|
||||
}
|
||||
final CollectionNotEmpty collectionNotEmpty = AnnotationTools.getCollectionNotEmpty(field);
|
||||
if (collectionNotEmpty != null) {
|
||||
if (!Collection.class.isAssignableFrom(field.getType())) {
|
||||
throw new DataAccessException(
|
||||
"Request @collectionNotEmpty 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;
|
||||
if (tmpCollection.isEmpty()) {
|
||||
throw new InputException(baseName + fieldName, "Can not be empty");
|
||||
}
|
||||
});
|
||||
}
|
||||
// keep this is last ==> take more time...
|
||||
|
Loading…
x
Reference in New Issue
Block a user