diff options
author | Ruslan Kashapov <ruslan.kashapov@pantheon.tech> | 2021-05-05 12:06:00 +0300 |
---|---|---|
committer | Ruslan Kashapov <ruslan.kashapov@pantheon.tech> | 2021-05-11 09:39:59 +0300 |
commit | 25e3306737b7284b051dfeaedb39ef83323504d9 (patch) | |
tree | 8f5b64bfb21bf11ebbbca558a23666128dcdc5e8 /cps-ri/src/main/java/org/onap | |
parent | 757b328b542d91a96d2c095744303d40edcb67f9 (diff) |
Create list-node elements (part1): CPS service and persistence layers
+ fix integrity violation exception exposed out of persistence layer
+ refactor CpsDataServiceImplSpec to eliminate repeated code
Issue-ID: CPS-360
Change-Id: Id70341fe54bf3c31af661f6aae04a7a80f4a1e9d
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Diffstat (limited to 'cps-ri/src/main/java/org/onap')
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java | 26 |
1 files changed, 25 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 343a0886b3..ae399a1381 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 @@ -27,6 +27,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -73,7 +74,30 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final var fragmentEntity = toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode); parentFragment.getChildFragments().add(fragmentEntity); - fragmentRepository.save(parentFragment); + try { + fragmentRepository.save(parentFragment); + } catch (final DataIntegrityViolationException exception) { + throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception); + } + } + + @Override + public void addListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath, + final Collection<DataNode> dataNodes) { + final FragmentEntity parentFragment = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath); + final List<FragmentEntity> newFragmentEntities = + dataNodes.stream().map( + dataNode -> toFragmentEntity(parentFragment.getDataspace(), parentFragment.getAnchor(), dataNode) + ).collect(Collectors.toUnmodifiableList()); + parentFragment.getChildFragments().addAll(newFragmentEntities); + try { + fragmentRepository.save(parentFragment); + } catch (final DataIntegrityViolationException exception) { + final List<String> conflictXpaths = dataNodes.stream() + .map(DataNode::getXpath) + .collect(Collectors.toList()); + throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception); + } } @Override |