argos/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/usertouched/UserTouchedIntegrationEvent...

97 lines
4.7 KiB
Java

package eu.eudat.integrationevent.outbox.usertouched;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.types.user.AdditionalInfoEntity;
import eu.eudat.data.UserContactInfoEntity;
import eu.eudat.data.UserCredentialEntity;
import eu.eudat.data.UserEntity;
import eu.eudat.integrationevent.outbox.OutboxIntegrationEvent;
import eu.eudat.integrationevent.outbox.OutboxService;
import eu.eudat.model.Reference;
import eu.eudat.model.User;
import eu.eudat.model.UserContactInfo;
import eu.eudat.model.UserCredential;
import eu.eudat.model.builder.UserAdditionalInfoBuilder;
import eu.eudat.query.UserContactInfoQuery;
import eu.eudat.query.UserCredentialQuery;
import eu.eudat.query.UserQuery;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Component("outboxusertouchedintegrationeventhandler")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegrationEventHandler {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserTouchedIntegrationEventHandlerImpl.class));
private final OutboxService outboxService;
private final JsonHandlingService jsonHandlingService;
private final MessageSource messageSource;
private final QueryFactory queryFactory;
public UserTouchedIntegrationEventHandlerImpl(
OutboxService outboxService, JsonHandlingService jsonHandlingService, MessageSource messageSource, QueryFactory queryFactory) {
this.outboxService = outboxService;
this.jsonHandlingService = jsonHandlingService;
this.messageSource = messageSource;
this.queryFactory = queryFactory;
}
@Override
public void handle(UUID userId) {
OutboxIntegrationEvent message = new OutboxIntegrationEvent();
message.setMessageId(UUID.randomUUID());
message.setType(OutboxIntegrationEvent.USER_TOUCH);
UserEntity user = this.queryFactory.query(UserQuery.class).ids(userId)
.firstAs(new BaseFieldSet().ensure(User._name).ensure(User._additionalInfo));
if (user == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{userId, User.class.getSimpleName()}, LocaleContextHolder.getLocale()));
List<UserContactInfoEntity> userContactInfoEntities = this.queryFactory.query(UserContactInfoQuery.class).userIds(userId)
.collectAs(new BaseFieldSet().ensure(UserContactInfo._type).ensure(UserContactInfo._ordinal).ensure(UserContactInfo._value));
List<UserCredentialEntity> userCredentialEntities = this.queryFactory.query(UserCredentialQuery.class).userIds(userId)
.collectAs(new BaseFieldSet().ensure(UserCredential._id, UserCredential._externalId));
UserTouchedIntegrationEvent event = new UserTouchedIntegrationEvent();
event.setId(userId);
event.setTenant(null); //TODO
event.setName(user.getName());
if (userCredentialEntities != null && !userCredentialEntities.isEmpty())
event.setSubjectId(userCredentialEntities.getFirst().getExternalId());
event.setProfile(new UserTouchedIntegrationEvent.UserProfile());
AdditionalInfoEntity definition = this.jsonHandlingService.fromJsonSafe(AdditionalInfoEntity.class, user.getAdditionalInfo());
if (definition != null) {
event.getProfile().setCulture(definition.getCulture());
event.getProfile().setLanguage(definition.getLanguage());
event.getProfile().setTimezone(definition.getTimezone());
}
event.setUserContactInfo(new ArrayList<>());
if (userContactInfoEntities != null){
for (UserContactInfoEntity contactInfoEntity : userContactInfoEntities){
UserTouchedIntegrationEvent.UserContactInfo contactInfo = new UserTouchedIntegrationEvent.UserContactInfo();
contactInfo.setType(contactInfoEntity.getType());
contactInfo.setValue(contactInfoEntity.getValue());
contactInfo.setOrdinal(contactInfoEntity.getOrdinal());
event.getUserContactInfo().add(contactInfo);
}
}
message.setEvent(event);
this.outboxService.publish(message);
}
}