Repository: atlas
Updated Branches:
  refs/heads/master f85ff28e0 -> f15995cc8


ATLAS-2670: Validate entity guid when processing term dissociation

Change-Id: I5e2db1b9968a37482b9ff97ba8602aa262a2db80


Project: http://git-wip-us.apache.org/repos/asf/atlas/repo
Commit: http://git-wip-us.apache.org/repos/asf/atlas/commit/f15995cc
Tree: http://git-wip-us.apache.org/repos/asf/atlas/tree/f15995cc
Diff: http://git-wip-us.apache.org/repos/asf/atlas/diff/f15995cc

Branch: refs/heads/master
Commit: f15995cc8471e498e2a86c3e4790889ccccac84e
Parents: f85ff28
Author: apoorvnaik <apoorvn...@apache.org>
Authored: Thu May 10 10:02:11 2018 -0700
Committer: apoorvnaik <apoorvn...@apache.org>
Committed: Thu May 10 12:10:38 2018 -0700

----------------------------------------------------------------------
 .../java/org/apache/atlas/AtlasErrorCode.java    |  3 ++-
 .../apache/atlas/glossary/GlossaryTermUtils.java | 19 ++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/atlas/blob/f15995cc/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
----------------------------------------------------------------------
diff --git a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java 
b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
index 5e3d818..7cf3fd1 100644
--- a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
+++ b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
@@ -142,7 +142,8 @@ public enum AtlasErrorCode {
     RELATIONSHIP_END_IS_NULL(400, "ATLAS-400-00-07D", "Relationship end is 
invalid. Expected {0} but is NULL"),
     INVALID_TERM_RELATION_TO_SELF(400, "ATLAS-400-00-07E", "Invalid Term 
relationship: Term can't have a relationship with self"),
     INVALID_CHILD_CATEGORY_DIFFERENT_GLOSSARY(400, "ATLAS-400-00-07F", 
"Invalid child category relationship: Child category (guid = {0}) belongs to 
different glossary"),
-    ATTRIBUTE_TYPE_INVALID(400, "ATLAS-400-00-080", "{0}.{1}: invalid 
attribute type. Attribute cannot be of type classification"),
+    INVALID_TERM_DISSOCIATION(400, "ATLAS-400-00-080", "Given term (guid={0}) 
is not associated to entity(guid={1})"),
+    ATTRIBUTE_TYPE_INVALID(400, "ATLAS-400-00-081", "{0}.{1}: invalid 
attribute type. Attribute cannot be of type classification"),
 
     UNAUTHORIZED_ACCESS(403, "ATLAS-403-00-001", "{0} is not authorized to 
perform {1}"),
 

http://git-wip-us.apache.org/repos/asf/atlas/blob/f15995cc/repository/src/main/java/org/apache/atlas/glossary/GlossaryTermUtils.java
----------------------------------------------------------------------
diff --git 
a/repository/src/main/java/org/apache/atlas/glossary/GlossaryTermUtils.java 
b/repository/src/main/java/org/apache/atlas/glossary/GlossaryTermUtils.java
index 5400b77..6977369 100644
--- a/repository/src/main/java/org/apache/atlas/glossary/GlossaryTermUtils.java
+++ b/repository/src/main/java/org/apache/atlas/glossary/GlossaryTermUtils.java
@@ -100,6 +100,14 @@ public class GlossaryTermUtils extends GlossaryUtils {
         }
 
         Objects.requireNonNull(glossaryTerm);
+        Set<AtlasRelatedObjectId> assignedEntities = 
glossaryTerm.getAssignedEntities();
+        Map<String, AtlasRelatedObjectId> assignedEntityMap = new HashMap<>();
+        if (CollectionUtils.isNotEmpty(assignedEntities)) {
+            for (AtlasRelatedObjectId relatedObjectId : assignedEntities) {
+                assignedEntityMap.put(relatedObjectId.getGuid(), 
relatedObjectId);
+            }
+        }
+
         if (CollectionUtils.isNotEmpty(relatedObjectIds)) {
             for (AtlasRelatedObjectId relatedObjectId : relatedObjectIds) {
                 if (DEBUG_ENABLED) {
@@ -108,7 +116,12 @@ public class GlossaryTermUtils extends GlossaryUtils {
                 if (Objects.isNull(relatedObjectId.getRelationshipGuid())) {
                     throw new 
AtlasBaseException(AtlasErrorCode.TERM_DISSOCIATION_MISSING_RELATION_GUID);
                 }
-                
relationshipStore.deleteById(relatedObjectId.getRelationshipGuid());
+                AtlasRelatedObjectId existingTermRelation = 
assignedEntityMap.get(relatedObjectId.getGuid());
+                if (CollectionUtils.isNotEmpty(assignedEntities) && 
isRelationshipGuidSame(existingTermRelation, relatedObjectId)) {
+                    
relationshipStore.deleteById(relatedObjectId.getRelationshipGuid());
+                } else {
+                    throw new 
AtlasBaseException(AtlasErrorCode.INVALID_TERM_DISSOCIATION, 
glossaryTerm.getGuid(), relatedObjectId.getGuid());
+                }
             }
         }
 
@@ -117,6 +130,10 @@ public class GlossaryTermUtils extends GlossaryUtils {
         }
     }
 
+    private boolean isRelationshipGuidSame(final AtlasRelatedObjectId 
existing, final AtlasRelatedObjectId relatedObjectId) {
+        return StringUtils.equals(relatedObjectId.getRelationshipGuid(), 
existing.getRelationshipGuid());
+    }
+
     private void processTermAnchor(AtlasGlossaryTerm updatedTerm, 
AtlasGlossaryTerm existing, RelationshipOperation op) throws AtlasBaseException 
{
         AtlasGlossaryHeader existingAnchor    = existing.getAnchor();
         AtlasGlossaryHeader updatedTermAnchor = updatedTerm.getAnchor();

Reply via email to