Search in sources :

Example 6 with AtlasAttribute

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

the class TypeConverterUtil method getAttributes.

private static AttributeDefinition[] getAttributes(AtlasStructType structType, AtlasTypeRegistry registry) throws AtlasBaseException {
    List<AttributeDefinition> ret = new ArrayList<>();
    List<AtlasAttributeDef> attrDefs = structType.getStructDef().getAttributeDefs();
    if (CollectionUtils.isNotEmpty(attrDefs)) {
        for (AtlasAttributeDef attrDef : attrDefs) {
            AtlasAttribute attribute = structType.getAttribute(attrDef.getName());
            ret.add(AtlasStructDefStoreV1.toAttributeDefintion(attribute));
        }
    }
    return ret.toArray(new AttributeDefinition[ret.size()]);
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) ArrayList(java.util.ArrayList) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition)

Example 7 with AtlasAttribute

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

the class AtlasEntityGraphDiscoveryV1 method visitStruct.

void visitStruct(AtlasStructType structType, Object val) throws AtlasBaseException {
    if (structType == null || val == null) {
        return;
    }
    AtlasStruct struct;
    if (val instanceof AtlasStruct) {
        struct = (AtlasStruct) val;
    } else if (val instanceof Map) {
        Map attributes = AtlasTypeUtil.toStructAttributes((Map) val);
        struct = new AtlasStruct(structType.getTypeName(), attributes);
    } else {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_STRUCT_VALUE, val.toString());
    }
    for (AtlasAttribute attribute : structType.getAllAttributes().values()) {
        AtlasType attrType = attribute.getAttributeType();
        Object attrVal = struct.getAttribute(attribute.getName());
        visitAttribute(attrType, attrVal);
    }
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType) Map(java.util.Map)

Example 8 with AtlasAttribute

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

the class RestUtilsTest method convertToJsonAndBack.

private AtlasAttributeDef convertToJsonAndBack(AtlasTypeRegistry registry, AtlasStructDef structDef, AtlasAttributeDef attributeDef, boolean compositeExpected) throws AtlasBaseException {
    AtlasTypeDefGraphStoreV1 typeDefStore = makeTypeStore(registry);
    AtlasStructType structType = (AtlasStructType) registry.getType(structDef.getName());
    AtlasAttribute attribute = structType.getAttribute(attributeDef.getName());
    String attribJson = AtlasStructDefStoreV1.toJsonFromAttribute(attribute);
    Map attrInfo = AtlasType.fromJson(attribJson, Map.class);
    Assert.assertEquals(attrInfo.get("isComposite"), compositeExpected);
    return AtlasStructDefStoreV1.toAttributeDefFromJson(structDef, attrInfo, typeDefStore);
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasTypeDefGraphStoreV1(org.apache.atlas.repository.store.graph.v1.AtlasTypeDefGraphStoreV1) AtlasStructType(org.apache.atlas.type.AtlasStructType) Map(java.util.Map)

Example 9 with AtlasAttribute

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

the class AtlasStructType method validateValue.

@Override
public boolean validateValue(Object obj, String objName, List<String> messages) {
    boolean ret = true;
    if (obj != null) {
        if (obj instanceof AtlasStruct) {
            AtlasStruct structObj = (AtlasStruct) obj;
            for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
                String attrName = attributeDef.getName();
                AtlasAttribute attribute = allAttributes.get(attributeDef.getName());
                if (attribute != null) {
                    AtlasType dataType = attribute.getAttributeType();
                    Object value = structObj.getAttribute(attrName);
                    String fieldName = objName + "." + attrName;
                    if (value != null) {
                        ret = dataType.validateValue(value, fieldName, messages) && ret;
                    } else if (!attributeDef.getIsOptional()) {
                        ret = false;
                        messages.add(fieldName + ": mandatory attribute value missing in type " + getTypeName());
                    }
                }
            }
        } else if (obj instanceof Map) {
            Map attributes = AtlasTypeUtil.toStructAttributes((Map) obj);
            for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
                String attrName = attributeDef.getName();
                AtlasAttribute attribute = allAttributes.get(attributeDef.getName());
                if (attribute != null) {
                    AtlasType dataType = attribute.getAttributeType();
                    Object value = attributes.get(attrName);
                    String fieldName = objName + "." + attrName;
                    if (value != null) {
                        ret = dataType.validateValue(value, fieldName, messages) && ret;
                    } else if (!attributeDef.getIsOptional()) {
                        ret = false;
                        messages.add(fieldName + ": mandatory attribute value missing in type " + getTypeName());
                    }
                }
            }
        } else {
            ret = false;
            messages.add(objName + "=" + obj + ": invalid value for type " + getTypeName());
        }
    }
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with AtlasAttribute

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

the class AtlasStructType method resolveReferencesPhase2.

@Override
public void resolveReferencesPhase2(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
    super.resolveReferencesPhase2(typeRegistry);
    for (AtlasAttribute attribute : allAttributes.values()) {
        if (attribute.getInverseRefAttributeName() == null) {
            continue;
        }
        // Set the inverse reference attribute.
        AtlasType referencedType = typeRegistry.getType(attribute.getAttributeDef().getTypeName());
        AtlasEntityType referencedEntityType = getReferencedEntityType(referencedType);
        AtlasAttribute inverseReference = referencedEntityType.getAttribute(attribute.getInverseRefAttributeName());
        attribute.setInverseRefAttribute(inverseReference);
    }
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute)

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