package gr.cite.notification.model.persist; import gr.cite.notification.common.enums.NotificationTemplateChannel; import gr.cite.notification.common.enums.NotificationTemplateKind; import gr.cite.notification.common.validation.BaseValidator; import gr.cite.notification.convention.ConventionService; import gr.cite.notification.errorcode.ErrorThesaurusProperties; import gr.cite.notification.model.persist.notificationtemplate.NotificationTemplateValuePersist; 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 NotificationTemplatePersist { private UUID id; public static final String _id = "id"; private NotificationTemplateChannel channel; public static final String _channel = "channel"; private UUID notificationType; public static final String _notificationType = "notificationType"; private NotificationTemplateKind kind; public static final String _kind = "kind"; private String languageCode; public static final String _languageId = "languageId"; private NotificationTemplateValuePersist value; public static final String _value = "value"; private String hash; public static final String _hash = "hash"; public UUID getId() { return id; } public void setId(UUID id) { this.id = id; } public NotificationTemplateChannel getChannel() { return channel; } public void setChannel(NotificationTemplateChannel channel) { this.channel = channel; } public UUID getNotificationType() { return notificationType; } public void setNotificationType(UUID notificationType) { this.notificationType = notificationType; } public NotificationTemplateKind getKind() { return kind; } public void setKind(NotificationTemplateKind kind) { this.kind = kind; } public String getLanguageCode() { return languageCode; } public void setLanguageCode(String languageCode) { this.languageCode = languageCode; } public NotificationTemplateValuePersist getValue() { return value; } public void setValue(NotificationTemplateValuePersist value) { this.value = value; } public String getHash() { return hash; } public void setHash(String hash) { this.hash = hash; } @Component(NotificationTemplatePersist.NotificationTemplatePersistValidator.ValidatorName) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public static class NotificationTemplatePersistValidator extends BaseValidator { public static final String ValidatorName = "NotificationTemplatePersistValidator"; private final MessageSource messageSource; private final ValidatorFactory validatorFactory; protected NotificationTemplatePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { super(conventionService, errors); this.messageSource = messageSource; this.validatorFactory = validatorFactory; } @Override protected Class modelClass() { return NotificationTemplatePersist.class; } @Override protected List specifications(NotificationTemplatePersist item) { return Arrays.asList( this.spec() .iff(() -> this.isValidGuid(item.getId())) .must(() -> this.isValidHash(item.getHash())) .failOn(NotificationTemplatePersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplatePersist._hash}, LocaleContextHolder.getLocale())), this.spec() .iff(() -> !this.isValidGuid(item.getId())) .must(() -> !this.isValidHash(item.getHash())) .failOn(NotificationTemplatePersist._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isNull(item.getLanguageCode())) .failOn(NotificationTemplatePersist._languageId).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplatePersist._languageId}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isNull(item.getNotificationType())) .failOn(NotificationTemplatePersist._notificationType).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplatePersist._notificationType}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isNull(item.getKind())) .failOn(NotificationTemplatePersist._kind).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplatePersist._kind}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isNull(item.getChannel())) .failOn(NotificationTemplatePersist._channel).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplatePersist._channel}, LocaleContextHolder.getLocale())), this.spec() .must(() -> !this.isNull(item.getValue())) .failOn(NotificationTemplatePersist._value).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplatePersist._value}, LocaleContextHolder.getLocale())), this.refSpec() .iff(() -> !this.isNull(item.getValue())) .on(NotificationTemplatePersist._value) .over(item.getValue()) .using(() -> this.validatorFactory.validator(NotificationTemplateValuePersist.NotificationTemplateValuePersistValidator.class)) ); } } }