From 7ea32faa9459df9937d468bd7da577da32515f56 Mon Sep 17 00:00:00 2001 From: sgiannopoulos Date: Thu, 25 Apr 2024 17:59:36 +0300 Subject: [PATCH] TenantDefaultLocale queue event --- .../outbox/OutboxIntegrationEvent.java | 2 + .../outbox/OutboxProperties.java | 20 ++- .../outbox/OutboxRepositoryImpl.java | 8 ++ ...tDefaultLocaleRemovalIntegrationEvent.java | 22 +++ ...tLocaleRemovalIntegrationEventHandler.java | 7 + ...aleRemovalIntegrationEventHandlerImpl.java | 40 ++++++ ...tDefaultLocaleTouchedIntegrationEvent.java | 49 +++++++ ...tLocaleTouchedIntegrationEventHandler.java | 7 + ...aleTouchedIntegrationEventHandlerImpl.java | 40 ++++++ .../TenantConfigurationServiceImpl.java | 27 +++- .../web/src/main/resources/config/queue.yml | 2 + ...ntf_Tenant_and_ntf_TenantConfiguration.sql | 2 +- .../src/main/resources/config/db.yml | 2 +- .../src/main/resources/config/errors.yml | 6 + .../src/main/resources/config/permissions.yml | 11 +- .../src/main/resources/config/queue.yml | 2 + .../notification/audit/AuditableAction.java | 2 + .../integrationevent/AppRabbitConfigurer.java | 2 + .../inbox/InboxProperties.java | 28 +++- .../inbox/InboxRepositoryImpl.java | 6 + ...efaultLocaleRemovalConsistencyHandler.java | 27 ++++ ...ultLocaleRemovalConsistencyPredicates.java | 23 ++++ ...tDefaultLocaleRemovalIntegrationEvent.java | 18 +++ ...tLocaleRemovalIntegrationEventHandler.java | 8 ++ ...aleRemovalIntegrationEventHandlerImpl.java | 106 ++++++++++++++ ...efaultLocaleTouchedConsistencyHandler.java | 27 ++++ ...ultLocaleTouchedConsistencyPredicates.java | 23 ++++ ...tDefaultLocaleTouchedIntegrationEvent.java | 46 +++++++ ...tLocaleTouchedIntegrationEventHandler.java | 8 ++ ...aleTouchedIntegrationEventHandlerImpl.java | 129 ++++++++++++++++++ .../TenantConfigurationService.java | 4 + .../TenantConfigurationServiceImpl.java | 25 ++-- 32 files changed, 700 insertions(+), 29 deletions(-) create mode 100644 dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEvent.java create mode 100644 dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandler.java create mode 100644 dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.java create mode 100644 dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEvent.java create mode 100644 dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandler.java create mode 100644 dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.java create mode 100644 notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalConsistencyHandler.java create mode 100644 notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalConsistencyPredicates.java create mode 100644 notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEvent.java create mode 100644 notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandler.java create mode 100644 notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.java create mode 100644 notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedConsistencyHandler.java create mode 100644 notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedConsistencyPredicates.java create mode 100644 notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEvent.java create mode 100644 notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandler.java create mode 100644 notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.java diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxIntegrationEvent.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxIntegrationEvent.java index 30809dbbd..2ead5f0a7 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxIntegrationEvent.java +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxIntegrationEvent.java @@ -10,6 +10,8 @@ public class OutboxIntegrationEvent extends IntegrationEvent { public static final String FORGET_ME_COMPLETED = "FORGET_ME_COMPLETED"; public static final String NOTIFY = "NOTIFY"; + public static final String TENANT_DEFAULT_LOCALE_REMOVAL = "TENANT_DEFAULT_LOCALE_REMOVAL"; + public static final String TENANT_DEFAULT_LOCALE_TOUCHED = "TENANT_DEFAULT_LOCALE_TOUCHED"; public static final String TENANT_REACTIVATE = "TENANT_REACTIVATE"; diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxProperties.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxProperties.java index 70fcef0c1..c17bbc507 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxProperties.java +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxProperties.java @@ -2,10 +2,14 @@ package eu.eudat.integrationevent.outbox; import org.springframework.boot.context.properties.ConfigurationProperties; +import java.util.List; + @ConfigurationProperties(prefix = "queue.task.publisher.options") public class OutboxProperties { private final String exchange; + private final String tenantDefaultLocaleRemovalTopic; + private final String tenantDefaultLocaleTouchedTopic; private final String tenantTouchTopic; @@ -34,7 +38,9 @@ public class OutboxProperties { private final String generateFileTopic; - public OutboxProperties(String exchange, + public OutboxProperties(String exchange, + String tenantDefaultLocaleRemovalTopic, + String tenantDefaultLocaleTouchedTopic, String tenantTouchTopic, String tenantRemovalTopic, String tenantReactivationTopic, @@ -43,7 +49,7 @@ public class OutboxProperties { String userTouchTopic, String dmpTouchTopic, String descriptionTouchTopic, - String annotationEntitiesTouchTopic, + String annotationEntitiesTouchTopic, String annotationEntitiesRemovalTopic, String notifyTopic, String forgetMeCompletedTopic, @@ -51,6 +57,8 @@ public class OutboxProperties { String generateFileTopic ) { this.exchange = exchange; + this.tenantDefaultLocaleRemovalTopic = tenantDefaultLocaleRemovalTopic; + this.tenantDefaultLocaleTouchedTopic = tenantDefaultLocaleTouchedTopic; this.tenantTouchTopic = tenantTouchTopic; this.tenantRemovalTopic = tenantRemovalTopic; this.tenantReactivationTopic = tenantReactivationTopic; @@ -71,6 +79,14 @@ public class OutboxProperties { return exchange; } + public String getTenantDefaultLocaleRemovalTopic() { + return tenantDefaultLocaleRemovalTopic; + } + + public String getTenantDefaultLocaleTouchedTopic() { + return tenantDefaultLocaleTouchedTopic; + } + public String getTenantTouchTopic() { return tenantTouchTopic; } diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxRepositoryImpl.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxRepositoryImpl.java index 98342b1fa..c5313968f 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxRepositoryImpl.java +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/OutboxRepositoryImpl.java @@ -429,6 +429,14 @@ public class OutboxRepositoryImpl implements OutboxRepository { routingKey = this.outboxProperties.getNotifyTopic(); break; } + case OutboxIntegrationEvent.TENANT_DEFAULT_LOCALE_REMOVAL: { + routingKey = this.outboxProperties.getTenantDefaultLocaleRemovalTopic(); + break; + } + case OutboxIntegrationEvent.TENANT_DEFAULT_LOCALE_TOUCHED: { + routingKey = this.outboxProperties.getTenantDefaultLocaleTouchedTopic(); + break; + } case OutboxIntegrationEvent.WHAT_YOU_KNOW_ABOUT_ME_COMPLETED: { routingKey = this.outboxProperties.getWhatYouKnowAboutMeCompletedTopic(); break; diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEvent.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEvent.java new file mode 100644 index 000000000..32808fb43 --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEvent.java @@ -0,0 +1,22 @@ +package eu.eudat.integrationevent.outbox.tenantdefaultlocaleremoval; + +import eu.eudat.integrationevent.TrackedEvent; + +import java.util.UUID; + +public class TenantDefaultLocaleRemovalIntegrationEvent extends TrackedEvent { + + private UUID tenantId; + + public TenantDefaultLocaleRemovalIntegrationEvent() { + } + + + public UUID getTenantId() { + return tenantId; + } + + public void setTenantId(UUID tenantId) { + this.tenantId = tenantId; + } +} diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandler.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandler.java new file mode 100644 index 000000000..bde0448b9 --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandler.java @@ -0,0 +1,7 @@ +package eu.eudat.integrationevent.outbox.tenantdefaultlocaleremoval; + +import javax.management.InvalidApplicationException; + +public interface TenantDefaultLocaleRemovalIntegrationEventHandler { + void handle(TenantDefaultLocaleRemovalIntegrationEvent event) throws InvalidApplicationException; +} diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.java new file mode 100644 index 000000000..d7e2b402a --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.java @@ -0,0 +1,40 @@ +package eu.eudat.integrationevent.outbox.tenantdefaultlocaleremoval; + +import eu.eudat.commons.scope.tenant.TenantScope; +import eu.eudat.integrationevent.outbox.OutboxIntegrationEvent; +import eu.eudat.integrationevent.outbox.OutboxService; +import gr.cite.tools.logging.LoggerService; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.annotation.RequestScope; + +import javax.management.InvalidApplicationException; +import java.util.UUID; + +@Component +@RequestScope +public class TenantDefaultLocaleRemovalIntegrationEventHandlerImpl implements TenantDefaultLocaleRemovalIntegrationEventHandler { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.class)); + + private final OutboxService outboxService; + private final TenantScope tenantScope; + + @Autowired + public TenantDefaultLocaleRemovalIntegrationEventHandlerImpl( + OutboxService outboxService, TenantScope tenantScope) { + this.outboxService = outboxService; + this.tenantScope = tenantScope; + } + + @Override + public void handle(TenantDefaultLocaleRemovalIntegrationEvent event) throws InvalidApplicationException { + OutboxIntegrationEvent message = new OutboxIntegrationEvent(); + message.setMessageId(UUID.randomUUID()); + message.setType(OutboxIntegrationEvent.TENANT_DEFAULT_LOCALE_REMOVAL); + message.setEvent(event); + if (this.tenantScope.isSet()) message.setTenantId(tenantScope.getTenant()); + this.outboxService.publish(message); + } +} diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEvent.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEvent.java new file mode 100644 index 000000000..690da0944 --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEvent.java @@ -0,0 +1,49 @@ +package eu.eudat.integrationevent.outbox.tenantdefaultlocaletouched; + +import eu.eudat.integrationevent.TrackedEvent; + +import java.util.UUID; + +public class TenantDefaultLocaleTouchedIntegrationEvent extends TrackedEvent { + + private UUID tenantId; + private String timezone; + private String language; + private String culture; + + + public TenantDefaultLocaleTouchedIntegrationEvent() { + } + + public UUID getTenantId() { + return tenantId; + } + + public void setTenantId(UUID tenantId) { + this.tenantId = tenantId; + } + + public String getTimezone() { + return timezone; + } + + public void setTimezone(String timezone) { + this.timezone = timezone; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public String getCulture() { + return culture; + } + + public void setCulture(String culture) { + this.culture = culture; + } +} diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandler.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandler.java new file mode 100644 index 000000000..c7f3506ea --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandler.java @@ -0,0 +1,7 @@ +package eu.eudat.integrationevent.outbox.tenantdefaultlocaletouched; + +import javax.management.InvalidApplicationException; + +public interface TenantDefaultLocaleTouchedIntegrationEventHandler { + void handle(TenantDefaultLocaleTouchedIntegrationEvent event) throws InvalidApplicationException; +} diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.java new file mode 100644 index 000000000..98290f0c5 --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.java @@ -0,0 +1,40 @@ +package eu.eudat.integrationevent.outbox.tenantdefaultlocaletouched; + +import eu.eudat.commons.scope.tenant.TenantScope; +import eu.eudat.integrationevent.outbox.OutboxIntegrationEvent; +import eu.eudat.integrationevent.outbox.OutboxService; +import gr.cite.tools.logging.LoggerService; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.annotation.RequestScope; + +import javax.management.InvalidApplicationException; +import java.util.UUID; + +@Component +@RequestScope +public class TenantDefaultLocaleTouchedIntegrationEventHandlerImpl implements TenantDefaultLocaleTouchedIntegrationEventHandler { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.class)); + + private final OutboxService outboxService; + private final TenantScope tenantScope; + + @Autowired + public TenantDefaultLocaleTouchedIntegrationEventHandlerImpl( + OutboxService outboxService, TenantScope tenantScope) { + this.outboxService = outboxService; + this.tenantScope = tenantScope; + } + + @Override + public void handle(TenantDefaultLocaleTouchedIntegrationEvent event) throws InvalidApplicationException { + OutboxIntegrationEvent message = new OutboxIntegrationEvent(); + message.setMessageId(UUID.randomUUID()); + message.setType(OutboxIntegrationEvent.TENANT_DEFAULT_LOCALE_TOUCHED); + message.setEvent(event); + if (this.tenantScope.isSet()) message.setTenantId(tenantScope.getTenant()); + this.outboxService.publish(message); + } +} diff --git a/dmp-backend/core/src/main/java/eu/eudat/service/tenantconfiguration/TenantConfigurationServiceImpl.java b/dmp-backend/core/src/main/java/eu/eudat/service/tenantconfiguration/TenantConfigurationServiceImpl.java index e0a943d93..ddfb1e5dd 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/service/tenantconfiguration/TenantConfigurationServiceImpl.java +++ b/dmp-backend/core/src/main/java/eu/eudat/service/tenantconfiguration/TenantConfigurationServiceImpl.java @@ -18,6 +18,10 @@ import eu.eudat.data.TenantEntityManager; import eu.eudat.errorcode.ErrorThesaurusProperties; import eu.eudat.event.EventBroker; import eu.eudat.event.TenantConfigurationTouchedEvent; +import eu.eudat.integrationevent.outbox.tenantdefaultlocaleremoval.TenantDefaultLocaleRemovalIntegrationEvent; +import eu.eudat.integrationevent.outbox.tenantdefaultlocaleremoval.TenantDefaultLocaleRemovalIntegrationEventHandler; +import eu.eudat.integrationevent.outbox.tenantdefaultlocaletouched.TenantDefaultLocaleTouchedIntegrationEvent; +import eu.eudat.integrationevent.outbox.tenantdefaultlocaletouched.TenantDefaultLocaleTouchedIntegrationEventHandler; import eu.eudat.model.StorageFile; import eu.eudat.model.builder.tenantconfiguration.TenantConfigurationBuilder; import eu.eudat.model.deleter.TenantConfigurationDeleter; @@ -88,6 +92,9 @@ public class TenantConfigurationServiceImpl implements TenantConfigurationServic private final QueryFactory queryFactory; private final EventBroker eventBroker; private final TenantScope tenantScope; + + private final TenantDefaultLocaleTouchedIntegrationEventHandler tenantDefaultLocaleTouchedIntegrationEventHandler; + private final TenantDefaultLocaleRemovalIntegrationEventHandler tenantDefaultLocaleRemovalIntegrationEventHandler; @Autowired public TenantConfigurationServiceImpl( TenantEntityManager entityManager, @@ -96,7 +103,7 @@ public class TenantConfigurationServiceImpl implements TenantConfigurationServic BuilderFactory builderFactory, ConventionService conventionService, ErrorThesaurusProperties errors, - MessageSource messageSource, JsonHandlingService jsonHandlingService, EncryptionService encryptionService, TenantProperties tenantProperties, StorageFileService storageFileService, QueryFactory queryFactory, EventBroker eventBroker, TenantScope tenantScope) { + MessageSource messageSource, JsonHandlingService jsonHandlingService, EncryptionService encryptionService, TenantProperties tenantProperties, StorageFileService storageFileService, QueryFactory queryFactory, EventBroker eventBroker, TenantScope tenantScope, TenantDefaultLocaleTouchedIntegrationEventHandler tenantDefaultLocaleTouchedIntegrationEventHandler, TenantDefaultLocaleRemovalIntegrationEventHandler tenantDefaultLocaleRemovalIntegrationEventHandler) { this.entityManager = entityManager; this.authorizationService = authorizationService; this.deleterFactory = deleterFactory; @@ -111,6 +118,8 @@ public class TenantConfigurationServiceImpl implements TenantConfigurationServic this.queryFactory = queryFactory; this.eventBroker = eventBroker; this.tenantScope = tenantScope; + this.tenantDefaultLocaleTouchedIntegrationEventHandler = tenantDefaultLocaleTouchedIntegrationEventHandler; + this.tenantDefaultLocaleRemovalIntegrationEventHandler = tenantDefaultLocaleRemovalIntegrationEventHandler; } public TenantConfiguration persist(TenantConfigurationPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JsonProcessingException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { @@ -166,6 +175,16 @@ public class TenantConfigurationServiceImpl implements TenantConfigurationServic this.eventBroker.emit(new TenantConfigurationTouchedEvent(data.getId(), this.tenantScope.getDefaultTenantCode(), data.getType())); } + if (data.getType().equals(TenantConfigurationType.DefaultUserLocale)){ + TenantDefaultLocaleTouchedIntegrationEvent event = new TenantDefaultLocaleTouchedIntegrationEvent(); + DefaultUserLocaleTenantConfigurationEntity defaultUserLocaleTenantConfiguration = this.jsonHandlingService.fromJson(DefaultUserLocaleTenantConfigurationEntity.class, data.getValue()); + event.setTenantId(data.getTenantId()); + event.setLanguage(defaultUserLocaleTenantConfiguration.getLanguage()); + event.setCulture(defaultUserLocaleTenantConfiguration.getCulture()); + event.setTimezone(defaultUserLocaleTenantConfiguration.getTimezone()); + this.tenantDefaultLocaleTouchedIntegrationEventHandler.handle(event); + } + return this.builderFactory.builder(TenantConfigurationBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(BaseFieldSet.build(fields, TenantConfiguration._id), data); } @@ -277,6 +296,12 @@ public class TenantConfigurationServiceImpl implements TenantConfigurationServic } else { this.eventBroker.emit(new TenantConfigurationTouchedEvent(data.getId(), this.tenantScope.getDefaultTenantCode(), data.getType())); } + + if (data.getType().equals(TenantConfigurationType.DefaultUserLocale)){ + TenantDefaultLocaleRemovalIntegrationEvent event = new TenantDefaultLocaleRemovalIntegrationEvent(); + event.setTenantId(data.getTenantId()); + this.tenantDefaultLocaleRemovalIntegrationEventHandler.handle(event); + } } } diff --git a/dmp-backend/web/src/main/resources/config/queue.yml b/dmp-backend/web/src/main/resources/config/queue.yml index 1de8f8db6..c58d9a681 100644 --- a/dmp-backend/web/src/main/resources/config/queue.yml +++ b/dmp-backend/web/src/main/resources/config/queue.yml @@ -25,6 +25,8 @@ queue: enable: true options: exchange: null + tenant-default-locale-removal-topic: tenant_default_locale.remove + tenant-default-locale-touched-topic: tenant_default_locale.touch forget-me-completed-topic: forgetme.completed notify-topic: notification.notify tenant-reactivation-topic: tenant.reactivated diff --git a/dmp-db-scema/updates/00.01.030_add_ntf_Tenant_and_ntf_TenantConfiguration.sql b/dmp-db-scema/updates/00.01.030_add_ntf_Tenant_and_ntf_TenantConfiguration.sql index 8f0fbb6f3..3dcd1ac38 100644 --- a/dmp-db-scema/updates/00.01.030_add_ntf_Tenant_and_ntf_TenantConfiguration.sql +++ b/dmp-db-scema/updates/00.01.030_add_ntf_Tenant_and_ntf_TenantConfiguration.sql @@ -17,7 +17,7 @@ BEGIN CREATE TABLE public."ntf_TenantConfiguration" ( id uuid NOT NULL, - tenant uuid NOT NULL, + tenant uuid NULL, type smallint NOT NULL, value character varying COLLATE pg_catalog."default" NOT NULL, is_active smallint NOT NULL, diff --git a/notification-service/notification-web/src/main/resources/config/db.yml b/notification-service/notification-web/src/main/resources/config/db.yml index 0de4aa778..18ca948e4 100644 --- a/notification-service/notification-web/src/main/resources/config/db.yml +++ b/notification-service/notification-web/src/main/resources/config/db.yml @@ -10,7 +10,7 @@ spring: dialect: org.hibernate.dialect.PostgreSQLDialect hibernate: naming: - physical-strategy: gr.cite.notification.config.db.PrefixPhysicalNamingStrategy + physical-strategy: gr.cite.notification.data.namingstrategy.PrefixPhysicalNamingStrategy implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy datasource: url: ${DB_CONNECTION_STRING} diff --git a/notification-service/notification-web/src/main/resources/config/errors.yml b/notification-service/notification-web/src/main/resources/config/errors.yml index 4094e5aed..fdc56fa48 100644 --- a/notification-service/notification-web/src/main/resources/config/errors.yml +++ b/notification-service/notification-web/src/main/resources/config/errors.yml @@ -47,3 +47,9 @@ error-thesaurus: tenant-tampering: code: 123 message: Tenant tampering + tenant-configuration-type-can-not-change: + code: 124 + message: Tenant configuration type can not change + multiple-tenant-configuration-type-not-allowed: + code: 125 + message: Multiple Tenant Configuration Type Not Allowed diff --git a/notification-service/notification-web/src/main/resources/config/permissions.yml b/notification-service/notification-web/src/main/resources/config/permissions.yml index 2eb495a02..d8b1096dd 100644 --- a/notification-service/notification-web/src/main/resources/config/permissions.yml +++ b/notification-service/notification-web/src/main/resources/config/permissions.yml @@ -88,17 +88,24 @@ permissions: clients: [ ] allowAnonymous: false allowAuthenticated: false - #Tenant Configuration + # TenantConfiguration BrowseTenantConfiguration: roles: - TenantAdmin + claims: [ ] clients: [ ] allowAnonymous: false allowAuthenticated: false EditTenantConfiguration: roles: - TenantAdmin - clients: [ ] + clients: [ "opendmp-api-dev" ] + allowAnonymous: false + allowAuthenticated: false + DeleteTenantConfiguration: + roles: + - TenantAdmin + clients: [ "opendmp-api-dev" ] allowAnonymous: false allowAuthenticated: false #User Notification Preference diff --git a/notification-service/notification-web/src/main/resources/config/queue.yml b/notification-service/notification-web/src/main/resources/config/queue.yml index c1af6b725..64bbeb07e 100644 --- a/notification-service/notification-web/src/main/resources/config/queue.yml +++ b/notification-service/notification-web/src/main/resources/config/queue.yml @@ -38,6 +38,8 @@ queue: enable: false options: exchange: null + tenant-default-locale-removal-topic: tenant_default_locale.remove + tenant-default-locale-touched-topic: tenant_default_locale.touch notify-topic: notification.notify tenant-removal-topic: tenant.remove tenant-touched-topic: tenant.touch diff --git a/notification-service/notification/src/main/java/gr/cite/notification/audit/AuditableAction.java b/notification-service/notification/src/main/java/gr/cite/notification/audit/AuditableAction.java index f64bd5b9e..2d8c92f7b 100644 --- a/notification-service/notification/src/main/java/gr/cite/notification/audit/AuditableAction.java +++ b/notification-service/notification/src/main/java/gr/cite/notification/audit/AuditableAction.java @@ -36,6 +36,8 @@ public class AuditableAction { public static final EventId Tenant_Configuration_Persist = new EventId(21002, "Tenant_Configuration_Persist"); public static final EventId Tenant_Configuration_Delete = new EventId(21003, "Tenant_Configuration_Delete"); public static final EventId TenantConfiguration_LookupByType = new EventId(210004, "TenantConfiguration_LookupByType"); + public static final EventId Tenant_Configuration_DefaultUserLocale_Delete = new EventId(21005, "Tenant_Configuration_DefaultUserLocale_Delete"); + public static final EventId Tenant_Configuration_DefaultUserLocale_Persist = new EventId(21006, "Tenant_Configuration_DefaultUserLocale_Persist"); public static final EventId User_Notification_Preference_Query = new EventId(22000, "User_Notification_Preference_Query"); public static final EventId User_Notification_Preference_Lookup = new EventId(22001, "User_Notification_Preference_Lookup"); diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/AppRabbitConfigurer.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/AppRabbitConfigurer.java index a0e003cb9..46dbd2ec5 100644 --- a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/AppRabbitConfigurer.java +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/AppRabbitConfigurer.java @@ -33,6 +33,8 @@ public class AppRabbitConfigurer extends RabbitConfigurer { public InboxBindings inboxBindingsCreator() { List bindingItems = new ArrayList<>(); bindingItems.addAll(this.inboxProperties.getNotifyTopic()); + bindingItems.addAll(this.inboxProperties.getTenantDefaultLocaleRemovalTopic()); + bindingItems.addAll(this.inboxProperties.getTenantDefaultLocaleTouchedTopic()); bindingItems.addAll(this.inboxProperties.getTenantRemovalTopic()); bindingItems.addAll(this.inboxProperties.getTenantTouchedTopic()); bindingItems.addAll(this.inboxProperties.getUserRemovalTopic()); diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/InboxProperties.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/InboxProperties.java index 18e14699b..c24e96488 100644 --- a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/InboxProperties.java +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/InboxProperties.java @@ -10,6 +10,8 @@ public class InboxProperties { private final String exchange; private final List notifyTopic; + private final List tenantDefaultLocaleRemovalTopic; + private final List tenantDefaultLocaleTouchedTopic; private final List tenantRemovalTopic; @@ -20,15 +22,19 @@ public class InboxProperties { private final List userTouchedTopic; public InboxProperties( - String exchange, - List notifyTopic, - List tenantRemovalTopic, - List tenantTouchedTopic, - List userRemovalTopic, - List userTouchedTopic) { + String exchange, + List notifyTopic, + List tenantDefaultLocaleRemovalTopic, + List tenantDefaultLocaleTouchedTopic, + List tenantRemovalTopic, + List tenantTouchedTopic, + List userRemovalTopic, + List userTouchedTopic) { this.exchange = exchange; this.notifyTopic = notifyTopic; - this.tenantRemovalTopic = tenantRemovalTopic; + this.tenantDefaultLocaleRemovalTopic = tenantDefaultLocaleRemovalTopic; + this.tenantDefaultLocaleTouchedTopic = tenantDefaultLocaleTouchedTopic; + this.tenantRemovalTopic = tenantRemovalTopic; this.tenantTouchedTopic = tenantTouchedTopic; this.userRemovalTopic = userRemovalTopic; this.userTouchedTopic = userTouchedTopic; @@ -38,6 +44,14 @@ public class InboxProperties { return notifyTopic; } + public List getTenantDefaultLocaleRemovalTopic() { + return tenantDefaultLocaleRemovalTopic; + } + + public List getTenantDefaultLocaleTouchedTopic() { + return tenantDefaultLocaleTouchedTopic; + } + public List getTenantRemovalTopic() { return tenantRemovalTopic; } diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/InboxRepositoryImpl.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/InboxRepositoryImpl.java index 890b03f27..a5c6d2553 100644 --- a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/InboxRepositoryImpl.java +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/InboxRepositoryImpl.java @@ -7,6 +7,8 @@ import gr.cite.notification.data.QueueInboxEntity; import gr.cite.notification.data.TenantEntityManager; import gr.cite.notification.integrationevent.TrackedEvent; import gr.cite.notification.integrationevent.inbox.notify.NotifyIntegrationEventHandler; +import gr.cite.notification.integrationevent.inbox.tenantdefaultlocaleremoval.TenantDefaultLocaleRemovalIntegrationEventHandler; +import gr.cite.notification.integrationevent.inbox.tenantdefaultlocaletouched.TenantDefaultLocaleTouchedIntegrationEventHandler; import gr.cite.notification.integrationevent.inbox.tenantremoval.TenantRemovalIntegrationEventHandler; import gr.cite.notification.integrationevent.inbox.tenanttouched.TenantTouchedIntegrationEventHandler; import gr.cite.notification.integrationevent.inbox.userremoval.UserRemovalIntegrationEventHandler; @@ -347,6 +349,10 @@ public class InboxRepositoryImpl implements InboxRepository { handler = this.applicationContext.getBean(UserTouchedIntegrationEventHandler.class); else if (this.routingKeyMatched(queueInboxMessage.getRoute(), this.inboxProperties.getNotifyTopic())) handler = this.applicationContext.getBean(NotifyIntegrationEventHandler.class); + else if (this.routingKeyMatched(queueInboxMessage.getRoute(), this.inboxProperties.getTenantDefaultLocaleRemovalTopic())) + handler = this.applicationContext.getBean(TenantDefaultLocaleRemovalIntegrationEventHandler.class); + else if (this.routingKeyMatched(queueInboxMessage.getRoute(), this.inboxProperties.getTenantDefaultLocaleTouchedTopic())) + handler = this.applicationContext.getBean(TenantDefaultLocaleTouchedIntegrationEventHandler.class); else { logger.error("No handler found for message routing key '{}'. Discarding.", queueInboxMessage.getRoute()); handler = null; diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalConsistencyHandler.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalConsistencyHandler.java new file mode 100644 index 000000000..58f9b33bd --- /dev/null +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalConsistencyHandler.java @@ -0,0 +1,27 @@ +package gr.cite.notification.integrationevent.inbox.tenantdefaultlocaleremoval; + +import gr.cite.notification.integrationevent.inbox.ConsistencyHandler; +import gr.cite.notification.query.TenantQuery; +import gr.cite.tools.data.query.QueryFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class TenantDefaultLocaleRemovalConsistencyHandler implements ConsistencyHandler { + + private final QueryFactory queryFactory; + + public TenantDefaultLocaleRemovalConsistencyHandler(QueryFactory queryFactory) { + this.queryFactory = queryFactory; + } + + @Override + public Boolean isConsistent(TenantDefaultLocaleRemovalConsistencyPredicates consistencyPredicates) { + if (consistencyPredicates.getTenantId() == null) return true; + long count = this.queryFactory.query(TenantQuery.class).ids(consistencyPredicates.getTenantId()).count(); + return count > 0; + } + +} diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalConsistencyPredicates.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalConsistencyPredicates.java new file mode 100644 index 000000000..a5954db7b --- /dev/null +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalConsistencyPredicates.java @@ -0,0 +1,23 @@ +package gr.cite.notification.integrationevent.inbox.tenantdefaultlocaleremoval; + +import gr.cite.notification.integrationevent.inbox.ConsistencyPredicates; + +import java.util.UUID; + +public class TenantDefaultLocaleRemovalConsistencyPredicates implements ConsistencyPredicates { + + private UUID tenantId; + + public TenantDefaultLocaleRemovalConsistencyPredicates(UUID tenantId) { + this.tenantId = tenantId; + } + + public UUID getTenantId() { + return tenantId; + } + + public void setTenantId(UUID tenantId) { + this.tenantId = tenantId; + } + +} diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEvent.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEvent.java new file mode 100644 index 000000000..29cd64baf --- /dev/null +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEvent.java @@ -0,0 +1,18 @@ +package gr.cite.notification.integrationevent.inbox.tenantdefaultlocaleremoval; + +import gr.cite.notification.integrationevent.TrackedEvent; + +import java.util.UUID; + +public class TenantDefaultLocaleRemovalIntegrationEvent extends TrackedEvent { + + private UUID tenantId; + + public UUID getTenantId() { + return tenantId; + } + + public void setTenantId(UUID tenantId) { + this.tenantId = tenantId; + } +} diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandler.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandler.java new file mode 100644 index 000000000..8fbd6e685 --- /dev/null +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandler.java @@ -0,0 +1,8 @@ +package gr.cite.notification.integrationevent.inbox.tenantdefaultlocaleremoval; + + +import gr.cite.notification.integrationevent.inbox.IntegrationEventHandler; + +public interface TenantDefaultLocaleRemovalIntegrationEventHandler extends IntegrationEventHandler { + +} diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.java new file mode 100644 index 000000000..204570ed8 --- /dev/null +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaleremoval/TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.java @@ -0,0 +1,106 @@ +package gr.cite.notification.integrationevent.inbox.tenantdefaultlocaleremoval; + +import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver; +import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties; +import gr.cite.notification.audit.AuditableAction; +import gr.cite.notification.common.JsonHandlingService; +import gr.cite.notification.common.enums.TenantConfigurationType; +import gr.cite.notification.common.scope.tenant.TenantScope; +import gr.cite.notification.data.TenantConfigurationEntity; +import gr.cite.notification.data.TenantEntity; +import gr.cite.notification.data.TenantEntityManager; +import gr.cite.notification.integrationevent.inbox.EventProcessingStatus; +import gr.cite.notification.integrationevent.inbox.InboxPrincipal; +import gr.cite.notification.integrationevent.inbox.IntegrationEventProperties; +import gr.cite.notification.model.Tenant; +import gr.cite.notification.query.TenantQuery; +import gr.cite.notification.service.tenantconfiguration.TenantConfigurationService; +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 org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.AbstractMap; +import java.util.Map; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class TenantDefaultLocaleRemovalIntegrationEventHandlerImpl implements TenantDefaultLocaleRemovalIntegrationEventHandler { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.class)); + + private final JsonHandlingService jsonHandlingService; + private final CurrentPrincipalResolver currentPrincipalResolver; + private final ClaimExtractorProperties claimExtractorProperties; + private final TenantConfigurationService tenantConfigurationService; + private final AuditService auditService; + private final TenantEntityManager tenantEntityManager; + private final TenantScope tenantScope; + private final QueryFactory queryFactory; + private final TenantDefaultLocaleRemovalConsistencyHandler tenantConfigurationRemovalConsistencyHandler; + public TenantDefaultLocaleRemovalIntegrationEventHandlerImpl(JsonHandlingService jsonHandlingService, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractorProperties claimExtractorProperties, TenantConfigurationService tenantConfigurationService, AuditService auditService, TenantEntityManager tenantEntityManager, TenantScope tenantScope, QueryFactory queryFactory, TenantDefaultLocaleRemovalConsistencyHandler tenantConfigurationRemovalConsistencyHandler) { + this.jsonHandlingService = jsonHandlingService; + this.currentPrincipalResolver = currentPrincipalResolver; + this.claimExtractorProperties = claimExtractorProperties; + this.tenantConfigurationService = tenantConfigurationService; + this.auditService = auditService; + this.tenantEntityManager = tenantEntityManager; + this.tenantScope = tenantScope; + this.queryFactory = queryFactory; + this.tenantConfigurationRemovalConsistencyHandler = tenantConfigurationRemovalConsistencyHandler; + } + + @Override + public EventProcessingStatus handle(IntegrationEventProperties properties, String message) { + TenantDefaultLocaleRemovalIntegrationEvent event = this.jsonHandlingService.fromJsonSafe(TenantDefaultLocaleRemovalIntegrationEvent.class, message); + if (event == null) + return EventProcessingStatus.Error; + + EventProcessingStatus status = EventProcessingStatus.Success; + try { + if (!(tenantConfigurationRemovalConsistencyHandler.isConsistent(new TenantDefaultLocaleRemovalConsistencyPredicates(event.getTenantId())))) { + status = EventProcessingStatus.Postponed; + return status; + } + + if (this.tenantScope.isMultitenant() && properties.getTenantId() != null) { + TenantEntity tenant = queryFactory.query(TenantQuery.class).ids(properties.getTenantId()).firstAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); + if (tenant == null) { + logger.error("missing tenant from event message"); + return EventProcessingStatus.Error; + } + this.tenantScope.setTempTenant(tenantEntityManager.getEntityManager(), properties.getTenantId(), tenant.getCode()); + } else if (this.tenantScope.isMultitenant()) { +// logger.error("missing tenant from event message"); +// return EventProcessingStatus.Error; + this.tenantScope.setTempTenant(tenantEntityManager.getEntityManager(), null, this.tenantScope.getDefaultTenantCode()); + } + currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties)); + + TenantConfigurationEntity tenantConfiguration = tenantConfigurationService.getTenantConfigurationEntityForType(TenantConfigurationType.DefaultUserLocale); + if (tenantConfiguration == null){ + status = EventProcessingStatus.Discard; + currentPrincipalResolver.pop(); + return status; + } + + tenantConfigurationService.deleteAndSave(tenantConfiguration.getId()); + + auditService.track(AuditableAction.Tenant_Configuration_DefaultUserLocale_Delete, Map.ofEntries( + new AbstractMap.SimpleEntry("tenantId", event.getTenantId() != null ? event.getTenantId() : "") + )); + //auditService.trackIdentity(AuditableAction.IdentityTracking_Action); + } catch (Exception ex) { + status = EventProcessingStatus.Error; + logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex); + } finally { + currentPrincipalResolver.pop(); + } + return status; + } + +} diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedConsistencyHandler.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedConsistencyHandler.java new file mode 100644 index 000000000..c0b56404f --- /dev/null +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedConsistencyHandler.java @@ -0,0 +1,27 @@ +package gr.cite.notification.integrationevent.inbox.tenantdefaultlocaletouched; + +import gr.cite.notification.integrationevent.inbox.ConsistencyHandler; +import gr.cite.notification.query.TenantQuery; +import gr.cite.tools.data.query.QueryFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class TenantDefaultLocaleTouchedConsistencyHandler implements ConsistencyHandler { + + private final QueryFactory queryFactory; + + public TenantDefaultLocaleTouchedConsistencyHandler(QueryFactory queryFactory) { + this.queryFactory = queryFactory; + } + + @Override + public Boolean isConsistent(TenantDefaultLocaleTouchedConsistencyPredicates consistencyPredicates) { + if (consistencyPredicates.getTenantId() == null) return true; + long count = this.queryFactory.query(TenantQuery.class).ids(consistencyPredicates.getTenantId()).count(); + return count > 0; + } + +} diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedConsistencyPredicates.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedConsistencyPredicates.java new file mode 100644 index 000000000..0b00aedfa --- /dev/null +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedConsistencyPredicates.java @@ -0,0 +1,23 @@ +package gr.cite.notification.integrationevent.inbox.tenantdefaultlocaletouched; + +import gr.cite.notification.integrationevent.inbox.ConsistencyPredicates; + +import java.util.UUID; + +public class TenantDefaultLocaleTouchedConsistencyPredicates implements ConsistencyPredicates { + + private UUID tenantId; + + public TenantDefaultLocaleTouchedConsistencyPredicates(UUID tenantId) { + this.tenantId = tenantId; + } + + public UUID getTenantId() { + return tenantId; + } + + public void setTenantId(UUID tenantId) { + this.tenantId = tenantId; + } + +} diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEvent.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEvent.java new file mode 100644 index 000000000..82cd208b5 --- /dev/null +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEvent.java @@ -0,0 +1,46 @@ +package gr.cite.notification.integrationevent.inbox.tenantdefaultlocaletouched; + +import gr.cite.notification.integrationevent.TrackedEvent; + +import java.util.UUID; + +public class TenantDefaultLocaleTouchedIntegrationEvent extends TrackedEvent { + + private UUID tenantId; + private String timezone; + private String language; + private String culture; + + public UUID getTenantId() { + return tenantId; + } + + public void setTenantId(UUID tenantId) { + this.tenantId = tenantId; + } + + public String getTimezone() { + return timezone; + } + + public void setTimezone(String timezone) { + this.timezone = timezone; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public String getCulture() { + return culture; + } + + public void setCulture(String culture) { + this.culture = culture; + } + +} diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandler.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandler.java new file mode 100644 index 000000000..f214182aa --- /dev/null +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandler.java @@ -0,0 +1,8 @@ +package gr.cite.notification.integrationevent.inbox.tenantdefaultlocaletouched; + + +import gr.cite.notification.integrationevent.inbox.IntegrationEventHandler; + +public interface TenantDefaultLocaleTouchedIntegrationEventHandler extends IntegrationEventHandler { + +} diff --git a/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.java b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.java new file mode 100644 index 000000000..d516d3e87 --- /dev/null +++ b/notification-service/notification/src/main/java/gr/cite/notification/integrationevent/inbox/tenantdefaultlocaletouched/TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.java @@ -0,0 +1,129 @@ +package gr.cite.notification.integrationevent.inbox.tenantdefaultlocaletouched; + +import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver; +import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties; +import gr.cite.notification.audit.AuditableAction; +import gr.cite.notification.common.JsonHandlingService; +import gr.cite.notification.common.enums.TenantConfigurationType; +import gr.cite.notification.common.scope.tenant.TenantScope; +import gr.cite.notification.convention.ConventionService; +import gr.cite.notification.data.TenantConfigurationEntity; +import gr.cite.notification.data.TenantEntity; +import gr.cite.notification.data.TenantEntityManager; +import gr.cite.notification.integrationevent.inbox.EventProcessingStatus; +import gr.cite.notification.integrationevent.inbox.InboxPrincipal; +import gr.cite.notification.integrationevent.inbox.IntegrationEventProperties; +import gr.cite.notification.model.Tenant; +import gr.cite.notification.model.persist.tenantconfiguration.DefaultUserLocaleTenantConfigurationPersist; +import gr.cite.notification.model.persist.tenantconfiguration.TenantConfigurationPersist; +import gr.cite.notification.query.TenantQuery; +import gr.cite.notification.service.tenantconfiguration.TenantConfigurationService; +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.Validator; +import gr.cite.tools.validation.ValidatorFactory; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.AbstractMap; +import java.util.Map; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class TenantDefaultLocaleTouchedIntegrationEventHandlerImpl implements TenantDefaultLocaleTouchedIntegrationEventHandler { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.class)); + + private final JsonHandlingService jsonHandlingService; + private final CurrentPrincipalResolver currentPrincipalResolver; + private final ClaimExtractorProperties claimExtractorProperties; + private final TenantConfigurationService tenantConfigurationService; + private final AuditService auditService; + private final TenantEntityManager tenantEntityManager; + private final TenantScope tenantScope; + private final QueryFactory queryFactory; + private final ConventionService conventionService; + private final ValidatorFactory validatorFactory; + private final TenantDefaultLocaleTouchedConsistencyHandler tenantDefaultLocaleTouchedConsistencyHandler; + public TenantDefaultLocaleTouchedIntegrationEventHandlerImpl(JsonHandlingService jsonHandlingService, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractorProperties claimExtractorProperties, TenantConfigurationService tenantConfigurationService, AuditService auditService, TenantEntityManager tenantEntityManager, TenantScope tenantScope, QueryFactory queryFactory, ConventionService conventionService, ValidatorFactory validatorFactory, TenantDefaultLocaleTouchedConsistencyHandler tenantDefaultLocaleTouchedConsistencyHandler) { + this.jsonHandlingService = jsonHandlingService; + this.currentPrincipalResolver = currentPrincipalResolver; + this.claimExtractorProperties = claimExtractorProperties; + this.tenantConfigurationService = tenantConfigurationService; + this.auditService = auditService; + this.tenantEntityManager = tenantEntityManager; + this.tenantScope = tenantScope; + this.queryFactory = queryFactory; + this.conventionService = conventionService; + this.validatorFactory = validatorFactory; + this.tenantDefaultLocaleTouchedConsistencyHandler = tenantDefaultLocaleTouchedConsistencyHandler; + } + + @Override + public EventProcessingStatus handle(IntegrationEventProperties properties, String message) { + TenantDefaultLocaleTouchedIntegrationEvent event = this.jsonHandlingService.fromJsonSafe(TenantDefaultLocaleTouchedIntegrationEvent.class, message); + if (event == null) + return EventProcessingStatus.Error; + + EventProcessingStatus status = EventProcessingStatus.Success; + try { + + if (!(tenantDefaultLocaleTouchedConsistencyHandler.isConsistent(new TenantDefaultLocaleTouchedConsistencyPredicates(event.getTenantId())))) { + status = EventProcessingStatus.Postponed; + return status; + } + if (this.tenantScope.isMultitenant() && properties.getTenantId() != null) { + TenantEntity tenant = queryFactory.query(TenantQuery.class).ids(properties.getTenantId()).firstAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); + if (tenant == null) { + logger.error("missing tenant from event message"); + return EventProcessingStatus.Error; + } + this.tenantScope.setTempTenant(tenantEntityManager.getEntityManager(), properties.getTenantId(), tenant.getCode()); + } else if (this.tenantScope.isMultitenant()) { +// logger.error("missing tenant from event message"); +// return EventProcessingStatus.Error; + this.tenantScope.setTempTenant(tenantEntityManager.getEntityManager(), null, this.tenantScope.getDefaultTenantCode()); + } + currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties)); + + TenantConfigurationPersist persist = new TenantConfigurationPersist(); + persist.setType(TenantConfigurationType.DefaultUserLocale); + persist.setDefaultUserLocale(new DefaultUserLocaleTenantConfigurationPersist()); + persist.getDefaultUserLocale().setCulture(event.getCulture()); + persist.getDefaultUserLocale().setLanguage(event.getLanguage()); + persist.getDefaultUserLocale().setTimezone(event.getTimezone()); + + TenantConfigurationEntity tenantConfiguration = tenantConfigurationService.getTenantConfigurationEntityForType(TenantConfigurationType.DefaultUserLocale); + if (tenantConfiguration != null){ + persist.setId(tenantConfiguration.getId()); + persist.setHash(this.conventionService.hashValue(tenantConfiguration.getUpdatedAt())); + } + + Validator validator = this.validatorFactory.validator(TenantConfigurationPersist.TenantConfigurationPersistValidator.class); + validator.validate(persist); + if (!validator.result().isValid()) { + status = EventProcessingStatus.Error; + currentPrincipalResolver.pop(); + return status; + } + + tenantConfigurationService.persist(persist, new BaseFieldSet()); + + auditService.track(AuditableAction.Tenant_Configuration_DefaultUserLocale_Persist, Map.ofEntries( + new AbstractMap.SimpleEntry("tenantId", event.getTenantId() != null ? event.getTenantId() : "") + )); + //auditService.trackIdentity(AuditableAction.IdentityTracking_Action); + } catch (Exception ex) { + status = EventProcessingStatus.Error; + logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex); + } finally { + currentPrincipalResolver.pop(); + } + return status; + } + +} diff --git a/notification-service/notification/src/main/java/gr/cite/notification/service/tenantconfiguration/TenantConfigurationService.java b/notification-service/notification/src/main/java/gr/cite/notification/service/tenantconfiguration/TenantConfigurationService.java index 119ea7c8e..611ae8ede 100644 --- a/notification-service/notification/src/main/java/gr/cite/notification/service/tenantconfiguration/TenantConfigurationService.java +++ b/notification-service/notification/src/main/java/gr/cite/notification/service/tenantconfiguration/TenantConfigurationService.java @@ -1,8 +1,10 @@ package gr.cite.notification.service.tenantconfiguration; import com.fasterxml.jackson.core.JsonProcessingException; +import gr.cite.notification.common.enums.TenantConfigurationType; import gr.cite.notification.common.types.tenantconfiguration.DefaultUserLocaleTenantConfigurationEntity; import gr.cite.notification.common.types.tenantconfiguration.NotifierListTenantConfigurationEntity; +import gr.cite.notification.data.TenantConfigurationEntity; import gr.cite.notification.model.persist.tenantconfiguration.TenantConfigurationPersist; import gr.cite.notification.model.tenantconfiguration.TenantConfiguration; import gr.cite.tools.exception.MyApplicationException; @@ -28,4 +30,6 @@ public interface TenantConfigurationService { NotifierListTenantConfigurationEntity collectTenantNotifierList(); DefaultUserLocaleTenantConfigurationEntity collectTenantUserLocale(); + + TenantConfigurationEntity getTenantConfigurationEntityForType(TenantConfigurationType type); } diff --git a/notification-service/notification/src/main/java/gr/cite/notification/service/tenantconfiguration/TenantConfigurationServiceImpl.java b/notification-service/notification/src/main/java/gr/cite/notification/service/tenantconfiguration/TenantConfigurationServiceImpl.java index a24c757cd..65e9b74aa 100644 --- a/notification-service/notification/src/main/java/gr/cite/notification/service/tenantconfiguration/TenantConfigurationServiceImpl.java +++ b/notification-service/notification/src/main/java/gr/cite/notification/service/tenantconfiguration/TenantConfigurationServiceImpl.java @@ -187,24 +187,21 @@ public class TenantConfigurationServiceImpl implements TenantConfigurationServic @Override public NotifierListTenantConfigurationEntity collectTenantNotifierList() { - TenantConfigurationQuery query = this.queryFactory.query(TenantConfigurationQuery.class).authorize(AuthorizationFlags.OwnerOrPermission).isActive(IsActive.Active).types(TenantConfigurationType.NotifierList); - if (this.tenantScope.isDefaultTenant()) query.tenantIsSet(false); - else { - try { - query.tenantIsSet(true).tenantIds(this.tenantScope.getTenant()); - } catch (InvalidApplicationException e) { - throw new RuntimeException(e); - } - } - - TenantConfigurationEntity data = query.first(); + TenantConfigurationEntity data = this.getTenantConfigurationEntityForType(TenantConfigurationType.NotifierList); if (data == null || this.conventionService.isNullOrEmpty(data.getValue())) return null; return this.jsonHandlingService.fromJsonSafe(NotifierListTenantConfigurationEntity.class, data.getValue()); } @Override public DefaultUserLocaleTenantConfigurationEntity collectTenantUserLocale() { - TenantConfigurationQuery query = this.queryFactory.query(TenantConfigurationQuery.class).authorize(AuthorizationFlags.OwnerOrPermission).isActive(IsActive.Active).types(TenantConfigurationType.DefaultUserLocale); + TenantConfigurationEntity data = this.getTenantConfigurationEntityForType(TenantConfigurationType.DefaultUserLocale); + if (data == null || this.conventionService.isNullOrEmpty(data.getValue())) return null; + return this.jsonHandlingService.fromJsonSafe(DefaultUserLocaleTenantConfigurationEntity.class, data.getValue()); + } + + @Override + public TenantConfigurationEntity getTenantConfigurationEntityForType(TenantConfigurationType type){ + TenantConfigurationQuery query = this.queryFactory.query(TenantConfigurationQuery.class).authorize(AuthorizationFlags.OwnerOrPermission).isActive(IsActive.Active).types(type); if (this.tenantScope.isDefaultTenant()) query.tenantIsSet(false); else { try { @@ -214,9 +211,7 @@ public class TenantConfigurationServiceImpl implements TenantConfigurationServic } } - TenantConfigurationEntity data = query.first(); - if (data == null || this.conventionService.isNullOrEmpty(data.getValue())) return null; - return this.jsonHandlingService.fromJsonSafe(DefaultUserLocaleTenantConfigurationEntity.class, data.getValue()); + return query.first(); } }