Repository: incubator-atlas Updated Branches: refs/heads/0.8-incubating e8d1d523a -> 8e198c21f
ATLAS-1664: Able to add already added tag to an entity through REST API: Signed-off-by: apoorvnaik <[email protected]> (cherry picked from commit 1612b3058a0bc8317dd6f3ff4980fead0d36df28) Project: http://git-wip-us.apache.org/repos/asf/incubator-atlas/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-atlas/commit/8e198c21 Tree: http://git-wip-us.apache.org/repos/asf/incubator-atlas/tree/8e198c21 Diff: http://git-wip-us.apache.org/repos/asf/incubator-atlas/diff/8e198c21 Branch: refs/heads/0.8-incubating Commit: 8e198c21f759ce74c05455b7846db7463c4a74d7 Parents: e8d1d52 Author: Sarath Subramanian <[email protected]> Authored: Fri Mar 17 09:36:08 2017 -0700 Committer: apoorvnaik <[email protected]> Committed: Sun Mar 19 22:22:01 2017 -0700 ---------------------------------------------------------------------- .../store/graph/v1/AtlasEntityStoreV1.java | 39 ++++++++++++++++++++ .../org/apache/atlas/web/rest/EntityREST.java | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/8e198c21/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityStoreV1.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityStoreV1.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityStoreV1.java index c8ac3b6..fa4c051 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityStoreV1.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityStoreV1.java @@ -433,6 +433,9 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { validateAndNormalize(classification); } + // validate if entity, not already associated with classifications + validateEntityAssociations(guid, classifications); + EntityGraphMapper graphMapper = new EntityGraphMapper(deleteHandler, typeRegistry); graphMapper.addClassifications(new EntityMutationContext(), guid, classifications); @@ -461,6 +464,9 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { List<AtlasClassification> classifications = Collections.singletonList(classification); for (String guid : guids) { + // validate if entity, not already associated with classifications + validateEntityAssociations(guid, classifications); + graphMapper.addClassifications(new EntityMutationContext(), guid, classifications); // notify listeners on classification addition @@ -599,4 +605,37 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { type.getNormalizedValue(classification); } + + /** + * Validate if classification is not already associated with the entities + * @param guid unique entity id + * @param classifications list of classifications to be associated + */ + private void validateEntityAssociations(String guid, List<AtlasClassification> classifications) throws AtlasBaseException { + List<String> entityClassifications = getClassificationNames(guid); + + for (AtlasClassification classification : classifications) { + String newClassification = classification.getTypeName(); + + if (CollectionUtils.isNotEmpty(entityClassifications) && entityClassifications.contains(newClassification)) { + throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "entity: " + guid + + ", already associated with classification: " + newClassification); + } + } + } + + private List<String> getClassificationNames(String guid) throws AtlasBaseException { + List<String> ret = null; + List<AtlasClassification> classifications = getClassifications(guid); + + if (CollectionUtils.isNotEmpty(classifications)) { + ret = new ArrayList<>(); + + for (AtlasClassification classification : classifications) { + ret.add(classification.getTypeName()); + } + } + + return ret; + } } http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/8e198c21/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java ---------------------------------------------------------------------- diff --git a/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java b/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java index 98dc918..671590d 100644 --- a/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java +++ b/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java @@ -474,7 +474,7 @@ public class EntityREST { AtlasClassification classification = request == null ? null : request.getClassification(); List<String> entityGuids = request == null ? null : request.getEntityGuids(); - if (classification == null || org.apache.commons.lang.StringUtils.isEmpty(classification.getTypeName())) { + if (classification == null || StringUtils.isEmpty(classification.getTypeName())) { throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "no classification"); }
