Search in sources :

Example 26 with Criteria

use of org.folio.rest.persist.Criteria.Criteria in project mod-inventory-storage by folio-org.

the class ContributorTypeAPI method getContributorTypesByContributorTypeId.

@Validate
@Override
public void getContributorTypesByContributorTypeId(String contributorTypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            Criterion c = new Criterion(new Criteria().addField(idFieldName).setJSONB(false).setOperation("=").setValue("'" + contributorTypeId + "'"));
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CONTRIBUTOR_TYPE_TABLE, ContributorType.class, c, true, reply -> {
                try {
                    if (reply.failed()) {
                        String msg = PgExceptionUtil.badRequestMessage(reply.cause());
                        if (msg == null) {
                            internalServerErrorDuringGetById(reply.cause(), lang, asyncResultHandler);
                            return;
                        }
                        log.info(msg);
                        asyncResultHandler.handle(Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    @SuppressWarnings("unchecked") List<ContributorType> contributorType = (List<ContributorType>) reply.result().getResults();
                    if (contributorType.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withPlainNotFound(contributorTypeId)));
                    } else {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withJsonOK(contributorType.get(0))));
                    }
                } catch (Exception e) {
                    internalServerErrorDuringGetById(e, lang, asyncResultHandler);
                }
            });
        } catch (Exception e) {
            internalServerErrorDuringGetById(e, lang, asyncResultHandler);
        }
    });
}
Also used : ContributorType(org.folio.rest.jaxrs.model.ContributorType) Criterion(org.folio.rest.persist.Criteria.Criterion) List(java.util.List) Criteria(org.folio.rest.persist.Criteria.Criteria) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 27 with Criteria

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

the class JobAPI method getJobsJobconfsByJobconfsId.

@Validate
@Override
public void getJobsJobconfsByJobconfsId(String jobconfsId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    System.out.println("sending... getJobsJobconfsByJobconfsId");
    try {
        Criteria c = new Criteria();
        c.addField("_id");
        c.setOperation("=");
        c.setValue(jobconfsId);
        c.setJSONB(false);
        vertxContext.runOnContext(v -> {
            try {
                PostgresClient.getInstance(vertxContext.owner()).get(RTFConsts.JOB_CONF_COLLECTION, JobConf.class, new Criterion(c), true, reply -> {
                    @SuppressWarnings("unchecked") List<JobConf> confs = (List<JobConf>) reply.result().getResults();
                    if (confs.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withPlainNotFound("JobConf " + messages.getMessage(lang, "10008"))));
                        return;
                    }
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withJsonOK(confs.get(0))));
                });
            } catch (Exception e) {
                log.error(e);
                asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
            }
        });
    } catch (Exception e) {
        log.error(e);
        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
    }
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) List(java.util.List) Criteria(org.folio.rest.persist.Criteria.Criteria) JobConf(org.folio.rest.jaxrs.model.JobConf) Validate(org.folio.rest.annotations.Validate)

Example 28 with Criteria

use of org.folio.rest.persist.Criteria.Criteria 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 29 with Criteria

use of org.folio.rest.persist.Criteria.Criteria 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)

Example 30 with Criteria

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

the class CriteriaTest method testCriteria.

@Test
public void testCriteria() {
    try {
        // pass schema so that type is known
        Criteria schema = new Criteria("userdata.json");
        schema.addField("'personal'").addField("'lastName'").setOperation("=").setValue("123");
        assertEquals("(jsonb->'personal'->>'lastName') =  '123'", schema.toString());
        // pass schema so that type is known
        schema = new Criteria("userdata.json");
        schema.addField("'active'").setOperation("=").setValue("true");
        assertEquals("(jsonb->>'active')::boolean = true", schema.toString());
        // guess op type is numeric
        schema = new Criteria();
        schema.addField("'personal'").addField("'lastName'").setOperation("=").setValue("123");
        assertEquals("(jsonb->'personal'->>'lastName')::numeric = 123", schema.toString());
        // guess op type is string since not null, numeric or boolean
        Criteria c = new Criteria();
        c.addField("'price'").addField("'po_currency'").addField("'value'");
        c.setOperation("like");
        c.setValue("USD");
        assertEquals("(jsonb->'price'->'po_currency'->>'value') like  'USD'", c.toString().trim());
        // guess op type is boolean by checking operation
        Criteria d = new Criteria();
        d.addField("'rush'");
        d.setOperation(Criteria.OP_IS_FALSE);
        d.setValue(null);
        assertEquals("(jsonb->>'rush')::boolean IS FALSE", d.toString().trim());
        // guess op type is boolean by checking value
        Criteria aa = new Criteria();
        aa.addField("'rush'");
        aa.setOperation("!=");
        aa.setValue("true");
        assertEquals("(jsonb->>'rush')::boolean != true", aa.toString().trim());
        Criteria nb = new Criteria();
        nb.addField("'transaction'");
        nb.addField("'status'");
        nb.setOperation("=");
        nb.setValue("rollbackComplete");
        nb.setArray(true);
        assertEquals("(transaction->'status') =  'rollbackComplete'", nb.toString());
        assertEquals("transaction", nb.getSelect().getSnippet());
        assertEquals("jsonb_array_elements(jsonb->'transaction')", nb.getFrom().getSnippet());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Criteria(org.folio.rest.persist.Criteria.Criteria) Test(org.junit.Test)

Aggregations

Criteria (org.folio.rest.persist.Criteria.Criteria)30 Criterion (org.folio.rest.persist.Criteria.Criterion)29 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)26 List (java.util.List)16 Validate (org.folio.rest.annotations.Validate)12 CQLParseException (org.z3950.zing.cql.CQLParseException)8 PostgresClient (org.folio.rest.persist.PostgresClient)3 Location (org.folio.rest.jaxrs.model.Location)2 Async (io.vertx.ext.unit.Async)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 NotImplementedException (org.apache.commons.lang.NotImplementedException)1 ClassificationType (org.folio.rest.jaxrs.model.ClassificationType)1 ContributorNameType (org.folio.rest.jaxrs.model.ContributorNameType)1 ContributorType (org.folio.rest.jaxrs.model.ContributorType)1 IdentifierType (org.folio.rest.jaxrs.model.IdentifierType)1 InstanceFormat (org.folio.rest.jaxrs.model.InstanceFormat)1 InstanceType (org.folio.rest.jaxrs.model.InstanceType)1 JobConf (org.folio.rest.jaxrs.model.JobConf)1 Loantype (org.folio.rest.jaxrs.model.Loantype)1