diff options
author | 2021-01-19 12:45:44 +0000 | |
---|---|---|
committer | 2021-01-22 13:42:26 +0000 | |
commit | 5b719d1cdc23da0c95310933fee09753401075f9 (patch) | |
tree | 7ae7993b6fe206a52479992bc26a5fc4e8e43d4e /cps-ri/src/main | |
parent | 4ee05a9592b9b97efdad56f7e83cac16cd6d1311 (diff) |
Add another Child to a Fragment that has already at least one Child
Issue-ID: CPS-164
Signed-off-by: niamhcore <niamh.core@est.tech>
Change-Id: Ib6a4f8ce4fc9247726e016ecaa0b1e45ff11bdbb
Diffstat (limited to 'cps-ri/src/main')
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java | 37 | ||||
-rwxr-xr-x | cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java | 13 |
2 files changed, 40 insertions, 10 deletions
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 ef64c34d5..c73b65ddd 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 @@ -49,6 +49,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); final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); @@ -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<FragmentEntity> fragmentEntityBuilder = ImmutableSet.builder(); + final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted); + final Builder<FragmentEntity> 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 4d4494396..6fc956c44 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<FragmentEntity, Long> {
+ Optional<FragmentEntity> 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<AnchorEntity> anchorEntities);
|