argos/annotation-service/annotation/src/main/java/gr/cite/annotation/integrationevent/inbox/tenanttouch/TenantTouchedIntegrationEve...

82 lines
3.9 KiB
Java
Raw Normal View History

2024-03-11 14:55:52 +01:00
package gr.cite.annotation.integrationevent.inbox.tenanttouch;
2024-02-09 17:50:34 +01:00
import gr.cite.annotation.audit.AuditableAction;
import gr.cite.annotation.common.JsonHandlingService;
2024-04-04 15:39:40 +02:00
import gr.cite.annotation.data.TenantEntityManager;
import gr.cite.annotation.integrationevent.inbox.EventProcessingStatus;
import gr.cite.annotation.integrationevent.inbox.InboxPrincipal;
import gr.cite.annotation.integrationevent.inbox.IntegrationEventProperties;
import gr.cite.annotation.model.persist.TenantTouchedIntegrationEventPersist;
import gr.cite.annotation.service.tenant.TenantService;
2024-04-04 15:39:40 +02:00
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
2024-04-04 11:21:12 +02:00
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties;
2024-02-09 17:50:34 +01:00
import gr.cite.tools.auditing.AuditService;
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 TenantTouchedIntegrationEventHandlerImpl implements TenantTouchedIntegrationEventHandler {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantTouchedIntegrationEventHandlerImpl.class));
private final JsonHandlingService jsonHandlingService;
private final ValidatorFactory validatorFactory;
2024-04-04 15:39:40 +02:00
private final CurrentPrincipalResolver currentPrincipalResolver;
private final ClaimExtractorProperties claimExtractorProperties;
private final TenantService tenantService;
private final AuditService auditService;
private final TenantEntityManager tenantEntityManager;
2024-02-09 17:50:34 +01:00
2024-04-04 15:39:40 +02:00
public TenantTouchedIntegrationEventHandlerImpl(JsonHandlingService jsonHandlingService, ValidatorFactory validatorFactory, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractorProperties claimExtractorProperties, TenantService tenantService, AuditService auditService, TenantEntityManager tenantEntityManager) {
2024-02-09 17:50:34 +01:00
this.jsonHandlingService = jsonHandlingService;
this.validatorFactory = validatorFactory;
2024-04-04 15:39:40 +02:00
this.currentPrincipalResolver = currentPrincipalResolver;
this.claimExtractorProperties = claimExtractorProperties;
this.tenantService = tenantService;
this.auditService = auditService;
this.tenantEntityManager = tenantEntityManager;
2024-02-09 17:50:34 +01:00
}
@Override
public EventProcessingStatus handle(IntegrationEventProperties properties, String message) {
TenantTouchedIntegrationEvent event = this.jsonHandlingService.fromJsonSafe(TenantTouchedIntegrationEvent.class, message);
if (event == null)
return EventProcessingStatus.Error;
TenantTouchedIntegrationEventPersist model = new TenantTouchedIntegrationEventPersist();
model.setId(event.getId());
model.setCode(event.getCode());
this.validatorFactory.validator(TenantTouchedIntegrationEventPersist.TenantTouchedIntegrationEventPersistValidator.class).validateForce(model);
2024-04-04 15:39:40 +02:00
EventProcessingStatus status = EventProcessingStatus.Success;
tenantEntityManager.disableTenantFilters();
try {
currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties));
2024-02-09 17:50:34 +01:00
2024-04-04 15:39:40 +02:00
tenantService.persist(model, null);
2024-02-09 17:50:34 +01:00
2024-04-04 15:39:40 +02:00
auditService.track(AuditableAction.Tenant_Persist, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model)
));
2024-02-09 17:50:34 +01:00
} catch (Exception ex) {
2024-04-04 15:39:40 +02:00
status = EventProcessingStatus.Error;
2024-02-09 17:50:34 +01:00
logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
2024-04-04 15:39:40 +02:00
} finally {
currentPrincipalResolver.pop();
2024-02-09 17:50:34 +01:00
}
2024-04-04 15:39:40 +02:00
return status;
2024-02-09 17:50:34 +01:00
}
}