Commit cabc1e55 by apoorvnaik

ATLAS-2534: Glossary REST API (bugfix)

Change-Id: Ic0b40d722f2a797db19dd7ee95ef30866e073128
parent 4a938d87
...@@ -168,6 +168,9 @@ public enum AtlasErrorCode { ...@@ -168,6 +168,9 @@ public enum AtlasErrorCode {
RELATIONSHIP_ALREADY_EXISTS(409, "ATLAS-409-00-004", "relationship {0} already exists between entities {1} and {2}"), RELATIONSHIP_ALREADY_EXISTS(409, "ATLAS-409-00-004", "relationship {0} already exists between entities {1} and {2}"),
TYPE_HAS_RELATIONSHIPS(409, "ATLAS-409-00-005", "Given type {0} has associated relationshipDefs"), TYPE_HAS_RELATIONSHIPS(409, "ATLAS-409-00-005", "Given type {0} has associated relationshipDefs"),
SAVED_SEARCH_ALREADY_EXISTS(409, "ATLAS-409-00-006", "search named {0} already exists for user {1}"), SAVED_SEARCH_ALREADY_EXISTS(409, "ATLAS-409-00-006", "search named {0} already exists for user {1}"),
GLOSSARY_ALREADY_EXISTS(409, "ATLAS-409-00-007", "Glossary with qualifiedName {0} already exists"),
GLOSSARY_TERM_ALREADY_EXISTS(409, "ATLAS-409-00-009", "Glossary term with qualifiedName {0} already exists"),
GLOSSARY_CATEGORY_ALREADY_EXISTS(409, "ATLAS-409-00-00A", "Glossary category with qualifiedName {0} already exists"),
// All internal errors go here // All internal errors go here
INTERNAL_ERROR(500, "ATLAS-500-00-001", "Internal server error {0}"), INTERNAL_ERROR(500, "ATLAS-500-00-001", "Internal server error {0}"),
......
...@@ -27,6 +27,7 @@ import org.apache.atlas.model.glossary.relations.AtlasRelatedCategoryHeader; ...@@ -27,6 +27,7 @@ import org.apache.atlas.model.glossary.relations.AtlasRelatedCategoryHeader;
import org.apache.atlas.model.glossary.relations.AtlasRelatedTermHeader; import org.apache.atlas.model.glossary.relations.AtlasRelatedTermHeader;
import org.apache.atlas.model.glossary.relations.AtlasTermCategorizationHeader; import org.apache.atlas.model.glossary.relations.AtlasTermCategorizationHeader;
import org.apache.atlas.model.instance.AtlasRelatedObjectId; import org.apache.atlas.model.instance.AtlasRelatedObjectId;
import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.repository.ogm.DataAccess; import org.apache.atlas.repository.ogm.DataAccess;
import org.apache.atlas.repository.store.graph.AtlasRelationshipStore; import org.apache.atlas.repository.store.graph.AtlasRelationshipStore;
import org.apache.atlas.repository.store.graph.v1.AtlasGraphUtilsV1; import org.apache.atlas.repository.store.graph.v1.AtlasGraphUtilsV1;
...@@ -53,13 +54,17 @@ public class GlossaryService { ...@@ -53,13 +54,17 @@ public class GlossaryService {
private static final Logger LOG = LoggerFactory.getLogger(GlossaryService.class); private static final Logger LOG = LoggerFactory.getLogger(GlossaryService.class);
private static final boolean DEBUG_ENABLED = LOG.isDebugEnabled(); private static final boolean DEBUG_ENABLED = LOG.isDebugEnabled();
private final DataAccess dataAccess; private static final String QUALIFIED_NAME_ATTR = "qualifiedName";
private final GlossaryTermUtils glossaryTermUtils;
private final GlossaryCategoryUtils glossaryCategoryUtils; private final DataAccess dataAccess;
private final GlossaryTermUtils glossaryTermUtils;
private final GlossaryCategoryUtils glossaryCategoryUtils;
private final AtlasTypeRegistry atlasTypeRegistry;
@Inject @Inject
public GlossaryService(DataAccess dataAccess, final AtlasRelationshipStore relationshipStore, final AtlasTypeRegistry typeRegistry) { public GlossaryService(DataAccess dataAccess, final AtlasRelationshipStore relationshipStore, final AtlasTypeRegistry typeRegistry) {
this.dataAccess = dataAccess; this.dataAccess = dataAccess;
this.atlasTypeRegistry = typeRegistry;
glossaryTermUtils = new GlossaryTermUtils(relationshipStore, typeRegistry); glossaryTermUtils = new GlossaryTermUtils(relationshipStore, typeRegistry);
glossaryCategoryUtils = new GlossaryCategoryUtils(relationshipStore, typeRegistry); glossaryCategoryUtils = new GlossaryCategoryUtils(relationshipStore, typeRegistry);
} }
...@@ -78,7 +83,7 @@ public class GlossaryService { ...@@ -78,7 +83,7 @@ public class GlossaryService {
LOG.debug("==> GlossaryService.getGlossaries({}, {}, {})", limit, offset, sortOrder); LOG.debug("==> GlossaryService.getGlossaries({}, {}, {})", limit, offset, sortOrder);
} }
List<String> glossaryGuids = AtlasGraphUtilsV1.findEntityGUIDsByType(GlossaryUtils.ATLAS_GLOSSARY_PREFIX, sortOrder); List<String> glossaryGuids = AtlasGraphUtilsV1.findEntityGUIDsByType(GlossaryUtils.ATLAS_GLOSSARY_TYPENAME, sortOrder);
PaginationHelper paginationHelper = new PaginationHelper<>(glossaryGuids, offset, limit); PaginationHelper paginationHelper = new PaginationHelper<>(glossaryGuids, offset, limit);
List<AtlasGlossary> ret; List<AtlasGlossary> ret;
...@@ -118,6 +123,7 @@ public class GlossaryService { ...@@ -118,6 +123,7 @@ public class GlossaryService {
if (Objects.isNull(atlasGlossary)) { if (Objects.isNull(atlasGlossary)) {
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Glossary definition missing"); throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Glossary definition missing");
} }
if (StringUtils.isEmpty(atlasGlossary.getQualifiedName())) { if (StringUtils.isEmpty(atlasGlossary.getQualifiedName())) {
if (StringUtils.isEmpty(atlasGlossary.getDisplayName())) { if (StringUtils.isEmpty(atlasGlossary.getDisplayName())) {
throw new AtlasBaseException(AtlasErrorCode.GLOSSARY_QUALIFIED_NAME_CANT_BE_DERIVED); throw new AtlasBaseException(AtlasErrorCode.GLOSSARY_QUALIFIED_NAME_CANT_BE_DERIVED);
...@@ -125,11 +131,17 @@ public class GlossaryService { ...@@ -125,11 +131,17 @@ public class GlossaryService {
atlasGlossary.setQualifiedName(atlasGlossary.getDisplayName()); atlasGlossary.setQualifiedName(atlasGlossary.getDisplayName());
} }
} }
if (glossaryExists(atlasGlossary)) {
throw new AtlasBaseException(AtlasErrorCode.GLOSSARY_ALREADY_EXISTS, atlasGlossary.getQualifiedName());
}
AtlasGlossary saved = dataAccess.save(atlasGlossary); AtlasGlossary saved = dataAccess.save(atlasGlossary);
if (DEBUG_ENABLED) { if (DEBUG_ENABLED) {
LOG.debug("<== GlossaryService.createGlossary() : {}", saved); LOG.debug("<== GlossaryService.createGlossary() : {}", saved);
} }
return saved; return saved;
} }
...@@ -297,6 +309,10 @@ public class GlossaryService { ...@@ -297,6 +309,10 @@ public class GlossaryService {
} }
} }
if (termExists(glossaryTerm)) {
throw new AtlasBaseException(AtlasErrorCode.GLOSSARY_TERM_ALREADY_EXISTS, glossaryTerm.getQualifiedName());
}
AtlasGlossaryTerm existing = dataAccess.save(glossaryTerm); AtlasGlossaryTerm existing = dataAccess.save(glossaryTerm);
glossaryTermUtils.processTermRelations(glossaryTerm, existing, GlossaryUtils.RelationshipOperation.CREATE); glossaryTermUtils.processTermRelations(glossaryTerm, existing, GlossaryUtils.RelationshipOperation.CREATE);
...@@ -457,6 +473,10 @@ public class GlossaryService { ...@@ -457,6 +473,10 @@ public class GlossaryService {
} }
} }
if (categoryExists(glossaryCategory)) {
throw new AtlasBaseException(AtlasErrorCode.GLOSSARY_CATEGORY_ALREADY_EXISTS, glossaryCategory.getQualifiedName());
}
AtlasGlossaryCategory saved = dataAccess.save(glossaryCategory); AtlasGlossaryCategory saved = dataAccess.save(glossaryCategory);
// Attempt relation creation // Attempt relation creation
...@@ -726,6 +746,27 @@ public class GlossaryService { ...@@ -726,6 +746,27 @@ public class GlossaryService {
return glossary; return glossary;
} }
private boolean glossaryExists(AtlasGlossary atlasGlossary) {
AtlasVertex vertex = AtlasGraphUtilsV1.findByUniqueAttributes(atlasTypeRegistry.getEntityTypeByName(GlossaryUtils.ATLAS_GLOSSARY_TYPENAME), new HashMap<String, Object>() {{
put(QUALIFIED_NAME_ATTR, atlasGlossary.getQualifiedName());
}});
return Objects.nonNull(vertex);
}
private boolean termExists(AtlasGlossaryTerm term) {
AtlasVertex vertex = AtlasGraphUtilsV1.findByUniqueAttributes(atlasTypeRegistry.getEntityTypeByName(GlossaryUtils.ATLAS_GLOSSARY_TERM_TYPENAME), new HashMap<String, Object>() {{
put(QUALIFIED_NAME_ATTR, term.getQualifiedName());
}});
return Objects.nonNull(vertex);
}
private boolean categoryExists(AtlasGlossaryCategory category) {
AtlasVertex vertex = AtlasGraphUtilsV1.findByUniqueAttributes(atlasTypeRegistry.getEntityTypeByName(GlossaryUtils.ATLAS_GLOSSARY_CATEGORY_TYPENAME), new HashMap<String, Object>() {{
put(QUALIFIED_NAME_ATTR, category.getQualifiedName());
}});
return Objects.nonNull(vertex);
}
private void deleteCategories(final AtlasGlossary existing, final Set<AtlasRelatedCategoryHeader> categories) throws AtlasBaseException { private void deleteCategories(final AtlasGlossary existing, final Set<AtlasRelatedCategoryHeader> categories) throws AtlasBaseException {
if (CollectionUtils.isNotEmpty(categories)) { if (CollectionUtils.isNotEmpty(categories)) {
if (DEBUG_ENABLED) { if (DEBUG_ENABLED) {
......
...@@ -36,8 +36,12 @@ public abstract class GlossaryUtils { ...@@ -36,8 +36,12 @@ public abstract class GlossaryUtils {
public static final String TERM_ASSIGNMENT_ATTR_STEWARD = "steward"; public static final String TERM_ASSIGNMENT_ATTR_STEWARD = "steward";
public static final String TERM_ASSIGNMENT_ATTR_SOURCE = "source"; public static final String TERM_ASSIGNMENT_ATTR_SOURCE = "source";
static final String ATLAS_GLOSSARY_PREFIX = "__AtlasGlossary"; static final String ATLAS_GLOSSARY_TYPENAME = "__AtlasGlossary";
static final String ATLAS_GLOSSARY_TERM_TYPENAME = "__AtlasGlossaryTerm";
static final String ATLAS_GLOSSARY_CATEGORY_TYPENAME = "__AtlasGlossaryCategory";
// Relation name constants // Relation name constants
protected static final String ATLAS_GLOSSARY_PREFIX = ATLAS_GLOSSARY_TYPENAME;
protected static final String TERM_ANCHOR = ATLAS_GLOSSARY_PREFIX + "TermAnchor"; protected static final String TERM_ANCHOR = ATLAS_GLOSSARY_PREFIX + "TermAnchor";
protected static final String CATEGORY_ANCHOR = ATLAS_GLOSSARY_PREFIX + "CategoryAnchor"; protected static final String CATEGORY_ANCHOR = ATLAS_GLOSSARY_PREFIX + "CategoryAnchor";
protected static final String CATEGORY_HIERARCHY = ATLAS_GLOSSARY_PREFIX + "CategoryHierarchyLink"; protected static final String CATEGORY_HIERARCHY = ATLAS_GLOSSARY_PREFIX + "CategoryHierarchyLink";
...@@ -49,7 +53,6 @@ public abstract class GlossaryUtils { ...@@ -49,7 +53,6 @@ public abstract class GlossaryUtils {
protected static final String TERM_RELATION_ATTR_SOURCE = "source"; protected static final String TERM_RELATION_ATTR_SOURCE = "source";
protected static final String TERM_RELATION_ATTR_STATUS = "status"; protected static final String TERM_RELATION_ATTR_STATUS = "status";
protected final AtlasRelationshipStore relationshipStore; protected final AtlasRelationshipStore relationshipStore;
protected final AtlasTypeRegistry typeRegistry; protected final AtlasTypeRegistry typeRegistry;
......
...@@ -500,12 +500,8 @@ public final class EntityGraphRetriever { ...@@ -500,12 +500,8 @@ public final class EntityGraphRetriever {
Iterable edges = entityVertex.query().direction(AtlasEdgeDirection.IN).label(TERM_ASSIGNMENT_LABEL).edges(); Iterable edges = entityVertex.query().direction(AtlasEdgeDirection.IN).label(TERM_ASSIGNMENT_LABEL).edges();
if (edges != null) { if (edges != null) {
Iterator<AtlasEdge> iterator = edges.iterator(); for (final AtlasEdge edge : (Iterable<AtlasEdge>) edges) {
if (edge != null && GraphHelper.getStatus(edge) != AtlasEntity.Status.DELETED) {
while (iterator.hasNext()) {
AtlasEdge edge = iterator.next();
if (edge != null) {
ret.add(toTermAssignmentHeader(edge)); ret.add(toTermAssignmentHeader(edge));
} }
} }
......
...@@ -214,6 +214,7 @@ public class GlossaryREST { ...@@ -214,6 +214,7 @@ public class GlossaryREST {
* @throws AtlasBaseException * @throws AtlasBaseException
* @HTTP 200 If glossary creation was successful * @HTTP 200 If glossary creation was successful
* @HTTP 400 If Glossary definition has invalid or missing information * @HTTP 400 If Glossary definition has invalid or missing information
* @HTTP 409 If Glossary definition already exists (duplicate qualifiedName)
*/ */
@POST @POST
public AtlasGlossary createGlossary(AtlasGlossary atlasGlossary) throws AtlasBaseException { public AtlasGlossary createGlossary(AtlasGlossary atlasGlossary) throws AtlasBaseException {
...@@ -236,6 +237,7 @@ public class GlossaryREST { ...@@ -236,6 +237,7 @@ public class GlossaryREST {
* @throws AtlasBaseException * @throws AtlasBaseException
* @HTTP 200 If glossary term creation was successful * @HTTP 200 If glossary term creation was successful
* @HTTP 400 If Glossary term definition has invalid or missing information * @HTTP 400 If Glossary term definition has invalid or missing information
* @HTTP 409 If Glossary term already exists (duplicate qualifiedName)
*/ */
@POST @POST
@Path("/term") @Path("/term")
...@@ -289,6 +291,7 @@ public class GlossaryREST { ...@@ -289,6 +291,7 @@ public class GlossaryREST {
* @throws AtlasBaseException * @throws AtlasBaseException
* @HTTP 200 If glossary category creation was successful * @HTTP 200 If glossary category creation was successful
* @HTTP 400 If Glossary category definition has invalid or missing information * @HTTP 400 If Glossary category definition has invalid or missing information
* @HTTP 409 If Glossary category already exists (duplicate qualifiedName)
*/ */
@POST @POST
@Path("/category") @Path("/category")
......
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