Search in sources :

Example 61 with AtlasType

use of org.apache.atlas.type.AtlasType in project atlas by apache.

the class AtlasEntityStoreV1 method updateEntityAttributeByGuid.

@Override
@GraphTransaction
public EntityMutationResponse updateEntityAttributeByGuid(String guid, String attrName, Object attrValue) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> updateEntityAttributeByGuid({}, {}, {})", guid, attrName, attrValue);
    }
    AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeaderWithClassifications(guid);
    AtlasEntityType entityType = (AtlasEntityType) typeRegistry.getType(entity.getTypeName());
    AtlasAttribute attr = entityType.getAttribute(attrName);
    if (attr == null) {
        throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_ATTRIBUTE, attrName, entity.getTypeName());
    }
    AtlasType attrType = attr.getAttributeType();
    AtlasEntity updateEntity = new AtlasEntity();
    updateEntity.setGuid(guid);
    updateEntity.setTypeName(entity.getTypeName());
    switch(attrType.getTypeCategory()) {
        case PRIMITIVE:
            updateEntity.setAttribute(attrName, attrValue);
            break;
        case OBJECT_ID_TYPE:
            AtlasObjectId objId;
            if (attrValue instanceof String) {
                objId = new AtlasObjectId((String) attrValue, attr.getAttributeDef().getTypeName());
            } else {
                objId = (AtlasObjectId) attrType.getNormalizedValue(attrValue);
            }
            updateEntity.setAttribute(attrName, objId);
            break;
        default:
            throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UPDATE_NOT_SUPPORTED, attrName, attrType.getTypeName());
    }
    return createOrUpdate(new AtlasEntityStream(updateEntity), true, false);
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 62 with AtlasType

use of org.apache.atlas.type.AtlasType in project atlas by apache.

the class EntityGraphMapper method createInverseReferenceUsingRelationship.

private AtlasEdge createInverseReferenceUsingRelationship(AtlasAttribute inverseAttribute, AtlasEdge edge, Map<String, Object> relationshipAttributes) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> createInverseReferenceUsingRelationship()");
    }
    String inverseAttributeName = inverseAttribute.getName();
    AtlasType inverseAttributeType = inverseAttribute.getDefinedInType();
    AtlasVertex inverseVertex = edge.getInVertex();
    AtlasVertex vertex = edge.getOutVertex();
    AtlasEdge ret;
    if (inverseAttributeType instanceof AtlasEntityType) {
        AtlasEntityType entityType = (AtlasEntityType) inverseAttributeType;
        if (entityType.hasRelationshipAttribute(inverseAttributeName)) {
            String relationshipName = graphHelper.getRelationshipDefName(inverseVertex, entityType, inverseAttributeName);
            ret = getOrCreateRelationship(inverseVertex, vertex, relationshipName, relationshipAttributes);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("No RelationshipDef defined between {} and {} on attribute: {}", inverseAttributeType, AtlasGraphUtilsV1.getTypeName(vertex), inverseAttributeName);
            }
            // if no RelationshipDef found, use legacy way to create edges
            ret = createInverseReference(inverseAttribute, (AtlasStructType) inverseAttributeType, inverseVertex, vertex);
        }
    } else {
        // inverseAttribute not of type AtlasEntityType, use legacy way to create edges
        ret = createInverseReference(inverseAttribute, (AtlasStructType) inverseAttributeType, inverseVertex, vertex);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== createInverseReferenceUsingRelationship()");
    }
    return ret;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasType(org.apache.atlas.type.AtlasType) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 63 with AtlasType

use of org.apache.atlas.type.AtlasType in project atlas by apache.

the class EntityGraphMapper method mapAttribute.

private void mapAttribute(AtlasAttribute attribute, Object attrValue, AtlasVertex vertex, EntityOperation op, EntityMutationContext context) throws AtlasBaseException {
    if (attrValue == null) {
        AtlasAttributeDef attributeDef = attribute.getAttributeDef();
        AtlasType attrType = attribute.getAttributeType();
        if (attrType.getTypeCategory() == TypeCategory.PRIMITIVE) {
            if (attributeDef.getDefaultValue() != null) {
                attrValue = attrType.createDefaultValue(attributeDef.getDefaultValue());
            } else {
                if (attribute.getAttributeDef().getIsOptional()) {
                    attrValue = attrType.createOptionalDefaultValue();
                } else {
                    attrValue = attrType.createDefaultValue();
                }
            }
        }
    }
    AttributeMutationContext ctx = new AttributeMutationContext(op, vertex, attribute, attrValue);
    mapToVertexByTypeCategory(ctx, context);
}
Also used : AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) AtlasType(org.apache.atlas.type.AtlasType)

Example 64 with AtlasType

use of org.apache.atlas.type.AtlasType in project atlas by apache.

the class AtlasMapFormatConverter method isValidValueV1.

@Override
public boolean isValidValueV1(Object v1Obj, AtlasType type) {
    boolean ret = false;
    if (v1Obj == null) {
        return true;
    }
    if (type instanceof AtlasMapType && v1Obj instanceof Map) {
        AtlasMapType mapType = (AtlasMapType) type;
        AtlasType keyType = mapType.getKeyType();
        AtlasType valueType = mapType.getValueType();
        AtlasFormatConverter keyConverter = null;
        AtlasFormatConverter valueConverter = null;
        Map v1Map = (Map) v1Obj;
        try {
            keyConverter = converterRegistry.getConverter(keyType.getTypeCategory());
            valueConverter = converterRegistry.getConverter(valueType.getTypeCategory());
        } catch (AtlasBaseException excp) {
            LOG.warn("failed to get key/value converter. type={}", type.getTypeName(), excp);
            ret = false;
        }
        if (keyConverter != null && valueConverter != null) {
            // for empty map
            ret = true;
            for (Object key : v1Map.keySet()) {
                Object value = v1Map.get(key);
                ret = keyConverter.isValidValueV1(key, keyType) && valueConverter.isValidValueV1(value, valueType);
                if (!ret) {
                    break;
                }
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("AtlasArrayFormatConverter.isValidValueV1(type={}, value={}): {}", (v1Obj != null ? v1Obj.getClass().getCanonicalName() : null), v1Obj, ret);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType) Map(java.util.Map) HashMap(java.util.HashMap) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 65 with AtlasType

use of org.apache.atlas.type.AtlasType in project atlas by apache.

the class AtlasMapFormatConverter method fromV2ToV1.

@Override
public Map fromV2ToV1(Object v2Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException {
    Map ret = null;
    if (v2Obj != null) {
        if (v2Obj instanceof Map) {
            AtlasMapType mapType = (AtlasMapType) type;
            AtlasType keyType = mapType.getKeyType();
            AtlasType valueType = mapType.getValueType();
            AtlasFormatConverter keyConverter = converterRegistry.getConverter(keyType.getTypeCategory());
            AtlasFormatConverter valueConverter = converterRegistry.getConverter(valueType.getTypeCategory());
            Map v2Map = (Map) v2Obj;
            ret = new HashMap<>();
            for (Object key : v2Map.keySet()) {
                Object value = v2Map.get(key);
                Object v2Key = keyConverter.fromV2ToV1(key, keyType, ctx);
                Object v2Value = valueConverter.fromV2ToV1(value, valueType, ctx);
                ret.put(v2Key, v2Value);
            }
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map", v2Obj.getClass().getCanonicalName());
        }
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType) Map(java.util.Map) HashMap(java.util.HashMap) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Aggregations

AtlasType (org.apache.atlas.type.AtlasType)95 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)51 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)33 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)23 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)17 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)17 AtlasStructType (org.apache.atlas.type.AtlasStructType)16 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)15 AtlasMapType (org.apache.atlas.type.AtlasMapType)13 ArrayList (java.util.ArrayList)11 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)9 HashMap (java.util.HashMap)8 AtlasTypeAccessRequest (org.apache.atlas.authorize.AtlasTypeAccessRequest)8 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)8 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)8 Map (java.util.Map)7 List (java.util.List)6 Collection (java.util.Collection)5 LinkedHashSet (java.util.LinkedHashSet)5 Set (java.util.Set)4