示例#1
0
  @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();
    }
  }