157 lines
7.8 KiB
Java
157 lines
7.8 KiB
Java
package gr.cite.notification.integrationevent.inbox.notify;
|
|
|
|
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.enums.NotificationNotifyState;
|
|
import gr.cite.notification.common.enums.NotificationTrackingProcess;
|
|
import gr.cite.notification.common.enums.NotificationTrackingState;
|
|
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.errorcode.ErrorThesaurusProperties;
|
|
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.NotificationPersist;
|
|
import gr.cite.notification.query.TenantQuery;
|
|
import gr.cite.notification.service.notification.NotificationService;
|
|
import gr.cite.tools.auditing.AuditService;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
import gr.cite.tools.exception.MyValidationException;
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
|
import gr.cite.tools.logging.LoggerService;
|
|
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.MessageSource;
|
|
import org.springframework.context.annotation.Scope;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.time.Instant;
|
|
import java.util.AbstractMap;
|
|
import java.util.Map;
|
|
|
|
@Component
|
|
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
|
public class NotifyIntegrationEventHandlerImpl implements NotifyIntegrationEventHandler {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(NotifyIntegrationEventHandlerImpl.class));
|
|
|
|
private final JsonHandlingService jsonHandlingService;
|
|
|
|
private final ApplicationContext applicationContext;
|
|
|
|
private final ErrorThesaurusProperties errors;
|
|
|
|
private final MessageSource messageSource;
|
|
|
|
public NotifyIntegrationEventHandlerImpl(JsonHandlingService jsonHandlingService, ApplicationContext applicationContext, ErrorThesaurusProperties errors, MessageSource messageSource) {
|
|
this.jsonHandlingService = jsonHandlingService;
|
|
this.applicationContext = applicationContext;
|
|
this.errors = errors;
|
|
this.messageSource = messageSource;
|
|
}
|
|
|
|
@Override
|
|
public EventProcessingStatus handle(IntegrationEventProperties properties, String message) {
|
|
NotifyIntegrationEvent event = this.jsonHandlingService.fromJsonSafe(NotifyIntegrationEvent.class, message);
|
|
if (event == null)
|
|
return EventProcessingStatus.Error;
|
|
if (event.getUserId() == null) {
|
|
throw new MyValidationException(this.errors.getModelValidation().getCode(), "userId", messageSource.getMessage("Validation_Required", new Object[]{"userId"}, LocaleContextHolder.getLocale()));
|
|
}
|
|
|
|
NotificationPersist model = new NotificationPersist();
|
|
model.setType(event.getNotificationType());
|
|
model.setUserId(event.getUserId());
|
|
model.setContactHint(event.getContactHint());
|
|
model.setContactTypeHint(event.getContactTypeHint());
|
|
model.setData(event.getData());
|
|
model.setNotifyState(NotificationNotifyState.PENDING);
|
|
model.setNotifiedWith(event.getContactTypeHint());
|
|
model.setRetryCount(0);
|
|
model.setTrackingState(NotificationTrackingState.UNDEFINED);
|
|
model.setTrackingProcess(NotificationTrackingProcess.PENDING);
|
|
model.setTrackingData(null);
|
|
model.setProvenanceRef(event.getProvenanceRef());
|
|
model.setNotifiedAt(Instant.now());
|
|
|
|
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.getTenantId() != null) {
|
|
TenantEntity tenant = queryFactory.query(TenantQuery.class).ids(event.getTenantId()).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.getTenantId(), tenant.getCode());
|
|
} else if (scope.isMultitenant()) {
|
|
logger.error("missing tenant from event message");
|
|
return EventProcessingStatus.Error;
|
|
}
|
|
|
|
CurrentPrincipalResolver currentPrincipalResolver = this.applicationContext.getBean(CurrentPrincipalResolver.class);
|
|
currentPrincipalResolver.push(InboxPrincipal.build(properties));
|
|
|
|
NotifyConsistencyHandler notifyConsistencyHandler = this.applicationContext.getBean(NotifyConsistencyHandler.class);
|
|
if (!(notifyConsistencyHandler.isConsistent(new NotifyConsistencyPredicates(event.getUserId(), event.getContactTypeHint(), event.getContactHint()))))
|
|
return EventProcessingStatus.Postponed;
|
|
|
|
EntityManagerFactory entityManagerFactory = this.applicationContext.getBean(EntityManagerFactory.class);
|
|
entityManager = entityManagerFactory.createEntityManager();
|
|
|
|
transaction = entityManager.getTransaction();
|
|
transaction.begin();
|
|
|
|
try {
|
|
NotificationService notificationService = this.applicationContext.getBean(NotificationService.class);
|
|
notificationService.persist(model, new BaseFieldSet());
|
|
|
|
AuditService auditService = this.applicationContext.getBean(AuditService.class);
|
|
|
|
auditService.track(AuditableAction.Notification_Persist, Map.ofEntries(
|
|
new AbstractMap.SimpleEntry<String, Object>("id", event.getUserId())
|
|
));
|
|
//auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
|
|
|
|
transaction.commit();
|
|
} catch (Exception e) {
|
|
transaction.rollback();
|
|
throw e;
|
|
} finally {
|
|
currentPrincipalResolver.pop();
|
|
}
|
|
|
|
transaction.commit();
|
|
} 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;
|
|
}
|
|
|
|
}
|