From c26c472ea81c72db8c8cf20579da1d72c660ca16 Mon Sep 17 00:00:00 2001 From: amentis Date: Mon, 22 Jan 2024 11:27:24 +0200 Subject: [PATCH] add notification service persist validators --- .../GlobalExceptionHandler.java | 193 ++++++++++++++++++ .../controllers/NotificationController.java | 5 +- .../NotificationTemplateController.java | 6 +- .../TenantConfigurationController.java | 8 +- .../UserNotificationPreferenceController.java | 5 +- .../resources/messages/messages.properties | 5 +- .../common/validation/BaseValidator.java | 63 ++++++ .../common/validation/EnumNotNull.java | 11 - .../validation/FieldNotNullIfOtherSet.java | 22 -- .../FieldNotNullIfOtherSetValidator.java | 35 ---- .../common/validation/FieldsValueMatch.java | 29 --- .../validation/FieldsValueMatchValidator.java | 31 --- .../common/validation/ValidEnum.java | 16 -- .../common/validation/ValidId.java | 18 -- .../common/validation/ValidIdValidator.java | 40 ---- .../validation/ValidationServiceImpl.java | 36 ---- .../notification/data/NotificationEntity.java | 1 + ...antTouchedIntegrationEventHandlerImpl.java | 6 +- ...serTouchedIntegrationEventHandlerImpl.java | 9 +- .../model/deleter/UserContactInfoDeleter.java | 1 - .../model/persist/NotificationPersist.java | 70 ++++++- .../persist/NotificationTemplatePersist.java | 88 ++++++-- .../TenantTouchedIntegrationEventPersist.java | 59 +++++- .../model/persist/UserContactInfoPersist.java | 117 ----------- .../UserNotificationPreferencePersist.java | 43 ++++ .../UserTouchedIntegrationEventPersist.java | 73 +++++-- .../FieldInfoPersist.java | 57 +++++- .../FieldOptionsPersist.java | 57 +++++- .../NotificationTemplateValuePersist.java | 103 ++++++++-- ...TenantConfigurationEmailClientPersist.java | 82 +++++++- ...enantConfigurationNotifierListPersist.java | 51 ++++- ...igurationUserLocaleIntegrationPersist.java | 51 +++++ .../query/UserContactInfoQuery.java | 1 - 33 files changed, 950 insertions(+), 442 deletions(-) create mode 100644 dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllerhandler/GlobalExceptionHandler.java create mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/BaseValidator.java delete mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/EnumNotNull.java delete mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldNotNullIfOtherSet.java delete mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldNotNullIfOtherSetValidator.java delete mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldsValueMatch.java delete mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldsValueMatchValidator.java delete mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidEnum.java delete mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidId.java delete mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidIdValidator.java delete mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidationServiceImpl.java delete mode 100644 dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserContactInfoPersist.java diff --git a/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllerhandler/GlobalExceptionHandler.java b/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllerhandler/GlobalExceptionHandler.java new file mode 100644 index 000000000..b8a3b685c --- /dev/null +++ b/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllerhandler/GlobalExceptionHandler.java @@ -0,0 +1,193 @@ +package gr.cite.notification.web.controllerhandler; + +import gr.cite.notification.common.JsonHandlingService; +import gr.cite.tools.exception.*; +import gr.cite.tools.logging.LoggerService; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.context.request.WebRequest; + +import java.text.MessageFormat; +import java.util.Map; + +@RestControllerAdvice +@ControllerAdvice +public class GlobalExceptionHandler { + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(GlobalExceptionHandler.class)); + + private final JsonHandlingService jsonHandlingService; + + public GlobalExceptionHandler(JsonHandlingService jsonHandlingService) { + this.jsonHandlingService = jsonHandlingService; + } + + + @ExceptionHandler(Exception.class) + public ResponseEntity handleUnexpectedErrors(Exception exception, WebRequest request) throws Exception { + HandledException handled = this.handleException(exception, request); + this.log(handled.getLevel(), exception, MessageFormat.format("returning code {0} and payload {1}", handled.getStatusCode(), handled.getMessage())); + return new ResponseEntity<>(handled.getMessage(), handled.getStatusCode()); + } + + public void log(System.Logger.Level level, Exception e, String message) { + if (level != null) { + switch (level) { + case TRACE: + logger.trace(message, e); + break; + case DEBUG: + logger.debug(message, e); + break; + case INFO: + logger.info(message, e); + break; + case WARNING: + logger.warn(message, e); + break; + case ERROR: + logger.error(message, e); + break; + } + } + } + + public HandledException handleException(Exception exception, WebRequest request) throws Exception { + HttpStatus statusCode; + Map result; + System.Logger.Level logLevel; + + switch (exception){ + case MyNotFoundException myNotFoundException -> { + logLevel = System.Logger.Level.DEBUG; + statusCode = HttpStatus.NOT_FOUND; + int code = myNotFoundException.getCode(); + if (code > 0) { + result = Map.ofEntries( + Map.entry("code", code), + Map.entry("error", myNotFoundException.getMessage()) + ); + } + else { + result = Map.ofEntries( + Map.entry("error", myNotFoundException.getMessage()) + ); + } + } + case MyUnauthorizedException myUnauthorizedException -> { + logLevel = System.Logger.Level.DEBUG; + statusCode = HttpStatus.UNAUTHORIZED; + int code = myUnauthorizedException.getCode(); + if (code > 0) { + result = Map.ofEntries( + Map.entry("code", code), + Map.entry("error", myUnauthorizedException.getMessage()) + ); + } + else { + result = Map.ofEntries( + Map.entry("error", myUnauthorizedException.getMessage()) + ); + } + } + case MyForbiddenException myForbiddenException -> { + logLevel = System.Logger.Level.DEBUG; + statusCode = HttpStatus.FORBIDDEN; + int code = myForbiddenException.getCode(); + if (code > 0) { + result = Map.ofEntries( + Map.entry("code", code), + Map.entry("error", myForbiddenException.getMessage()) + ); + } + else { + result = Map.ofEntries( + Map.entry("error", myForbiddenException.getMessage()) + ); + } + } + case MyValidationException myValidationException -> { + logLevel = System.Logger.Level.DEBUG; + statusCode = HttpStatus.BAD_REQUEST; + int code = myValidationException.getCode(); + if (code > 0) { + result = Map.ofEntries( + Map.entry("code", code), + Map.entry("error", myValidationException.getMessage()), + Map.entry("message", myValidationException.getErrors()) + ); + } + else { + result = Map.ofEntries( + Map.entry("error", myValidationException.getMessage()), + Map.entry("message", myValidationException.getErrors()) + ); + } + } + case MyApplicationException myApplicationException -> { + logLevel = System.Logger.Level.ERROR; + statusCode = HttpStatus.INTERNAL_SERVER_ERROR; + int code = myApplicationException.getCode(); + if (code > 0) { + result = Map.ofEntries( + Map.entry("code", code), + Map.entry("error", myApplicationException.getMessage()) + ); + } + else { + result = Map.ofEntries( + Map.entry("error", myApplicationException.getMessage()) + ); + } + } + default -> { + logLevel = System.Logger.Level.ERROR; + statusCode = HttpStatus.INTERNAL_SERVER_ERROR; + result = Map.ofEntries( + Map.entry("error", "System error") + ); + } + }; + String serialization = this.jsonHandlingService.toJsonSafe(result); + return new HandledException(statusCode, serialization, logLevel); + } + + public static class HandledException{ + public HttpStatus statusCode; + public String message; + public System.Logger.Level level; + + public HandledException(HttpStatus statusCode, String message, System.Logger.Level level) { + this.statusCode = statusCode; + this.message = message; + this.level = level; + } + + public HttpStatus getStatusCode() { + return statusCode; + } + + public void setStatusCode(HttpStatus statusCode) { + this.statusCode = statusCode; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public System.Logger.Level getLevel() { + return level; + } + + public void setLevel(System.Logger.Level level) { + this.level = level; + } + } +} diff --git a/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/NotificationController.java b/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/NotificationController.java index 789ab6d5e..913a96a1b 100644 --- a/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/NotificationController.java +++ b/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/NotificationController.java @@ -21,7 +21,7 @@ import gr.cite.tools.exception.MyNotFoundException; import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.MapLogEntry; -import gr.cite.tools.validation.MyValidate; +import gr.cite.tools.validation.ValidationFilterAnnotation; import jakarta.transaction.Transactional; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -97,7 +97,8 @@ public class NotificationController { @PostMapping("persist") @Transactional - public Notification Persist(@MyValidate @RequestBody NotificationPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { + @ValidationFilterAnnotation(validator = NotificationPersist.NotificationPersistValidator.ValidatorName, argumentName = "model") + public Notification Persist(@RequestBody NotificationPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { logger.debug(new MapLogEntry("persisting" + Notification.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); Notification persisted = this.notificationService.persist(model, fieldSet); diff --git a/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/NotificationTemplateController.java b/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/NotificationTemplateController.java index ba3050101..807e71d3e 100644 --- a/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/NotificationTemplateController.java +++ b/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/NotificationTemplateController.java @@ -21,7 +21,7 @@ import gr.cite.tools.exception.MyNotFoundException; import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.MapLogEntry; -import gr.cite.tools.validation.MyValidate; +import gr.cite.tools.validation.ValidationFilterAnnotation; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; @@ -76,7 +76,6 @@ public class NotificationTemplateController { } @GetMapping("{id}") - @Transactional public NotificationTemplate get(@PathVariable UUID id, FieldSet fieldSet, Locale locale) throws MyApplicationException, MyForbiddenException, MyNotFoundException { logger.debug(new MapLogEntry("retrieving" + NotificationTemplate.class.getSimpleName()).And("id", id).And("fields", fieldSet)); @@ -97,7 +96,8 @@ public class NotificationTemplateController { @PostMapping("persist") @Transactional - public NotificationTemplate persist(@MyValidate @RequestBody NotificationTemplatePersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { + @ValidationFilterAnnotation(validator = NotificationTemplatePersist.NotificationTemplatePersistValidator.ValidatorName, argumentName = "model") + public NotificationTemplate persist(@RequestBody NotificationTemplatePersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { logger.debug(new MapLogEntry("persisting" + NotificationTemplate.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); NotificationTemplate persisted = this.notificationTemplateService.persist(model, fieldSet); diff --git a/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/TenantConfigurationController.java b/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/TenantConfigurationController.java index 82de8be48..dfa0de411 100644 --- a/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/TenantConfigurationController.java +++ b/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/TenantConfigurationController.java @@ -8,6 +8,7 @@ import gr.cite.notification.data.TenantConfigurationEntity; import gr.cite.notification.model.TenantConfiguration; import gr.cite.notification.model.builder.TenantConfigurationBuilder; import gr.cite.notification.model.censorship.TenantConfigurationCensor; +import gr.cite.notification.model.persist.NotificationTemplatePersist; import gr.cite.notification.model.persist.tenantconfiguration.TenantConfigurationEmailClientPersist; import gr.cite.notification.model.persist.tenantconfiguration.TenantConfigurationNotifierListPersist; import gr.cite.notification.query.TenantConfigurationQuery; @@ -25,6 +26,7 @@ import gr.cite.tools.exception.MyNotFoundException; import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.MapLogEntry; +import gr.cite.tools.validation.ValidationFilterAnnotation; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; @@ -101,7 +103,8 @@ public class TenantConfigurationController { @PostMapping("persist/email-client") @Transactional - public TenantConfiguration persist(@RequestBody @Valid TenantConfigurationEmailClientPersist model, FieldSet fieldSet) + @ValidationFilterAnnotation(validator = TenantConfigurationEmailClientPersist.TenantConfigurationEmailClientPersistValidator.ValidatorName, argumentName = "model") + public TenantConfiguration persist(@RequestBody TenantConfigurationEmailClientPersist model, FieldSet fieldSet) { logger.debug(new MapLogEntry("persisting").And("type", TenantConfigurationType.EMAIL_CLIENT_CONFIGURATION).And("model", model).And("fields", fieldSet)); @@ -134,7 +137,8 @@ public class TenantConfigurationController { @PostMapping("persist/notifier-list") @Transactional - public TenantConfiguration persist(@RequestBody @Valid TenantConfigurationNotifierListPersist model, FieldSet fieldSet) + @ValidationFilterAnnotation(validator = TenantConfigurationNotifierListPersist.TenantConfigurationNotifierListPersistValidator.ValidatorName, argumentName = "model") + public TenantConfiguration persist(@RequestBody TenantConfigurationNotifierListPersist model, FieldSet fieldSet) { logger.debug(new MapLogEntry("persisting").And("type", TenantConfigurationType.NOTIFIER_LIST).And("model", model).And("fields", fieldSet)); diff --git a/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/UserNotificationPreferenceController.java b/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/UserNotificationPreferenceController.java index 1a54be1b0..8ff1323fe 100644 --- a/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/UserNotificationPreferenceController.java +++ b/dmp-backend/notification-service/notification-web/src/main/java/gr/cite/notification/web/controllers/UserNotificationPreferenceController.java @@ -25,7 +25,7 @@ import gr.cite.tools.exception.MyNotFoundException; import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.MapLogEntry; -import jakarta.validation.Valid; +import gr.cite.tools.validation.ValidationFilterAnnotation; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; @@ -116,7 +116,8 @@ public class UserNotificationPreferenceController { @PostMapping("persist") @Transactional - public List persist(@RequestBody @Valid UserNotificationPreferencePersist model, FieldSet fieldSet) + @ValidationFilterAnnotation(validator = UserNotificationPreferencePersist.UserNotificationPreferencePersistValidator.ValidatorName, argumentName = "model") + public List persist(@RequestBody UserNotificationPreferencePersist model, FieldSet fieldSet) { logger.debug(new MapLogEntry("persisting").And("type", TenantConfigurationType.NOTIFIER_LIST).And("model", model).And("fields", fieldSet)); diff --git a/dmp-backend/notification-service/notification-web/src/main/resources/messages/messages.properties b/dmp-backend/notification-service/notification-web/src/main/resources/messages/messages.properties index 9f8ede6f6..93d6cd27a 100644 --- a/dmp-backend/notification-service/notification-web/src/main/resources/messages/messages.properties +++ b/dmp-backend/notification-service/notification-web/src/main/resources/messages/messages.properties @@ -4,4 +4,7 @@ validation.lowerthanmin=Value must be larger than {value} validation.largerthanmax=Value must be less than {value} validation.invalidid=Not valid id General_ItemNotFound=Item {0} of type {1} not found -Validation_Required={0} is required \ No newline at end of file +Validation_Required={0} is required +Validation_OverPosting=Too much info +Validation_MaxLength={0} too long +Validation_UnexpectedValue=Unexpected value in field {0} \ No newline at end of file diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/BaseValidator.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/BaseValidator.java new file mode 100644 index 000000000..97f72042a --- /dev/null +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/BaseValidator.java @@ -0,0 +1,63 @@ +package gr.cite.notification.common.validation; + +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.errorcode.ErrorThesaurusProperties; +import gr.cite.tools.exception.MyValidationException; +import gr.cite.tools.validation.AbstractValidator; +import gr.cite.tools.validation.ValidationResult; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public abstract class BaseValidator extends AbstractValidator { + protected final ConventionService conventionService; + protected final ErrorThesaurusProperties errors; + + protected BaseValidator(ConventionService conventionService, ErrorThesaurusProperties errors) { + this.conventionService = conventionService; + this.errors = errors; + } + + @Override + public void validateForce(Object target) { + this.validate(target); + ValidationResult result = result(); + if (!result.isValid()) { + List>> errorsMap = this.flattenValidationResult(); + throw new MyValidationException(this.errors.getModelValidation().getCode(), errorsMap); + } + } + + protected Boolean isValidGuid(UUID guid) { + return this.conventionService.isValidGuid(guid); + } + + protected Boolean isValidHash(String hash) { + return this.conventionService.isValidHash(hash); + } + + protected Boolean isEmpty(String value) { + return this.conventionService.isNullOrEmpty(value); + } + protected Boolean isListNullOrEmpty(List value) { + return this.conventionService.isListNullOrEmpty(value); + } + protected Boolean isNull(Object value) { + return value == null; + } + + protected Boolean isNull(Collection value) { + return value == null; + } + + protected Boolean lessEqualLength(String value, int size) { + return value.length() <= size; + } + + protected Boolean lessEqual(Integer value, int target) { + return value <= target; + } +} + diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/EnumNotNull.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/EnumNotNull.java deleted file mode 100644 index 9b6cc2a3f..000000000 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/EnumNotNull.java +++ /dev/null @@ -1,11 +0,0 @@ -package gr.cite.notification.common.validation; - -import jakarta.validation.ConstraintValidator; -import jakarta.validation.ConstraintValidatorContext; - -public class EnumNotNull implements ConstraintValidator { - @Override - public boolean isValid(Object value, ConstraintValidatorContext context) { - return value != null; - } -} diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldNotNullIfOtherSet.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldNotNullIfOtherSet.java deleted file mode 100644 index 23b812d12..000000000 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldNotNullIfOtherSet.java +++ /dev/null @@ -1,22 +0,0 @@ -package gr.cite.notification.common.validation; - - -import jakarta.validation.Constraint; -import jakarta.validation.Payload; -import java.lang.annotation.*; - -@Constraint( validatedBy = { FieldNotNullIfOtherSetValidator.class } ) -@Documented -@Target( { ElementType.TYPE } ) -@Retention( RetentionPolicy.RUNTIME ) -public @interface FieldNotNullIfOtherSet { - Class[] groups () default {}; - - String notNullField() default "id"; - String otherSetField() default "hash"; - String failOn() default "hash"; - - String message () default "hash is required if id is set"; - - Class[] payload () default {}; -} diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldNotNullIfOtherSetValidator.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldNotNullIfOtherSetValidator.java deleted file mode 100644 index dc537b9d0..000000000 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldNotNullIfOtherSetValidator.java +++ /dev/null @@ -1,35 +0,0 @@ -package gr.cite.notification.common.validation; - -import org.springframework.beans.BeanWrapperImpl; - -import jakarta.validation.ConstraintValidator; -import jakarta.validation.ConstraintValidatorContext; -import java.util.Objects; - -public class FieldNotNullIfOtherSetValidator implements ConstraintValidator { - - private String notNullField; - private String otherSetField; - - @Override - public void initialize(FieldNotNullIfOtherSet constraintAnnotation) { - this.notNullField = constraintAnnotation.notNullField(); - this.otherSetField = constraintAnnotation.otherSetField(); - } - - @Override - public boolean isValid(Object entity, ConstraintValidatorContext context) { - Object notNullValue = new BeanWrapperImpl(entity) - .getPropertyValue(this.notNullField); - Object otherSetValue = new BeanWrapperImpl(entity) - .getPropertyValue(this.otherSetField); - - boolean hashIsString = Objects.equals(new BeanWrapperImpl(entity) - .getPropertyType(this.otherSetField), String.class); - - boolean hashValueEmpty = otherSetValue == null || (hashIsString && ((String)otherSetValue).isBlank()); - - if (notNullValue != null && hashValueEmpty) return false; - return true; - } -} diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldsValueMatch.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldsValueMatch.java deleted file mode 100644 index 3935b5f7e..000000000 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldsValueMatch.java +++ /dev/null @@ -1,29 +0,0 @@ -package gr.cite.notification.common.validation; - -import jakarta.validation.Constraint; -import jakarta.validation.Payload; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Constraint(validatedBy = FieldsValueMatchValidator.class) -@Target({ ElementType.TYPE }) -@Retention(RetentionPolicy.RUNTIME) -public @interface FieldsValueMatch { - Class[] groups () default {}; - - String field(); - String fieldMatch(); - String failOn(); - - String message() default "Fields values don't match!"; - - Class[] payload () default {}; - - @Target({ ElementType.TYPE }) - @Retention(RetentionPolicy.RUNTIME) - @interface List { - FieldsValueMatch[] value(); - } -} diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldsValueMatchValidator.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldsValueMatchValidator.java deleted file mode 100644 index 15097deb8..000000000 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/FieldsValueMatchValidator.java +++ /dev/null @@ -1,31 +0,0 @@ -package gr.cite.notification.common.validation; - -import org.springframework.beans.BeanWrapperImpl; - -import jakarta.validation.ConstraintValidator; -import jakarta.validation.ConstraintValidatorContext; - -public class FieldsValueMatchValidator implements ConstraintValidator { - - private String field; - private String fieldMatch; - - @Override - public void initialize(FieldsValueMatch constraintAnnotation) { - this.field = constraintAnnotation.field(); - this.fieldMatch = constraintAnnotation.fieldMatch(); - } - - @Override - public boolean isValid(Object entity, ConstraintValidatorContext context) { - - Object fieldValue = new BeanWrapperImpl(entity).getPropertyValue(field); - Object fieldMatchValue = new BeanWrapperImpl(entity).getPropertyValue(fieldMatch); - - if (fieldValue != null) { - return fieldValue.equals(fieldMatchValue); - } else { - return fieldMatchValue == null; - } - } -} diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidEnum.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidEnum.java deleted file mode 100644 index 5c3002235..000000000 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidEnum.java +++ /dev/null @@ -1,16 +0,0 @@ -package gr.cite.notification.common.validation; - -import jakarta.validation.Constraint; -import jakarta.validation.Payload; -import java.lang.annotation.*; - - -@Constraint(validatedBy = EnumNotNull.class) -@Documented -@Target({ ElementType.FIELD }) -@Retention(RetentionPolicy.RUNTIME) -public @interface ValidEnum { - String message() default "enum is required"; - Class[] groups() default {}; - Class[] payload() default {}; -} diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidId.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidId.java deleted file mode 100644 index 8fa3989fa..000000000 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidId.java +++ /dev/null @@ -1,18 +0,0 @@ -package gr.cite.notification.common.validation; - - -import jakarta.validation.Constraint; -import jakarta.validation.Payload; -import java.lang.annotation.*; - -@Constraint( validatedBy = { ValidIdValidator.class } ) -@Documented -@Target( { ElementType.FIELD } ) -@Retention( RetentionPolicy.RUNTIME ) -public @interface ValidId { - Class[] groups() default {}; - - String message() default "id set but not valid"; - - Class[] payload() default {}; -} diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidIdValidator.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidIdValidator.java deleted file mode 100644 index c1ce3886c..000000000 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidIdValidator.java +++ /dev/null @@ -1,40 +0,0 @@ -package gr.cite.notification.common.validation; - -import gr.cite.notification.convention.ConventionService; -import org.springframework.beans.factory.annotation.Autowired; - -import jakarta.validation.ConstraintValidator; -import jakarta.validation.ConstraintValidatorContext; -import java.util.UUID; - -public class ValidIdValidator implements ConstraintValidator { - - @Autowired - private ConventionService conventionService; - - @Override - public void initialize(ValidId constraintAnnotation) { } - - @Override - public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) { - if(o == null) return true; - else if(o instanceof UUID){ - UUID uuidId = (UUID)o; - return this.conventionService.isValidGuid(uuidId); - } - else if(o instanceof Integer){ - Integer intId = (Integer)o; - return this.conventionService.isValidId(intId); - } - else{ - String stringId = o.toString(); - UUID uuidId = null; - try { - uuidId = UUID.fromString(stringId); - }catch (Exception ex){ - return false; - } - return this.conventionService.isValidGuid(uuidId); - } - } -} diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidationServiceImpl.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidationServiceImpl.java deleted file mode 100644 index 47e0b807b..000000000 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/common/validation/ValidationServiceImpl.java +++ /dev/null @@ -1,36 +0,0 @@ -package gr.cite.notification.common.validation; - -import gr.cite.notification.errorcode.ErrorThesaurusProperties; -import gr.cite.tools.exception.MyValidationException; -import gr.cite.tools.validation.BaseValidationService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.context.annotation.Scope; -import org.springframework.stereotype.Service; - -import jakarta.validation.Validator; -import java.util.List; -import java.util.Map; - -@Service -@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) -public class ValidationServiceImpl extends BaseValidationService { - - private ErrorThesaurusProperties errors; - - @Autowired - public ValidationServiceImpl(Validator validator, ErrorThesaurusProperties errors){ - super(validator); - this.errors = errors; - } - - @Override - public void validateForce(T item, Class... groups) { - List>> validationErrors = this.validate(item, groups); - if (validationErrors != null && !validationErrors.isEmpty()) { - throw new MyValidationException(this.errors.getModelValidation().getCode(), - this.errors.getModelValidation().getMessage(), - validationErrors); - } - } -} diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/data/NotificationEntity.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/data/NotificationEntity.java index b6fee25a3..aa398981c 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/data/NotificationEntity.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/data/NotificationEntity.java @@ -18,6 +18,7 @@ public class NotificationEntity extends TenantScopedBaseEntity { public static final String _type = "type"; public static final String _contactTypeHint = "contactTypeHint"; public static final String _contactHint = "contactHint"; + public static final int _contactHintLength = 200; public final static String _notifiedAt = "notifiedAt"; public final static String _isActive = "isActive"; public final static String _createdAt = "createdAt"; diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenanttouched/TenantTouchedIntegrationEventHandlerImpl.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenanttouched/TenantTouchedIntegrationEventHandlerImpl.java index a33114668..013a2698f 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenanttouched/TenantTouchedIntegrationEventHandlerImpl.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenanttouched/TenantTouchedIntegrationEventHandlerImpl.java @@ -11,6 +11,7 @@ import gr.cite.notification.model.persist.TenantTouchedIntegrationEventPersist; import gr.cite.notification.service.tenant.TenantService; import gr.cite.tools.auditing.AuditService; import gr.cite.tools.logging.LoggerService; +import gr.cite.tools.validation.ValidatorFactory; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.EntityTransaction; @@ -33,10 +34,12 @@ public class TenantTouchedIntegrationEventHandlerImpl implements TenantTouchedIn protected final ApplicationContext applicationContext; private final JsonHandlingService jsonHandlingService; + private final ValidatorFactory validatorFactory; - public TenantTouchedIntegrationEventHandlerImpl(ApplicationContext applicationContext, JsonHandlingService jsonHandlingService) { + public TenantTouchedIntegrationEventHandlerImpl(ApplicationContext applicationContext, JsonHandlingService jsonHandlingService, ValidatorFactory validatorFactory) { this.applicationContext = applicationContext; this.jsonHandlingService = jsonHandlingService; + this.validatorFactory = validatorFactory; } @Override @@ -48,6 +51,7 @@ public class TenantTouchedIntegrationEventHandlerImpl implements TenantTouchedIn TenantTouchedIntegrationEventPersist model = new TenantTouchedIntegrationEventPersist(); model.setId(event.getId()); model.setCode(event.getCode()); + this.validatorFactory.validator(TenantTouchedIntegrationEventPersist.TenantTouchedIntegrationEventPersistValidator.class).validateForce(model); EntityManager entityManager = null; EntityTransaction transaction = null; diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/usertouched/UserTouchedIntegrationEventHandlerImpl.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/usertouched/UserTouchedIntegrationEventHandlerImpl.java index 4adc71d59..16ca28924 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/usertouched/UserTouchedIntegrationEventHandlerImpl.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/usertouched/UserTouchedIntegrationEventHandlerImpl.java @@ -17,6 +17,7 @@ import gr.cite.tools.auditing.AuditService; import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.fieldset.BaseFieldSet; import gr.cite.tools.logging.LoggerService; +import gr.cite.tools.validation.ValidatorFactory; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.EntityTransaction; @@ -39,13 +40,16 @@ public class UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegr protected final ApplicationContext applicationContext; private final JsonHandlingService jsonHandlingService; + private final ValidatorFactory validatorFactory; + public UserTouchedIntegrationEventHandlerImpl( JsonHandlingService jsonHandlingService, - ApplicationContext applicationContext - ) { + ApplicationContext applicationContext, + ValidatorFactory validatorFactory) { this.jsonHandlingService = jsonHandlingService; this.applicationContext = applicationContext; + this.validatorFactory = validatorFactory; } @Override @@ -58,6 +62,7 @@ public class UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegr model.setId(event.getId()); model.setFirstName(event.getFirstName()); model.setLastName(event.getLastName()); + this.validatorFactory.validator(UserTouchedIntegrationEventPersist.UserTouchedIntegrationEventPersistValidator.class).validateForce(model); EntityManager entityManager = null; EntityTransaction transaction = null; diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/deleter/UserContactInfoDeleter.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/deleter/UserContactInfoDeleter.java index fce879690..6dadfa2a1 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/deleter/UserContactInfoDeleter.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/deleter/UserContactInfoDeleter.java @@ -2,7 +2,6 @@ package gr.cite.notification.model.deleter; import gr.cite.notification.data.TenantScopedEntityManager; import gr.cite.notification.data.UserContactInfoEntity; -import gr.cite.notification.model.persist.UserContactInfoPersist; import gr.cite.notification.query.UserContactInfoQuery; import gr.cite.tools.data.deleter.Deleter; import gr.cite.tools.data.deleter.DeleterFactory; diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/NotificationPersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/NotificationPersist.java index 3131eeb8f..72af20832 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/NotificationPersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/NotificationPersist.java @@ -4,10 +4,21 @@ import gr.cite.notification.common.enums.NotificationContactType; import gr.cite.notification.common.enums.NotificationNotifyState; import gr.cite.notification.common.enums.NotificationTrackingProcess; import gr.cite.notification.common.enums.NotificationTrackingState; -import gr.cite.notification.common.validation.ValidId; +import gr.cite.notification.common.validation.BaseValidator; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.data.NotificationEntity; +import gr.cite.notification.errorcode.ErrorThesaurusProperties; +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 jakarta.validation.constraints.NotNull; import java.time.Instant; +import java.util.Arrays; +import java.util.List; import java.util.UUID; public class NotificationPersist { @@ -23,17 +34,20 @@ public class NotificationPersist { public final static String _notifiedWith = "notifiedWith"; public final static String _data = "data"; public final static String _retryCount = "retryCount"; + public final static String _trackingState = "trackingState"; + public final static String _trackingProcess = "trackingProcess"; + public final static String _trackingData = "trackingData"; + public final static String _provenanceRef = "provenanceRef"; + public static final String _hash = "hash"; } - @ValidId(message = "{validation.invalidid}") private UUID id; - @NotNull(message = "{validation.empty}") private UUID userId; private UUID type; - private gr.cite.notification.common.enums.NotificationContactType contactTypeHint; + private NotificationContactType contactTypeHint; private String contactHint; @@ -176,4 +190,50 @@ public class NotificationPersist { public void setHash(String hash) { this.hash = hash; } + + @Component(NotificationPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class NotificationPersistValidator extends BaseValidator { + + public static final String ValidatorName = "NotificationPersistValidator"; + + private final MessageSource messageSource; + + private final ValidatorFactory validatorFactory; + + protected NotificationPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { + super(conventionService, errors); + this.messageSource = messageSource; + this.validatorFactory = validatorFactory; + } + + @Override + protected Class modelClass() { + return NotificationPersist.class; + } + + @Override + protected List specifications(NotificationPersist item) { + return Arrays.asList( + this.spec() + .iff(() -> this.isValidGuid(item.getId())) + .must(() -> this.isValidHash(item.getHash())) + .failOn(NotificationPersist.Field._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationPersist.Field._hash}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isValidGuid(item.getId())) + .must(() -> !this.isValidHash(item.getHash())) + .failOn(NotificationPersist.Field._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getUserId())) + .failOn(NotificationPersist.Field._userId).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationPersist.Field._userId}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getType())) + .failOn(Field._type).failWith(messageSource.getMessage("Validation_Required", new Object[]{Field._type}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isEmpty(item.getContactHint())) + .must(() -> this.lessEqualLength(item.getContactHint(), NotificationEntity.Field._contactHintLength)) + .failOn(Field._contactHint).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{Field._contactHint}, LocaleContextHolder.getLocale())) + ); + } + } } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/NotificationTemplatePersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/NotificationTemplatePersist.java index 1803029e6..c0aeed0b0 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/NotificationTemplatePersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/NotificationTemplatePersist.java @@ -2,38 +2,44 @@ 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.ValidEnum; -import gr.cite.notification.common.validation.ValidId; +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 jakarta.validation.Valid; -import jakarta.validation.constraints.NotNull; +import java.util.Arrays; +import java.util.List; import java.util.UUID; public class NotificationTemplatePersist { - @ValidId(message = "{validation.invalidid}") private UUID id; + public static final String _id = "id"; - @ValidEnum private NotificationTemplateChannel channel; + public static final String _channel = "channel"; - @NotNull - @Valid private UUID notificationType; + public static final String _notificationType = "notificationType"; - @ValidEnum private NotificationTemplateKind kind; + public static final String _kind = "kind"; - @NotNull(message = "{validation.empty}") - @ValidId(message = "{validation.invalidid}") private UUID languageId; + public static final String _languageId = "languageId"; - @NotNull(message = "{validation.empty}") - @Valid private NotificationTemplateValuePersist value; + public static final String _value = "value"; private String hash; + public static final String _hash = "hash"; public UUID getId() { return id; @@ -90,4 +96,60 @@ public class NotificationTemplatePersist { 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.getLanguageId())) + .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)) + ); + } + } } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/TenantTouchedIntegrationEventPersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/TenantTouchedIntegrationEventPersist.java index c2f807d89..e8932c3de 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/TenantTouchedIntegrationEventPersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/TenantTouchedIntegrationEventPersist.java @@ -1,22 +1,27 @@ package gr.cite.notification.model.persist; -import gr.cite.notification.common.validation.ValidId; +import gr.cite.notification.common.validation.BaseValidator; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.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 jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; -import jakarta.validation.constraints.Size; +import java.util.Arrays; +import java.util.List; import java.util.UUID; public class TenantTouchedIntegrationEventPersist { - @ValidId(message = "{validation.invalidid}") - @NotNull(message = "{validation.empty}") private UUID id; + public static final String _id = "id"; - @NotNull(message = "{validation.empty}") - @NotEmpty(message = "{validation.empty}") - @Size(max = 50, message = "{validation.largerthanmax}") private String code; + public static final String _code = "code"; + public static final int _codeLength = 50; public UUID getId() { return id; @@ -34,4 +39,40 @@ public class TenantTouchedIntegrationEventPersist { this.code = code; } + @Component(TenantTouchedIntegrationEventPersist.TenantTouchedIntegrationEventPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class TenantTouchedIntegrationEventPersistValidator extends BaseValidator { + + public static final String ValidatorName = "TenantTouchedIntegrationEventPersistValidator"; + + private final MessageSource messageSource; + + protected TenantTouchedIntegrationEventPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { + super(conventionService, errors); + this.messageSource = messageSource; + } + + @Override + protected Class modelClass() { + return TenantTouchedIntegrationEventPersist.class; + } + + @Override + protected List specifications(TenantTouchedIntegrationEventPersist item) { + return Arrays.asList( + this.spec() + .iff(() -> !this.isNull(item.getId())) + .must(() -> this.isValidGuid(item.getId())) + .failOn(TenantTouchedIntegrationEventPersist._id).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantTouchedIntegrationEventPersist._id}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getCode())) + .failOn(TenantTouchedIntegrationEventPersist._code).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantTouchedIntegrationEventPersist._code}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isEmpty(item.getCode())) + .must(() -> this.lessEqualLength(item.getCode(), TenantTouchedIntegrationEventPersist._codeLength)) + .failOn(TenantTouchedIntegrationEventPersist._code).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{TenantTouchedIntegrationEventPersist._code}, LocaleContextHolder.getLocale())) + ); + } + } + } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserContactInfoPersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserContactInfoPersist.java deleted file mode 100644 index 4f2506ae5..000000000 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserContactInfoPersist.java +++ /dev/null @@ -1,117 +0,0 @@ -package gr.cite.notification.model.persist; - -import gr.cite.notification.common.enums.ContactInfoType; -import gr.cite.notification.common.enums.IsActive; -import gr.cite.notification.common.validation.FieldNotNullIfOtherSet; -import gr.cite.notification.common.validation.ValidId; - -import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; -import java.time.Instant; -import java.util.UUID; - -@FieldNotNullIfOtherSet(message = "{validation.hashempty}") -public class UserContactInfoPersist { - - @ValidId(message = "{validation.invalidid}") - private ID id; - - @NotNull(message = "{validation.empty}") - @NotEmpty(message = "{validation.empty}") - private String value; - - @NotNull(message = "{validation.empty}") - @ValidId(message = "{validation.invalidid}") - private UUID tenantId; - - private Instant createdAt; - - private Instant updatedAt; - - private IsActive isActive; - - private String hash; - - public ID getId() { - return id; - } - - public void setId(ID id) { - this.id = id; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - public UUID getTenantId() { - return tenantId; - } - - public void setTenantId(UUID tenantId) { - this.tenantId = tenantId; - } - - public Instant getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Instant createdAt) { - this.createdAt = createdAt; - } - - public Instant getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(Instant updatedAt) { - this.updatedAt = updatedAt; - } - - public IsActive getIsActive() { - return isActive; - } - - public void setIsActive(IsActive isActive) { - this.isActive = isActive; - } - - public String getHash() { - return hash; - } - - public void setHash(String hash) { - this.hash = hash; - } - - public static class ID { - - @NotNull(message = "{validation.empty}") - @ValidId(message = "{validation.invalidid}") - private UUID userId; - - @NotNull(message = "{validation.empty}") - private ContactInfoType type; - - public UUID getUserId() { - return userId; - } - - public void setUserId(UUID userId) { - this.userId = userId; - } - - public ContactInfoType getType() { - return type; - } - - public void setType(ContactInfoType type) { - this.type = type; - } - } - -} diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserNotificationPreferencePersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserNotificationPreferencePersist.java index 7d7a3c550..cc2a6110e 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserNotificationPreferencePersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserNotificationPreferencePersist.java @@ -1,7 +1,17 @@ package gr.cite.notification.model.persist; import gr.cite.notification.common.enums.NotificationContactType; +import gr.cite.notification.common.validation.BaseValidator; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.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.Map; import java.util.UUID; @@ -9,7 +19,9 @@ import java.util.UUID; public class UserNotificationPreferencePersist { private UUID userId; + public static final String _userId = "userId"; private Map> notificationPreferences; + public static final String _notificationPreferences = "notificationPreferences"; public UUID getUserId() { return userId; @@ -26,4 +38,35 @@ public class UserNotificationPreferencePersist { public void setNotificationPreferences(Map> notificationPreferences) { this.notificationPreferences = notificationPreferences; } + + @Component(UserNotificationPreferencePersist.UserNotificationPreferencePersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class UserNotificationPreferencePersistValidator extends BaseValidator { + + public static final String ValidatorName = "UserNotificationPreferencePersistValidator"; + + private final MessageSource messageSource; + + protected UserNotificationPreferencePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { + super(conventionService, errors); + this.messageSource = messageSource; + } + + @Override + protected Class modelClass() { + return UserNotificationPreferencePersist.class; + } + + @Override + protected List specifications(UserNotificationPreferencePersist item) { + return Arrays.asList( + this.spec() + .must(() -> !this.isNull(item.getUserId())) + .failOn(UserNotificationPreferencePersist._userId).failWith(messageSource.getMessage("Validation_Required", new Object[]{UserNotificationPreferencePersist._userId}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getNotificationPreferences())) + .failOn(UserNotificationPreferencePersist._notificationPreferences).failWith(messageSource.getMessage("Validation_Required", new Object[]{UserNotificationPreferencePersist._notificationPreferences}, LocaleContextHolder.getLocale())) + ); + } + } } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserTouchedIntegrationEventPersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserTouchedIntegrationEventPersist.java index 64bf7865f..f77a1fb41 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserTouchedIntegrationEventPersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/UserTouchedIntegrationEventPersist.java @@ -1,26 +1,32 @@ package gr.cite.notification.model.persist; -import gr.cite.notification.common.validation.ValidId; +import gr.cite.notification.common.validation.BaseValidator; -import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; -import jakarta.validation.constraints.Size; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.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 UserTouchedIntegrationEventPersist { - @ValidId(message = "{validation.invalidid}") - @NotNull(message = "{validation.empty}") + private UUID id; + public static final String _id = "id"; - @NotNull(message = "{validation.empty}") - @NotEmpty(message = "{validation.empty}") - @Size(max = 200, message = "{validation.largerthanmax}") private String firstName; + public static final String _firstName = "firstName"; + public static final int _firstNameLength = 200; - @NotNull(message = "{validation.empty}") - @NotEmpty(message = "{validation.empty}") - @Size(max = 200, message = "{validation.largerthanmax}") private String lastName; + public static final String _lastName = "lastName"; + public static final int _lastNameLength = 200; public UUID getId() { @@ -46,4 +52,47 @@ public class UserTouchedIntegrationEventPersist { public void setLastName(String lastName) { this.lastName = lastName; } + + @Component(UserTouchedIntegrationEventPersist.UserTouchedIntegrationEventPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class UserTouchedIntegrationEventPersistValidator extends BaseValidator { + + public static final String ValidatorName = "UserTouchedIntegrationEventPersistValidator"; + + private final MessageSource messageSource; + + protected UserTouchedIntegrationEventPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { + super(conventionService, errors); + this.messageSource = messageSource; + } + + @Override + protected Class modelClass() { + return UserTouchedIntegrationEventPersist.class; + } + + @Override + protected List specifications(UserTouchedIntegrationEventPersist item) { + return Arrays.asList( + this.spec() + .iff(() -> !this.isNull(item.getId())) + .must(() -> this.isValidGuid(item.getId())) + .failOn(UserTouchedIntegrationEventPersist._id).failWith(messageSource.getMessage("Validation_Required", new Object[]{UserTouchedIntegrationEventPersist._id}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getFirstName())) + .failOn(UserTouchedIntegrationEventPersist._firstName).failWith(messageSource.getMessage("Validation_Required", new Object[]{UserTouchedIntegrationEventPersist._firstName}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isEmpty(item.getFirstName())) + .must(() -> this.lessEqualLength(item.getFirstName(), UserTouchedIntegrationEventPersist._firstNameLength)) + .failOn(UserTouchedIntegrationEventPersist._firstName).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{UserTouchedIntegrationEventPersist._firstName}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getLastName())) + .failOn(UserTouchedIntegrationEventPersist._lastName).failWith(messageSource.getMessage("Validation_Required", new Object[]{UserTouchedIntegrationEventPersist._lastName}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isEmpty(item.getLastName())) + .must(() -> this.lessEqualLength(item.getLastName(), UserTouchedIntegrationEventPersist._lastNameLength)) + .failOn(UserTouchedIntegrationEventPersist._lastName).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{UserTouchedIntegrationEventPersist._lastName}, LocaleContextHolder.getLocale())) + ); + } + } } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/FieldInfoPersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/FieldInfoPersist.java index 371941dba..0658f8852 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/FieldInfoPersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/FieldInfoPersist.java @@ -1,23 +1,30 @@ package gr.cite.notification.model.persist.notificationtemplate; import gr.cite.notification.common.enums.NotificationDataType; -import gr.cite.notification.common.validation.ValidEnum; +import gr.cite.notification.common.validation.BaseValidator; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.errorcode.ErrorThesaurusProperties; +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 jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; +import java.util.Arrays; +import java.util.List; public class FieldInfoPersist { - @NotNull - @NotEmpty private String key; + public static final String _key = "key"; - @ValidEnum private NotificationDataType type; + public static final String _type = "type"; - @NotNull - @NotEmpty private String value; + public static final String _value = "value"; public FieldInfoPersist(String key, NotificationDataType type, String value) { this.key = key; @@ -51,4 +58,38 @@ public class FieldInfoPersist { public void setValue(String value) { this.value = value; } + + @Component(FieldInfoPersist.FieldInfoPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class FieldInfoPersistValidator extends BaseValidator { + + public static final String ValidatorName = " FieldInfoPersistValidator"; + + private final MessageSource messageSource; + + protected FieldInfoPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { + super(conventionService, errors); + this.messageSource = messageSource; + } + + @Override + protected Class modelClass() { + return FieldInfoPersist.class; + } + + @Override + protected List specifications(FieldInfoPersist item) { + return Arrays.asList( + this.spec() + .must(() -> !this.isEmpty(item.getKey())) + .failOn(FieldInfoPersist._key).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldInfoPersist._key}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getType())) + .failOn(FieldInfoPersist._type).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldInfoPersist._type}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getValue())) + .failOn(FieldInfoPersist._value).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldInfoPersist._value}, LocaleContextHolder.getLocale())) + ); + } + } } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/FieldOptionsPersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/FieldOptionsPersist.java index 4af0a3efe..dbe3c43c7 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/FieldOptionsPersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/FieldOptionsPersist.java @@ -1,20 +1,30 @@ package gr.cite.notification.model.persist.notificationtemplate; +import gr.cite.notification.common.validation.BaseValidator; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.errorcode.ErrorThesaurusProperties; +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 jakarta.validation.Valid; +import java.util.Arrays; import java.util.List; import java.util.Map; public class FieldOptionsPersist { - @Valid private List mandatory; + public static final String _mandatory = "mandatory"; - @Valid private List optional; + public static final String _optional = "optional"; - @Valid private Map formatting; + public static final String _formatting = "formatting"; public List getMandatory() { return mandatory; @@ -39,4 +49,43 @@ public class FieldOptionsPersist { public void setFormatting(Map formatting) { this.formatting = formatting; } + + + @Component(FieldOptionsPersist.FieldOptionsPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class FieldOptionsPersistValidator extends BaseValidator { + + public static final String ValidatorName = " FieldOptionsPersistValidator"; + + private final MessageSource messageSource; + + private final ValidatorFactory validatorFactory; + + protected FieldOptionsPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { + super(conventionService, errors); + this.messageSource = messageSource; + this.validatorFactory = validatorFactory; + } + + @Override + protected Class modelClass() { + return FieldOptionsPersist.class; + } + + @Override + protected List specifications(FieldOptionsPersist item) { + return Arrays.asList( + this.navSpec() + .iff(() -> !this.isListNullOrEmpty(item.getOptional())) + .on(FieldOptionsPersist._optional) + .over(item.getOptional()) + .using((itm) -> this.validatorFactory.validator(FieldInfoPersist.FieldInfoPersistValidator.class)), + this.spec() + .iff(() -> !this.isListNullOrEmpty(item.getMandatory()) || !this.isNull(item.getOptional())) + .must(() -> !this.isNull(item.getFormatting())) + .failOn(FieldOptionsPersist._formatting).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldOptionsPersist._formatting}, LocaleContextHolder.getLocale())) + ); + + } + } } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/NotificationTemplateValuePersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/NotificationTemplateValuePersist.java index 12acb9887..4768488e2 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/NotificationTemplateValuePersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/notificationtemplate/NotificationTemplateValuePersist.java @@ -1,48 +1,55 @@ package gr.cite.notification.model.persist.notificationtemplate; import gr.cite.notification.common.enums.EmailOverrideMode; -import gr.cite.notification.common.validation.ValidEnum; +import gr.cite.notification.common.validation.BaseValidator; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.errorcode.ErrorThesaurusProperties; +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 jakarta.validation.Valid; -import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; +import java.util.Arrays; import java.util.List; public class NotificationTemplateValuePersist { - @NotNull - @NotEmpty private String subjectText; + public static final String _subjectText = "subjectText"; - @NotNull - @NotEmpty private String subjectKey; + public static final String _subjectKey = "subjectKey"; - @Valid private FieldOptionsPersist subjectFieldOptions; + public static final String _subjectFieldOptions = "subjectFieldOptions"; - @NotNull - @NotEmpty private String bodyText; + public static final String _bodyText = "bodyText"; - @NotNull - @NotEmpty private String bodyKey; + public static final String _bodyKey = "bodyKey"; private String priorityKey; - @NotNull + public static final String _priorityKey = "priorityKey"; private Boolean allowAttachments = false; + public static final String _allowAttachments = "allowAttachments"; private List cc; + public static final String _cc = "cc"; - @ValidEnum private EmailOverrideMode ccMode; + public static final String _ccMode = "ccMode"; private List bcc; + public static final String _bcc = "bcc"; - @ValidEnum private EmailOverrideMode bccMode; + public static final String _bccMode = "bccMode"; private List extraDataKeys; + public static final String _extraDataKeys = "extraDataKeys"; - @Valid private FieldOptionsPersist bodyFieldOptions; + public static final String _bodyFieldOptions = "bodyFieldOptions"; public String getSubjectText() { return subjectText; @@ -147,4 +154,66 @@ public class NotificationTemplateValuePersist { public void setBodyFieldOptions(FieldOptionsPersist bodyFieldOptions) { this.bodyFieldOptions = bodyFieldOptions; } + + @Component(NotificationTemplateValuePersist.NotificationTemplateValuePersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class NotificationTemplateValuePersistValidator extends BaseValidator { + + public static final String ValidatorName = "NotificationTemplateValuePersistValidator"; + + private final MessageSource messageSource; + + private final ValidatorFactory validatorFactory; + + protected NotificationTemplateValuePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { + super(conventionService, errors); + this.messageSource = messageSource; + this.validatorFactory = validatorFactory; + } + + @Override + protected Class modelClass() { + return NotificationTemplateValuePersist.class; + } + + @Override + protected List specifications(NotificationTemplateValuePersist item) { + return Arrays.asList( + this.spec() + .must(() -> !this.isEmpty(item.getSubjectKey())) + .failOn(NotificationTemplateValuePersist._subjectKey).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplateValuePersist._subjectKey}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getSubjectText())) + .failOn(NotificationTemplateValuePersist._subjectText).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplateValuePersist._subjectText}, LocaleContextHolder.getLocale())), + this.refSpec() + .iff(() -> !this.isNull(item.getSubjectFieldOptions())) + .on(NotificationTemplateValuePersist._subjectFieldOptions) + .over(item.getSubjectFieldOptions()) + .using(() -> this.validatorFactory.validator(FieldOptionsPersist.FieldOptionsPersistValidator.class)), + this.spec() + .must(() -> !this.isEmpty(item.getBodyKey())) + .failOn(NotificationTemplateValuePersist._bodyKey).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplateValuePersist._bodyKey}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getBodyText())) + .failOn(NotificationTemplateValuePersist._bodyText).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplateValuePersist._bodyText}, LocaleContextHolder.getLocale())), + this.refSpec() + .iff(() -> !this.isNull(item.getBodyFieldOptions())) + .on(NotificationTemplateValuePersist._bodyFieldOptions) + .over(item.getBodyFieldOptions()) + .using(() -> this.validatorFactory.validator(FieldOptionsPersist.FieldOptionsPersistValidator.class)), + this.spec() + .must(() -> !this.isNull(item.getAllowAttachments())) + .failOn(NotificationTemplateValuePersist._allowAttachments).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplateValuePersist._allowAttachments}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getPriorityKey())) + .failOn(NotificationTemplateValuePersist._priorityKey).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplateValuePersist._priorityKey}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getCcMode())) + .failOn(NotificationTemplateValuePersist._ccMode).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplateValuePersist._ccMode}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getBccMode())) + .failOn(NotificationTemplateValuePersist._bccMode).failWith(messageSource.getMessage("Validation_Required", new Object[]{NotificationTemplateValuePersist._bccMode}, LocaleContextHolder.getLocale())) + ); + } + } } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationEmailClientPersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationEmailClientPersist.java index 4529dc988..7f3063e6d 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationEmailClientPersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationEmailClientPersist.java @@ -1,21 +1,40 @@ package gr.cite.notification.model.persist.tenantconfiguration; -import gr.cite.notification.common.validation.ValidId; +import gr.cite.notification.common.validation.BaseValidator; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.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 TenantConfigurationEmailClientPersist { - @ValidId(message = "{validation.invalidid}") private UUID id; + public static final String _id = "id"; private Boolean requireCredentials; + public static final String _requireCredentials = "requireCredentials"; private Boolean enableSSL; + public static final String _enableSSL = "enableSSL"; private String certificatePath; + public static final String _certificatePath = "certificatePath"; private String hostServer; + public static final String _hostServer = "hostServer"; private Integer hostPortNo; + public static final String _hostPortNo = "hostPortNo"; private String emailAddress; + public static final String _emailAddress = "emailAddress"; private String emailUserName; + public static final String _emailUserName = "emailUserName"; private String emailPassword; + public static final String _emailPassword = "emailPassword"; private String hash; + public static final String _hash = "hash"; public UUID getId() { return id; @@ -96,4 +115,63 @@ public class TenantConfigurationEmailClientPersist { public void setHash(String hash) { this.hash = hash; } + + @Component(TenantConfigurationEmailClientPersist.TenantConfigurationEmailClientPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class TenantConfigurationEmailClientPersistValidator extends BaseValidator { + + public static final String ValidatorName = "TenantConfigurationEmailClientPersistValidator"; + + private final MessageSource messageSource; + + protected TenantConfigurationEmailClientPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { + super(conventionService, errors); + this.messageSource = messageSource; + } + + @Override + protected Class modelClass() { + return TenantConfigurationEmailClientPersist.class; + } + + @Override + protected List specifications(TenantConfigurationEmailClientPersist item) { + return Arrays.asList( + this.spec() + .iff(() -> this.isValidGuid(item.getId())) + .must(() -> this.isValidHash(item.getHash())) + .failOn(TenantConfigurationEmailClientPersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationEmailClientPersist._hash}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isValidGuid(item.getId())) + .must(() -> !this.isValidHash(item.getHash())) + .failOn(TenantConfigurationEmailClientPersist._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())), + + this.spec() + .must(() -> !this.isNull(item.getRequireCredentials())) + .failOn(TenantConfigurationEmailClientPersist._requireCredentials).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationEmailClientPersist._requireCredentials}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getEnableSSL())) + .failOn(TenantConfigurationEmailClientPersist._enableSSL).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationEmailClientPersist._enableSSL}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getCertificatePath())) + .failOn(TenantConfigurationEmailClientPersist._certificatePath).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationEmailClientPersist._certificatePath}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getHostServer())) + .failOn(TenantConfigurationEmailClientPersist._hostServer).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationEmailClientPersist._hostServer}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getHostPortNo())) + .failOn(TenantConfigurationEmailClientPersist._hostPortNo).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationEmailClientPersist._hostPortNo}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getEmailAddress())) + .failOn(TenantConfigurationEmailClientPersist._emailAddress).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationEmailClientPersist._emailAddress}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getEmailUserName())) + .failOn(TenantConfigurationEmailClientPersist._emailUserName).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationEmailClientPersist._emailUserName}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getEmailPassword())) + .failOn(TenantConfigurationEmailClientPersist._emailPassword).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationEmailClientPersist._emailPassword}, LocaleContextHolder.getLocale())) + + ); + } + } } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationNotifierListPersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationNotifierListPersist.java index 11247b19b..7152ff49b 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationNotifierListPersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationNotifierListPersist.java @@ -1,17 +1,28 @@ package gr.cite.notification.model.persist.tenantconfiguration; import gr.cite.notification.common.enums.NotificationContactType; -import gr.cite.notification.common.validation.ValidId; +import gr.cite.notification.common.validation.BaseValidator; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.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.Map; import java.util.UUID; public class TenantConfigurationNotifierListPersist { - @ValidId(message = "{validation.invalidid}") private UUID id; + public static final String _id = "id"; private Map> notifiers; + public static final String _notifiers = "notifiers"; private String hash; + public static final String _hash = "hash"; public UUID getId() { return id; @@ -36,4 +47,40 @@ public class TenantConfigurationNotifierListPersist { public void setHash(String hash) { this.hash = hash; } + + @Component(TenantConfigurationNotifierListPersist.TenantConfigurationNotifierListPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class TenantConfigurationNotifierListPersistValidator extends BaseValidator { + + public static final String ValidatorName = "TenantConfigurationNotifierListPersistValidator"; + + private final MessageSource messageSource; + + protected TenantConfigurationNotifierListPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { + super(conventionService, errors); + this.messageSource = messageSource; + } + + @Override + protected Class modelClass() { + return TenantConfigurationNotifierListPersist.class; + } + + @Override + protected List specifications(TenantConfigurationNotifierListPersist item) { + return Arrays.asList( + this.spec() + .iff(() -> this.isValidGuid(item.getId())) + .must(() -> this.isValidHash(item.getHash())) + .failOn(TenantConfigurationNotifierListPersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationNotifierListPersist._hash}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isValidGuid(item.getId())) + .must(() -> !this.isValidHash(item.getHash())) + .failOn(TenantConfigurationNotifierListPersist._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getNotifiers())) + .failOn(TenantConfigurationNotifierListPersist._notifiers).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationNotifierListPersist._notifiers}, LocaleContextHolder.getLocale())) + ); + } + } } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationUserLocaleIntegrationPersist.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationUserLocaleIntegrationPersist.java index e9105c39e..a6e15c081 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationUserLocaleIntegrationPersist.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/model/persist/tenantconfiguration/TenantConfigurationUserLocaleIntegrationPersist.java @@ -1,9 +1,25 @@ package gr.cite.notification.model.persist.tenantconfiguration; +import gr.cite.notification.common.validation.BaseValidator; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.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; + public class TenantConfigurationUserLocaleIntegrationPersist { private String language; + public static final String _language = "language"; private String timeZone; + public static final String _timeZone = "timeZone"; private String culture; + public static final String _culture = "culture"; private String hash; @@ -38,4 +54,39 @@ public class TenantConfigurationUserLocaleIntegrationPersist { public void setHash(String hash) { this.hash = hash; } + + @Component(TenantConfigurationUserLocaleIntegrationPersist.TenantConfigurationUserLocaleIntegrationPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class TenantConfigurationUserLocaleIntegrationPersistValidator extends BaseValidator { + + public static final String ValidatorName = "TenantConfigurationUserLocaleIntegrationPersistValidator"; + + private final MessageSource messageSource; + + protected TenantConfigurationUserLocaleIntegrationPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { + super(conventionService, errors); + this.messageSource = messageSource; + } + + @Override + protected Class modelClass() { + return TenantConfigurationUserLocaleIntegrationPersist.class; + } + + @Override + protected List specifications(TenantConfigurationUserLocaleIntegrationPersist item) { + return Arrays.asList( + this.spec() + .must(() -> !this.isNull(item.getLanguage())) + .failOn(TenantConfigurationUserLocaleIntegrationPersist._language).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationUserLocaleIntegrationPersist._language}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getTimeZone())) + .failOn(TenantConfigurationUserLocaleIntegrationPersist._timeZone).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationUserLocaleIntegrationPersist._timeZone}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getCulture())) + .failOn(TenantConfigurationUserLocaleIntegrationPersist._culture).failWith(messageSource.getMessage("Validation_Required", new Object[]{TenantConfigurationUserLocaleIntegrationPersist._culture}, LocaleContextHolder.getLocale())) + + ); + } + } } diff --git a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/query/UserContactInfoQuery.java b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/query/UserContactInfoQuery.java index 6454117b3..d8f8549d0 100644 --- a/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/query/UserContactInfoQuery.java +++ b/dmp-backend/notification-service/notification/src/main/java/gr/cite/notification/query/UserContactInfoQuery.java @@ -10,7 +10,6 @@ import gr.cite.notification.data.UserContactInfoEntity; import gr.cite.notification.data.UserEntity; import gr.cite.notification.model.User; import gr.cite.notification.model.UserContactInfo; -import gr.cite.notification.model.persist.UserContactInfoPersist; import gr.cite.tools.data.query.FieldResolver; import gr.cite.tools.data.query.QueryBase; import gr.cite.tools.data.query.QueryContext;