[FIX] foreinKeyValidator
This commit is contained in:
parent
55957278a0
commit
f7ba570913
@ -9,7 +9,15 @@ import jakarta.validation.Constraint;
|
||||
import jakarta.validation.Payload;
|
||||
|
||||
@Constraint(validatedBy = CheckForeignKeyValidator.class)
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
|
||||
@Target({ ElementType.TYPE, //
|
||||
ElementType.METHOD, //
|
||||
ElementType.FIELD, //
|
||||
ElementType.ANNOTATION_TYPE, //
|
||||
ElementType.CONSTRUCTOR, //
|
||||
ElementType.PARAMETER, //
|
||||
ElementType.TYPE_USE, //
|
||||
})
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface CheckForeignKey {
|
||||
Class<?> target();
|
||||
|
@ -1,14 +1,15 @@
|
||||
package org.kar.archidata.annotation.checker;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
|
||||
public class CheckForeignKeyValidator implements ConstraintValidator<CheckForeignKey, Object> {
|
||||
Class<?> target = null;
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(CheckForeignKeyValidator.class);
|
||||
|
||||
@Override
|
||||
public void initialize(final CheckForeignKey annotation) {
|
||||
@ -17,27 +18,19 @@ public class CheckForeignKeyValidator implements ConstraintValidator<CheckForeig
|
||||
|
||||
@Override
|
||||
public boolean isValid(final Object value, final ConstraintValidatorContext context) {
|
||||
if (value != null) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
if (value instanceof final Collection<?> tmpCollection) {
|
||||
final Object[] elements = tmpCollection.toArray();
|
||||
for (final Object element : elements) {
|
||||
if (element == null) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
final long count = DataAccess.count(this.target, element);
|
||||
final long count = DataAccess.count(this.target, value);
|
||||
if (count != 1) {
|
||||
return false;
|
||||
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
// TODO ...
|
||||
LOGGER.error("Fail to access to the DB");
|
||||
context.buildConstraintViolationWithTemplate("fail to access on the DB").addConstraintViolation();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user