Search in sources :

Example 1 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class GraphBackedMetadataRepository method updatePartial.

@Override
@GraphTransaction
public CreateUpdateEntitiesResult updatePartial(ITypedReferenceableInstance entity) throws RepositoryException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("updating entity {}", entity);
    }
    try {
        TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler);
        instanceToGraphMapper.mapTypedInstanceToGraph(TypedInstanceToGraphMapper.Operation.UPDATE_PARTIAL, entity);
        RequestContext requestContext = RequestContext.get();
        CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult();
        GuidMapping mapping = instanceToGraphMapper.createGuidMapping();
        result.setEntityResult(createEntityResultFromContext(requestContext));
        result.setGuidMapping(mapping);
        return result;
    } catch (AtlasException e) {
        throw new RepositoryException(e);
    }
}
Also used : CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) RepositoryException(org.apache.atlas.repository.RepositoryException) RequestContext(org.apache.atlas.RequestContext) AtlasException(org.apache.atlas.AtlasException) GuidMapping(org.apache.atlas.model.instance.GuidMapping) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 2 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class GraphBackedMetadataRepository method updateEntities.

@Override
@GraphTransaction
public CreateUpdateEntitiesResult updateEntities(ITypedReferenceableInstance... entitiesUpdated) throws RepositoryException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("updating entity {}", entitiesUpdated);
    }
    try {
        TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler);
        instanceToGraphMapper.mapTypedInstanceToGraph(TypedInstanceToGraphMapper.Operation.UPDATE_FULL, entitiesUpdated);
        CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult();
        RequestContext requestContext = RequestContext.get();
        result.setEntityResult(createEntityResultFromContext(requestContext));
        GuidMapping mapping = instanceToGraphMapper.createGuidMapping();
        result.setGuidMapping(mapping);
        return result;
    } catch (AtlasException e) {
        throw new RepositoryException(e);
    }
}
Also used : CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) RepositoryException(org.apache.atlas.repository.RepositoryException) RequestContext(org.apache.atlas.RequestContext) AtlasException(org.apache.atlas.AtlasException) GuidMapping(org.apache.atlas.model.instance.GuidMapping) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 3 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryTest method testCreateEntityWithTwoNestingLevels.

@Test
public void testCreateEntityWithTwoNestingLevels() throws AtlasException {
    List<Referenceable> toVerify = new ArrayList<>();
    Referenceable dept = new Referenceable(TestUtils.DEPARTMENT_TYPE);
    toVerify.add(dept);
    dept.set(TestUtils.NAME, "test2");
    Referenceable wallace = new Referenceable(TestUtils.PERSON_TYPE);
    toVerify.add(wallace);
    wallace.set(TestUtils.NAME, "Wallace");
    wallace.set(TestUtils.DEPARTMENT_ATTR, dept);
    Referenceable wallaceComputer = new Referenceable(TestUtils.ASSET_TYPE);
    toVerify.add(wallaceComputer);
    wallaceComputer.set("name", "wallaceComputer");
    wallace.set(TestUtils.ASSETS_ATTR, ImmutableList.of(wallaceComputer));
    Referenceable jordan = new Referenceable(TestUtils.PERSON_TYPE);
    toVerify.add(jordan);
    jordan.set(TestUtils.NAME, "Jordan");
    jordan.set(TestUtils.DEPARTMENT_ATTR, dept);
    Referenceable jordanComputer = new Referenceable(TestUtils.ASSET_TYPE);
    toVerify.add(jordanComputer);
    jordanComputer.set("name", "jordanComputer");
    jordan.set(TestUtils.ASSETS_ATTR, ImmutableList.of(jordanComputer));
    dept.set(TestUtils.EMPLOYEES_ATTR, ImmutableList.of(wallace, jordan));
    Map<String, Referenceable> positions = new HashMap<>();
    final String JANITOR = "janitor";
    final String RECEPTIONIST = "receptionist";
    positions.put(JANITOR, wallace);
    positions.put(RECEPTIONIST, jordan);
    dept.set(TestUtils.POSITIONS_ATTR, positions);
    ClassType deptType = TypeSystem.getInstance().getDataType(ClassType.class, TestUtils.DEPARTMENT_TYPE);
    ITypedReferenceableInstance deptInstance = deptType.convert(dept, Multiplicity.REQUIRED);
    CreateUpdateEntitiesResult result = repositoryService.createEntities(deptInstance);
    validateGuidMapping(toVerify, result);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) HashMap(java.util.HashMap) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) ArrayList(java.util.ArrayList) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test)

Example 4 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class EntityResource method partialUpdateEntityAttrByGuid.

/**
     * Supports Partial updates
     * Adds/Updates given entity specified by its GUID
     * Supports updation of only simple primitive attributes like strings, ints, floats, enums, class references and
     * does not support updation of complex types like arrays, maps
     * @param guid entity id
     * @param property property to add
     * @postbody property's value
     * @return response payload as json
     */
private Response partialUpdateEntityAttrByGuid(String guid, String property, HttpServletRequest request) {
    String value = null;
    try {
        Preconditions.checkNotNull(property, "Entity property cannot be null");
        value = Servlets.getRequestPayload(request);
        Preconditions.checkNotNull(value, "Entity value cannot be null");
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updating entity {} for property {} = {}", guid, property, value);
        }
        EntityMutationResponse mutationResponse = entitiesStore.updateEntityAttributeByGuid(guid, property, value);
        CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updated entities: {}", result.getEntityResult());
        }
        JSONObject response = getResponse(result);
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to add property {} to entity id {} {} ", property, guid, value, e);
        throw toWebApplicationException(e);
    } catch (EntityNotFoundException e) {
        LOG.error("An entity with GUID={} does not exist {} ", guid, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to add property {} to entity id {} {} ", property, guid, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to add property {} to entity id {} {} ", property, guid, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to add property {} to entity id {} {} ", property, guid, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasException(org.apache.atlas.AtlasException)

Example 5 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class EntityResource method partialUpdateEntityByGuid.

private Response partialUpdateEntityByGuid(String guid, HttpServletRequest request) {
    String entityJson = null;
    try {
        guid = ParamChecker.notEmpty(guid, "Guid property cannot be null");
        entityJson = Servlets.getRequestPayload(request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("partially updating entity for guid {} : {} ", guid, entityJson);
        }
        Referenceable updatedEntity = InstanceSerialization.fromJsonReferenceable(entityJson, true);
        // update referenceable with Id if not specified in payload
        Id updateId = updatedEntity.getId();
        if (updateId != null && !updateId.isAssigned()) {
            updatedEntity.replaceWithNewId(new Id(guid, 0, updatedEntity.getTypeName()));
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntity(updatedEntity);
        EntityMutationResponse mutationResponse = entitiesStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), true);
        CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updated entities: {}", result.getEntityResult());
        }
        JSONObject response = getResponse(result);
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to update entity by GUID {} {} ", guid, entityJson, e);
        throw toWebApplicationException(e);
    } catch (EntityNotFoundException e) {
        LOG.error("An entity with GUID={} does not exist {} ", guid, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to update entity by GUID {} {}", guid, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to update entity by GUID {} {} ", guid, entityJson, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to update entity by GUID {} {} ", guid, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
}
Also used : EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasException(org.apache.atlas.AtlasException) AtlasEntityStream(org.apache.atlas.repository.store.graph.v1.AtlasEntityStream) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.typesystem.Referenceable) JSONObject(org.codehaus.jettison.json.JSONObject) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) Id(org.apache.atlas.typesystem.persistence.Id)

Aggregations

CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)22 AtlasException (org.apache.atlas.AtlasException)9 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)9 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)5 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)5 Referenceable (org.apache.atlas.typesystem.Referenceable)5 JSONObject (org.codehaus.jettison.json.JSONObject)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)4 GuidMapping (org.apache.atlas.model.instance.GuidMapping)4 EntityExistsException (org.apache.atlas.typesystem.exception.EntityExistsException)4 Id (org.apache.atlas.typesystem.persistence.Id)4 Test (org.testng.annotations.Test)4 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)3 RepositoryException (org.apache.atlas.repository.RepositoryException)3 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)3 EntityNotFoundException (org.apache.atlas.typesystem.exception.EntityNotFoundException)3 ReferenceableInstance (org.apache.atlas.typesystem.persistence.ReferenceableInstance)3 ClassType (org.apache.atlas.typesystem.types.ClassType)3