Compare commits
4 Commits
8f3c14e28d
...
d011b3a587
Author | SHA1 | Date | |
---|---|---|---|
d011b3a587 | |||
6974adbfdf | |||
461aece7a0 | |||
3f15d560ed |
@ -85,6 +85,30 @@ public class AnnotationTools {
|
|||||||
return tmp;
|
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 CollectionItemUnique getCollectionItemUnique(final Field element) {
|
||||||
|
final Annotation[] annotation = element.getDeclaredAnnotationsByType(CollectionItemUnique.class);
|
||||||
|
if (annotation.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
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) {
|
public static boolean getSchemaReadOnly(final Field element) {
|
||||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Schema.class);
|
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Schema.class);
|
||||||
if (annotation.length == 0) {
|
if (annotation.length == 0) {
|
||||||
|
12
src/org/kar/archidata/annotation/CollectionItemNotNull.java
Normal file
12
src/org/kar/archidata/annotation/CollectionItemNotNull.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 CollectionItemNotNull {
|
||||||
|
|
||||||
|
}
|
12
src/org/kar/archidata/annotation/CollectionItemUnique.java
Normal file
12
src/org/kar/archidata/annotation/CollectionItemUnique.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 CollectionItemUnique {
|
||||||
|
|
||||||
|
}
|
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 {
|
||||||
|
|
||||||
|
}
|
@ -8,12 +8,17 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.kar.archidata.annotation.AnnotationTools;
|
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.annotation.DataJson;
|
||||||
import org.kar.archidata.dataAccess.DBAccess;
|
import org.kar.archidata.dataAccess.DBAccess;
|
||||||
import org.kar.archidata.dataAccess.DataAccess;
|
import org.kar.archidata.dataAccess.DataAccess;
|
||||||
@ -476,7 +481,53 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
|||||||
if (dataJson != null && dataJson.checker() != null) {
|
if (dataJson != null && dataJson.checker() != null) {
|
||||||
final CheckFunctionInterface checkerInstance = dataJson.checker().getDeclaredConstructor()
|
final CheckFunctionInterface checkerInstance = dataJson.checker().getDeclaredConstructor()
|
||||||
.newInstance();
|
.newInstance();
|
||||||
// check if the type is a list, set, ...
|
if (Collection.class.isAssignableFrom(field.getType())) {
|
||||||
|
add(fieldName,
|
||||||
|
(
|
||||||
|
final DBAccess ioDb,
|
||||||
|
final String baseName,
|
||||||
|
final T data,
|
||||||
|
final List<String> modifiedValue,
|
||||||
|
final QueryOptions options) -> {
|
||||||
|
// get the field of the specific element
|
||||||
|
final Object tmpData = field.get(data);
|
||||||
|
// It is not the objective of this element to check if it is authorize to set NULL
|
||||||
|
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) {
|
||||||
|
checkerInstance.check(ioDb, baseName + fieldName + '[' + iii + "].",
|
||||||
|
elements[iii], null, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
add(fieldName,
|
||||||
|
(
|
||||||
|
final DBAccess ioDb,
|
||||||
|
final String baseName,
|
||||||
|
final T data,
|
||||||
|
final List<String> modifiedValue,
|
||||||
|
final QueryOptions options) -> {
|
||||||
|
// get the field of the specific element
|
||||||
|
final Object tmpData = field.get(data);
|
||||||
|
// It is not the objective of this element to check if it is authorize to set NULL
|
||||||
|
if (tmpData == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
checkerInstance.check(ioDb, baseName + fieldName + '.', tmpData, null, options);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
final CollectionItemUnique collectionUnique = AnnotationTools.getCollectionItemUnique(field);
|
||||||
|
if (collectionUnique != null) {
|
||||||
|
if (!Collection.class.isAssignableFrom(field.getType())) {
|
||||||
|
throw new DataAccessException(
|
||||||
|
"Request @CollectionItemUnique on a non collection field: '" + fieldName + "'");
|
||||||
|
}
|
||||||
add(fieldName,
|
add(fieldName,
|
||||||
(
|
(
|
||||||
final DBAccess ioDb,
|
final DBAccess ioDb,
|
||||||
@ -484,26 +535,67 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
|||||||
final T data,
|
final T data,
|
||||||
final List<String> modifiedValue,
|
final List<String> modifiedValue,
|
||||||
final QueryOptions options) -> {
|
final QueryOptions options) -> {
|
||||||
// get the field of the specific element
|
|
||||||
final Object tmpData = field.get(data);
|
final Object tmpData = field.get(data);
|
||||||
// It is not the objective of this element to check if it is authorize to set NULL
|
|
||||||
if (tmpData == null) {
|
if (tmpData == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (tmpData instanceof Collection) {
|
final Collection<?> tmpCollection = (Collection<?>) tmpData;
|
||||||
final Collection<?> tmpCollection = (Collection<?>) tmpData;
|
final Set<Object> uniqueValues = new HashSet<>(tmpCollection);
|
||||||
final Object[] elements = tmpCollection.toArray();
|
if (uniqueValues.size() != tmpCollection.size()) {
|
||||||
for (int iii = 0; iii < elements.length; iii++) {
|
throw new InputException(baseName + fieldName,
|
||||||
if (elements[iii] != null) {
|
"Cannot insert multiple times the same elements");
|
||||||
checkerInstance.check(ioDb, baseName + '.' + fieldName + '[' + iii + ']',
|
}
|
||||||
elements[iii], null, options);
|
});
|
||||||
}
|
}
|
||||||
}
|
final CollectionItemNotNull collectionNotNull = AnnotationTools.getCollectionItemNotNull(field);
|
||||||
|
if (collectionNotNull != null) {
|
||||||
} else {
|
if (!Collection.class.isAssignableFrom(field.getType())) {
|
||||||
checkerInstance.check(ioDb, baseName + '.' + fieldName, tmpData, null, options);
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
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...
|
// keep this is last ==> take more time...
|
||||||
|
Loading…
x
Reference in New Issue
Block a user