Repository: atlas
Updated Branches:
  refs/heads/master d218fb4f2 -> 96e5f1cbd


ATLAS-2711: Fix duplicate qualifiedName issue

Change-Id: I23b53086b2bb2380f451e7d85b59096edc610181


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

Branch: refs/heads/master
Commit: 96e5f1cbda1ce2d645cf4159d0f02051005437a9
Parents: d218fb4
Author: apoorvnaik <apoorvn...@apache.org>
Authored: Mon May 21 17:20:13 2018 -0700
Committer: apoorvnaik <apoorvn...@apache.org>
Committed: Tue May 22 09:22:22 2018 -0700

----------------------------------------------------------------------
 .../apache/atlas/glossary/GlossaryService.java  | 63 +++++++++++++-------
 1 file changed, 42 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/atlas/blob/96e5f1cb/repository/src/main/java/org/apache/atlas/glossary/GlossaryService.java
----------------------------------------------------------------------
diff --git 
a/repository/src/main/java/org/apache/atlas/glossary/GlossaryService.java 
b/repository/src/main/java/org/apache/atlas/glossary/GlossaryService.java
index bd9095b..a5499e0 100644
--- a/repository/src/main/java/org/apache/atlas/glossary/GlossaryService.java
+++ b/repository/src/main/java/org/apache/atlas/glossary/GlossaryService.java
@@ -352,6 +352,11 @@ public class GlossaryService {
             storeObject = dataAccess.load(glossaryTerm);
         } else {
             glossaryTerm.setQualifiedName(storeObject.getQualifiedName());
+
+            if (termExists(glossaryTerm)) {
+                throw new 
AtlasBaseException(AtlasErrorCode.GLOSSARY_TERM_ALREADY_EXISTS, 
glossaryTerm.getQualifiedName());
+            }
+
             storeObject = dataAccess.save(glossaryTerm);
         }
         setInfoForRelations(storeObject);
@@ -412,16 +417,21 @@ public class GlossaryService {
                 storeObject = dataAccess.save(atlasGlossaryTerm);
             } catch (AtlasBaseException e) {
                 LOG.debug("Glossary term had no immediate attr updates. 
Exception: {}", e.getMessage());
-            } finally {
-                glossaryTermUtils.processTermRelations(storeObject, 
atlasGlossaryTerm, GlossaryUtils.RelationshipOperation.UPDATE);
+            }
 
-                // If qualifiedName changes due to anchor change, we need to 
persist the term again with updated qualifiedName
-                if (StringUtils.equals(storeObject.getQualifiedName(), 
atlasGlossaryTerm.getQualifiedName())) {
-                    storeObject = dataAccess.load(atlasGlossaryTerm);
-                } else {
-                    
atlasGlossaryTerm.setQualifiedName(storeObject.getQualifiedName());
-                    storeObject = dataAccess.save(atlasGlossaryTerm);
+            glossaryTermUtils.processTermRelations(storeObject, 
atlasGlossaryTerm, GlossaryUtils.RelationshipOperation.UPDATE);
+
+            // If qualifiedName changes due to anchor change, we need to 
persist the term again with updated qualifiedName
+            if (StringUtils.equals(storeObject.getQualifiedName(), 
atlasGlossaryTerm.getQualifiedName())) {
+                storeObject = dataAccess.load(atlasGlossaryTerm);
+            } else {
+                
atlasGlossaryTerm.setQualifiedName(storeObject.getQualifiedName());
+
+                if (termExists(atlasGlossaryTerm)) {
+                    throw new 
AtlasBaseException(AtlasErrorCode.GLOSSARY_TERM_ALREADY_EXISTS, 
atlasGlossaryTerm.getQualifiedName());
                 }
+
+                storeObject = dataAccess.save(atlasGlossaryTerm);
             }
         }
 
@@ -546,17 +556,22 @@ public class GlossaryService {
         // Attempt relation creation and gather all impacted categories
         Map<String, AtlasGlossaryCategory> impactedCategories = 
glossaryCategoryUtils.processCategoryRelations(storeObject, glossaryCategory, 
GlossaryUtils.RelationshipOperation.CREATE);
 
-        // Re save the categories in case any qualifiedName change has occurred
-        dataAccess.save(impactedCategories.values());
-
         // Since the current category is also affected, we need to update 
qualifiedName and save again
         if (StringUtils.equals(glossaryCategory.getQualifiedName(), 
storeObject.getQualifiedName())) {
             storeObject = dataAccess.load(glossaryCategory);
         } else {
             glossaryCategory.setQualifiedName(storeObject.getQualifiedName());
+
+            if (categoryExists(glossaryCategory)) {
+                throw new 
AtlasBaseException(AtlasErrorCode.GLOSSARY_CATEGORY_ALREADY_EXISTS, 
glossaryCategory.getQualifiedName());
+            }
+
             storeObject = dataAccess.save(glossaryCategory);
         }
 
+        // Re save the categories in case any qualifiedName change has occurred
+        dataAccess.save(impactedCategories.values());
+
         setInfoForRelations(storeObject);
 
         if (DEBUG_ENABLED) {
@@ -575,7 +590,6 @@ public class GlossaryService {
             throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, 
"glossaryCategory is null/empty");
         }
 
-
         List<AtlasGlossaryCategory> ret = new ArrayList<>();
         for (AtlasGlossaryCategory category : glossaryCategory) {
             ret.add(createCategory(category));
@@ -614,17 +628,24 @@ public class GlossaryService {
                 storeObject = dataAccess.save(glossaryCategory);
             } catch (AtlasBaseException e) {
                 LOG.debug("No immediate attribute update. Exception: {}", 
e.getMessage());
-            } finally {
-                Map<String, AtlasGlossaryCategory> impactedCategories = 
glossaryCategoryUtils.processCategoryRelations(storeObject, glossaryCategory, 
GlossaryUtils.RelationshipOperation.UPDATE);
-                dataAccess.save(impactedCategories.values());
-                // Since the current category is also affected, we need to 
update qualifiedName and save again
-                if (!StringUtils.equals(glossaryCategory.getQualifiedName(), 
storeObject.getQualifiedName())){
-                    
glossaryCategory.setQualifiedName(storeObject.getQualifiedName());
-                    storeObject = dataAccess.save(glossaryCategory);
-                } else {
-                    storeObject = dataAccess.load(glossaryCategory);
+            }
+
+            Map<String, AtlasGlossaryCategory> impactedCategories = 
glossaryCategoryUtils.processCategoryRelations(storeObject, glossaryCategory, 
GlossaryUtils.RelationshipOperation.UPDATE);
+
+            // Since the current category is also affected, we need to update 
qualifiedName and save again
+            if (StringUtils.equals(glossaryCategory.getQualifiedName(), 
storeObject.getQualifiedName())) {
+                storeObject = dataAccess.load(glossaryCategory);
+            } else {
+                
glossaryCategory.setQualifiedName(storeObject.getQualifiedName());
+
+                if (categoryExists(glossaryCategory)) {
+                    throw new 
AtlasBaseException(AtlasErrorCode.GLOSSARY_CATEGORY_ALREADY_EXISTS, 
glossaryCategory.getQualifiedName());
                 }
+
+                storeObject = dataAccess.save(glossaryCategory);
             }
+
+            dataAccess.save(impactedCategories.values());
         }
 
         if (DEBUG_ENABLED) {

Reply via email to