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