summaryrefslogtreecommitdiffstats
path: root/cps-ri/src/main
diff options
context:
space:
mode:
authorlukegleeson <luke.gleeson@est.tech>2021-07-27 15:07:05 +0100
committerlukegleeson <luke.gleeson@est.tech>2021-08-17 10:26:50 +0100
commit3724abc1912f93bf1caa104a55da7178f43fd731 (patch)
tree24a5c7d64322adab13958f5680eae7d45a4be5c2 /cps-ri/src/main
parentdca30738006c456afd807512a2f80c207b53d3c6 (diff)
Delete list-node p1 service and persistence layers
Persistence Layer of delete List-Node Content Service Layer of delete List-Node Content Updating of deprecated FragementRepository.getOne() method to FragmentRepository.getByID() in CpsDataPersistenceServiceIntegration.groovy Remove Runtime Exceptions thrown from CpsDataService.java javadoc Issue-ID: CPS-361 Signed-off-by: lukegleeson <luke.gleeson@est.tech> Change-Id: Ib5762e73a6e8620c50c2e07b00086b9287770bc3
Diffstat (limited to 'cps-ri/src/main')
-rw-r--r--cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java30
1 files changed, 29 insertions, 1 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 c638b9113e..af010f4fcd 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
@@ -34,6 +34,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
@@ -48,6 +49,7 @@ import org.onap.cps.spi.entities.FragmentEntity;
import org.onap.cps.spi.exceptions.AlreadyDefinedException;
import org.onap.cps.spi.exceptions.ConcurrencyException;
import org.onap.cps.spi.exceptions.CpsPathException;
+import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
import org.onap.cps.spi.model.DataNode;
import org.onap.cps.spi.model.DataNodeBuilder;
import org.onap.cps.spi.repository.AnchorRepository;
@@ -82,6 +84,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
private static final Gson GSON = new GsonBuilder().create();
private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?]){0,1})";
+ private static final String REG_EX_FOR_LIST_NODE_KEY = "\\[(\\@([^/]*?))+( and)*\\]$";
@Override
public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath,
@@ -315,8 +318,33 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
fragmentRepository.save(parentEntity);
}
+ @Override
+ @Transactional
+ public void deleteListDataNodes(final String dataspaceName, final String anchorName, final String listNodeXpath) {
+ final var parentNodeXpath = listNodeXpath.substring(0, listNodeXpath.lastIndexOf('/'));
+ final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
+ final var descendantNode = listNodeXpath.substring(listNodeXpath.lastIndexOf('/'));
+ final Matcher descendantNodeHasListNodeKey = Pattern.compile(REG_EX_FOR_LIST_NODE_KEY).matcher(descendantNode);
+
+ final boolean xpathPointsToAValidChildNodeWithKey = parentEntity.getChildFragments().stream().anyMatch(
+ (fragment) -> fragment.getXpath().equals(listNodeXpath));
+
+ final boolean xpathPointsToAValidChildNodeWithoutKey = parentEntity.getChildFragments().stream().anyMatch(
+ (fragment) -> fragment.getXpath().replaceAll(REG_EX_FOR_LIST_NODE_KEY, "").equals(listNodeXpath));
+
+ if ((descendantNodeHasListNodeKey.find() && xpathPointsToAValidChildNodeWithKey)
+ ||
+ (!descendantNodeHasListNodeKey.find() && xpathPointsToAValidChildNodeWithoutKey)) {
+ removeListNodeDescendants(parentEntity, listNodeXpath);
+ } else {
+ throw new DataNodeNotFoundException(parentEntity.getDataspace().getName(),
+ parentEntity.getAnchor().getName(), listNodeXpath);
+ }
+ }
+
private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) {
- final String listNodeXpathPrefix = listNodeXpath + "[";
+ final Matcher descendantNodeHasListNodeKey = Pattern.compile(REG_EX_FOR_LIST_NODE_KEY).matcher(listNodeXpath);
+ final String listNodeXpathPrefix = listNodeXpath + (descendantNodeHasListNodeKey.find() ? "" : "[");
if (parentFragmentEntity.getChildFragments()
.removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) {
fragmentRepository.save(parentFragmentEntity);