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