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

65 lines
1.9 KiB
Java

package eu.eudat.commons.validation;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import gr.cite.tools.exception.MyValidationException;
import gr.cite.tools.validation.AbstractValidator;
import gr.cite.tools.validation.ValidationResult;
import org.apache.commons.validator.routines.EmailValidator;
import java.util.*;
public abstract class BaseValidator<T> extends AbstractValidator<T> {
protected final ConventionService conventionService;
protected final ErrorThesaurusProperties errors;
protected BaseValidator(ConventionService conventionService, ErrorThesaurusProperties errors) {
this.conventionService = conventionService;
this.errors = errors;
}
@Override
public void validateForce(Object target) {
this.validate(target);
ValidationResult result = result();
if (!result.isValid()) {
List<Map.Entry<String, List<String>>> errorsMap = this.flattenValidationResult();
throw new MyValidationException(this.errors.getModelValidation().getCode(), errorsMap);
}
}
protected Boolean isValidGuid(UUID guid) {
return this.conventionService.isValidGuid(guid);
}
protected Boolean isValidEmail(String value) {
return EmailValidator.getInstance().isValid(value);
}
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 isNull(Collection<?> value) {
return value == null;
}
protected Boolean lessEqualLength(String value, int size) {
return value.length() <= size;
}
protected Boolean lessEqual(Integer value, int target) {
return value <= target;
}
}