package gr.cite.notification.integrationevent.inbox.usertouched; 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.scope.tenant.TenantScope; 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.user.UserService; 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 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 UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegrationEventHandler { private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserTouchedIntegrationEventHandlerImpl.class)); private final JsonHandlingService jsonHandlingService; private final ValidatorFactory validatorFactory; private final QueryFactory queryFactory; private final TenantScope tenantScope; private final CurrentPrincipalResolver currentPrincipalResolver; private final ClaimExtractorProperties claimExtractorProperties; private final UserService userService; private final AuditService auditService; private final TenantEntityManager tenantEntityManager; public UserTouchedIntegrationEventHandlerImpl( JsonHandlingService jsonHandlingService, ValidatorFactory validatorFactory, QueryFactory queryFactory, TenantScope tenantScope, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractorProperties claimExtractorProperties, UserService userService, AuditService auditService, TenantEntityManager tenantEntityManager) { this.jsonHandlingService = jsonHandlingService; this.validatorFactory = validatorFactory; this.queryFactory = queryFactory; this.tenantScope = tenantScope; this.currentPrincipalResolver = currentPrincipalResolver; this.claimExtractorProperties = claimExtractorProperties; this.userService = userService; this.auditService = auditService; this.tenantEntityManager = tenantEntityManager; } @Override public EventProcessingStatus handle(IntegrationEventProperties properties, String message) { UserTouchedIntegrationEvent event = this.jsonHandlingService.fromJsonSafe(UserTouchedIntegrationEvent.class, message); if (event == null) return EventProcessingStatus.Error; logger.debug("Handling {}", UserTouchedIntegrationEvent.class.getSimpleName()); this.validatorFactory.validator(UserTouchedIntegrationEvent.UserTouchedIntegrationEventValidator.class).validateForce(event); EventProcessingStatus status = EventProcessingStatus.Success; try { 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)); userService.persist(event, null); auditService.track(AuditableAction.User_Persist, Map.ofEntries( new AbstractMap.SimpleEntry("model", event) )); } catch (Exception ex) { status = EventProcessingStatus.Error; logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex); } finally { currentPrincipalResolver.pop(); tenantScope.removeTempTenant(this.tenantEntityManager.getEntityManager()); } return status; } }