diff options
author | Ruslan Kashapov <ruslan.kashapov@pantheon.tech> | 2021-02-08 11:02:39 +0200 |
---|---|---|
committer | Ruslan Kashapov <ruslan.kashapov@pantheon.tech> | 2021-02-16 14:06:40 +0200 |
commit | d1ea2af79952a41264552247441410fc24cd48f5 (patch) | |
tree | 5b11d435ff104c013d0ab5e5430d27fae630508d /cps-ri/src/main/java/org | |
parent | 10edcd916622acd3cb7069a069231153e715b467 (diff) |
Data fragment update by xpath #2 - persistence layer
Issue-ID: CPS-58
Change-Id: Ifc4580936d06c6907d6b5ab20657063b6707ccbe
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Diffstat (limited to 'cps-ri/src/main/java/org')
-rwxr-xr-x | cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java | 3 | ||||
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java | 49 |
2 files changed, 42 insertions, 10 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java index 677652899d..53ceaaae1f 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntity.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2020 Nordix Foundation. All rights reserved. + * Modifications Copyright (C) 2021 Pantheon.tech * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -83,7 +84,7 @@ public class FragmentEntity implements Serializable { @JoinColumn(name = "anchor_id") private AnchorEntity anchor; - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) @JoinColumn(name = "parent_id") private Set<FragmentEntity> childFragments; } 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 d8f3df112f..fd4e768cab 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 @@ -29,6 +29,7 @@ import com.google.gson.GsonBuilder; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.FetchDescendantsOption; @@ -60,12 +61,10 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService @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); + final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentXpath); + final FragmentEntity fragmentEntity = + toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode); + parentFragment.getChildFragments().add(fragmentEntity); fragmentRepository.save(parentFragment); } @@ -114,11 +113,15 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService @Override public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath, final FetchDescendantsOption fetchDescendantsOption) { + final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath); + return toDataNode(fragmentEntity, fetchDescendantsOption); + } + + private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName, + final String xpath) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); - final FragmentEntity fragmentEntity = - fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath); - return toDataNode(fragmentEntity, fetchDescendantsOption); + return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath); } private static DataNode toDataNode(final FragmentEntity fragmentEntity, @@ -140,4 +143,32 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService } return Collections.emptyList(); } + + @Override + public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath, + final Map<String, Object> leaves) { + final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath); + fragmentEntity.setAttributes(GSON.toJson(leaves)); + fragmentRepository.save(fragmentEntity); + } + + @Override + public void replaceDataNodeTree(final String dataspaceName, final String anchorName, final DataNode dataNode) { + final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath()); + removeExistingDescendants(fragmentEntity); + + fragmentEntity.setAttributes(GSON.toJson(dataNode.getLeaves())); + final Set<FragmentEntity> childFragmentEntities = dataNode.getChildDataNodes().stream().map( + childDataNode -> convertToFragmentWithAllDescendants( + fragmentEntity.getDataspace(), fragmentEntity.getAnchor(), childDataNode) + ).collect(Collectors.toUnmodifiableSet()); + fragmentEntity.setChildFragments(childFragmentEntities); + + fragmentRepository.save(fragmentEntity); + } + + private void removeExistingDescendants(final FragmentEntity fragmentEntity) { + fragmentEntity.setChildFragments(Collections.emptySet()); + fragmentRepository.save(fragmentEntity); + } } |