package eu.eudat.model.persist; import eu.eudat.commons.validation.BaseValidator; import gr.cite.tools.validation.specification.Specification; import eu.eudat.convention.ConventionService; import eu.eudat.data.LanguageEntity; 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; import java.util.List; import java.util.UUID; public class LanguagePersist { private UUID id; private String code; public static final String _code = "code"; private String payload; private Integer ordinal; public static final String _ordinal = "ordinal"; private String hash; public static final String _hash = "hash"; public UUID getId() { return id; } public void setId(UUID id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getPayload() { return payload; } public void setPayload(String payload) { this.payload = payload; } public Integer getOrdinal() { return ordinal; } public void setOrdinal(Integer ordinal) { this.ordinal = ordinal; } public String getHash() { return hash; } public void setHash(String hash) { this.hash = hash; } @Component(LanguagePersistValidator.ValidatorName) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public static class LanguagePersistValidator extends BaseValidator { public static final String ValidatorName = "LanguagePersistValidator"; private final MessageSource messageSource; protected LanguagePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { super(conventionService, errors); this.messageSource = messageSource; } @Override protected Class modelClass() { return LanguagePersist.class; } @Override protected List specifications(LanguagePersist item) { return Arrays.asList( this.spec() .iff(() -> this.isValidGuid(item.getId())) .must(() -> this.isValidHash(item.getHash())) .failOn(LanguagePersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{LanguagePersist._hash}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> !this.isValidGuid(item.getId())) .must(() -> !this.isValidHash(item.getHash())) .failOn(LanguagePersist._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isEmpty(item.getCode())) .failOn(LanguagePersist._code).failWith(messageSource.getMessage("Validation_Required", new Object[]{LanguagePersist._code}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> !this.isEmpty(item.getCode())) .must(() -> this.lessEqualLength(item.getCode(), LanguageEntity._codeLength)) .failOn(LanguagePersist._code).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{LanguagePersist._code}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isNull(item.getOrdinal())) .failOn(LanguagePersist._ordinal).failWith(messageSource.getMessage("Validation_Required", new Object[]{LanguagePersist._ordinal}, LocaleContextHolder.getLocale())) ); } } }