argos/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/notification/NotifyIntegrationEventHandl...

89 lines
3.4 KiB
Java

package eu.eudat.integrationevent.outbox.notification;
import eu.eudat.commons.enums.ContactInfoType;
import eu.eudat.data.UserContactInfoEntity;
import eu.eudat.data.UserEntity;
import eu.eudat.integrationevent.outbox.OutboxIntegrationEvent;
import eu.eudat.integrationevent.outbox.OutboxService;
import eu.eudat.model.persist.notification.NotificationPersist;
import eu.eudat.query.UserContactInfoQuery;
import eu.eudat.query.UserQuery;
import eu.eudat.service.notification.NotificationService;
import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.validation.ValidatorFactory;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
import javax.management.InvalidApplicationException;
import java.util.List;
import java.util.UUID;
@Component
@RequestScope
public class NotifyIntegrationEventHandlerImpl implements NotifyIntegrationEventHandler {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(NotifyIntegrationEventHandlerImpl.class));
private final NotificationService notificationService;
private final QueryFactory queryFactory;
private final AuditService auditService;
private final OutboxService outboxService;
private final ValidatorFactory validatorFactory;
@Autowired
public NotifyIntegrationEventHandlerImpl(
OutboxService outboxService,
NotificationService notificationService,
QueryFactory queryFactory,
AuditService auditService, ValidatorFactory validatorFactory) {
this.outboxService = outboxService;
this.notificationService = notificationService;
this.validatorFactory = validatorFactory;
this.queryFactory = queryFactory;
this.auditService = auditService;
}
@Override
public void handle(NotifyIntegrationEvent event) throws InvalidApplicationException {
OutboxIntegrationEvent message = new OutboxIntegrationEvent();
message.setMessageId(UUID.randomUUID());
message.setType(OutboxIntegrationEvent.NOTIFY);
message.setEvent(event);
this.outboxService.publish(message);
}
private boolean isNotificationConsistent(NotificationPersist notification) {
switch (notification.getContactTypeHint()) {
case IN_APP: {
if (notification.getUserId() == null)
return false;
List<UserEntity> users = this.queryFactory.query(UserQuery.class).ids(notification.getUserId()).collect();
return !users.isEmpty();
}
case EMAIL: {
if (notification.getContactHint() != null && !notification.getContactHint().isBlank())
return true;
if (notification.getUserId() == null)
return false;
List<UserContactInfoEntity> userContactInfoEntities = this.queryFactory.query(UserContactInfoQuery.class).types(ContactInfoType.Email).userIds(notification.getUserId()).collect();
return !userContactInfoEntities.isEmpty();
}
default:
throw new MyApplicationException("invalid type " + notification.getContactTypeHint());
}
}
}