diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphMapper.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphMapper.java index 90a50fbb8c..70135ab906 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphMapper.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphMapper.java @@ -4908,50 +4908,63 @@ public List unlinkBusinessPolicy(String policyId, Set unlin }).collect(Collectors.toList()); } - public List linkMeshEntityToAssets(String meshEntityId, Set linkGuids) { - return linkGuids.stream().map(guid -> findByGuid(graph, guid)).filter(Objects::nonNull).filter(ev -> { - Set existingValues = ev.getMultiValuedSetProperty(DOMAIN_GUIDS_ATTR, String.class); - return !existingValues.contains(meshEntityId); - }).peek(ev -> { - try { - isAuthorizedToLink(ev); - } catch (AtlasBaseException e) { - throw new RuntimeException("Permission denied to link entity to asset", e); - } - Set existingValues = ev.getMultiValuedSetProperty(DOMAIN_GUIDS_ATTR, String.class); - updateDomainAttribute(ev, existingValues, meshEntityId); - existingValues.clear(); - existingValues.add(meshEntityId); + public List linkMeshEntityToAssets(String meshEntityId, Set linkGuids) throws AtlasBaseException { + List linkedVertices = new ArrayList<>(); - updateModificationMetadata(ev); + for (String guid : linkGuids) { + AtlasVertex ev = findByGuid(graph, guid); - cacheDifferentialMeshEntity(ev, existingValues); - }).collect(Collectors.toList()); - } + if (ev != null) { + Set existingValues = ev.getMultiValuedSetProperty(DOMAIN_GUIDS_ATTR, String.class); - public List unlinkMeshEntityFromAssets(String meshEntityId, Set unlinkGuids) { - return unlinkGuids.stream().map(guid -> AtlasGraphUtilsV2.findByGuid(graph, guid)).filter(Objects::nonNull).filter(ev -> { - Set existingValues = ev.getMultiValuedSetProperty(DOMAIN_GUIDS_ATTR, String.class); - return meshEntityId.isEmpty() != existingValues.contains(meshEntityId); - }).peek(ev -> { - try { - isAuthorizedToLink(ev); - } catch (AtlasBaseException e) { - throw new RuntimeException("Permission denied to unlink entity from asset", e); - } - Set existingValues = ev.getMultiValuedSetProperty(DOMAIN_GUIDS_ATTR, String.class); - if (meshEntityId.isEmpty() || meshEntityId == null){ - existingValues.clear(); - ev.removeProperty(DOMAIN_GUIDS_ATTR); - } else { - existingValues.remove(meshEntityId); - ev.removePropertyValue(DOMAIN_GUIDS_ATTR, meshEntityId); + if (!existingValues.contains(meshEntityId)) { + isAuthorizedToLink(ev); + + updateDomainAttribute(ev, existingValues, meshEntityId); + existingValues.clear(); + existingValues.add(meshEntityId); + + updateModificationMetadata(ev); + + cacheDifferentialMeshEntity(ev, existingValues); + + linkedVertices.add(ev); + } } + } - updateModificationMetadata(ev); + return linkedVertices; + } - cacheDifferentialMeshEntity(ev, existingValues); - }).collect(Collectors.toList()); + public List unlinkMeshEntityFromAssets(String meshEntityId, Set unlinkGuids) throws AtlasBaseException { + List unlinkedVertices = new ArrayList<>(); + + for (String guid : unlinkGuids) { + AtlasVertex ev = AtlasGraphUtilsV2.findByGuid(graph, guid); + + if (ev != null) { + Set existingValues = ev.getMultiValuedSetProperty(DOMAIN_GUIDS_ATTR, String.class); + + if (meshEntityId.isEmpty() != existingValues.contains(meshEntityId)) { + isAuthorizedToLink(ev); + + if (meshEntityId.isEmpty() || meshEntityId == null) { + existingValues.clear(); + ev.removeProperty(DOMAIN_GUIDS_ATTR); + } else { + existingValues.remove(meshEntityId); + ev.removePropertyValue(DOMAIN_GUIDS_ATTR, meshEntityId); + } + + updateModificationMetadata(ev); + cacheDifferentialMeshEntity(ev, existingValues); + + unlinkedVertices.add(ev); + } + } + } + + return unlinkedVertices; } private void updateDomainAttribute(AtlasVertex vertex, Set existingValues, String meshEntityId){