130 lines
6.1 KiB
Java
130 lines
6.1 KiB
Java
|
package gr.cite.notification.integrationevent.inbox.usertouched;
|
||
|
|
||
|
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
|
||
|
import gr.cite.notification.audit.AuditableAction;
|
||
|
import gr.cite.notification.common.JsonHandlingService;
|
||
|
import gr.cite.notification.common.scope.fake.FakeRequestScope;
|
||
|
import gr.cite.notification.common.scope.tenant.TenantScope;
|
||
|
import gr.cite.notification.data.TenantEntity;
|
||
|
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 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;
|
||
|
|
||
|
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));
|
||
|
|
||
|
protected final ApplicationContext applicationContext;
|
||
|
|
||
|
private final JsonHandlingService jsonHandlingService;
|
||
|
|
||
|
private final ValidatorFactory validatorFactory;
|
||
|
|
||
|
public UserTouchedIntegrationEventHandlerImpl(
|
||
|
JsonHandlingService jsonHandlingService,
|
||
|
ApplicationContext applicationContext,
|
||
|
ValidatorFactory validatorFactory) {
|
||
|
this.jsonHandlingService = jsonHandlingService;
|
||
|
this.applicationContext = applicationContext;
|
||
|
this.validatorFactory = validatorFactory;
|
||
|
}
|
||
|
|
||
|
@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);
|
||
|
|
||
|
EntityManager entityManager = null;
|
||
|
EntityTransaction transaction = null;
|
||
|
try (FakeRequestScope ignored = new FakeRequestScope()) {
|
||
|
try {
|
||
|
QueryFactory queryFactory = this.applicationContext.getBean(QueryFactory.class);
|
||
|
TenantScope scope = this.applicationContext.getBean(TenantScope.class);
|
||
|
if (scope.isMultitenant() && event.getTenant() != null) {
|
||
|
TenantEntity tenant = queryFactory.query(TenantQuery.class).ids(event.getTenant()).firstAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
|
||
|
if (tenant == null) {
|
||
|
logger.error("missing tenant from event message");
|
||
|
return EventProcessingStatus.Error;
|
||
|
}
|
||
|
scope.setTenant(event.getTenant(), tenant.getCode());
|
||
|
} else if (scope.isMultitenant()) {
|
||
|
logger.error("missing tenant from event message");
|
||
|
return EventProcessingStatus.Error;
|
||
|
}
|
||
|
//
|
||
|
// ValidationService validator = this.applicationContext.getBean(ValidationService.class);
|
||
|
// validator.validateForce(model);
|
||
|
|
||
|
CurrentPrincipalResolver currentPrincipalResolver = this.applicationContext.getBean(CurrentPrincipalResolver.class);
|
||
|
currentPrincipalResolver.push(InboxPrincipal.build(properties));
|
||
|
|
||
|
EntityManagerFactory entityManagerFactory = this.applicationContext.getBean(EntityManagerFactory.class);
|
||
|
entityManager = entityManagerFactory.createEntityManager();
|
||
|
|
||
|
transaction = entityManager.getTransaction();
|
||
|
transaction.begin();
|
||
|
|
||
|
try {
|
||
|
UserService userService = this.applicationContext.getBean(UserService.class);
|
||
|
userService.persist(event, null);
|
||
|
|
||
|
AuditService auditService = this.applicationContext.getBean(AuditService.class);
|
||
|
|
||
|
auditService.track(AuditableAction.User_Persist, Map.ofEntries(
|
||
|
new AbstractMap.SimpleEntry<String, Object>("model", event)
|
||
|
));
|
||
|
|
||
|
transaction.commit();
|
||
|
} catch (Exception e) {
|
||
|
transaction.rollback();
|
||
|
throw e;
|
||
|
} finally {
|
||
|
currentPrincipalResolver.pop();
|
||
|
}
|
||
|
} catch (OptimisticLockException ex) {
|
||
|
// we get this if/when someone else already modified the notifications. We want to essentially ignore this, and keep working
|
||
|
logger.debug("Concurrency exception getting queue outbox. Skipping: {} ", ex.getMessage());
|
||
|
if (transaction != null)
|
||
|
transaction.rollback();
|
||
|
} catch (Exception ex) {
|
||
|
logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
|
||
|
if (transaction != null)
|
||
|
transaction.rollback();
|
||
|
} finally {
|
||
|
if (entityManager != null)
|
||
|
entityManager.close();
|
||
|
}
|
||
|
} catch (Exception ex) {
|
||
|
logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
|
||
|
}
|
||
|
return EventProcessingStatus.Success;
|
||
|
}
|
||
|
}
|