protected List saveAttributes()

in ddm-notification-service/src/main/java/com/epam/digital/data/platform/notification/service/AbstractSaveNotificationTemplateService.java [66:102]


  protected List<NotificationTemplateAttribute> saveAttributes(UUID templateId,
      SaveNotificationTemplateInputDto inputDto) {
    var existingAttributes =
        notificationTemplateAttributeRepository.findByTemplateId(templateId).stream()
            .collect(
                Collectors.toMap(
                    NotificationTemplateAttribute::getName,
                    Function.identity()));
    var newAttributes =
        Optional.ofNullable(inputDto.getAttributes())
            .stream()
            .flatMap(Collection::stream)
            .collect(
                Collectors.toMap(
                    NotificationTemplateAttributeDto::getName,
                    NotificationTemplateAttributeDto::getValue,
                    (k1, k2) -> k2));
    newAttributes.forEach((name, newValue) -> {
      var existingAttrEntity = existingAttributes.get(name);
      if (existingAttrEntity == null) {
        var newAttr = new NotificationTemplateAttribute();
        newAttr.setTemplateId(templateId);
        newAttr.setName(name);
        newAttr.setValue(newValue);
        existingAttributes.put(name, notificationTemplateAttributeRepository.save(newAttr));
      } else if (!newValue.equals(existingAttrEntity.getValue())) {
        existingAttrEntity.setValue(newValue);
        existingAttributes.put(name,
            notificationTemplateAttributeRepository.save(existingAttrEntity));
      }
    });
    var outdatedAttributes = existingAttributes.values().stream()
        .filter(attr -> newAttributes.get(attr.getName()) == null)
        .collect(Collectors.toList());
    notificationTemplateAttributeRepository.deleteAll(outdatedAttributes);
    return new ArrayList<>(notificationTemplateAttributeRepository.findByTemplateId(templateId));
  }