diff options
Diffstat (limited to 'cps-ri/src/main')
3 files changed, 29 insertions, 11 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java index 50187a487b..27891c525e 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java @@ -57,6 +57,7 @@ public class FragmentEntityArranger { fragmentEntity.setAttributes(fragmentExtract.getAttributes()); fragmentEntity.setParentId(fragmentExtract.getParentId()); fragmentEntity.setChildFragments(new HashSet<>()); + fragmentEntity.setDataspace(anchorEntity.getDataspace()); return fragmentEntity; } @@ -70,10 +71,7 @@ public class FragmentEntityArranger { parentFragmentEntity.getChildFragments().add(fragmentEntity); } } - if (fragmentEntitiesWithoutParentInResultSet.iterator().hasNext()) { - return fragmentEntitiesWithoutParentInResultSet.iterator().next(); - } - return null; + return fragmentEntitiesWithoutParentInResultSet.stream().findFirst().orElse(null); } } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index dc848e657c..ac1be1cd30 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -230,7 +230,11 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); if (isRootXpath(xpath)) { - return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity); + final List<FragmentExtract> fragmentExtracts = fragmentRepository.getTopLevelFragments(dataspaceEntity, + anchorEntity); + final FragmentEntity fragmentEntity = FragmentEntityArranger.toFragmentEntityTree(anchorEntity, + fragmentExtracts); + return fragmentEntity; } else { final String normalizedXpath = getNormalizedXpath(xpath); final FragmentEntity fragmentEntity; @@ -500,7 +504,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final boolean onlySupportListNodeDeletion) { final String parentNodeXpath; FragmentEntity parentFragmentEntity = null; - boolean targetDeleted = false; + boolean targetDeleted; if (isRootXpath(targetXpath)) { deleteDataNodes(dataspaceName, anchorName); targetDeleted = true; diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java index 112ebfda70..2c25a61a7e 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java @@ -1,6 +1,6 @@ /*
* ============LICENSE_START=======================================================
- * Copyright (C) 2020-2021 Nordix Foundation.
+ * Copyright (C) 2021-2022 Nordix Foundation.
* Modifications Copyright (C) 2020-2021 Bell Canada.
* Modifications Copyright (C) 2020-2021 Pantheon.tech.
* ================================================================================
@@ -58,10 +58,26 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>, List<FragmentEntity> findRootsByDataspaceAndAnchor(@Param("dataspace") int dataspaceId,
@Param("anchor") int anchorId);
- default FragmentEntity findFirstRootByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity,
- @NonNull AnchorEntity anchorEntity) {
- return findRootsByDataspaceAndAnchor(dataspaceEntity.getId(), anchorEntity.getId()).stream().findFirst()
- .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName()));
+ @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"
+ + " CAST(attributes AS TEXT) AS attributes"
+ + " FROM FRAGMENT WHERE anchor_id = :anchorId",
+ nativeQuery = true)
+ List<FragmentExtract> findRootsByAnchorId(@Param("anchorId") int anchorId);
+
+ /**
+ * find top level fragment by anchor.
+ *
+ * @param dataspaceEntity dataspace entity
+ * @param anchorEntity anchor entity
+ * @return FragmentEntity fragment entity
+ */
+ default List<FragmentExtract> getTopLevelFragments(DataspaceEntity dataspaceEntity,
+ AnchorEntity anchorEntity) {
+ final List<FragmentExtract> fragmentExtracts = findRootsByAnchorId(anchorEntity.getId());
+ if (fragmentExtracts.isEmpty()) {
+ throw new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName());
+ }
+ return fragmentExtracts;
}
List<FragmentEntity> findAllByAnchorAndXpathIn(@NonNull AnchorEntity anchorEntity,
|