Search in sources :

Example 16 with AtlasAttribute

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.

the class EntityGraphMapper method mapMapValue.

private Map<String, Object> mapMapValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapMapValue({})", ctx);
    }
    @SuppressWarnings("unchecked") Map<Object, Object> newVal = (Map<Object, Object>) ctx.getValue();
    Map<String, Object> newMap = new HashMap<>();
    AtlasMapType mapType = (AtlasMapType) ctx.getAttrType();
    try {
        AtlasAttribute attribute = ctx.getAttribute();
        List<String> currentKeys = GraphHelper.getListProperty(ctx.getReferringVertex(), ctx.getVertexProperty());
        Map<String, Object> currentMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(currentKeys)) {
            for (String key : currentKeys) {
                String propertyNameForKey = GraphHelper.getQualifiedNameForMapKey(ctx.getVertexProperty(), GraphHelper.encodePropertyKey(key));
                Object propertyValueForKey = getMapValueProperty(mapType.getValueType(), ctx.getReferringVertex(), propertyNameForKey);
                currentMap.put(key, propertyValueForKey);
            }
        }
        if (MapUtils.isNotEmpty(newVal)) {
            boolean isReference = AtlasGraphUtilsV1.isReference(mapType.getValueType());
            AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
            for (Map.Entry<Object, Object> entry : newVal.entrySet()) {
                String key = entry.getKey().toString();
                String propertyName = GraphHelper.getQualifiedNameForMapKey(ctx.getVertexProperty(), GraphHelper.encodePropertyKey(key));
                AtlasEdge existingEdge = getEdgeIfExists(mapType, currentMap, key);
                AttributeMutationContext mapCtx = new AttributeMutationContext(ctx.getOp(), ctx.getReferringVertex(), attribute, entry.getValue(), propertyName, mapType.getValueType(), existingEdge);
                //Add/Update/Remove property value
                Object newEntry = mapCollectionElementsToVertex(mapCtx, context);
                setMapValueProperty(mapType.getValueType(), ctx.getReferringVertex(), propertyName, newEntry);
                newMap.put(key, newEntry);
                // update the inverse reference value.
                if (isReference && newEntry instanceof AtlasEdge && inverseRefAttribute != null) {
                    AtlasEdge newEdge = (AtlasEdge) newEntry;
                    addInverseReference(mapCtx, inverseRefAttribute, newEdge);
                }
            }
        }
        Map<String, Object> finalMap = removeUnusedMapEntries(attribute, ctx.getReferringVertex(), ctx.getVertexProperty(), currentMap, newMap);
        for (Object newEntry : newMap.values()) {
            updateInConsistentOwnedMapVertices(ctx, mapType, newEntry);
        }
        Set<String> newKeys = new LinkedHashSet<>(newMap.keySet());
        newKeys.addAll(finalMap.keySet());
        // for dereference on way out
        GraphHelper.setListProperty(ctx.getReferringVertex(), ctx.getVertexProperty(), new ArrayList<>(newKeys));
    } catch (AtlasException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapMapValue({})", ctx);
    }
    return newMap;
}
Also used : AtlasException(org.apache.atlas.AtlasException) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasMapType(org.apache.atlas.type.AtlasMapType) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException)

Example 17 with AtlasAttribute

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.

the class AtlasStructType method createDefaultValue.

private Object createDefaultValue(AtlasAttributeDef attributeDef) {
    Object ret = null;
    if (attributeDef != null) {
        AtlasAttribute attribute = allAttributes.get(attributeDef.getName());
        if (attribute != null) {
            AtlasType dataType = attribute.getAttributeType();
            ret = dataType.createDefaultValue();
        }
    }
    return ret;
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute)

Example 18 with AtlasAttribute

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.

the class AtlasStructType method validateValueForUpdate.

@Override
public boolean validateValueForUpdate(Object obj, String objName, List<String> messages) {
    boolean ret = true;
    Map<String, Object> attributes = null;
    if (obj != null) {
        if (obj instanceof AtlasStruct) {
            AtlasStruct structObj = (AtlasStruct) obj;
            attributes = structObj.getAttributes();
        } else if (obj instanceof Map) {
            attributes = AtlasTypeUtil.toStructAttributes((Map) obj);
        } else {
            ret = false;
            messages.add(objName + "=" + obj + ": invalid value for type " + getTypeName());
        }
        if (MapUtils.isNotEmpty(attributes)) {
            for (Map.Entry<String, Object> e : attributes.entrySet()) {
                String attrName = e.getKey();
                Object attrValue = e.getValue();
                AtlasAttribute attribute = allAttributes.get(attrName);
                if (attrValue == null) {
                    continue;
                }
                if (attribute != null) {
                    AtlasType dataType = attribute.getAttributeType();
                    String fieldName = objName + "." + attrName;
                    ret = dataType.validateValueForUpdate(attrValue, fieldName, messages) && ret;
                }
            }
        }
    }
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with AtlasAttribute

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-atlas by apache.

the class AtlasStructFormatConverter method fromV2ToV1.

protected Map<String, Object> fromV2ToV1(AtlasStructType structType, Map<String, Object> attributes, ConverterContext context) throws AtlasBaseException {
    Map<String, Object> ret = null;
    if (MapUtils.isNotEmpty(attributes)) {
        ret = new HashMap<>();
        // Only process the requested/set attributes
        for (String attrName : attributes.keySet()) {
            AtlasAttribute attr = structType.getAttribute(attrName);
            if (attr == null) {
                LOG.warn("ignored unknown attribute {}.{}", structType.getTypeName(), attrName);
                continue;
            }
            AtlasType attrType = attr.getAttributeType();
            Object v2Value = attributes.get(attr.getName());
            Object v1Value;
            AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory());
            v1Value = attrConverter.fromV2ToV1(v2Value, attrType, context);
            ret.put(attr.getName(), v1Value);
        }
    }
    return ret;
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasType(org.apache.atlas.type.AtlasType)

Example 20 with AtlasAttribute

use of org.apache.atlas.type.AtlasStructType.AtlasAttribute in project incubator-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);
    }
    AtlasEntityWithExtInfo entityInfo = getById(guid);
    if (entityInfo == null || entityInfo.getEntity() == null) {
        throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
    }
    AtlasEntity entity = entityInfo.getEntity();
    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);
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Aggregations

AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)21 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)6 AtlasType (org.apache.atlas.type.AtlasType)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)3 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)3 AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)3 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)3 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)3 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)3 List (java.util.List)2 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)2 AtlasTypeDefGraphStoreV1 (org.apache.atlas.repository.store.graph.v1.AtlasTypeDefGraphStoreV1)2 AtlasStructType (org.apache.atlas.type.AtlasStructType)2 ImmutableList (com.google.common.collect.ImmutableList)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptException (javax.script.ScriptException)1 AtlasException (org.apache.atlas.AtlasException)1