diff options
Diffstat (limited to 'cps-ri/src/main/java/org')
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java | 50 |
1 files changed, 46 insertions, 4 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 fdbafd4be..af010f4fc 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; @@ -81,7 +83,8 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService } private static final Gson GSON = new GsonBuilder().create(); - private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@\\S+?]){0,1})"; + 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, @@ -108,6 +111,9 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService parentFragment.getChildFragments().addAll(newFragmentEntities); try { fragmentRepository.save(parentFragment); + dataNodes.forEach( + dataNode -> getChildFragments(dataspaceName, anchorName, dataNode) + ); } catch (final DataIntegrityViolationException exception) { final List<String> conflictXpaths = dataNodes.stream() .map(DataNode::getXpath) @@ -152,6 +158,17 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService return parentFragment; } + private void getChildFragments(final String dataspaceName, final String anchorName, final DataNode dataNode) { + for (final DataNode childDataNode: dataNode.getChildDataNodes()) { + final FragmentEntity getChildsParentFragmentByXPath = + getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath()); + final FragmentEntity childFragmentEntity = toFragmentEntity(getChildsParentFragmentByXPath.getDataspace(), + getChildsParentFragmentByXPath.getAnchor(), childDataNode); + getChildsParentFragmentByXPath.getChildFragments().add(childFragmentEntity); + fragmentRepository.save(getChildsParentFragmentByXPath); + } + } + private static FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity, final AnchorEntity anchorEntity, final DataNode dataNode) { return FragmentEntity.builder() @@ -208,8 +225,8 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final CpsPathQuery cpsPathQuery) { final Set<String> ancestorXpath = new HashSet<>(); final var pattern = - Pattern.compile("(\\S*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier()) - + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/\\S*"); + Pattern.compile("([\\s\\S]*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier()) + + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/[\\s\\S]*"); for (final FragmentEntity fragmentEntity : fragmentEntities) { final var matcher = pattern.matcher(fragmentEntity.getXpath()); if (matcher.matches()) { @@ -301,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); |