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(); } }
@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, this.nameGenerator); 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(); } }
@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, this.nameGenerator); 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(); } }
@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, this.nameGenerator); 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, this.nameGenerator); 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, this.nameGenerator); 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(); } }
@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>(); 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, this.nameGenerator); Query query = visitor.selectWithEntityKey( entityType, entity, updateResponse.getGeneratedKeys(), expands); LogManager.logDetail( LogConstants.CTX_ODATA, null, "created entity = ", entityType.getName(), " with key=", query.getCriteria().toString()); // $NON-NLS-1$ //$NON-NLS-2$ 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 (SQLException e) { throw new ODataApplicationException( e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e); } catch (URISyntaxException 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); } catch (EdmPrimitiveTypeException 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 } } } }