package gr.cite.notification.service.message.builder; import gr.cite.notification.cache.NotificationTemplateCache; import gr.cite.notification.common.StringUtils; import gr.cite.notification.common.enums.InAppNotificationPriority; import gr.cite.notification.common.enums.IsActive; import gr.cite.notification.common.enums.NotificationContactType; import gr.cite.notification.common.enums.NotificationTemplateChannel; import gr.cite.notification.common.types.notification.FieldInfo; import gr.cite.notification.config.notification.NotificationConfig; import gr.cite.notification.config.notification.NotificationProperties; import gr.cite.notification.data.NotificationEntity; import gr.cite.notification.errorcode.ErrorThesaurusProperties; import gr.cite.notification.model.NotificationTemplate; import gr.cite.notification.service.formatting.FormattingService; import gr.cite.notification.service.message.common.MessageBuilderBase; import gr.cite.notification.service.message.infobuilder.MessageInfoBuilderService; import gr.cite.notification.service.message.model.InAppMessage; import gr.cite.notification.service.message.model.Message; import gr.cite.notification.service.message.model.MessageInfo; import gr.cite.notification.service.notificationtemplate.NotificationTemplateService; import gr.cite.tools.cipher.CipherService; import gr.cite.tools.cipher.config.CipherProfileProperties; import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.logging.LoggerService; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.web.context.annotation.RequestScope; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; @Component @RequestScope public class InAppMessageBuilder extends MessageBuilderBase implements MessageBuilder{ private final static LoggerService logger = new LoggerService(LoggerFactory.getLogger(InAppMessageBuilder.class)); private final MessageInfoBuilderService messageInfoBuilderService; private final Map> flowMap; private final NotificationProperties properties; private final NotificationTemplateService notificationTemplateService; private final QueryFactory queryFactory; @Autowired public InAppMessageBuilder(MessageInfoBuilderService messageInfoBuilderService, @Qualifier(NotificationConfig.BeanQualifier.FLOW_MAP) Map> flowMap, ErrorThesaurusProperties errors, NotificationProperties properties, CipherService cipherService, CipherProfileProperties cipherProfileProperties, FormattingService formattingService, NotificationTemplateCache cache, NotificationTemplateService notificationTemplateService, QueryFactory queryFactory) { super(logger, errors, cipherService, cipherProfileProperties, formattingService, cache); this.messageInfoBuilderService = messageInfoBuilderService; this.flowMap = flowMap; this.properties = properties; this.notificationTemplateService = notificationTemplateService; this.queryFactory = queryFactory; } @Override public Message buildMessage(NotificationEntity notification) { MessageInfo messageInfo = this.messageInfoBuilderService.buildMessageInfo(notification); if (messageInfo == null) { logger.error("Could not retrieve message info for notification " + notification.getId()); return null; } NotificationProperties.Flow options = this.flowMap.get("in-app").getOrDefault(notification.getType(), null); NotificationTemplate template = notificationTemplateService.lookupOverriddenTemplates(notification.getType(), NotificationTemplateChannel.InApp, messageInfo.getLanguage()); if (options == null && template == null) { logger.error("Could not retrieve flow options for notification " + notification.getId() + " of type " + notification.getType()); return null; } MessageBuilderBase.FieldCiphering ciphering = options.getCipherFields() == null || options.getCipherFields().isEmpty() ? new MessageBuilderBase.FieldCiphering() : new MessageBuilderBase.FieldCiphering(options.getCipherFields()); String subjectTemplate = null; ReplaceResult subjectResult = null; String bodyTemplate = null; ReplaceResult bodyResult = null; // fallback if ( template != null && template.getValue() != null){ if (!StringUtils.isNullOrEmpty(template.getValue().getSubjectKey())) subjectTemplate = messageInfo.getFields().stream().filter(fieldInfo -> fieldInfo.getKey().equals(template.getValue().getSubjectKey())).findFirst().orElse(new FieldInfo()).getValue(); if (StringUtils.isNullOrEmpty(subjectTemplate)) subjectTemplate = template.getValue().getSubjectText(); FieldFormatting subjectFormatting = this.buildFieldFormatting(template.getValue().getSubjectFieldOptions()); subjectResult = this.buildTemplate(notification.getId(), subjectTemplate, messageInfo, template.getValue().getSubjectFieldOptions(), subjectFormatting, ciphering); if(!StringUtils.isNullOrEmpty(template.getValue().getBodyKey())) bodyTemplate = messageInfo.getFields().stream().filter(fieldInfo -> fieldInfo.getKey().equals(template.getValue().getBodyKey())).findFirst().orElse(new FieldInfo()).getValue(); if (StringUtils.isNullOrEmpty(bodyTemplate)) bodyTemplate = template.getValue().getBodyText(); FieldFormatting bodyFormatting = this.buildFieldFormatting(template.getValue().getBodyFieldOptions()); bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, template.getValue().getBodyFieldOptions(), bodyFormatting, ciphering); }else{ if (!StringUtils.isNullOrEmpty(options.getSubjectKey())) subjectTemplate = messageInfo.getFields().stream().filter(x -> x.getKey().equals(options.getSubjectKey())).findFirst().orElse(new FieldInfo()).getValue(); if (StringUtils.isNullOrEmpty(subjectTemplate)) subjectTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getSubjectPath(), messageInfo.getLanguage()); FieldFormatting subjectFormatting = this.buildFieldFormatting(options.getSubjectFieldOptions()); subjectResult = this.buildTemplate(notification.getId(), subjectTemplate, messageInfo, options.getSubjectFieldOptions(), subjectFormatting, ciphering); if (!StringUtils.isNullOrEmpty(options.getBodyKey())) bodyTemplate = messageInfo.getFields().stream().filter(x -> x.getKey().equals(options.getBodyKey())).findFirst().orElse(new FieldInfo()).getValue(); if (StringUtils.isNullOrEmpty(bodyTemplate)) bodyTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getBodyPath(), messageInfo.getLanguage()); FieldFormatting bodyFormatting = this.buildFieldFormatting(options.getBodyFieldOptions()); bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, options.getBodyFieldOptions(), bodyFormatting, ciphering); } String priorityString = ""; if ( template != null && template.getValue() != null){ if (!StringUtils.isNullOrEmpty(template.getValue().getPriorityKey())) priorityString = messageInfo.getFields().stream().filter(x -> x.getKey().equals(template.getValue().getPriorityKey())).findFirst().orElse(new FieldInfo()).getValue(); }else { if (!StringUtils.isNullOrEmpty(options.getPriorityKey())) priorityString = messageInfo.getFields().stream().filter(x -> x.getKey().equals(options.getPriorityKey())).findFirst().orElse(new FieldInfo()).getValue(); } InAppNotificationPriority inAppNotificationPriority = InAppNotificationPriority.NORMAL; if (!StringUtils.isNullOrEmpty(priorityString)) { inAppNotificationPriority = InAppNotificationPriority.valueOf(priorityString.toLowerCase()); } List extraData = null; if (template != null && template.getValue() != null){ if (template.getValue().getExtraDataKeys() != null && !template.getValue().getExtraDataKeys().isEmpty()) extraData = messageInfo.getFields().stream().filter(x -> template.getValue().getExtraDataKeys().contains(x.getKey())).collect(Collectors.toList()); }else { if (options.getExtraDataKeys() != null && !options.getExtraDataKeys().isEmpty()) extraData = messageInfo.getFields().stream().filter(x -> options.getExtraDataKeys().contains(x.getKey())).collect(Collectors.toList()); } return new InAppMessage(subjectResult.getText(), bodyResult.getText(), notification.getType(), inAppNotificationPriority, extraData); } private NotificationProperties.Template getTemplate() { return this.properties.getMessage().get("in-app"); } @Override public NotificationContactType supports() { return NotificationContactType.IN_APP; } }