Search in sources :

Example 6 with UpdateResponse

use of org.teiid.odata.api.UpdateResponse in project teiid by teiid.

the class TeiidServiceHandler method createEntity.

@Override
public void createEntity(DataRequest request, Entity entity, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
    EdmEntityType entityType = request.getEntitySet().getEntityType();
    String txn;
    try {
        txn = getClient().startTransaction();
    } catch (SQLException e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    boolean success = false;
    try {
        List<ExpandNode> expands = new ArrayList<TeiidServiceHandler.ExpandNode>();
        int insertDepth = insertDepth(entityType, entity);
        // don't count the root
        ODataSQLBuilder.checkExpandLevel(insertDepth - 1);
        UpdateResponse updateResponse = performDeepInsert(request.getODataRequest().getRawBaseUri(), request.getUriInfo(), entityType, entity, expands);
        if (updateResponse != null && updateResponse.getUpdateCount() == 1) {
            ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), true, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            Query query = visitor.selectWithEntityKey(entityType, entity, updateResponse.getGeneratedKeys(), expands);
            // $NON-NLS-1$ //$NON-NLS-2$
            LogManager.logDetail(LogConstants.CTX_ODATA, null, "created entity = ", entityType.getName(), " with key=", query.getCriteria().toString());
            EntityCollectionResponse result = new EntityCollectionResponse(request.getODataRequest().getRawBaseUri(), visitor.getContext());
            getClient().executeSQL(query, visitor.getParameters(), false, null, null, null, 1, result);
            if (!result.getEntities().isEmpty()) {
                entity = result.getEntities().get(0);
                String location = EntityResponse.buildLocation(request.getODataRequest().getRawBaseUri(), entity, request.getEntitySet().getName(), entityType);
                entity.setId(new URI(location));
            }
            response.writeCreatedEntity(request.getEntitySet(), entity);
        } else {
            response.writeNotModified();
        }
        getClient().commit(txn);
        success = true;
    } catch (EdmPrimitiveTypeException | TeiidException | SQLException | URISyntaxException e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    } finally {
        if (!success) {
            try {
                getClient().rollback(txn);
            } catch (SQLException e1) {
            // ignore
            }
        }
    }
}
Also used : Query(org.teiid.query.sql.lang.Query) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) EdmPrimitiveTypeException(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) TeiidException(org.teiid.core.TeiidException) UpdateResponse(org.teiid.odata.api.UpdateResponse)

Example 7 with UpdateResponse

use of org.teiid.odata.api.UpdateResponse in project teiid by teiid.

the class TeiidServiceHandler method updateEntity.

@Override
public void updateEntity(DataRequest request, Entity entity, boolean merge, String entityETag, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
    // TODO: need to match entityETag.
    checkETag(entityETag);
    UpdateResponse updateResponse = null;
    if (merge) {
        try {
            ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            visitor.visit(request.getUriInfo());
            EdmEntityType entityType = request.getEntitySet().getEntityType();
            Update update = visitor.update(entityType, entity, this.prepared);
            updateResponse = getClient().executeUpdate(update, visitor.getParameters());
        } catch (SQLException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } catch (TeiidException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        }
    } else {
        // delete, then insert
        String txn = startTransaction();
        boolean success = false;
        try {
            // build insert first as it could fail to validate
            ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            visitor.visit(request.getUriInfo());
            EdmEntityType entityType = request.getEntitySet().getEntityType();
            List<UriParameter> keys = request.getKeyPredicates();
            Insert command = visitor.insert(entityType, entity, keys, this.prepared);
            // run delete
            ODataSQLBuilder deleteVisitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            deleteVisitor.visit(request.getUriInfo());
            Delete delete = deleteVisitor.delete();
            updateResponse = getClient().executeUpdate(delete, deleteVisitor.getParameters());
            // run insert
            updateResponse = getClient().executeUpdate(command, visitor.getParameters());
            commit(txn);
            success = true;
        } catch (SQLException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } catch (TeiidException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } finally {
            if (!success) {
                rollback(txn);
            }
        }
    }
    if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
        response.writeUpdatedEntity();
    } else {
        response.writeNotModified();
    }
}
Also used : Delete(org.teiid.query.sql.lang.Delete) SQLException(java.sql.SQLException) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) Update(org.teiid.query.sql.lang.Update) Insert(org.teiid.query.sql.lang.Insert) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) TeiidException(org.teiid.core.TeiidException) UpdateResponse(org.teiid.odata.api.UpdateResponse) UriParameter(org.apache.olingo.server.api.uri.UriParameter)

Example 8 with UpdateResponse

use of org.teiid.odata.api.UpdateResponse in project teiid by teiid.

the class TeiidServiceHandler method manageReference.

private void manageReference(DataRequest request, URI referenceId, NoContentResponse response, boolean delete) throws ODataApplicationException {
    UpdateResponse updateResponse = null;
    try {
        ReferenceUpdateSQLBuilder visitor = new ReferenceUpdateSQLBuilder(getClient().getMetadataStore(), request.getODataRequest().getRawBaseUri(), this.serviceMetadata, this.odata);
        visitor.visit(request.getUriInfo());
        Update update = visitor.updateReference(referenceId, this.prepared, delete);
        updateResponse = getClient().executeUpdate(update, visitor.getParameters());
    } catch (SQLException e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
        response.writeNoContent();
    } else {
        response.writeNotModified();
    }
}
Also used : UpdateResponse(org.teiid.odata.api.UpdateResponse) SQLException(java.sql.SQLException) Update(org.teiid.query.sql.lang.Update) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException)

Aggregations

UpdateResponse (org.teiid.odata.api.UpdateResponse)8 SQLException (java.sql.SQLException)6 ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)6 TeiidException (org.teiid.core.TeiidException)4 Update (org.teiid.query.sql.lang.Update)4 EdmEntityType (org.apache.olingo.commons.api.edm.EdmEntityType)2 EdmProperty (org.apache.olingo.commons.api.edm.EdmProperty)2 Delete (org.teiid.query.sql.lang.Delete)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Entity (org.apache.olingo.commons.api.data.Entity)1 Link (org.apache.olingo.commons.api.data.Link)1 EdmNavigationProperty (org.apache.olingo.commons.api.edm.EdmNavigationProperty)1 EdmPrimitiveTypeException (org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException)1 UriParameter (org.apache.olingo.server.api.uri.UriParameter)1 PreparedStatementImpl (org.teiid.jdbc.PreparedStatementImpl)1 CacheHint (org.teiid.query.sql.lang.CacheHint)1 Insert (org.teiid.query.sql.lang.Insert)1 Query (org.teiid.query.sql.lang.Query)1