package gr.cite.annotation.model.persist; import gr.cite.annotation.common.validation.BaseValidator; import gr.cite.annotation.convention.ConventionService; import gr.cite.annotation.errorcode.ErrorThesaurusProperties; 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 AnnotationPersist { private UUID subjectId; public static final String _subjectId = "subjectId"; private UUID entityId; public static final String _entityId = "entityId"; private String entityType; public static final String _entityType = "entityType"; private String anchor; public static final String _anchor = "anchor"; private String payload; public static final String _payload = "payload"; public UUID getSubjectId() { return subjectId; } public void setSubjectId(UUID subjectId) { this.subjectId = subjectId; } public UUID getEntityId() { return entityId; } public void setEntityId(UUID entityId) { this.entityId = entityId; } public String getEntityType() { return entityType; } public void setEntityType(String entityType) { this.entityType = entityType; } public String getAnchor() { return anchor; } public void setAnchor(String anchor) { this.anchor = anchor; } public String getPayload() { return payload; } public void setPayload(String payload) { this.payload = payload; } @Component(AnnotationPersistValidator.ValidatorName) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public static class AnnotationPersistValidator extends BaseValidator { public static final String ValidatorName = "AnnotationIntegrationEventPersistValidator"; private final MessageSource messageSource; protected AnnotationPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { super(conventionService, errors); this.messageSource = messageSource; } @Override protected Class modelClass() { return AnnotationPersist.class; } @Override protected List specifications(AnnotationPersist item) { return Arrays.asList( this.spec() .iff(() -> !this.isNull(item.getSubjectId())) .must(() -> this.isValidGuid(item.getSubjectId())) .failOn(AnnotationPersist._subjectId).failWith(messageSource.getMessage("Validation_Required", new Object[]{AnnotationPersist._subjectId}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isNull(item.getEntityId())) .failOn(AnnotationPersist._entityId).failWith(messageSource.getMessage("Validation_Required", new Object[]{AnnotationPersist._entityId}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> !this.isNull(item.getEntityId())) .must(() -> this.isValidGuid(item.getEntityId())) .failOn(AnnotationPersist._entityId).failWith(messageSource.getMessage("Validation_Required", new Object[]{AnnotationPersist._entityId}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isEmpty(item.getEntityType())) .failOn(AnnotationPersist._entityType).failWith(messageSource.getMessage("Validation_Required", new Object[]{AnnotationPersist._entityType}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isEmpty(item.getPayload())) .failOn(AnnotationPersist._payload).failWith(messageSource.getMessage("Validation_Required", new Object[]{AnnotationPersist._payload}, LocaleContextHolder.getLocale())) ); } } }