From 5b719d1cdc23da0c95310933fee09753401075f9 Mon Sep 17 00:00:00 2001 From: niamhcore Date: Tue, 19 Jan 2021 12:45:44 +0000 Subject: Add another Child to a Fragment that has already at least one Child Issue-ID: CPS-164 Signed-off-by: niamhcore Change-Id: Ib6a4f8ce4fc9247726e016ecaa0b1e45ff11bdbb --- .../spi/impl/CpsDataPersistenceServiceImpl.java | 37 ++++++++++++++++------ .../cps/spi/repository/FragmentRepository.java | 13 ++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) (limited to 'cps-ri/src/main') 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 ef64c34d5c..c73b65ddd8 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 @@ -48,6 +48,18 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService private static final Gson GSON = new GsonBuilder().create(); + @Override + public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath, + final DataNode dataNode) { + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); + final FragmentEntity parentFragment = + fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, parentXpath); + final FragmentEntity childFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNode); + parentFragment.getChildFragments().add(childFragment); + fragmentRepository.save(parentFragment); + } + @Override public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); @@ -68,21 +80,26 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService */ private static FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity, final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) { - final FragmentEntity parentFragment = FragmentEntity.builder() - .dataspace(dataspaceEntity) - .anchor(anchorEntity) - .xpath(dataNodeToBeConverted.getXpath()) - .attributes(GSON.toJson(dataNodeToBeConverted.getLeaves())) - .build(); - - final Builder fragmentEntityBuilder = ImmutableSet.builder(); + final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted); + final Builder childFragmentsImmutableSetBuilder = ImmutableSet.builder(); for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) { final FragmentEntity childFragment = convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(), childDataNode); - fragmentEntityBuilder.add(childFragment); + childFragmentsImmutableSetBuilder.add(childFragment); } - parentFragment.setChildFragments(fragmentEntityBuilder.build()); + parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build()); return parentFragment; } + + private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity, + final AnchorEntity anchorEntity, + final DataNode dataNode) { + return FragmentEntity.builder() + .dataspace(dataspaceEntity) + .anchor(anchorEntity) + .xpath(dataNode.getXpath()) + .attributes(GSON.toJson(dataNode.getLeaves())) + .build(); + } } 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 4d44943963..6fc956c44d 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 @@ -22,9 +22,13 @@ package org.onap.cps.spi.repository; import java.util.Collection; +import java.util.Optional; import javax.validation.constraints.NotNull; +import org.checkerframework.checker.nullness.qual.NonNull; import org.onap.cps.spi.entities.AnchorEntity; +import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.entities.FragmentEntity; +import org.onap.cps.spi.exceptions.NotFoundInDataspaceException; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -34,6 +38,15 @@ import org.springframework.stereotype.Repository; @Repository public interface FragmentRepository extends JpaRepository { + Optional findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity, + @NonNull AnchorEntity anchorEntity, @NonNull String xpath); + + default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity, + @NonNull AnchorEntity anchorEntity, @NonNull String xpath) { + return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath) + .orElseThrow(() -> new NotFoundInDataspaceException(dataspaceEntity.getName(), xpath)); + } + @Modifying @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)") void deleteByAnchorIn(@NotNull @Param("anchors") Collection anchorEntities); -- cgit 1.2.3-korg