argos/dmp-backend/core/src/main/java/eu/eudat/commons/validation/BaseValidator.java

79 lines
2.2 KiB
Java
Raw Normal View History

2023-12-18 17:23:37 +01:00
package eu.eudat.commons.validation;
2023-12-19 13:54:17 +01:00
import eu.eudat.convention.ConventionService;
2023-12-18 17:23:37 +01:00
import eu.eudat.errorcode.ErrorThesaurusProperties;
import gr.cite.tools.exception.MyValidationException;
2024-01-17 10:20:02 +01:00
import gr.cite.tools.validation.AbstractValidator;
import gr.cite.tools.validation.ValidationResult;
2024-01-15 12:57:33 +01:00
import org.apache.commons.validator.routines.EmailValidator;
2023-12-19 13:54:17 +01:00
import java.util.*;
2023-12-18 17:23:37 +01:00
2023-12-19 14:46:31 +01:00
public abstract class BaseValidator<T> extends AbstractValidator<T> {
2023-12-19 13:54:17 +01:00
protected final ConventionService conventionService;
2023-12-18 17:23:37 +01:00
protected final ErrorThesaurusProperties errors;
2023-12-19 13:54:17 +01:00
protected BaseValidator(ConventionService conventionService, ErrorThesaurusProperties errors) {
this.conventionService = conventionService;
2023-12-18 17:23:37 +01:00
this.errors = errors;
}
@Override
public void validateForce(Object target) {
this.validate(target);
2023-12-19 13:54:17 +01:00
ValidationResult result = result();
2023-12-19 14:46:31 +01:00
if (!result.isValid()) {
2023-12-19 13:54:17 +01:00
List<Map.Entry<String, List<String>>> errorsMap = this.flattenValidationResult();
2023-12-18 17:23:37 +01:00
throw new MyValidationException(this.errors.getModelValidation().getCode(), errorsMap);
}
}
2023-12-19 13:54:17 +01:00
protected Boolean isValidGuid(UUID guid) {
return this.conventionService.isValidGuid(guid);
}
2024-01-15 12:57:33 +01:00
protected Boolean isValidEmail(String value) {
return EmailValidator.getInstance().isValid(value);
}
2023-12-19 13:54:17 +01:00
protected Boolean isValidHash(String hash) {
return this.conventionService.isValidHash(hash);
}
protected Boolean isEmpty(String value) {
return this.conventionService.isNullOrEmpty(value);
}
protected Boolean isListNullOrEmpty(List<?> value) {
return this.conventionService.isListNullOrEmpty(value);
}
protected Boolean isNull(Object value) {
return value == null;
}
protected boolean isBoolean(String value) {
return value != null && Arrays.stream(new String[]{"true", "false"})
.anyMatch(b -> b.equalsIgnoreCase(value));
}
protected boolean isUUID(String value) {
try {
UUID.fromString(value);
return true;
} catch (Exception e){
return false;
}
}
protected Boolean isNull(Collection<?> value) {
return value == null;
}
protected Boolean lessEqualLength(String value, int size) {
2023-12-19 13:54:17 +01:00
return value.length() <= size;
2023-12-18 17:23:37 +01:00
}
protected Boolean lessEqual(Integer value, int target) {
return value <= target;
}
2023-12-18 17:23:37 +01:00
}