Merge "Support for Patch across multiple data nodes"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / entities / FragmentEntityArranger.java
index 50187a4..b7ce98e 100644 (file)
@@ -1,6 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2022 Nordix Foundation
+ *  Modifications Copyright (C) 2023 TechMahindra Ltd.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -31,17 +32,36 @@ import lombok.NoArgsConstructor;
 public class FragmentEntityArranger {
 
     /**
-     * Convert a collection of (related) FragmentExtracts into a FragmentEntity (tree) with descendants.
-     * Multiple top level nodes not yet support. If found only the first top level element is returned
+     * Convert a collection of (related) FragmentExtracts into  FragmentEntities (trees) with descendants.
      *
      * @param anchorEntity the anchor(entity) all the fragments belong to
      * @param fragmentExtracts FragmentExtracts to convert
-     * @return a FragmentEntity (tree) with descendants, null if none found.
+     * @return a collection of FragmentEntities (trees) with descendants.
      */
-    public static FragmentEntity toFragmentEntityTree(final AnchorEntity anchorEntity,
+    public static Collection<FragmentEntity> toFragmentEntityTrees(final AnchorEntity anchorEntity,
                                                       final Collection<FragmentExtract> fragmentExtracts) {
         final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
+        if (fragmentExtracts !=  null) {
+            for (final FragmentExtract fragmentExtract : fragmentExtracts) {
+                final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
+                fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
+            }
+        }
+        return reuniteChildrenWithTheirParents(fragmentEntityPerId);
+    }
+
+    /**
+     * Convert a collection of (related) FragmentExtracts into  FragmentEntities (trees) with descendants.
+     *
+     * @param anchorEntityPerId the anchor(entities) the fragments belong to
+     * @param fragmentExtracts FragmentExtracts to convert
+     * @return a collection of FragmentEntities (trees) with descendants.
+     */
+    public static Collection<FragmentEntity> toFragmentEntityTreesAcrossAnchors(
+            final Map<Integer, AnchorEntity> anchorEntityPerId, final Collection<FragmentExtract> fragmentExtracts) {
+        final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
         for (final FragmentExtract fragmentExtract : fragmentExtracts) {
+            final AnchorEntity anchorEntity = anchorEntityPerId.get(fragmentExtract.getAnchorId());
             final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
             fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
         }
@@ -51,7 +71,10 @@ public class FragmentEntityArranger {
     private static FragmentEntity toFragmentEntity(final AnchorEntity anchorEntity,
                                                    final FragmentExtract fragmentExtract) {
         final FragmentEntity fragmentEntity = new FragmentEntity();
-        fragmentEntity.setAnchor(anchorEntity);
+        if (anchorEntity != null) {
+            fragmentEntity.setAnchor(anchorEntity);
+            fragmentEntity.setDataspace(anchorEntity.getDataspace());
+        }
         fragmentEntity.setId(fragmentExtract.getId());
         fragmentEntity.setXpath(fragmentExtract.getXpath());
         fragmentEntity.setAttributes(fragmentExtract.getAttributes());
@@ -60,7 +83,8 @@ public class FragmentEntityArranger {
         return fragmentEntity;
     }
 
-    private static FragmentEntity reuniteChildrenWithTheirParents(final Map<Long, FragmentEntity> fragmentEntityPerId) {
+    private static Collection<FragmentEntity> reuniteChildrenWithTheirParents(
+        final Map<Long, FragmentEntity> fragmentEntityPerId) {
         final Collection<FragmentEntity> fragmentEntitiesWithoutParentInResultSet = new HashSet<>();
         for (final FragmentEntity fragmentEntity : fragmentEntityPerId.values()) {
             final FragmentEntity parentFragmentEntity = fragmentEntityPerId.get(fragmentEntity.getParentId());
@@ -70,10 +94,7 @@ public class FragmentEntityArranger {
                 parentFragmentEntity.getChildFragments().add(fragmentEntity);
             }
         }
-        if (fragmentEntitiesWithoutParentInResultSet.iterator().hasNext()) {
-            return fragmentEntitiesWithoutParentInResultSet.iterator().next();
-        }
-        return null;
+        return fragmentEntitiesWithoutParentInResultSet;
     }
 
 }