Issue with CPSData API to add an item to an existing list node
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsDataPersistenceServiceImpl.java
index 81205a1..adb29b6 100644 (file)
@@ -40,7 +40,6 @@ import javax.transaction.Transactional;
 import lombok.extern.slf4j.Slf4j;
 import org.hibernate.StaleStateException;
 import org.onap.cps.cpspath.parser.CpsPathQuery;
-import org.onap.cps.cpspath.parser.CpsPathQueryType;
 import org.onap.cps.spi.CpsDataPersistenceService;
 import org.onap.cps.spi.FetchDescendantsOption;
 import org.onap.cps.spi.entities.AnchorEntity;
@@ -109,6 +108,9 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
         parentFragment.getChildFragments().addAll(newFragmentEntities);
         try {
             fragmentRepository.save(parentFragment);
+            dataNodes.forEach(
+                dataNode -> getChildFragments(dataspaceName, anchorName, dataNode)
+            );
         } catch (final DataIntegrityViolationException exception) {
             final List<String> conflictXpaths = dataNodes.stream()
                 .map(DataNode::getXpath)
@@ -153,6 +155,17 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
         return parentFragment;
     }
 
+    private void getChildFragments(final String dataspaceName, final String anchorName, final DataNode dataNode) {
+        for (final DataNode childDataNode: dataNode.getChildDataNodes()) {
+            final FragmentEntity getChildsParentFragmentByXPath =
+                getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
+            final FragmentEntity childFragmentEntity = toFragmentEntity(getChildsParentFragmentByXPath.getDataspace(),
+                getChildsParentFragmentByXPath.getAnchor(), childDataNode);
+            getChildsParentFragmentByXPath.getChildFragments().add(childFragmentEntity);
+            fragmentRepository.save(getChildsParentFragmentByXPath);
+        }
+    }
+
     private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
         final AnchorEntity anchorEntity, final DataNode dataNode) {
         return FragmentEntity.builder()
@@ -193,20 +206,8 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
         } catch (final IllegalStateException e) {
             throw new CpsPathException(e.getMessage());
         }
-        List<FragmentEntity> fragmentEntities;
-        if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
-            fragmentEntities = fragmentRepository
-                .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(),
-                    cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
-        } else if (CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES.equals(cpsPathQuery.getCpsPathQueryType())) {
-            final String leafDataAsJson = GSON.toJson(cpsPathQuery.getLeavesData());
-            fragmentEntities = fragmentRepository
-                .getByAnchorAndDescendentNameAndLeafValues(anchorEntity.getId(), cpsPathQuery.getDescendantName(),
-                    leafDataAsJson);
-        } else {
-            fragmentEntities = fragmentRepository
-                .getByAnchorAndXpathEndsInDescendantName(anchorEntity.getId(), cpsPathQuery.getDescendantName());
-        }
+        List<FragmentEntity> fragmentEntities =
+            fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
         if (cpsPathQuery.hasAncestorAxis()) {
             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
             fragmentEntities = ancestorXpaths.isEmpty()