Search in sources :

Example 31 with Criterion

use of org.folio.rest.persist.Criteria.Criterion in project raml-module-builder by folio-org.

the class JobAPI method putJobsJobconfsByJobconfsIdJobsByJobId.

@Validate
@Override
public void putJobsJobconfsByJobconfsIdJobsByJobId(String jobId, String jobconfsId, String lang, Job entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    System.out.println("sending... putJobsJobconfsByJobconfsId");
    try {
        Criteria c = new Criteria();
        c.addField("_id");
        c.setOperation("=");
        c.setValue(jobId);
        c.setJSONB(false);
        Criteria d = new Criteria();
        d.addField("job_conf_id");
        d.setOperation("=");
        d.setValue(jobconfsId);
        d.setJSONB(false);
        vertxContext.runOnContext(v -> {
            try {
                PostgresClient.getInstance(vertxContext.owner()).update(RTFConsts.JOBS_COLLECTION, entity, new Criterion().addCriterion(c, "AND", d), true, reply -> {
                    if (reply.succeeded() && reply.result().getUpdated() == 0) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutJobsJobconfsByJobconfsIdJobsByJobIdResponse.withPlainNotFound(jobId)));
                    } else {
                        try {
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutJobsJobconfsByJobconfsIdJobsByJobIdResponse.withNoContent()));
                        } catch (Exception e) {
                            log.error(e);
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutJobsJobconfsByJobconfsIdJobsByJobIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                        }
                    }
                });
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutJobsJobconfsByJobconfsIdJobsByJobIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
            }
        });
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutJobsJobconfsByJobconfsIdJobsByJobIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
    }
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) Criteria(org.folio.rest.persist.Criteria.Criteria) Validate(org.folio.rest.annotations.Validate)

Example 32 with Criterion

use of org.folio.rest.persist.Criteria.Criterion in project raml-module-builder by folio-org.

the class PostgresClientTransactionsIT method deleteTransaction.

private void deleteTransaction(TestContext context, String schema) {
    PostgresClient c1 = PostgresClient.getInstance(vertx, TENANT);
    Async async = context.async();
    // create connection
    c1.startTx(handler -> {
        if (handler.succeeded()) {
            Criteria c = new Criteria();
            c.addField("'id'").setOperation(Criteria.OP_EQUAL).setValue("2");
            c1.delete(handler, "z", new Criterion(c), reply -> {
                if (reply.succeeded()) {
                    // make sure record is deleted when querying using this connection
                    // but since not committed yet we should still get it back
                    // when sending the query on a different connection
                    c1.get(handler, "z", SimplePojo.class, new Criterion(c), true, true, reply2 -> {
                        if (!reply2.succeeded()) {
                            context.fail(reply2.cause());
                            async.complete();
                        } else {
                            try {
                                int size = reply2.result().getResults().size();
                                context.assertEquals(0, size);
                                // call get() without the connection. since we did not commit yet
                                // this should still return the deleted record
                                c1.get("z", SimplePojo.class, new Criterion(c), true, true, reply3 -> {
                                    if (!reply3.succeeded()) {
                                        context.fail(reply3.cause());
                                        async.complete();
                                    } else {
                                        int size2 = reply3.result().getResults().size();
                                        context.assertEquals(1, size2);
                                        // end transaction / commit
                                        // doesnt seem like a good idea to reuse the handler within the get()
                                        // which lives outside of this connection, but for testing ok.
                                        c1.endTx(handler, done -> {
                                            if (done.succeeded()) {
                                                // record should have been deleted, so only one record should return
                                                // not both
                                                c1.select("SELECT jsonb FROM " + schema + ".z;", selectReply -> {
                                                    if (!selectReply.succeeded()) {
                                                        context.fail(selectReply.cause());
                                                        async.complete();
                                                    } else {
                                                        try {
                                                            int size3 = selectReply.result().getResults().size();
                                                            context.assertEquals(1, size3);
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                            context.fail(e.getMessage());
                                                        }
                                                        async.complete();
                                                    }
                                                });
                                            } else {
                                                context.fail(done.cause());
                                            }
                                        });
                                    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                                context.fail(e.getMessage());
                                async.complete();
                            }
                        }
                    });
                } else {
                    context.fail(reply.cause());
                }
            });
        } else {
            context.fail(handler.cause());
        }
    });
    async.await();
    c1.closeClient(context.asyncAssertSuccess());
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) Async(io.vertx.ext.unit.Async) Criteria(org.folio.rest.persist.Criteria.Criteria) IOException(java.io.IOException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Aggregations

Criterion (org.folio.rest.persist.Criteria.Criterion)32 Criteria (org.folio.rest.persist.Criteria.Criteria)31 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)26 List (java.util.List)18 Validate (org.folio.rest.annotations.Validate)14 CQLParseException (org.z3950.zing.cql.CQLParseException)8 PostgresClient (org.folio.rest.persist.PostgresClient)3 IOException (java.io.IOException)2 JobConf (org.folio.rest.jaxrs.model.JobConf)2 Location (org.folio.rest.jaxrs.model.Location)2 Limit (org.folio.rest.persist.Criteria.Limit)2 Offset (org.folio.rest.persist.Criteria.Offset)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)1 SQLConnection (io.vertx.ext.sql.SQLConnection)1 Async (io.vertx.ext.unit.Async)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 NotImplementedException (org.apache.commons.lang.NotImplementedException)1