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.DmpEntity; 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.ArrayList; import java.util.Arrays; import java.util.List; import java.util.UUID; public class NewVersionDmpPersist { private UUID id = null; public static final String _id = "id"; private String label = null; public static final String _label = "label"; private String description = null; public static final String _description = "description"; private UUID blueprintId = null; public static final String _blueprintId = "blueprintId"; private List descriptions = new ArrayList<>(); public static final String _descriptions = "descriptions"; 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 String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public UUID getBlueprintId() { return blueprintId; } public void setBlueprintId(UUID blueprintId) { this.blueprintId = blueprintId; } public List getDescriptions() { return descriptions; } public void setDescriptions(List descriptions) { this.descriptions = descriptions; } public String getHash() { return hash; } public void setHash(String hash) { this.hash = hash; } @Component(NewVersionDmpPersist.NewVersionDmpPersistValidator.ValidatorName) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public static class NewVersionDmpPersistValidator extends BaseValidator { public static final String ValidatorName = "NewVersionDmpPersistValidator"; private final MessageSource messageSource; protected NewVersionDmpPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { super(conventionService, errors); this.messageSource = messageSource; } @Override protected Class modelClass() { return NewVersionDmpPersist.class; } @Override protected List specifications(NewVersionDmpPersist item) { return Arrays.asList( this.spec() .iff(() -> this.isValidGuid(item.getId())) .must(() -> this.isValidHash(item.getHash())) .failOn(NewVersionDmpPersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{NewVersionDmpPersist._hash}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isEmpty(item.getLabel())) .failOn(NewVersionDmpPersist._label).failWith(messageSource.getMessage("Validation_Required", new Object[]{NewVersionDmpPersist._label}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> !this.isEmpty(item.getLabel())) .must(() -> this.lessEqualLength(item.getLabel(), DmpEntity._labelLength)) .failOn(NewVersionDmpPersist._label).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{NewVersionDmpPersist._label}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isEmpty(item.getDescription())) .failOn(NewVersionDmpPersist._description).failWith(messageSource.getMessage("Validation_Required", new Object[]{NewVersionDmpPersist._description}, LocaleContextHolder.getLocale())), this.spec() .must(() -> this.isValidGuid(item.getBlueprintId())) .failOn(NewVersionDmpPersist._blueprintId).failWith(messageSource.getMessage("Validation_Required", new Object[]{NewVersionDmpPersist._blueprintId}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isNull(item.getDescriptions())) .failOn(NewVersionDmpPersist._descriptions).failWith(messageSource.getMessage("Validation_Required", new Object[]{NewVersionDmpPersist._descriptions}, LocaleContextHolder.getLocale())) ); } } }