Search in sources :

Example 6 with CQL2PgJSON

use of org.z3950.zing.cql.cql2pgjson.CQL2PgJSON in project mod-inventory-storage by folio-org.

the class HoldingsStorageAPI method getHoldingsStorageHoldingsByHoldingsRecordId.

@Override
public void getHoldingsStorageHoldingsByHoldingsRecordId(@NotNull String holdingsRecordId, @DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    String tenantId = okapiHeaders.get(TENANT_HEADER);
    try {
        PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
        vertxContext.runOnContext(v -> {
            try {
                String[] fieldList = { "*" };
                CQL2PgJSON cql2pgJson = new CQL2PgJSON(HOLDINGS_RECORD_TABLE + ".jsonb");
                CQLWrapper cql = new CQLWrapper(cql2pgJson, String.format("id==%s", holdingsRecordId)).setLimit(new Limit(1)).setOffset(new Offset(0));
                log.info(String.format("SQL generated from CQL: %s", cql.toString()));
                postgresClient.get(HOLDINGS_RECORD_TABLE, HoldingsRecord.class, fieldList, cql, true, false, reply -> {
                    try {
                        if (reply.succeeded()) {
                            List<HoldingsRecord> holdingsList = (List<HoldingsRecord>) reply.result().getResults();
                            if (holdingsList.size() == 1) {
                                HoldingsRecord holdingsRecord = holdingsList.get(0);
                                asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsByHoldingsRecordIdResponse.withJsonOK(holdingsRecord)));
                            } else {
                                asyncResultHandler.handle(Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainNotFound("Not Found")));
                            }
                        } else {
                            asyncResultHandler.handle(Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(reply.cause().getMessage())));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
    }
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) HoldingsRecord(org.folio.rest.jaxrs.model.HoldingsRecord) PostgresClient(org.folio.rest.persist.PostgresClient) List(java.util.List) Limit(org.folio.rest.persist.Criteria.Limit) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Offset(org.folio.rest.persist.Criteria.Offset)

Example 7 with CQL2PgJSON

use of org.z3950.zing.cql.cql2pgjson.CQL2PgJSON in project raml-module-builder by folio-org.

the class PostgresClientTransactionsIT method updateTransaction.

private void updateTransaction(TestContext context, String schema) {
    PostgresClient c1 = PostgresClient.getInstance(vertx, TENANT);
    Async async = context.async();
    // create connection
    c1.startTx(handler -> {
        if (handler.succeeded()) {
            SimplePojo z = new SimplePojo();
            z.setId("99");
            z.setName("me");
            // update record
            CQL2PgJSON cql2pgJson = null;
            try {
                cql2pgJson = new CQL2PgJSON("z.jsonb");
            } catch (FieldException e1) {
                e1.printStackTrace();
                context.fail(e1);
            }
            CQLWrapper cql = new CQLWrapper(cql2pgJson, "id==1");
            c1.update(handler, "z", z, cql, true, reply -> {
                if (reply.succeeded()) {
                    // make sure record is not updated since not committed yet
                    c1.select("SELECT jsonb FROM " + schema + ".z;", reply2 -> {
                        if (!reply2.succeeded()) {
                            context.fail(reply2.cause());
                        }
                        try {
                            SimplePojo sp = ObjectMapperTool.getMapper().readValue(reply2.result().getResults().get(0).getString(0), SimplePojo.class);
                            context.assertEquals(sp.getName(), "d", "Name property should not have been changed");
                        } catch (Exception e) {
                            e.printStackTrace();
                            context.fail(e.getMessage());
                        }
                        // end transaction / commit
                        c1.endTx(handler, done -> {
                            if (done.succeeded()) {
                                // record should have been updated
                                c1.select("SELECT jsonb FROM " + schema + ".z;", selectReply -> {
                                    if (!selectReply.succeeded()) {
                                        context.fail(selectReply.cause());
                                    } else {
                                        try {
                                            SimplePojo sp = ObjectMapperTool.getMapper().readValue(selectReply.result().getResults().get(0).getString(0), SimplePojo.class);
                                            context.assertEquals(sp.getName(), "me", "Name property should have been changed");
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                            context.fail(e.getMessage());
                                        }
                                        async.complete();
                                    }
                                });
                            } else {
                                context.fail(done.cause());
                            }
                        });
                    });
                } else {
                    context.fail(reply.cause());
                }
            });
        } else {
            context.fail(handler.cause());
        }
    });
    async.await();
    c1.closeClient(context.asyncAssertSuccess());
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Async(io.vertx.ext.unit.Async) SimplePojo(org.folio.rest.persist.helpers.SimplePojo) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) IOException(java.io.IOException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Example 8 with CQL2PgJSON

use of org.z3950.zing.cql.cql2pgjson.CQL2PgJSON in project raml-module-builder by folio-org.

the class CQLWrapperTest method returnsWhere.

@Test
public void returnsWhere() throws FieldException {
    CQLWrapper wrapper = new CQLWrapper().setField(new CQL2PgJSON("field")).setQuery("name=miller");
    assertThat(wrapper.toString(), startsWith(" WHERE "));
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Test(org.junit.Test)

Example 9 with CQL2PgJSON

use of org.z3950.zing.cql.cql2pgjson.CQL2PgJSON in project raml-module-builder by folio-org.

the class CQLWrapperTest method allRecords.

@Test
public void allRecords() throws FieldException {
    CQLWrapper wrapper = new CQLWrapper().setField(new CQL2PgJSON("field")).setQuery("cql.allRecords=1");
    assertThat(wrapper.toString(), is(" WHERE true"));
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Test(org.junit.Test)

Example 10 with CQL2PgJSON

use of org.z3950.zing.cql.cql2pgjson.CQL2PgJSON in project raml-module-builder by folio-org.

the class CQLWrapperTest method wrap.

@Test
public void wrap() throws FieldException {
    CQLWrapper wrapper = new CQLWrapper().setField(new CQL2PgJSON("field")).setQuery("cql.allRecords=1");
    wrapper.addWrapper(wrapper);
    wrapper.addWrapper(wrapper, "or");
    assertThat(wrapper.toString(), is(" WHERE true and true or true"));
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Test(org.junit.Test)

Aggregations

CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)11 CQL2PgJSON (org.z3950.zing.cql.cql2pgjson.CQL2PgJSON)11 Test (org.junit.Test)6 List (java.util.List)3 HoldingsRecord (org.folio.rest.jaxrs.model.HoldingsRecord)3 Limit (org.folio.rest.persist.Criteria.Limit)3 Offset (org.folio.rest.persist.Criteria.Offset)3 PostgresClient (org.folio.rest.persist.PostgresClient)3 Async (io.vertx.ext.unit.Async)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HoldingsRecords (org.folio.rest.jaxrs.model.HoldingsRecords)1 SimplePojo (org.folio.rest.persist.helpers.SimplePojo)1 OutStream (org.folio.rest.tools.utils.OutStream)1 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)1