2024-03-29 10:47:27 +01:00
|
|
|
package gr.cite.annotation.integrationevent.inbox.annotationentitiestouch;
|
2024-02-09 17:50:34 +01:00
|
|
|
|
2024-02-12 10:49:37 +01:00
|
|
|
import gr.cite.annotation.audit.AuditableAction;
|
|
|
|
import gr.cite.annotation.common.JsonHandlingService;
|
2024-03-11 14:55:52 +01:00
|
|
|
import gr.cite.annotation.common.enums.IsActive;
|
2024-02-12 10:49:37 +01:00
|
|
|
import gr.cite.annotation.common.scope.fake.FakeRequestScope;
|
2024-04-03 17:35:37 +02:00
|
|
|
import gr.cite.annotation.common.scope.tenant.TenantScope;
|
2024-03-11 14:55:52 +01:00
|
|
|
import gr.cite.annotation.data.EntityUserEntity;
|
2024-04-03 17:35:37 +02:00
|
|
|
import gr.cite.annotation.data.TenantEntity;
|
|
|
|
import gr.cite.annotation.data.TenantEntityManager;
|
2024-02-12 10:49:37 +01:00
|
|
|
import gr.cite.annotation.integrationevent.inbox.EventProcessingStatus;
|
|
|
|
import gr.cite.annotation.integrationevent.inbox.InboxPrincipal;
|
|
|
|
import gr.cite.annotation.integrationevent.inbox.IntegrationEventProperties;
|
2024-04-03 17:35:37 +02:00
|
|
|
import gr.cite.annotation.model.Tenant;
|
2024-03-11 14:55:52 +01:00
|
|
|
import gr.cite.annotation.query.EntityUserQuery;
|
2024-04-03 17:35:37 +02:00
|
|
|
import gr.cite.annotation.query.TenantQuery;
|
2024-04-04 15:39:40 +02:00
|
|
|
import gr.cite.annotation.service.tenant.TenantService;
|
2024-03-11 14:55:52 +01: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;
|
2024-03-29 10:47:27 +01:00
|
|
|
import gr.cite.tools.data.deleter.DeleterFactory;
|
2024-02-09 17:50:34 +01:00
|
|
|
import gr.cite.tools.data.query.QueryFactory;
|
2024-04-03 17:35:37 +02:00
|
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
2024-02-09 17:50:34 +01:00
|
|
|
import gr.cite.tools.logging.LoggerService;
|
|
|
|
import gr.cite.tools.validation.ValidatorFactory;
|
|
|
|
import jakarta.persistence.EntityManager;
|
|
|
|
import jakarta.persistence.EntityManagerFactory;
|
|
|
|
import jakarta.persistence.EntityTransaction;
|
|
|
|
import jakarta.persistence.OptimisticLockException;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
import org.springframework.context.annotation.Scope;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
2024-04-04 15:39:40 +02:00
|
|
|
import javax.management.InvalidApplicationException;
|
2024-03-11 14:55:52 +01:00
|
|
|
import java.time.Instant;
|
2024-03-29 10:47:27 +01:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.stream.Collectors;
|
2024-02-09 17:50:34 +01:00
|
|
|
|
|
|
|
@Component
|
|
|
|
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
2024-03-29 10:47:27 +01:00
|
|
|
public class AnnotationEntitiesTouchedIntegrationEventHandlerImpl implements AnnotationEntitiesTouchedIntegrationEventHandler {
|
2024-02-09 17:50:34 +01:00
|
|
|
|
2024-03-29 10:47:27 +01:00
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(AnnotationEntitiesTouchedIntegrationEventHandlerImpl.class));
|
2024-02-09 17:50:34 +01:00
|
|
|
|
|
|
|
private final JsonHandlingService jsonHandlingService;
|
|
|
|
|
|
|
|
private final ValidatorFactory validatorFactory;
|
|
|
|
|
2024-03-11 14:55:52 +01:00
|
|
|
private final QueryFactory queryFactory;
|
2024-04-04 15:39:40 +02:00
|
|
|
private final CurrentPrincipalResolver currentPrincipalResolver;
|
|
|
|
private final ClaimExtractorProperties claimExtractorProperties;
|
|
|
|
private final AuditService auditService;
|
|
|
|
private final TenantEntityManager tenantEntityManager;
|
|
|
|
private final DeleterFactory deleterFactory;
|
|
|
|
private final TenantScope tenantScope;
|
|
|
|
|
|
|
|
public AnnotationEntitiesTouchedIntegrationEventHandlerImpl(JsonHandlingService jsonHandlingService, ValidatorFactory validatorFactory, QueryFactory queryFactory, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractorProperties claimExtractorProperties, AuditService auditService, TenantEntityManager tenantEntityManager, DeleterFactory deleterFactory, TenantScope tenantScope) {
|
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.auditService = auditService;
|
|
|
|
this.tenantEntityManager = tenantEntityManager;
|
2024-03-11 14:55:52 +01:00
|
|
|
this.queryFactory = queryFactory;
|
2024-04-04 15:39:40 +02:00
|
|
|
this.deleterFactory = deleterFactory;
|
|
|
|
this.tenantScope = tenantScope;
|
2024-02-09 17:50:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EventProcessingStatus handle(IntegrationEventProperties properties, String message) {
|
2024-03-29 10:47:27 +01:00
|
|
|
AnnotationEntitiesTouchedIntegrationEvent event = this.jsonHandlingService.fromJsonSafe(AnnotationEntitiesTouchedIntegrationEvent.class, message);
|
2024-02-09 17:50:34 +01:00
|
|
|
if (event == null)
|
|
|
|
return EventProcessingStatus.Error;
|
|
|
|
|
2024-03-29 10:47:27 +01:00
|
|
|
logger.debug("Handling {}", AnnotationEntitiesTouchedIntegrationEvent.class.getSimpleName());
|
2024-02-09 17:50:34 +01:00
|
|
|
|
2024-03-29 10:47:27 +01:00
|
|
|
this.validatorFactory.validator(AnnotationEntitiesTouchedIntegrationEvent.AnnotationEntitiesTouchedIntegrationEventValidator.class).validateForce(event);
|
2024-02-09 17:50:34 +01:00
|
|
|
|
2024-04-04 15:39:40 +02:00
|
|
|
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()) {
|
2024-04-03 17:35:37 +02:00
|
|
|
// logger.error("missing tenant from event message");
|
|
|
|
// return EventProcessingStatus.Error;
|
2024-04-04 15:39:40 +02:00
|
|
|
this.tenantScope.setTempTenant(tenantEntityManager.getEntityManager(), null, this.tenantScope.getDefaultTenantCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties));
|
|
|
|
|
|
|
|
tenantEntityManager.disableTenantFilters();
|
|
|
|
for (AnnotationEntitiesTouchedIntegrationEvent.AnnotationEntityTouchedIntegrationEvent entityEvent : event.getEvents()) {
|
|
|
|
|
|
|
|
EntityUserQuery entityUserQuery = this.queryFactory.query(EntityUserQuery.class);
|
|
|
|
List<EntityUserEntity> items = entityUserQuery
|
|
|
|
.entityIds(entityEvent.getEntityId())
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
.collect();
|
|
|
|
List<UUID> updatedCreatedIds = new ArrayList<>();
|
|
|
|
for (UUID user : entityEvent.getUserIds()) {
|
|
|
|
EntityUserEntity data = items.stream().filter(x -> x.getUserId().equals(user)).findFirst().orElse(null);
|
|
|
|
if (data == null) {
|
|
|
|
data = new EntityUserEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setEntityId(entityEvent.getEntityId());
|
|
|
|
data.setUserId(user);
|
|
|
|
data.setTenantId(properties.getTenantId());
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
data.setIsActive(IsActive.Active);
|
2024-04-08 10:21:51 +02:00
|
|
|
tenantEntityManager.persist(data);
|
2024-03-11 14:55:52 +01:00
|
|
|
}
|
2024-04-04 15:39:40 +02:00
|
|
|
updatedCreatedIds.add(data.getId());
|
|
|
|
}
|
2024-02-09 17:50:34 +01:00
|
|
|
|
2024-04-04 15:39:40 +02:00
|
|
|
List<EntityUserEntity> toDelete = items.stream().filter(x -> updatedCreatedIds.stream().noneMatch(y -> y.equals(x.getId()))).collect(Collectors.toList());
|
|
|
|
deleterFactory.deleter(gr.cite.EntityUser.model.deleter.EntityUserDeleter.class).delete(toDelete);
|
2024-02-09 17:50:34 +01:00
|
|
|
|
2024-04-04 15:39:40 +02:00
|
|
|
tenantEntityManager.flush();
|
2024-02-09 17:50:34 +01:00
|
|
|
|
2024-04-04 15:39:40 +02:00
|
|
|
|
|
|
|
auditService.track(AuditableAction.User_Persist, Map.ofEntries(
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("model", event)
|
|
|
|
));
|
2024-02-09 17:50:34 +01:00
|
|
|
}
|
2024-04-04 15:39:40 +02:00
|
|
|
|
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();
|
|
|
|
tenantScope.removeTempTenant(this.tenantEntityManager.getEntityManager());
|
|
|
|
try {
|
|
|
|
this.tenantEntityManager.enableTenantFilters();
|
|
|
|
} catch (InvalidApplicationException e) {
|
|
|
|
}
|
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
|
|
|
}
|
2024-03-11 14:55:52 +01:00
|
|
|
|
2024-02-09 17:50:34 +01:00
|
|
|
}
|