Skip to content
Snippets Groups Projects
Commit 10e4077a authored by OZGCloud's avatar OZGCloud
Browse files

OZG-5841 OZG-5952 Refactor the validator

parent 286eab25
No related branches found
No related tags found
No related merge requests found
package de.ozgcloud.alfa.export;
import java.util.Optional;
import java.util.function.Supplier;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
......@@ -12,24 +15,33 @@ public class BehoerdenschluesselPropertiesValidator implements Validator {
@Override
public void validate(Object target, Errors errors) {
var props = getBehoerdenschluesselProperties((XdomeaProperties) target);
if (props.getBehoerdenschluessel().isEmpty()) {
return;
var properties = getBehoerdenschluesselProperties((XdomeaProperties) target);
if (properties.getBehoerdenschluessel().isPresent()) {
validateProperties(properties, errors);
}
try {
if (props.getBehoerdenschluesselUri().isEmpty()) {
errors.rejectValue("behoerdenschluesselUri", "ozgcloud.xdomea.behoerdenschluessel-uri.empty", "behoerdenschluessel-uri must be set");
}
if (props.getBehoerdenschluesselVersion().isEmpty()) {
errors.rejectValue("behoerdenschluesselVersion", "ozgcloud.xdomea.behoerdenschluessel-version.empty",
"behoerdenschluessel-version must be set");
BehoerdenschluesselProperties getBehoerdenschluesselProperties(XdomeaProperties xdomeaProperties) {
return new BehoerdenschluesselProperties(xdomeaProperties);
}
private void validateProperties(BehoerdenschluesselProperties props, Errors errors) {
try {
validateNotEmpty(props, errors);
} catch (AmbiguousCodelistePatternException e) {
errors.reject("ozgcloud.xdomea.codeliste.pattern.ambiguous", e.getMessage());
}
}
BehoerdenschluesselProperties getBehoerdenschluesselProperties(XdomeaProperties xdomeaProperties) {
return new BehoerdenschluesselProperties(xdomeaProperties);
private void validateNotEmpty(BehoerdenschluesselProperties props, Errors errors) {
validateNotEmpty(props::getBehoerdenschluesselUri, "behoerdenschluesselUri", "behoerdenschluessel-uri", errors);
validateNotEmpty(props::getBehoerdenschluesselVersion, "behoerdenschluesselVersion", "behoerdenschluessel-version", errors);
}
private <T> void validateNotEmpty(Supplier<Optional<T>> propertyAccessor, String fieldName, String propertyName, Errors errors) {
var propertyValue = propertyAccessor.get();
if (propertyValue.isEmpty()) {
errors.rejectValue(fieldName, String.format("ozgcloud.xdomea.%s.empty", propertyName), String.format("%s must be set", propertyName));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment