Search in sources :

Example 91 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance in project incubator-atlas by apache.

the class GraphHelper method deserializeClassInstances.

public static ITypedReferenceableInstance[] deserializeClassInstances(TypeSystem typeSystem, String entityInstanceDefinition) throws AtlasException {
    try {
        JSONArray referableInstances = new JSONArray(entityInstanceDefinition);
        ITypedReferenceableInstance[] instances = new ITypedReferenceableInstance[referableInstances.length()];
        for (int index = 0; index < referableInstances.length(); index++) {
            Referenceable entityInstance = InstanceSerialization.fromJsonReferenceable(referableInstances.getString(index), true);
            ITypedReferenceableInstance typedInstrance = getTypedReferenceableInstance(typeSystem, entityInstance);
            instances[index] = typedInstrance;
        }
        return instances;
    } catch (ValueConversionException | TypeNotFoundException e) {
        throw e;
    } catch (Exception e) {
        // exception from deserializer
        LOG.error("Unable to deserialize json={}", entityInstanceDefinition, e);
        throw new IllegalArgumentException("Unable to deserialize json", e);
    }
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) TypeNotFoundException(org.apache.atlas.typesystem.exception.TypeNotFoundException) JSONArray(org.codehaus.jettison.json.JSONArray) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException) RepositoryException(org.apache.atlas.repository.RepositoryException) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) TypeNotFoundException(org.apache.atlas.typesystem.exception.TypeNotFoundException) AtlasException(org.apache.atlas.AtlasException) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException)

Example 92 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance in project incubator-atlas by apache.

the class GraphBackedMetadataRepository method getEntityDefinition.

@Override
@GraphTransaction
public ITypedReferenceableInstance getEntityDefinition(String entityType, String attribute, Object value) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Retrieving entity with type={} and {}={}", entityType, attribute, value);
    }
    IDataType type = typeSystem.getDataType(IDataType.class, entityType);
    String propertyKey = getFieldNameInVertex(type, attribute);
    AtlasVertex instanceVertex = graphHelper.findVertex(propertyKey, value, Constants.ENTITY_TYPE_PROPERTY_KEY, entityType, Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name());
    String guid = GraphHelper.getGuid(instanceVertex);
    ITypedReferenceableInstance cached = RequestContext.get().getInstanceV1(guid);
    if (cached != null) {
        return cached;
    }
    return graphToInstanceMapper.mapGraphToTypedInstance(guid, instanceVertex);
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) IDataType(org.apache.atlas.typesystem.types.IDataType) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 93 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance in project incubator-atlas by apache.

the class DefaultMetadataService method getEntityDefinition.

/**
     * Return the definition for the given guid.
     *
     * @param guid guid
     * @return entity definition as JSON
     */
@Override
public ITypedReferenceableInstance getEntityDefinition(String guid) throws AtlasException {
    guid = ParamChecker.notEmpty(guid, "entity id");
    final ITypedReferenceableInstance instance = repository.getEntityDefinition(guid);
    return instance;
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance)

Example 94 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance in project incubator-atlas by apache.

the class DefaultMetadataService method deleteEntityByUniqueAttribute.

@Override
public EntityResult deleteEntityByUniqueAttribute(String typeName, String uniqueAttributeName, String attrValue) throws AtlasException {
    typeName = ParamChecker.notEmpty(typeName, "delete candidate typeName");
    uniqueAttributeName = ParamChecker.notEmpty(uniqueAttributeName, "delete candidate unique attribute name");
    attrValue = ParamChecker.notEmpty(attrValue, "delete candidate unique attribute value");
    //Throws EntityNotFoundException if the entity could not be found by its unique attribute
    ITypedReferenceableInstance instance = getEntityDefinitionReference(typeName, uniqueAttributeName, attrValue);
    final Id instanceId = instance.getId();
    List<String> deleteCandidateGuids = new ArrayList<String>() {

        {
            add(instanceId._getId());
        }
    };
    return deleteGuids(deleteCandidateGuids);
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) Id(org.apache.atlas.typesystem.persistence.Id)

Example 95 with ITypedReferenceableInstance

use of org.apache.atlas.typesystem.ITypedReferenceableInstance in project incubator-atlas by apache.

the class DefaultMetadataService method validateAndConvertToTypedInstance.

@Override
public ITypedReferenceableInstance validateAndConvertToTypedInstance(IReferenceableInstance updatedEntity, String typeName) throws AtlasException {
    ClassType type = typeSystem.getDataType(ClassType.class, typeName);
    ITypedReferenceableInstance newInstance = type.createInstance(updatedEntity.getId());
    for (String attributeName : updatedEntity.getValuesMap().keySet()) {
        AttributeInfo attributeInfo = type.fieldMapping.fields.get(attributeName);
        if (attributeInfo == null) {
            throw new AtlasException("Invalid property " + attributeName + " for entity " + updatedEntity);
        }
        DataTypes.TypeCategory attrTypeCategory = attributeInfo.dataType().getTypeCategory();
        Object value = updatedEntity.get(attributeName);
        switch(attrTypeCategory) {
            case CLASS:
                if (value != null) {
                    if (value instanceof Referenceable) {
                        newInstance.set(attributeName, value);
                    } else {
                        Id id = new Id((String) value, 0, attributeInfo.dataType().getName());
                        newInstance.set(attributeName, id);
                    }
                }
                break;
            case ENUM:
            case PRIMITIVE:
            case ARRAY:
            case STRUCT:
            case MAP:
                newInstance.set(attributeName, value);
                break;
            case TRAIT:
            //TODO - handle trait updates as well?
            default:
                throw new AtlasException("Update of " + attrTypeCategory + " is not supported");
        }
    }
    return newInstance;
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) JSONObject(org.codehaus.jettison.json.JSONObject) Id(org.apache.atlas.typesystem.persistence.Id) AtlasException(org.apache.atlas.AtlasException)

Aggregations

ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)142 Test (org.testng.annotations.Test)54 List (java.util.List)34 ArrayList (java.util.ArrayList)29 Id (org.apache.atlas.typesystem.persistence.Id)28 Referenceable (org.apache.atlas.typesystem.Referenceable)22 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)21 ImmutableList (com.google.common.collect.ImmutableList)20 ClassType (org.apache.atlas.typesystem.types.ClassType)19 AtlasException (org.apache.atlas.AtlasException)16 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)16 HashMap (java.util.HashMap)15 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)14 EntityResult (org.apache.atlas.model.legacy.EntityResult)12 IStruct (org.apache.atlas.typesystem.IStruct)10 Map (java.util.Map)9 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)9 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)9 RepositoryException (org.apache.atlas.repository.RepositoryException)9 BeforeTest (org.testng.annotations.BeforeTest)9