Commit 3f9fc5c9 by apoorvnaik

ATLAS-2653: Disallow term self relation

Change-Id: I372a02bf8d19b5c6862bd98166cfeef63ceafcf0
parent c0bf0b8a
...@@ -140,6 +140,8 @@ public enum AtlasErrorCode { ...@@ -140,6 +140,8 @@ public enum AtlasErrorCode {
GLOSSARY_TERM_QUALIFIED_NAME_CANT_BE_DERIVED(400, "ATLAS-400-00-07A", "Attributes qualifiedName, displayName & glossary name are missing. Failed to derive a unique name for Glossary term"), GLOSSARY_TERM_QUALIFIED_NAME_CANT_BE_DERIVED(400, "ATLAS-400-00-07A", "Attributes qualifiedName, displayName & glossary name are missing. Failed to derive a unique name for Glossary term"),
GLOSSARY_CATEGORY_QUALIFIED_NAME_CANT_BE_DERIVED(400, "ATLAS-400-00-07B", "Attributes qualifiedName, displayName & glossary name are missing. Failed to derive a unique name for Glossary category"), GLOSSARY_CATEGORY_QUALIFIED_NAME_CANT_BE_DERIVED(400, "ATLAS-400-00-07B", "Attributes qualifiedName, displayName & glossary name are missing. Failed to derive a unique name for Glossary category"),
RELATIONSHIP_END_IS_NULL(400, "ATLAS-400-00-07D", "Relationship end is invalid. Expected {0} but is NULL"), 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 belongs to different glossary"),
UNAUTHORIZED_ACCESS(403, "ATLAS-403-00-001", "{0} is not authorized to perform {1}"), UNAUTHORIZED_ACCESS(403, "ATLAS-403-00-001", "{0} is not authorized to perform {1}"),
...@@ -188,7 +190,7 @@ public enum AtlasErrorCode { ...@@ -188,7 +190,7 @@ public enum AtlasErrorCode {
CURATOR_FRAMEWORK_UPDATE(500, "ATLAS-500-00-00B", "ActiveInstanceState.update resulted in exception."), CURATOR_FRAMEWORK_UPDATE(500, "ATLAS-500-00-00B", "ActiveInstanceState.update resulted in exception."),
QUICK_START(500, "ATLAS-500-00-00C", "Failed to run QuickStart: {0}"), QUICK_START(500, "ATLAS-500-00-00C", "Failed to run QuickStart: {0}"),
EMBEDDED_SERVER_START(500, "ATLAS-500-00-00D", "EmbeddedServer.Start: failed!"), EMBEDDED_SERVER_START(500, "ATLAS-500-00-00D", "EmbeddedServer.Start: failed!"),
STORM_TOPOLOGY_UTIL(500, "ATLAS-500-00-00E", "StormToplogyUtil: {0}"), STORM_TOPOLOGY_UTIL(500, "ATLAS-500-00-00E", "StormTopologyUtil: {0}"),
SQOOP_HOOK(500, "ATLAS-500-00-00F", "SqoopHook: {0}"), SQOOP_HOOK(500, "ATLAS-500-00-00F", "SqoopHook: {0}"),
HIVE_HOOK(500, "ATLAS-500-00-010", "HiveHook: {0}"), HIVE_HOOK(500, "ATLAS-500-00-010", "HiveHook: {0}"),
HIVE_HOOK_METASTORE_BRIDGE(500, "ATLAS-500-00-011", "HiveHookMetaStoreBridge: {0}"), HIVE_HOOK_METASTORE_BRIDGE(500, "ATLAS-500-00-011", "HiveHookMetaStoreBridge: {0}"),
......
...@@ -440,33 +440,43 @@ public class AtlasGlossaryTerm extends AtlasGlossaryBaseObject { ...@@ -440,33 +440,43 @@ public class AtlasGlossaryTerm extends AtlasGlossaryBaseObject {
SEE_ALSO("AtlasGlossaryRelatedTerm", "seeAlso"), SEE_ALSO("AtlasGlossaryRelatedTerm", "seeAlso"),
SYNONYMS("AtlasGlossarySynonym", "synonyms"), SYNONYMS("AtlasGlossarySynonym", "synonyms"),
ANTONYMS("AtlasGlossaryAntonym", "antonyms"), ANTONYMS("AtlasGlossaryAntonym", "antonyms"),
PREFERRED_TO_TERMS("AtlasGlossaryPreferredTerm", "preferredToTerms", true),
PREFERRED_TERMS("AtlasGlossaryPreferredTerm", "preferredTerms"), PREFERRED_TERMS("AtlasGlossaryPreferredTerm", "preferredTerms"),
PREFERRED_TO_TERMS("AtlasGlossaryPreferredTerm", "preferredToTerms"), REPLACEMENT_TERMS("AtlasGlossaryReplacementTerm", "replacementTerms", true),
REPLACEMENT_TERMS("AtlasGlossaryReplacementTerm", "replacementTerms"),
REPLACED_BY("AtlasGlossaryReplacementTerm", "replacedBy"), REPLACED_BY("AtlasGlossaryReplacementTerm", "replacedBy"),
TRANSLATION_TERMS("AtlasGlossaryTranslation", "translationTerms"), TRANSLATION_TERMS("AtlasGlossaryTranslation", "translationTerms", true),
TRANSLATED_TERMS("AtlasGlossaryTranslation", "translatedTerms"), TRANSLATED_TERMS("AtlasGlossaryTranslation", "translatedTerms"),
ISA("AtlasGlossaryIsARelationship", "isA"), ISA("AtlasGlossaryIsARelationship", "isA", true),
CLASSIFIES("AtlasGlossaryIsARelationship", "classifies"), CLASSIFIES("AtlasGlossaryIsARelationship", "classifies"),
VALID_VALUES("AtlasGlossaryValidValue", "validValues"), VALID_VALUES("AtlasGlossaryValidValue", "validValues", true),
VALID_VALUES_FOR("AtlasGlossaryValidValue", "validValuesFor"), VALID_VALUES_FOR("AtlasGlossaryValidValue", "validValuesFor"),
; ;
private String relationName; private String name;
private String relationAttrName; private String attrName;
private boolean isEnd2Attr;
Relation(final String relationName, final String relationAttrName) { Relation(final String name, final String attrName) {
this.relationName = relationName; this(name, attrName, false);
this.relationAttrName = relationAttrName;
} }
public String getRelationName() { Relation(final String name, final String attrName, final boolean isEnd2Attr) {
return relationName; this.name = name;
this.attrName = attrName;
this.isEnd2Attr = isEnd2Attr;
}
public String getName() {
return name;
} }
@JsonValue @JsonValue
public String getRelationAttrName() { public String getAttrName() {
return relationAttrName; return attrName;
}
public boolean isEnd2Attr() {
return isEnd2Attr;
} }
} }
} }
...@@ -355,7 +355,7 @@ public class GlossaryTermUtils extends GlossaryUtils { ...@@ -355,7 +355,7 @@ public class GlossaryTermUtils extends GlossaryUtils {
if (Objects.nonNull(existing.getRelatedTerms()) && Objects.nonNull(existing.getRelatedTerms().get(relation))) { if (Objects.nonNull(existing.getRelatedTerms()) && Objects.nonNull(existing.getRelatedTerms().get(relation))) {
existingRelations = existing.getRelatedTerms().get(relation).stream().collect(Collectors.toMap(AtlasRelatedTermHeader::getTermGuid, t -> t)); existingRelations = existing.getRelatedTerms().get(relation).stream().collect(Collectors.toMap(AtlasRelatedTermHeader::getTermGuid, t -> t));
} else { } else {
existingRelations = Collections.EMPTY_MAP; existingRelations = Collections.emptyMap();
} }
for (AtlasRelatedTermHeader term : terms) { for (AtlasRelatedTermHeader term : terms) {
if (Objects.nonNull(existingRelations) && existingRelations.containsKey(term.getTermGuid())) { if (Objects.nonNull(existingRelations) && existingRelations.containsKey(term.getTermGuid())) {
...@@ -364,10 +364,16 @@ public class GlossaryTermUtils extends GlossaryUtils { ...@@ -364,10 +364,16 @@ public class GlossaryTermUtils extends GlossaryUtils {
} }
continue; continue;
} }
if (existing.getGuid().equals(term.getTermGuid())) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_TERM_RELATION_TO_SELF);
}
if (DEBUG_ENABLED) { if (DEBUG_ENABLED) {
LOG.debug("Creating new term relation = {}, terms = {}", relation, term.getDisplayText()); LOG.debug("Creating new term relation = {}, terms = {}", relation, term.getDisplayText());
} }
createRelationship(defineTermRelation(relation.getRelationName(), existing.getGuid(), term));
createRelationship(defineTermRelation(relation, existing.getGuid(), term));
} }
} }
} }
...@@ -403,11 +409,18 @@ public class GlossaryTermUtils extends GlossaryUtils { ...@@ -403,11 +409,18 @@ public class GlossaryTermUtils extends GlossaryUtils {
return new AtlasRelationship(TERM_ANCHOR, new AtlasObjectId(glossaryGuid), new AtlasObjectId(termGuid), defaultAttrs.getAttributes()); return new AtlasRelationship(TERM_ANCHOR, new AtlasObjectId(glossaryGuid), new AtlasObjectId(termGuid), defaultAttrs.getAttributes());
} }
private AtlasRelationship defineTermRelation(String relation, String end1TermGuid, AtlasRelatedTermHeader end2RelatedTerm) { private AtlasRelationship defineTermRelation(AtlasGlossaryTerm.Relation relation, String end1TermGuid, AtlasRelatedTermHeader end2RelatedTerm) {
AtlasRelationshipType relationshipType = typeRegistry.getRelationshipTypeByName(relation); AtlasRelationshipType relationshipType = typeRegistry.getRelationshipTypeByName(relation.getName());
AtlasStruct defaultAttrs = relationshipType.createDefaultValue(); AtlasStruct defaultAttrs = relationshipType.createDefaultValue();
AtlasRelationship relationship = new AtlasRelationship(relation, new AtlasObjectId(end1TermGuid), new AtlasObjectId(end2RelatedTerm.getTermGuid()), defaultAttrs.getAttributes()); AtlasRelationship relationship;
// End1 and End2 ObjectIds depend on the attribute
if (relation.isEnd2Attr()) {
relationship = new AtlasRelationship(relation.getName(), new AtlasObjectId(end2RelatedTerm.getTermGuid()), new AtlasObjectId(end1TermGuid), defaultAttrs.getAttributes());
} else {
relationship = new AtlasRelationship(relation.getName(), new AtlasObjectId(end1TermGuid), new AtlasObjectId(end2RelatedTerm.getTermGuid()), defaultAttrs.getAttributes());
}
updateRelationshipAttributes(relationship, end2RelatedTerm); updateRelationshipAttributes(relationship, end2RelatedTerm);
return relationship; return relationship;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment