Search in sources :

Example 26 with CQLWrapper

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

the class ContributorNameTypeAPI method getContributorNameTypes.

@Validate
@Override
public void getContributorNameTypes(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/contributor-name-types
     */
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            CQLWrapper cql = getCQL(query, limit, offset);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CONTRIBUTOR_NAME_TYPE_TABLE, ContributorNameType.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        ContributorNameTypes ContributorNameTypes = new ContributorNameTypes();
                        @SuppressWarnings("unchecked") List<ContributorNameType> ContributorNameType = (List<ContributorNameType>) reply.result().getResults();
                        ContributorNameTypes.setContributorNameTypes(ContributorNameType);
                        ContributorNameTypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorNameTypesResponse.withJsonOK(ContributorNameTypes)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorNameTypesResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorNameTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String message = messages.getMessage(lang, MessageConsts.InternalServerError);
            if (e.getCause() instanceof CQLParseException) {
                message = " CQL parse error " + e.getLocalizedMessage();
            }
            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorNameTypesResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : ContributorNameTypes(org.folio.rest.jaxrs.model.ContributorNameTypes) ContributorNameType(org.folio.rest.jaxrs.model.ContributorNameType) List(java.util.List) CQLParseException(org.z3950.zing.cql.CQLParseException) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 27 with CQLWrapper

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

the class ContributorTypeAPI method getContributorTypes.

@Validate
@Override
public void getContributorTypes(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/contributor-types
     */
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            CQLWrapper cql = getCQL(query, limit, offset);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CONTRIBUTOR_TYPE_TABLE, ContributorType.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        ContributorTypes contributorTypes = new ContributorTypes();
                        @SuppressWarnings("unchecked") List<ContributorType> contributorType = (List<ContributorType>) reply.result().getResults();
                        contributorTypes.setContributorTypes(contributorType);
                        contributorTypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesResponse.withJsonOK(contributorTypes)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String message = messages.getMessage(lang, MessageConsts.InternalServerError);
            if (e.getCause() instanceof CQLParseException) {
                message = " CQL parse error " + e.getLocalizedMessage();
            }
            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : ContributorType(org.folio.rest.jaxrs.model.ContributorType) List(java.util.List) ContributorTypes(org.folio.rest.jaxrs.model.ContributorTypes) CQLParseException(org.z3950.zing.cql.CQLParseException) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 28 with CQLWrapper

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

use of org.folio.rest.persist.cql.CQLWrapper 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 30 with CQLWrapper

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

Aggregations

CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)32 List (java.util.List)24 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)22 Validate (org.folio.rest.annotations.Validate)12 CQL2PgJSON (org.z3950.zing.cql.cql2pgjson.CQL2PgJSON)12 PostgresClient (org.folio.rest.persist.PostgresClient)9 CQLParseException (org.z3950.zing.cql.CQLParseException)8 Test (org.junit.Test)6 Limit (org.folio.rest.persist.Criteria.Limit)4 Offset (org.folio.rest.persist.Criteria.Offset)4 HoldingsRecord (org.folio.rest.jaxrs.model.HoldingsRecord)3 Instance (org.folio.rest.jaxrs.model.Instance)3 ArrayList (java.util.ArrayList)2 OutStream (org.folio.rest.tools.utils.OutStream)2 io.vertx.core (io.vertx.core)1 Logger (io.vertx.core.logging.Logger)1 LoggerFactory (io.vertx.core.logging.LoggerFactory)1 Async (io.vertx.ext.unit.Async)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1