import org.onap.cps.spi.entities.AnchorEntity;
import org.onap.cps.spi.entities.DataspaceEntity;
import org.onap.cps.spi.entities.FragmentEntity;
+import org.onap.cps.spi.exceptions.AlreadyDefinedException;
import org.onap.cps.spi.model.DataNode;
import org.onap.cps.spi.model.DataNodeBuilder;
import org.onap.cps.spi.query.CpsPathQuery;
import org.onap.cps.spi.repository.DataspaceRepository;
import org.onap.cps.spi.repository.FragmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
@Service
final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
dataNode);
- fragmentRepository.save(fragmentEntity);
+ try {
+ fragmentRepository.save(fragmentEntity);
+ } catch (final DataIntegrityViolationException exception) {
+ throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
+ }
}
/**
.getLeafName(), cpsPathQuery.getLeafValue());
} else {
fragmentEntities = fragmentRepository
- .getByAnchorAndEndsWithXpath(anchorEntity.getId(), cpsPathQuery.getEndsWith());
+ .getByAnchorAndEndsWithXpath(anchorEntity.getId(), cpsPathQuery.getEndsWith());
}
return fragmentEntities.stream()
.map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))