diff options
Diffstat (limited to 'cps-ri/src/main/java')
-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 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 @@ -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 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<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);
|