Search in sources :

Example 6 with AtlasRelationship

use of org.apache.atlas.model.instance.AtlasRelationship in project incubator-atlas by apache.

the class AtlasRelationshipStoreV1 method createRelationship.

private AtlasRelationship createRelationship(AtlasRelationship relationship, AtlasVertex end1Vertex, AtlasVertex end2Vertex) throws AtlasBaseException {
    AtlasRelationship ret;
    try {
        AtlasEdge relationshipEdge = getRelationshipEdge(end1Vertex, end2Vertex, relationship);
        if (relationshipEdge == null) {
            relationshipEdge = createRelationshipEdge(end1Vertex, end2Vertex, relationship);
            AtlasRelationshipType relationType = typeRegistry.getRelationshipTypeByName(relationship.getTypeName());
            if (MapUtils.isNotEmpty(relationType.getAllAttributes())) {
                for (AtlasAttribute attr : relationType.getAllAttributes().values()) {
                    String attrName = attr.getName();
                    String attrVertexProperty = attr.getVertexPropertyName();
                    Object attrValue = relationship.getAttribute(attrName);
                    AtlasGraphUtilsV1.setProperty(relationshipEdge, attrVertexProperty, attrValue);
                }
            }
            ret = mapEdgeToAtlasRelationship(relationshipEdge);
        } else {
            throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIP_ALREADY_EXISTS, relationship.getTypeName(), relationship.getEnd1().getGuid(), relationship.getEnd2().getGuid());
        }
    } catch (RepositoryException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
    return ret;
}
Also used : AtlasRelationshipType(org.apache.atlas.type.AtlasRelationshipType) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasRelationship(org.apache.atlas.model.instance.AtlasRelationship) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 7 with AtlasRelationship

use of org.apache.atlas.model.instance.AtlasRelationship in project incubator-atlas by apache.

the class AtlasRelationshipStoreV1 method getById.

@Override
@GraphTransaction
public AtlasRelationship getById(String guid) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> getById({})", guid);
    }
    AtlasRelationship ret;
    try {
        AtlasEdge edge = graphHelper.getEdgeForGUID(guid);
        ret = mapEdgeToAtlasRelationship(edge);
    } catch (EntityNotFoundException ex) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIP_GUID_NOT_FOUND, guid);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== getById({}): {}", guid, ret);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasRelationship(org.apache.atlas.model.instance.AtlasRelationship) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 8 with AtlasRelationship

use of org.apache.atlas.model.instance.AtlasRelationship in project atlas by apache.

the class EntityGraphRetriever method mapVertexToRelatedObjectId.

private AtlasRelatedObjectId mapVertexToRelatedObjectId(AtlasVertex entityVertex, AtlasEdge edge) throws AtlasBaseException {
    AtlasRelatedObjectId ret = null;
    if (GraphHelper.elementExists(edge)) {
        AtlasVertex referenceVertex = edge.getInVertex();
        if (StringUtils.equals(getIdFromVertex(referenceVertex), getIdFromVertex(entityVertex))) {
            referenceVertex = edge.getOutVertex();
        }
        if (referenceVertex != null) {
            String entityTypeName = getTypeName(referenceVertex);
            String entityGuid = getGuid(referenceVertex);
            AtlasRelationship relationship = mapEdgeToAtlasRelationship(edge);
            ret = new AtlasRelatedObjectId(entityGuid, entityTypeName, relationship.getGuid(), new AtlasStruct(relationship.getTypeName(), relationship.getAttributes()));
            Object displayText = getDisplayText(referenceVertex, entityTypeName);
            if (displayText != null) {
                ret.setDisplayText(displayText.toString());
            }
        }
    }
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasRelatedObjectId(org.apache.atlas.model.instance.AtlasRelatedObjectId) AtlasRelationship(org.apache.atlas.model.instance.AtlasRelationship)

Example 9 with AtlasRelationship

use of org.apache.atlas.model.instance.AtlasRelationship in project atlas by apache.

the class AtlasRelationshipStoreV1 method getOrCreate.

@Override
public AtlasRelationship getOrCreate(AtlasRelationship relationship) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> getOrCreate({})", relationship);
    }
    validateRelationship(relationship);
    AtlasVertex end1Vertex = getVertexFromEndPoint(relationship.getEnd1());
    AtlasVertex end2Vertex = getVertexFromEndPoint(relationship.getEnd2());
    AtlasRelationship ret = null;
    // check if relationship exists
    AtlasEdge relationshipEdge = getRelationshipEdge(end1Vertex, end2Vertex, relationship.getTypeName());
    if (relationshipEdge == null) {
        validateRelationship(relationship);
        relationshipEdge = createRelationship(end1Vertex, end2Vertex, relationship);
    }
    if (relationshipEdge != null) {
        ret = entityRetriever.mapEdgeToAtlasRelationship(relationshipEdge);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== getOrCreate({}): {}", relationship, ret);
    }
    return ret;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasRelationship(org.apache.atlas.model.instance.AtlasRelationship) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 10 with AtlasRelationship

use of org.apache.atlas.model.instance.AtlasRelationship in project atlas by apache.

the class AtlasRelationshipStoreV1 method update.

@Override
@GraphTransaction
public AtlasRelationship update(AtlasRelationship relationship) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> update({})", relationship);
    }
    String guid = relationship.getGuid();
    if (StringUtils.isEmpty(guid)) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIP_GUID_NOT_FOUND, guid);
    }
    AtlasEdge edge = graphHelper.getEdgeForGUID(guid);
    String edgeType = AtlasGraphUtilsV1.getTypeName(edge);
    AtlasVertex end1Vertex = edge.getOutVertex();
    AtlasVertex end2Vertex = edge.getInVertex();
    // update shouldn't change endType
    if (StringUtils.isNotEmpty(relationship.getTypeName()) && !StringUtils.equalsIgnoreCase(edgeType, relationship.getTypeName())) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIP_UPDATE_TYPE_CHANGE_NOT_ALLOWED, guid, edgeType, relationship.getTypeName());
    }
    // update shouldn't change ends
    if (relationship.getEnd1() != null) {
        String updatedEnd1Guid = relationship.getEnd1().getGuid();
        if (updatedEnd1Guid == null) {
            AtlasVertex updatedEnd1Vertex = getVertexFromEndPoint(relationship.getEnd1());
            updatedEnd1Guid = updatedEnd1Vertex == null ? null : AtlasGraphUtilsV1.getIdFromVertex(updatedEnd1Vertex);
        }
        if (updatedEnd1Guid != null) {
            String end1Guid = AtlasGraphUtilsV1.getIdFromVertex(end1Vertex);
            if (!StringUtils.equalsIgnoreCase(relationship.getEnd1().getGuid(), end1Guid)) {
                throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIP_UPDATE_END_CHANGE_NOT_ALLOWED, edgeType, guid, end1Guid, relationship.getEnd1().getGuid());
            }
        }
    }
    // update shouldn't change ends
    if (relationship.getEnd2() != null) {
        String updatedEnd2Guid = relationship.getEnd2().getGuid();
        if (updatedEnd2Guid == null) {
            AtlasVertex updatedEnd2Vertex = getVertexFromEndPoint(relationship.getEnd2());
            updatedEnd2Guid = updatedEnd2Vertex == null ? null : AtlasGraphUtilsV1.getIdFromVertex(updatedEnd2Vertex);
        }
        if (updatedEnd2Guid != null) {
            String end2Guid = AtlasGraphUtilsV1.getIdFromVertex(end2Vertex);
            if (!StringUtils.equalsIgnoreCase(relationship.getEnd2().getGuid(), end2Guid)) {
                throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIP_UPDATE_END_CHANGE_NOT_ALLOWED, AtlasGraphUtilsV1.getTypeName(edge), guid, end2Guid, relationship.getEnd2().getGuid());
            }
        }
    }
    validateRelationship(end1Vertex, end2Vertex, edgeType, relationship.getAttributes());
    AtlasRelationship ret = updateRelationship(edge, relationship);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== update({}): {}", relationship, ret);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasRelationship(org.apache.atlas.model.instance.AtlasRelationship) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Aggregations

AtlasRelationship (org.apache.atlas.model.instance.AtlasRelationship)13 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)8 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)6 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)5 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)4 AtlasRelationshipType (org.apache.atlas.type.AtlasRelationshipType)2 ArrayList (java.util.ArrayList)1 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)1 AtlasRelatedObjectId (org.apache.atlas.model.instance.AtlasRelatedObjectId)1 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)1 RepositoryException (org.apache.atlas.repository.RepositoryException)1 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)1 EntityNotFoundException (org.apache.atlas.typesystem.exception.EntityNotFoundException)1