From b0e78acb6aefdcb5866955802099e6091b2a6952 Mon Sep 17 00:00:00 2001 From: Ruslan Kashapov Date: Fri, 14 May 2021 12:04:34 +0300 Subject: Replace list-node content (part 1): CPS Service and persistence layers Issue-ID: CPS-362 Change-Id: I669c9fc6ef67c1992fe95e17a765f0c616b00f7e Signed-off-by: Ruslan Kashapov --- .../spi/impl/CpsDataPersistenceServiceImpl.java | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (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 ae399a1381..10440924e9 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 @@ -35,6 +35,7 @@ import java.util.Map; import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; +import javax.transaction.Transactional; import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.entities.AnchorEntity; @@ -253,6 +254,30 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService fragmentRepository.save(fragmentEntity); } + @Override + @Transactional + public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath, + final Collection dataNodes) { + final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath); + final var firstChildNodeXpath = dataNodes.iterator().next().getXpath(); + final var listNodeXpath = firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("[")); + removeListNodeDescendants(parentEntity, listNodeXpath); + final Set childFragmentEntities = dataNodes.stream().map( + dataNode -> convertToFragmentWithAllDescendants( + parentEntity.getDataspace(), parentEntity.getAnchor(), dataNode) + ).collect(Collectors.toUnmodifiableSet()); + parentEntity.getChildFragments().addAll(childFragmentEntities); + fragmentRepository.save(parentEntity); + } + + private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) { + final String listNodeXpathPrefix = listNodeXpath + "["; + if (parentFragmentEntity.getChildFragments() + .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) { + fragmentRepository.save(parentFragmentEntity); + } + } + private void removeExistingDescendants(final FragmentEntity fragmentEntity) { fragmentEntity.setChildFragments(Collections.emptySet()); fragmentRepository.save(fragmentEntity); -- cgit 1.2.3-korg