Search in sources :

Example 1 with ODataApplicationException

use of org.apache.olingo.server.api.ODataApplicationException in project teiid by teiid.

the class TeiidServiceHandler method deleteEntity.

@Override
public void deleteEntity(DataRequest request, String entityETag, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
    // TODO: need to match entityETag.
    checkETag(entityETag);
    UpdateResponse updateResponse = null;
    try {
        ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
        visitor.visit(request.getUriInfo());
        Delete delete = visitor.delete();
        updateResponse = getClient().executeUpdate(delete, 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.writeDeletedEntityOrReference();
    } else {
        // since DELETE is idempotent same response as otherwise success operation.
        response.writeDeletedEntityOrReference();
    }
}
Also used : Delete(org.teiid.query.sql.lang.Delete) UpdateResponse(org.teiid.odata.api.UpdateResponse) SQLException(java.sql.SQLException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException)

Example 2 with ODataApplicationException

use of org.apache.olingo.server.api.ODataApplicationException in project teiid by teiid.

the class TeiidServiceHandler method upsertEntity.

@Override
public void upsertEntity(DataRequest request, Entity entity, boolean merge, String entityETag, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
    final ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, true, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
    visitor.visit(request.getUriInfo());
    final EntityCollectionResponse queryResponse;
    try {
        Query query = visitor.selectQuery();
        queryResponse = (EntityCollectionResponse) executeQuery(request, request.isCountRequest(), visitor, query);
    } catch (Exception e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    if (!queryResponse.getEntities().isEmpty()) {
        updateEntity(request, entity, merge, entityETag, response);
    } else {
        createEntity(request, entity, response);
    }
}
Also used : Query(org.teiid.query.sql.lang.Query) URISyntaxException(java.net.URISyntaxException) TransformationException(org.teiid.core.types.TransformationException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) ODataLibraryException(org.apache.olingo.server.api.ODataLibraryException) TeiidException(org.teiid.core.TeiidException) EdmPrimitiveTypeException(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException) SerializerException(org.apache.olingo.server.api.serializer.SerializerException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException)

Example 3 with ODataApplicationException

use of org.apache.olingo.server.api.ODataApplicationException in project teiid by teiid.

the class TeiidServiceHandler method upsertStreamProperty.

@Override
public void upsertStreamProperty(DataRequest request, String entityETag, InputStream streamContent, NoContentResponse response) throws ODataLibraryException, ODataApplicationException {
    UpdateResponse updateResponse = null;
    EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
    try {
        ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
        visitor.visit(request.getUriInfo());
        Update update = visitor.updateStreamProperty(edmProperty, streamContent);
        updateResponse = getClient().executeUpdate(update, visitor.getParameters());
    } catch (SQLException | TeiidException 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) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) Update(org.teiid.query.sql.lang.Update) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) TeiidException(org.teiid.core.TeiidException)

Example 4 with ODataApplicationException

use of org.apache.olingo.server.api.ODataApplicationException in project teiid by teiid.

the class TeiidServiceHandler method updateProperty.

/**
 * since Teiid only deals with primitive types, merge does not apply
 */
@Override
public void updateProperty(DataRequest request, Property property, boolean rawValue, boolean merge, String entityETag, PropertyResponse response) throws ODataLibraryException, ODataApplicationException {
    // TODO: need to match entityETag.
    checkETag(entityETag);
    UpdateResponse updateResponse = null;
    EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
    try {
        ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
        visitor.visit(request.getUriInfo());
        Update update = visitor.updateProperty(edmProperty, property, this.prepared, rawValue);
        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);
    }
    if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
        response.writePropertyUpdated();
    } else {
        response.writeNotModified();
    }
}
Also used : UpdateResponse(org.teiid.odata.api.UpdateResponse) SQLException(java.sql.SQLException) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) Update(org.teiid.query.sql.lang.Update) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) TeiidException(org.teiid.core.TeiidException)

Example 5 with ODataApplicationException

use of org.apache.olingo.server.api.ODataApplicationException 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)

Aggregations

ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)12 SQLException (java.sql.SQLException)10 TeiidException (org.teiid.core.TeiidException)6 UpdateResponse (org.teiid.odata.api.UpdateResponse)6 URISyntaxException (java.net.URISyntaxException)5 Query (org.teiid.query.sql.lang.Query)5 EdmProperty (org.apache.olingo.commons.api.edm.EdmProperty)4 ODataLibraryException (org.apache.olingo.server.api.ODataLibraryException)4 Update (org.teiid.query.sql.lang.Update)4 MalformedURLException (java.net.MalformedURLException)3 URI (java.net.URI)3 EdmPrimitiveTypeException (org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException)3 Property (org.apache.olingo.commons.api.data.Property)2 EdmEntityType (org.apache.olingo.commons.api.edm.EdmEntityType)2 EdmNavigationProperty (org.apache.olingo.commons.api.edm.EdmNavigationProperty)2 SerializerException (org.apache.olingo.server.api.serializer.SerializerException)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)2 TransformationException (org.teiid.core.types.TransformationException)2 BaseResponse (org.teiid.odata.api.BaseResponse)2