Upload Tosca Model changes to remove policy model type parsing from UI.
[clamp.git] / src / main / java / org / onap / clamp / tosca / DictionaryService.java
index 21ca1f7..98e3516 100644 (file)
@@ -26,13 +26,13 @@ package org.onap.clamp.tosca;
 import com.google.common.collect.Sets;
 import java.util.List;
 import java.util.Set;
+import java.util.stream.Collectors;
 import javax.persistence.EntityNotFoundException;
-import org.onap.clamp.clds.service.SecureServiceBase;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 @Service
-public class DictionaryService extends SecureServiceBase {
+public class DictionaryService {
 
     private final DictionaryRepository dictionaryRepository;
     private final DictionaryElementsRepository dictionaryElementsRepository;
@@ -64,20 +64,19 @@ public class DictionaryService extends SecureServiceBase {
 
         Set<DictionaryElement> newDictionaryElements = dictionary.getDictionaryElements();
 
-        for (DictionaryElement dictionaryElement : newDictionaryElements) {
-            if (dict.getDictionaryElements().contains(dictionaryElement)) {
-                // Update the Dictionary Element
-                getAndUpdateDictionaryElement(dict, dictionaryElement);
-            } else {
-                // Create the Dictionary Element
-                dict.addDictionaryElements(getAndUpdateDictionaryElement(dict, dictionaryElement));
-                dictionaryRepository.save(dict);
-            }
+        if (newDictionaryElements != null && !newDictionaryElements.isEmpty()) {
+            Set<DictionaryElement> updatedDictionaryElements = newDictionaryElements.stream()
+                .map(dictionaryElement -> getAndUpdateDictionaryElement(dict, dictionaryElement))
+                .collect(Collectors.toSet());
+
+            dict.getDictionaryElements().forEach(dictElement -> {
+                if (!updatedDictionaryElements.contains(dictElement)) {
+                    updatedDictionaryElements.add(dictElement);
+                }
+            });
+            dict.setDictionaryElements(updatedDictionaryElements);
         }
-
-        // Fetch again to get Dictionary with most recent updates.
-        return dictionaryRepository.findById(dictionaryName).orElseThrow(
-            () -> new EntityNotFoundException("Couldn't find Dictionary named: " + dictionaryName));
+        return dictionaryRepository.save(dict);
 
     }