package eu.eudat.model.persist; import eu.eudat.commons.validation.BaseValidator; import eu.eudat.convention.ConventionService; import eu.eudat.errorcode.ErrorThesaurusProperties; import eu.eudat.model.persist.prefillingsourcedefinition.PrefillingSourceDefinitionPersist; import gr.cite.tools.validation.ValidatorFactory; import gr.cite.tools.validation.specification.Specification; 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; import java.util.List; import java.util.UUID; public class PrefillingSourcePersist { private UUID id; private String label; public static final String _label = "label"; private PrefillingSourceDefinitionPersist definition; public static final String _definition = "definition"; private String hash; public static final String _hash = "hash"; public UUID getId() { return id; } public void setId(UUID id) { this.id = id; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public PrefillingSourceDefinitionPersist getDefinition() { return definition; } public void setDefinition(PrefillingSourceDefinitionPersist definition) { this.definition = definition; } public String getHash() { return hash; } public void setHash(String hash) { this.hash = hash; } @Component(PrefillingSourcePersistValidator.ValidatorName) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public static class PrefillingSourcePersistValidator extends BaseValidator { public static final String ValidatorName = "PrefillingSourcePersistValidator"; private final MessageSource messageSource; private final ValidatorFactory validatorFactory; protected PrefillingSourcePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { super(conventionService, errors); this.messageSource = messageSource; this.validatorFactory = validatorFactory; } @Override protected Class modelClass() { return PrefillingSourcePersist.class; } @Override protected List specifications(PrefillingSourcePersist item) { return Arrays.asList( this.spec() .iff(() -> this.isValidGuid(item.getId())) .must(() -> this.isValidHash(item.getHash())) .failOn(PrefillingSourcePersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{PrefillingSourcePersist._hash}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> !this.isValidGuid(item.getId())) .must(() -> !this.isValidHash(item.getHash())) .failOn(PrefillingSourcePersist._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isEmpty(item.getLabel())) .failOn(PrefillingSourcePersist._label).failWith(messageSource.getMessage("Validation_Required", new Object[]{PrefillingSourcePersist._label}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isNull(item.getDefinition())) .failOn(PrefillingSourcePersist._definition).failWith(messageSource.getMessage("Validation_Required", new Object[]{PrefillingSourcePersist._definition}, LocaleContextHolder.getLocale())), this.refSpec() .iff(() -> !this.isNull(item.getDefinition())) .on(PrefillingSourcePersist._definition) .over(item.getDefinition()) .using(() -> this.validatorFactory.validator(PrefillingSourceDefinitionPersist.PrefillingSourceDefinitionPersistValidator.class)) ); } } }