Implementing validators for Reference persist models (completed)

This commit is contained in:
Thomas Georgios Giannos 2024-01-04 10:15:29 +02:00
parent 659c213360
commit eb2b5d4df3
5 changed files with 151 additions and 60 deletions

View File

@ -8,6 +8,7 @@ import eu.eudat.errorcode.ErrorThesaurusProperties;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@ -108,7 +109,24 @@ public class AuthenticationConfigurationPersist {
@Override
protected List<Specification> specifications(AuthenticationConfigurationPersist item) {
return Arrays.asList(
this.spec()
.must(() -> !this.isNull(item.getEnabled()))
.failOn(AuthenticationConfigurationPersist._enabled).failWith(messageSource.getMessage("Validation_Required", new Object[]{AuthenticationConfigurationPersist._enabled}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getAuthUrl()))
.failOn(AuthenticationConfigurationPersist._authUrl).failWith(messageSource.getMessage("Validation_Required", new Object[]{AuthenticationConfigurationPersist._authUrl}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isNull(item.getAuthMethod()))
.failOn(AuthenticationConfigurationPersist._authMethod).failWith(messageSource.getMessage("Validation_Required", new Object[]{AuthenticationConfigurationPersist._authMethod}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getAuthTokenPath()))
.failOn(AuthenticationConfigurationPersist._authTokenPath).failWith(messageSource.getMessage("Validation_Required", new Object[]{AuthenticationConfigurationPersist._authTokenPath}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getAuthRequestBody()))
.failOn(AuthenticationConfigurationPersist._authRequestBody).failWith(messageSource.getMessage("Validation_Required", new Object[]{AuthenticationConfigurationPersist._authRequestBody}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getType()))
.failOn(AuthenticationConfigurationPersist._type).failWith(messageSource.getMessage("Validation_Required", new Object[]{AuthenticationConfigurationPersist._type}, LocaleContextHolder.getLocale()))
);
}
}

View File

@ -2,14 +2,14 @@ package eu.eudat.model.persist.referencetypedefinition;
import eu.eudat.commons.enums.ReferenceFieldDataType;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.commons.validation.old.ValidEnum;
import eu.eudat.commons.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import eu.eudat.model.persist.DescriptionPersist;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@ -17,74 +17,85 @@ import java.util.List;
public class ReferenceTypeFieldPersist {
private String code = null;
private String code = null;
public static final String _code = "code";
public static final String _code = "code";
private String label = null;
private String label = null;
public static final String _label = "label";
public static final String _label = "label";
private String description;
private String description;
private ReferenceFieldDataType dataType;
private ReferenceFieldDataType dataType;
public static final String _dataType = "dataType";
public static final String _dataType = "dataType";
public String getCode() {
return code;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public void setCode(String code) {
this.code = code;
}
public String getLabel() {
return label;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public void setLabel(String label) {
this.label = label;
}
public String getDescription() {
return description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setDescription(String description) {
this.description = description;
}
public ReferenceFieldDataType getDataType() {
return dataType;
}
public ReferenceFieldDataType getDataType() {
return dataType;
}
public void setDataType(ReferenceFieldDataType dataType) {
this.dataType = dataType;
}
public void setDataType(ReferenceFieldDataType dataType) {
this.dataType = dataType;
}
@Component(ReferenceTypeFieldPersistValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class ReferenceTypeFieldPersistValidator extends BaseValidator<ReferenceTypeFieldPersist> {
@Component(ReferenceTypeFieldPersistValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class ReferenceTypeFieldPersistValidator extends BaseValidator<ReferenceTypeFieldPersist> {
public static final String ValidatorName = "ReferenceTypeFieldPersistValidator";
public static final String ValidatorName = "ReferenceTypeFieldPersistValidator";
protected ReferenceTypeFieldPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors) {
super(conventionService, errors);
}
private final MessageSource messageSource;
@Override
protected Class<ReferenceTypeFieldPersist> modelClass() {
return ReferenceTypeFieldPersist.class;
}
protected ReferenceTypeFieldPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) {
super(conventionService, errors);
this.messageSource = messageSource;
}
@Override
protected List<Specification> specifications(ReferenceTypeFieldPersist item) {
return Arrays.asList(
@Override
protected Class<ReferenceTypeFieldPersist> modelClass() {
return ReferenceTypeFieldPersist.class;
}
);
}
}
@Override
protected List<Specification> specifications(ReferenceTypeFieldPersist item) {
return Arrays.asList(
this.spec()
.must(() -> !this.isEmpty(item.getCode()))
.failOn(ReferenceTypeFieldPersist._code).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeFieldPersist._code}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getLabel()))
.failOn(ReferenceTypeFieldPersist._label).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeFieldPersist._label}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isNull(item.getDataType()))
.failOn(ReferenceTypeFieldPersist._dataType).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeFieldPersist._dataType}, LocaleContextHolder.getLocale()))
);
}
}
}

View File

@ -2,16 +2,13 @@ package eu.eudat.model.persist.referencetypedefinition;
import eu.eudat.commons.enums.ReferenceTypeExternalApiHTTPMethodType;
import eu.eudat.commons.validation.ValidatorFactory;
import eu.eudat.commons.validation.old.ValidEnum;
import eu.eudat.commons.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@ -21,7 +18,7 @@ public class ReferenceTypeSourceExternalApiConfigurationPersist extends Referenc
private String url;
public static final String _uri = "uri";
public static final String _url = "url";
private ResultsConfigurationPersist results;
@ -154,7 +151,46 @@ public class ReferenceTypeSourceExternalApiConfigurationPersist extends Referenc
protected List<Specification> specifications(ReferenceTypeSourceExternalApiConfigurationPersist item) {
List<Specification> specifications = getBaseSpecifications(item);
specifications.addAll(Arrays.asList(
this.spec()
.must(() -> !this.isEmpty(item.getUrl()))
.failOn(ReferenceTypeSourceExternalApiConfigurationPersist._url).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeSourceExternalApiConfigurationPersist._url}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getPaginationPath()))
.failOn(ReferenceTypeSourceExternalApiConfigurationPersist._paginationPath).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeSourceExternalApiConfigurationPersist._paginationPath}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getContentType()))
.failOn(ReferenceTypeSourceExternalApiConfigurationPersist._contentType).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeSourceExternalApiConfigurationPersist._contentType}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getFirstPage()))
.failOn(ReferenceTypeSourceExternalApiConfigurationPersist._firstPage).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeSourceExternalApiConfigurationPersist._firstPage}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isNull(item.getHttpMethod()))
.failOn(ReferenceTypeSourceExternalApiConfigurationPersist._httpMethod).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeSourceExternalApiConfigurationPersist._httpMethod}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isNull(item.getResults()))
.failOn(ReferenceTypeSourceExternalApiConfigurationPersist._results).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeSourceExternalApiConfigurationPersist._results}, LocaleContextHolder.getLocale())),
this.refSpec()
.iff(() -> !this.isNull(item.getResults()))
.on(ReferenceTypeSourceExternalApiConfigurationPersist._results)
.over(item.getResults())
.using(() -> this.validatorFactory.validator(ResultsConfigurationPersist.ResultsConfigurationPersistValidator.class)),
this.spec()
.must(() -> !this.isNull(item.getAuth()))
.failOn(ReferenceTypeSourceExternalApiConfigurationPersist._auth).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeSourceExternalApiConfigurationPersist._auth}, LocaleContextHolder.getLocale())),
this.refSpec()
.iff(() -> !this.isNull(item.getAuth()))
.on(ReferenceTypeSourceExternalApiConfigurationPersist._auth)
.over(item.getAuth())
.using(() -> this.validatorFactory.validator(AuthenticationConfigurationPersist.AuthenticationConfigurationPersistValidator.class)),
this.spec()
.must(() -> !this.isNull(item.getQueries()))
.failOn(ReferenceTypeSourceExternalApiConfigurationPersist._queries).failWith(messageSource.getMessage("Validation_Required", new Object[]{ReferenceTypeSourceExternalApiConfigurationPersist._queries}, LocaleContextHolder.getLocale())),
this.navSpec()
.iff(() -> !this.isNull(item.getQueries()))
.on(ReferenceTypeSourceExternalApiConfigurationPersist._queries)
.over(item.getQueries())
.using(() -> this.validatorFactory.validator(QueryConfigPersist.QueryConfigPersistValidator.class))
));
return specifications;
}

View File

@ -5,7 +5,9 @@ import eu.eudat.commons.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@ -43,8 +45,11 @@ public class ResultFieldsMappingConfigurationPersist {
public static final String ValidatorName = "ResultFieldsMappingConfigurationPersistValidator";
protected ResultFieldsMappingConfigurationPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors) {
private final MessageSource messageSource;
protected ResultFieldsMappingConfigurationPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) {
super(conventionService, errors);
this.messageSource = messageSource;
}
@Override
@ -55,7 +60,12 @@ public class ResultFieldsMappingConfigurationPersist {
@Override
protected List<Specification> specifications(ResultFieldsMappingConfigurationPersist item) {
return Arrays.asList(
this.spec()
.must(() -> !this.isEmpty(item.getCode()))
.failOn(ResultFieldsMappingConfigurationPersist._code).failWith(messageSource.getMessage("Validation_Required", new Object[]{ResultFieldsMappingConfigurationPersist._code}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getResponsePath()))
.failOn(ResultFieldsMappingConfigurationPersist._responsePath).failWith(messageSource.getMessage("Validation_Required", new Object[]{ResultFieldsMappingConfigurationPersist._responsePath}, LocaleContextHolder.getLocale()))
);
}
}

View File

@ -1,12 +1,14 @@
package eu.eudat.model.persist.referencetypedefinition;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.commons.validation.ValidatorFactory;
import eu.eudat.commons.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@ -46,9 +48,12 @@ public class ResultsConfigurationPersist {
private final MessageSource messageSource;
protected ResultsConfigurationPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) {
private final ValidatorFactory validatorFactory;
protected ResultsConfigurationPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) {
super(conventionService, errors);
this.messageSource = messageSource;
this.validatorFactory = validatorFactory;
}
@Override
@ -59,7 +64,18 @@ public class ResultsConfigurationPersist {
@Override
protected List<Specification> specifications(ResultsConfigurationPersist item) {
return Arrays.asList(
this.spec()
.must(() -> !this.isEmpty(item.getResultsArrayPath()))
.failOn(ResultsConfigurationPersist._resultsArrayPath).failWith(messageSource.getMessage("Validation_Required", new Object[]{ResultsConfigurationPersist._resultsArrayPath}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isNull(item.getFieldsMapping()))
.failOn(ResultsConfigurationPersist._fieldsMapping).failWith(messageSource.getMessage("Validation_Required", new Object[]{ResultsConfigurationPersist._fieldsMapping}, LocaleContextHolder.getLocale())),
this.navSpec()
.iff(() -> !this.isNull(item.getFieldsMapping()))
.on(ResultsConfigurationPersist._fieldsMapping)
.over(item.getFieldsMapping())
.using(() -> this.validatorFactory.validator(ResultFieldsMappingConfigurationPersist.ResultFieldsMappingConfigurationPersistValidator.class))
);
}
}