Search in sources :

Example 11 with AtlasGlossary

use of org.apache.atlas.model.glossary.AtlasGlossary in project atlas by apache.

the class GlossaryService method createCategory.

@GraphTransaction
public AtlasGlossaryCategory createCategory(AtlasGlossaryCategory glossaryCategory) throws AtlasBaseException {
    if (DEBUG_ENABLED) {
        LOG.debug("==> GlossaryService.createCategory({})", glossaryCategory);
    }
    if (Objects.isNull(glossaryCategory)) {
        throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "GlossaryCategory definition missing");
    }
    if (Objects.isNull(glossaryCategory.getAnchor())) {
        throw new AtlasBaseException(AtlasErrorCode.MISSING_MANDATORY_ANCHOR);
    }
    if (StringUtils.isEmpty(glossaryCategory.getName())) {
        throw new AtlasBaseException(AtlasErrorCode.GLOSSARY_CATEGORY_QUALIFIED_NAME_CANT_BE_DERIVED);
    }
    if (isNameInvalid(glossaryCategory.getName())) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_DISPLAY_NAME);
    } else {
        // Derive the qualifiedName
        String anchorGlossaryGuid = glossaryCategory.getAnchor().getGlossaryGuid();
        AtlasGlossary glossary = dataAccess.load(getGlossarySkeleton(anchorGlossaryGuid));
        glossaryCategory.setQualifiedName(glossaryCategory.getName() + "@" + glossary.getQualifiedName());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Derived qualifiedName = {}", glossaryCategory.getQualifiedName());
        }
    }
    // and the duplicate request comes in with old name
    if (categoryExists(glossaryCategory)) {
        throw new AtlasBaseException(AtlasErrorCode.GLOSSARY_CATEGORY_ALREADY_EXISTS, glossaryCategory.getQualifiedName());
    }
    AtlasGlossaryCategory storeObject = dataAccess.save(glossaryCategory);
    // Attempt relation creation and gather all impacted categories
    Map<String, AtlasGlossaryCategory> impactedCategories = glossaryCategoryUtils.processCategoryRelations(storeObject, glossaryCategory, GlossaryUtils.RelationshipOperation.CREATE);
    // 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) {
        LOG.debug("<== GlossaryService.createCategory() : {}", storeObject);
    }
    return storeObject;
}
Also used : AtlasGlossary(org.apache.atlas.model.glossary.AtlasGlossary) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasGlossaryCategory(org.apache.atlas.model.glossary.AtlasGlossaryCategory) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 12 with AtlasGlossary

use of org.apache.atlas.model.glossary.AtlasGlossary in project atlas by apache.

the class GlossaryService method getGlossaryCategoriesHeaders.

@GraphTransaction
public List<AtlasRelatedCategoryHeader> getGlossaryCategoriesHeaders(String glossaryGuid, int offset, int limit, SortOrder sortOrder) throws AtlasBaseException {
    if (Objects.isNull(glossaryGuid)) {
        throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "glossaryGuid is null/empty");
    }
    if (DEBUG_ENABLED) {
        LOG.debug("==> GlossaryService.getGlossaryCategoriesHeaders({}, {}, {}, {})", glossaryGuid, offset, limit, sortOrder);
    }
    List<AtlasRelatedCategoryHeader> ret;
    AtlasGlossary glossary = getGlossary(glossaryGuid);
    if (CollectionUtils.isNotEmpty(glossary.getCategories())) {
        List<AtlasRelatedCategoryHeader> categories = new ArrayList<>(glossary.getCategories());
        if (sortOrder != null) {
            categories.sort((o1, o2) -> sortOrder == SortOrder.ASCENDING ? o1.getDisplayText().compareTo(o2.getDisplayText()) : o2.getDisplayText().compareTo(o1.getDisplayText()));
        }
        ret = new PaginationHelper<>(categories, offset, limit).getPaginatedList();
    } else {
        ret = Collections.emptyList();
    }
    if (DEBUG_ENABLED) {
        LOG.debug("<== GlossaryService.getGlossaryCategoriesHeaders() : {}", ret);
    }
    return ret;
}
Also used : AtlasGlossary(org.apache.atlas.model.glossary.AtlasGlossary) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) ArrayList(java.util.ArrayList) AtlasRelatedCategoryHeader(org.apache.atlas.model.glossary.relations.AtlasRelatedCategoryHeader) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 13 with AtlasGlossary

use of org.apache.atlas.model.glossary.AtlasGlossary in project atlas by apache.

the class GlossaryService method getGlossaryTermsHeaders.

@GraphTransaction
public List<AtlasRelatedTermHeader> getGlossaryTermsHeaders(String glossaryGuid, int offset, int limit, SortOrder sortOrder) throws AtlasBaseException {
    if (Objects.isNull(glossaryGuid)) {
        throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "glossaryGuid is null/empty");
    }
    if (DEBUG_ENABLED) {
        LOG.debug("==> GlossaryService.getGlossaryTermsHeaders({}, {}, {}, {})", glossaryGuid, offset, limit, sortOrder);
    }
    AtlasGlossary glossary = getGlossary(glossaryGuid);
    List<AtlasRelatedTermHeader> ret;
    if (CollectionUtils.isNotEmpty(glossary.getTerms())) {
        List<AtlasRelatedTermHeader> terms = new ArrayList<>(glossary.getTerms());
        if (sortOrder != null) {
            terms.sort((o1, o2) -> sortOrder == SortOrder.ASCENDING ? o1.getDisplayText().compareTo(o2.getDisplayText()) : o2.getDisplayText().compareTo(o1.getDisplayText()));
        }
        ret = new PaginationHelper<>(terms, offset, limit).getPaginatedList();
    } else {
        ret = Collections.emptyList();
    }
    if (DEBUG_ENABLED) {
        LOG.debug("<== GlossaryService.getGlossaryTermsHeaders() : {}", ret);
    }
    return ret;
}
Also used : AtlasGlossary(org.apache.atlas.model.glossary.AtlasGlossary) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasRelatedTermHeader(org.apache.atlas.model.glossary.relations.AtlasRelatedTermHeader) ArrayList(java.util.ArrayList) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 14 with AtlasGlossary

use of org.apache.atlas.model.glossary.AtlasGlossary in project atlas by apache.

the class GlossaryTermUtils method processTermAnchor.

private void processTermAnchor(AtlasGlossaryTerm currentTerm, AtlasGlossaryTerm updatedTerm, RelationshipOperation op) throws AtlasBaseException {
    if (Objects.isNull(updatedTerm.getAnchor()) && op != RelationshipOperation.DELETE) {
        throw new AtlasBaseException(AtlasErrorCode.MISSING_MANDATORY_ANCHOR);
    }
    // glossary_g1
    AtlasGlossaryHeader currentTermGlossary = currentTerm.getAnchor();
    // glossary_g2
    AtlasGlossaryHeader updatedTermGlossary = updatedTerm.getAnchor();
    String updatedTermGlossaryGuid = updatedTermGlossary.getGlossaryGuid();
    String currentTermGlossaryGuid = currentTermGlossary.getGlossaryGuid();
    switch(op) {
        case CREATE:
            if (Objects.isNull(updatedTermGlossaryGuid)) {
                throw new AtlasBaseException(AtlasErrorCode.INVALID_NEW_ANCHOR_GUID);
            } else {
                if (DEBUG_ENABLED) {
                    LOG.debug("Creating new term anchor, category = {}, glossary = {}", currentTerm.getGuid(), updatedTerm.getAnchor().getGlossaryGuid());
                }
                if (!StringUtils.equals(updatedTermGlossaryGuid, currentTermGlossaryGuid)) {
                    createRelationship(defineTermAnchorRelation(updatedTermGlossaryGuid, currentTerm.getGuid()));
                }
            }
            break;
        case UPDATE:
            if (!Objects.equals(updatedTermGlossary, currentTermGlossary)) {
                if (Objects.isNull(updatedTermGlossaryGuid)) {
                    throw new AtlasBaseException(AtlasErrorCode.INVALID_NEW_ANCHOR_GUID);
                }
                if (DEBUG_ENABLED) {
                    LOG.debug("Updating term anchor, currAnchor = {}, newAnchor = {} and term = {}", currentTermGlossaryGuid, updatedTermGlossaryGuid, currentTerm.getName());
                }
                relationshipStore.deleteById(currentTermGlossary.getRelationGuid(), true);
                // Derive the qualifiedName when anchor changes
                String anchorGlossaryGuid = updatedTermGlossaryGuid;
                AtlasGlossary glossary = dataAccess.load(getGlossarySkeleton(anchorGlossaryGuid));
                currentTerm.setQualifiedName(currentTerm.getName() + "@" + glossary.getQualifiedName());
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Derived qualifiedName = {}", currentTerm.getQualifiedName());
                }
                createRelationship(defineTermAnchorRelation(updatedTermGlossaryGuid, currentTerm.getGuid()));
            }
            break;
        case DELETE:
            if (Objects.nonNull(currentTermGlossary)) {
                if (DEBUG_ENABLED) {
                    LOG.debug("Deleting term anchor");
                }
                relationshipStore.deleteById(currentTermGlossary.getRelationGuid(), true);
            }
            break;
    }
}
Also used : AtlasGlossary(org.apache.atlas.model.glossary.AtlasGlossary) AtlasGlossaryHeader(org.apache.atlas.model.glossary.relations.AtlasGlossaryHeader) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException)

Example 15 with AtlasGlossary

use of org.apache.atlas.model.glossary.AtlasGlossary in project atlas by apache.

the class GlossaryTermUtils method createGlossary.

private String createGlossary(String glossaryName, List<String> failedTermMsgs) throws AtlasBaseException {
    String ret = null;
    if (GlossaryService.isNameInvalid(glossaryName)) {
        LOG.error("The provided Glossary Name is invalid : " + glossaryName);
        failedTermMsgs.add("The provided Glossary Name {" + glossaryName + "} is invalid : " + AtlasErrorCode.INVALID_DISPLAY_NAME.getFormattedErrorMessage());
    } else {
        AtlasGlossary glossary = new AtlasGlossary();
        glossary.setQualifiedName(glossaryName);
        glossary.setName(glossaryName);
        glossary = dataAccess.save(glossary);
        ret = glossary.getGuid();
    }
    return ret;
}
Also used : AtlasGlossary(org.apache.atlas.model.glossary.AtlasGlossary)

Aggregations

AtlasGlossary (org.apache.atlas.model.glossary.AtlasGlossary)29 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)15 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)8 AtlasRelatedCategoryHeader (org.apache.atlas.model.glossary.relations.AtlasRelatedCategoryHeader)6 Test (org.testng.annotations.Test)6 ArrayList (java.util.ArrayList)5 AtlasGlossaryHeader (org.apache.atlas.model.glossary.relations.AtlasGlossaryHeader)5 AtlasGlossaryCategory (org.apache.atlas.model.glossary.AtlasGlossaryCategory)4 AtlasGlossaryTerm (org.apache.atlas.model.glossary.AtlasGlossaryTerm)4 AtlasRelatedTermHeader (org.apache.atlas.model.glossary.relations.AtlasRelatedTermHeader)4 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Path (javax.ws.rs.Path)2 Timed (org.apache.atlas.annotation.Timed)2 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)2 AtlasRelatedObjectId (org.apache.atlas.model.instance.AtlasRelatedObjectId)2 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)2 IOException (java.io.IOException)1