package eu.eudat.model.persist; import eu.eudat.commons.enums.DmpAccessType; import eu.eudat.commons.enums.DmpStatus; import eu.eudat.commons.validation.BaseValidator; import gr.cite.tools.validation.ValidatorFactory; import gr.cite.tools.validation.specification.Specification; import eu.eudat.convention.ConventionService; import eu.eudat.data.DmpEntity; import eu.eudat.errorcode.ErrorThesaurusProperties; import eu.eudat.model.persist.dmpproperties.DmpPropertiesPersist; 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 DmpPersist { private UUID id; private String label; public static final String _label = "label"; private DmpStatus status; public static final String _status = "status"; private DmpPropertiesPersist properties; public static final String _properties = "properties"; private String description; public static final String _description = "description"; private String language; public static final String _language = "language"; private UUID blueprint; public static final String _blueprint = "blueprint"; private DmpAccessType accessType; public static final String _accessType = "accessType"; // private List references; // // public static final String _references = "references"; private List descriptionTemplates; public static final String _descriptionTemplates = "descriptionTemplates"; private List users; public static final String _users = "users"; 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 DmpStatus getStatus() { return status; } public void setStatus(DmpStatus status) { this.status = status; } public DmpPropertiesPersist getProperties() { return properties; } public void setProperties(DmpPropertiesPersist properties) { this.properties = properties; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } public UUID getBlueprint() { return blueprint; } public void setBlueprint(UUID blueprint) { this.blueprint = blueprint; } public DmpAccessType getAccessType() { return accessType; } public void setAccessType(DmpAccessType accessType) { this.accessType = accessType; } public List getDescriptionTemplates() { return descriptionTemplates; } public void setDescriptionTemplates(List descriptionTemplates) { this.descriptionTemplates = descriptionTemplates; } public List getUsers() { return users; } public void setUsers(List users) { this.users = users; } public String getHash() { return hash; } public void setHash(String hash) { this.hash = hash; } @Component(DmpPersistValidator.ValidatorName) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public static class DmpPersistValidator extends BaseValidator { public static final String ValidatorName = "DmpPersistValidator"; private final MessageSource messageSource; private final ValidatorFactory validatorFactory; protected DmpPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { super(conventionService, errors); this.messageSource = messageSource; this.validatorFactory = validatorFactory; } @Override protected Class modelClass() { return DmpPersist.class; } @Override protected List specifications(DmpPersist item) { return Arrays.asList( this.spec() .iff(() -> this.isValidGuid(item.getId())) .must(() -> this.isValidHash(item.getHash())) .failOn(DmpPersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._hash}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> !this.isValidGuid(item.getId())) .must(() -> !this.isValidHash(item.getHash())) .failOn(DmpPersist._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isEmpty(item.getLabel())) .failOn(DmpPersist._label).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._label}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> !this.isEmpty(item.getLabel())) .must(() -> this.lessEqualLength(item.getLabel(), DmpEntity._labelLength)) .failOn(DmpPersist._label).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{DmpPersist._label}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isNull(item.getStatus())) .failOn(DmpPersist._status).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._status}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> item.getStatus() == DmpStatus.Finalized) .must(() -> this.isValidGuid(item.getBlueprint())) .failOn(DmpPersist._blueprint).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._blueprint}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> item.getStatus() == DmpStatus.Finalized) .must(() -> !this.isNull(item.getProperties())) .failOn(DmpPersist._properties).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._properties}, LocaleContextHolder.getLocale())), this.refSpec() .iff(() -> !this.isNull(item.getProperties())) .on(DmpPersist._properties) .over(item.getProperties()) .using(() -> this.validatorFactory.validator(DmpPropertiesPersist.DmpPropertiesPersistValidator.class).setStatus(item.getStatus())), this.spec() .iff(() -> item.getStatus() == DmpStatus.Finalized) .must(() -> !this.isNull(item.getLanguage())) .failOn(DmpPersist._language).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._language}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> item.getStatus() == DmpStatus.Finalized) .must(() -> !this.isNull(item.getAccessType())) .failOn(DmpPersist._accessType).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._accessType}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> item.getStatus() == DmpStatus.Finalized) .must(() -> !this.isListNullOrEmpty(item.getDescriptionTemplates())) .failOn(DmpPersist._descriptionTemplates).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._descriptionTemplates}, LocaleContextHolder.getLocale())), this.navSpec() .iff(() -> !this.isListNullOrEmpty(item.getDescriptionTemplates())) .on(DmpPersist._descriptionTemplates) .over(item.getDescriptionTemplates()) .using((itm) -> this.validatorFactory.validator(DmpDescriptionTemplatePersist.DmpDescriptionTemplatePersistValidator.class)), this.navSpec() .iff(() -> !this.isListNullOrEmpty(item.getUsers())) .on(DmpPersist._users) .over(item.getUsers()) .using((itm) -> this.validatorFactory.validator(DmpUserPersist.DmpUserPersistValidator.class)) ); } } }