- final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
- final Collection<Fragment> fragments = fragmentRepository.findFragmentsThatAreAnchorsByDataspace(dataspace);
- return fragments.stream().map(
- entity -> Anchor.builder()
- .name(entity.getAnchorName())
- .dataspaceName(dataspaceName)
- .schemaSetName(entity.getSchemaSet().getName())
- .build()
- ).collect(Collectors.toList());
+ final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity);
+ return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toList());
+ }
+
+ @Override
+ public Collection<Anchor> queryAnchors(final String dataspaceName, final Collection<String> inputModuleNames) {
+ validateDataspaceAndModuleNames(dataspaceName, inputModuleNames);
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ final Collection<AnchorEntity> anchorEntities = anchorRepository
+ .getAnchorsByDataspaceIdAndModuleNames(dataspaceEntity.getId(), inputModuleNames, inputModuleNames.size());
+ return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet());
+ }
+
+ @Override
+ public Anchor getAnchor(final String dataspaceName, final String anchorName) {
+ return toAnchor(getAnchorEntity(dataspaceName, anchorName));
+ }
+
+ @Transactional
+ @Override
+ public void deleteAnchor(final String dataspaceName, final String anchorName) {
+ final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
+ fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity));
+ anchorRepository.delete(anchorEntity);
+ }
+
+ private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) {
+ final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
+ }
+
+ private static Anchor toAnchor(final AnchorEntity anchorEntity) {
+ return Anchor.builder()
+ .name(anchorEntity.getName())
+ .dataspaceName(anchorEntity.getDataspace().getName())
+ .schemaSetName(anchorEntity.getSchemaSet().getName())
+ .build();
+ }
+
+ private void validateDataspaceAndModuleNames(final String dataspaceName,
+ final Collection<String> inputModuleNames) {
+ final Collection<String> retrievedModuleNames =
+ yangResourceRepository.findAllModuleReferences(dataspaceName, inputModuleNames)
+ .stream().map(module -> module.getModuleName())
+ .collect(Collectors.toList());
+ if (retrievedModuleNames.isEmpty()) {
+ dataspaceRepository.getByName(dataspaceName);
+ }
+ if (inputModuleNames.size() > retrievedModuleNames.size()) {
+ final List<String> moduleNamesNotFound = inputModuleNames.stream()
+ .filter(moduleName -> !retrievedModuleNames.contains(moduleName))
+ .collect(Collectors.toList());
+ if (!moduleNamesNotFound.isEmpty()) {
+ throw new ModuleNamesNotFoundException(dataspaceName, moduleNamesNotFound);
+ }
+ }