Issue with CPSData API to add an item to an existing list node 42/123242/5
authorDylanB95EST <dylan.byrne@est.tech>
Mon, 9 Aug 2021 17:39:14 +0000 (18:39 +0100)
committerDylanB95EST <dylan.byrne@est.tech>
Mon, 16 Aug 2021 15:28:20 +0000 (16:28 +0100)
Issueing Fix for Bug around 2 Api's
/cps/api/v1/dataspaces/{dataspacename}/anchors/{anchor-name}/list-node
and cps-ncmp/api/ncmp-dmi/v1/ch where child data nodes do not get
persisted to the DB.

Issue-ID: CPS-524

Change-Id: I4e433a2fddd56b8714b798a2d5b0e5fb683e3205
Signed-off-by: DylanB95EST <dylan.byrne@est.tech>
cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java

index fdbafd4..adb29b6 100644 (file)
@@ -108,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)
@@ -152,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()