104 lines
5.1 KiB
Java
104 lines
5.1 KiB
Java
package gr.cite.annotation.integrationevent.inbox.usertouch;
|
|
|
|
import gr.cite.annotation.data.TenantEntityManager;
|
|
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
|
|
import gr.cite.annotation.audit.AuditableAction;
|
|
import gr.cite.annotation.common.JsonHandlingService;
|
|
import gr.cite.annotation.common.scope.tenant.TenantScope;
|
|
import gr.cite.annotation.data.TenantEntity;
|
|
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.Tenant;
|
|
import gr.cite.annotation.query.TenantQuery;
|
|
import gr.cite.annotation.service.user.UserService;
|
|
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties;
|
|
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<String, Object>("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;
|
|
}
|
|
} |