Compare commits

..

No commits in common. "015c38ff5b132bd47cc377db5da1992431f338ab" and "3e81673d38f44136368f830d7fba0081e1c352b4" have entirely different histories.

4 changed files with 83 additions and 115 deletions

View File

@ -41,24 +41,6 @@ import jakarta.ws.rs.DefaultValue;
public class AnnotationTools { public class AnnotationTools {
static final Logger LOGGER = LoggerFactory.getLogger(AnnotationTools.class); static final Logger LOGGER = LoggerFactory.getLogger(AnnotationTools.class);
public static <TYPE extends Annotation> TYPE get(final Field element, final Class<TYPE> clazz) {
final TYPE[] annotations = element.getDeclaredAnnotationsByType(clazz);
if (annotations.length == 0) {
return null;
}
return annotations[0];
}
public static <TYPE extends Annotation> TYPE[] gets(final Field element, final Class<TYPE> clazz) {
final TYPE[] annotations = element.getDeclaredAnnotationsByType(clazz);
if (annotations.length == 0) {
return null;
}
return annotations;
}
// For SQL declaration table Name // For SQL declaration table Name
public static String getTableName(final Class<?> clazz, final QueryOptions options) throws DataAccessException { public static String getTableName(final Class<?> clazz, final QueryOptions options) throws DataAccessException {
if (options != null) { if (options != null) {
@ -110,15 +92,27 @@ public class AnnotationTools {
} }
public static CollectionItemNotNull getCollectionItemNotNull(final Field element) { public static CollectionItemNotNull getCollectionItemNotNull(final Field element) {
return get(element, CollectionItemNotNull.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(CollectionItemNotNull.class);
if (annotation.length == 0) {
return null;
}
return (CollectionItemNotNull) annotation[0];
} }
public static CollectionItemUnique getCollectionItemUnique(final Field element) { public static CollectionItemUnique getCollectionItemUnique(final Field element) {
return get(element, CollectionItemUnique.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(CollectionItemUnique.class);
if (annotation.length == 0) {
return null;
}
return (CollectionItemUnique) annotation[0];
} }
public static CollectionNotEmpty getCollectionNotEmpty(final Field element) { public static CollectionNotEmpty getCollectionNotEmpty(final Field element) {
return get(element, CollectionNotEmpty.class); 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) {
@ -170,39 +164,75 @@ public class AnnotationTools {
} }
public static ManyToOne getManyToOne(final Field element) { public static ManyToOne getManyToOne(final Field element) {
return get(element, ManyToOne.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(ManyToOne.class);
if (annotation.length == 0) {
return null;
}
return (ManyToOne) annotation[0];
} }
public static ManyToMany getManyToMany(final Field element) { public static ManyToMany getManyToMany(final Field element) {
return get(element, ManyToMany.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(ManyToMany.class);
if (annotation.length == 0) {
return null;
}
return (ManyToMany) annotation[0];
} }
public static OneToMany getOneToMany(final Field element) { public static OneToMany getOneToMany(final Field element) {
return get(element, OneToMany.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(OneToMany.class);
if (annotation.length == 0) {
return null;
}
return (OneToMany) annotation[0];
} }
public static DataJson getDataJson(final Field element) { public static DataJson getDataJson(final Field element) {
return get(element, DataJson.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(DataJson.class);
if (annotation.length == 0) {
return null;
}
return (DataJson) annotation[0];
} }
public static Checker[] getConstraintsCheckers(final Field element) { public static Checker[] getConstraintsCheckers(final Field element) {
return gets(element, Checker.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Checker.class);
if (annotation.length == 0) {
return null;
}
return (Checker[]) annotation;
} }
public static DecimalMin getConstraintsDecimalMin(final Field element) { public static DecimalMin getConstraintsDecimalMin(final Field element) {
return get(element, DecimalMin.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(DecimalMin.class);
if (annotation.length == 0) {
return null;
}
return ((DecimalMin) annotation[0]);
} }
public static DecimalMax getConstraintsDecimalMax(final Field element) { public static DecimalMax getConstraintsDecimalMax(final Field element) {
return get(element, DecimalMax.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(DecimalMax.class);
if (annotation.length == 0) {
return null;
}
return ((DecimalMax) annotation[0]);
} }
public static Max getConstraintsMax(final Field element) { public static Max getConstraintsMax(final Field element) {
return get(element, Max.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Max.class);
if (annotation.length == 0) {
return null;
}
return ((Max) annotation[0]);
} }
public static Min getConstraintsMin(final Field element) { public static Min getConstraintsMin(final Field element) {
return get(element, Min.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Min.class);
if (annotation.length == 0) {
return null;
}
return ((Min) annotation[0]);
} }
public static int getLimitSize(final Field element) { public static int getLimitSize(final Field element) {
@ -215,15 +245,27 @@ public class AnnotationTools {
} }
public static Size getConstraintsSize(final Field element) { public static Size getConstraintsSize(final Field element) {
return get(element, Size.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Size.class);
if (annotation.length == 0) {
return null;
}
return (Size) annotation[0];
} }
public static Pattern getConstraintsPattern(final Field element) { public static Pattern getConstraintsPattern(final Field element) {
return get(element, Pattern.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Pattern.class);
if (annotation.length == 0) {
return null;
}
return (Pattern) annotation[0];
} }
public static Email getConstraintsEmail(final Field element) { public static Email getConstraintsEmail(final Field element) {
return get(element, Email.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Email.class);
if (annotation.length == 0) {
return null;
}
return (Email) annotation[0];
} }
public static boolean isAnnotationGroup(final Field field, final Class<?> annotationType) { public static boolean isAnnotationGroup(final Field field, final Class<?> annotationType) {

View File

@ -1,12 +0,0 @@
package org.kar.archidata.annotation.checker;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.TYPE, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckForeignKey {
Class<?> target();
}

View File

@ -15,7 +15,6 @@ import java.util.Set;
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.checker.CheckForeignKey;
import org.kar.archidata.annotation.checker.Checker; import org.kar.archidata.annotation.checker.Checker;
import org.kar.archidata.annotation.checker.CollectionItemNotNull; import org.kar.archidata.annotation.checker.CollectionItemNotNull;
import org.kar.archidata.annotation.checker.CollectionItemUnique; import org.kar.archidata.annotation.checker.CollectionItemUnique;
@ -652,64 +651,6 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
} }
} }
final CheckForeignKey foreighKey = AnnotationTools.get(field, CheckForeignKey.class);
if (foreighKey != null) {
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 List<ConditionChecker> condCheckers = options.get(ConditionChecker.class);
final Condition conditionCheck = condCheckers.isEmpty() ? null
: condCheckers.get(0).toCondition();
final Collection<?> tmpCollection = (Collection<?>) tmpData;
final Object[] elements = tmpCollection.toArray();
for (int iii = 0; iii < elements.length; iii++) {
if (elements[iii] == null) {
continue;
}
final Long count = ioDb.count(foreighKey.target(), elements[iii],
conditionCheck);
if (count != 1) {
throw new InputException(baseName + fieldName + '[' + iii + ']',
"Foreign-key does not exist in the DB:" + elements[iii]);
}
}
});
} else {
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 List<ConditionChecker> condCheckers = options.get(ConditionChecker.class);
final Condition conditionCheck = condCheckers.isEmpty() ? null
: condCheckers.get(0).toCondition();
final Long count = ioDb.count(foreighKey.target(), tmpData, conditionCheck);
if (count != 1) {
throw new InputException(baseName + fieldName,
"Foreign-key does not exist in the DB:" + tmpData);
}
});
}
}
// check if we really want to keep it ...
final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field); final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field);
if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) { if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) {
add(fieldName, add(fieldName,

View File

@ -260,17 +260,14 @@ public class TsClassElement {
final Class<?> clazz = field.model().getOriginClasses(); final Class<?> clazz = field.model().getOriginClasses();
if (clazz == String.class) { if (clazz == String.class) {
if (field.stringSize() != null) { if (field.stringSize() != null) {
if (field.stringSize().min() > 0) { builder.append(".min(");
// A string size can not be lower at 0 builder.append(field.stringSize().min());
builder.append(".min("); builder.append(")");
builder.append(field.stringSize().min()); }
builder.append(")"); if (field.stringSize() != null) {
} builder.append(".max(");
if (field.stringSize().max() != Integer.MAX_VALUE) { builder.append(field.stringSize().max());
builder.append(".max("); builder.append(")");
builder.append(field.stringSize().max());
builder.append(")");
}
} }
/*Must be tested before /*Must be tested before
if (field.pattern() != null) { if (field.pattern() != null) {