Search in sources :

Example 1 with CQL2PgJSON

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

the class CQLWrapperTest method invalidCQLvalidation.

@Test
public void invalidCQLvalidation() throws FieldException {
    CQLWrapper wrapper = new CQLWrapper().setField(new CQL2PgJSON("field")).setQuery("or name=miller");
    try {
        wrapper.toString();
        fail("exception expected");
    } catch (CQLQueryValidationException e) {
        assertThat(e.getMessage(), allOf(containsString("ParseException"), containsString("unexpected relation")));
    }
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Test(org.junit.Test)

Example 2 with CQL2PgJSON

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

the class CQLWrapperTest method sortBy.

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

Example 3 with CQL2PgJSON

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

the class FacetManager method main.

public static void main(String[] args) throws Exception {
    FacetManager fm = new FacetManager("myuniversity_new1_mod_users.users");
    List<FacetField> facets = new ArrayList<>();
    facets.add(new FacetField("jsonb->>'lastUpdateDate'", 5));
    facets.add(new FacetField("jsonb->'personal'->>'phone'", 5));
    facets.add(new FacetField("jsonb->>'username'", 5));
    fm.setSupportFacets(facets);
    fm.setWhere(new CQLWrapper(new CQL2PgJSON("jsonb"), "username=jha* OR username=szeev*").toString());
    fm.setMainQuery("SELECT jsonb FROM myuniversity_new1_mod_users.users where jsonb->>'username' like 'jha%' OR jsonb->>'username' like 'szeev%'");
    System.out.println(fm.generateFacetQuery());
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) ArrayList(java.util.ArrayList) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper)

Example 4 with CQL2PgJSON

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

the class HoldingsStorageAPI method putHoldingsStorageHoldingsByHoldingsRecordId.

@Override
public void putHoldingsStorageHoldingsByHoldingsRecordId(@NotNull String holdingsRecordId, @DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, HoldingsRecord entity, 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 -> {
                    if (reply.succeeded()) {
                        List<HoldingsRecord> itemList = (List<HoldingsRecord>) reply.result().getResults();
                        if (itemList.size() == 1) {
                            try {
                                postgresClient.update(HOLDINGS_RECORD_TABLE, entity, entity.getId(), update -> {
                                    try {
                                        if (update.succeeded()) {
                                            OutStream stream = new OutStream();
                                            stream.setData(entity);
                                            asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withNoContent()));
                                        } else {
                                            asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(update.cause().getMessage())));
                                        }
                                    } catch (Exception e) {
                                        asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
                                    }
                                });
                            } catch (Exception e) {
                                asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
                            }
                        } else {
                            try {
                                postgresClient.save(HOLDINGS_RECORD_TABLE, entity.getId(), entity, save -> {
                                    try {
                                        if (save.succeeded()) {
                                            OutStream stream = new OutStream();
                                            stream.setData(entity);
                                            asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withNoContent()));
                                        } else {
                                            asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(save.cause().getMessage())));
                                        }
                                    } catch (Exception e) {
                                        asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
                                    }
                                });
                            } catch (Exception e) {
                                asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
                            }
                        }
                    } else {
                        asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(reply.cause().getMessage())));
                    }
                });
            } catch (Exception e) {
                asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
            }
        });
    } catch (Exception e) {
        asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.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) OutStream(org.folio.rest.tools.utils.OutStream) Limit(org.folio.rest.persist.Criteria.Limit) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Offset(org.folio.rest.persist.Criteria.Offset)

Example 5 with CQL2PgJSON

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

the class HoldingsStorageAPI method getHoldingsStorageHoldings.

@Override
public void getHoldingsStorageHoldings(@DefaultValue("0") @Min(0L) @Max(1000L) int offset, @DefaultValue("10") @Min(1L) @Max(100L) int limit, String query, @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 {
        vertxContext.runOnContext(v -> {
            try {
                PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
                String[] fieldList = { "*" };
                CQL2PgJSON cql2pgJson = new CQL2PgJSON(HOLDINGS_RECORD_TABLE + ".jsonb");
                CQLWrapper cql = new CQLWrapper(cql2pgJson, query).setLimit(new Limit(limit)).setOffset(new Offset(offset));
                postgresClient.get(HOLDINGS_RECORD_TABLE, HoldingsRecord.class, fieldList, cql, true, false, reply -> {
                    try {
                        if (reply.succeeded()) {
                            List<HoldingsRecord> holdingsRecords = (List<HoldingsRecord>) reply.result().getResults();
                            HoldingsRecords holdingsList = new HoldingsRecords();
                            holdingsList.setHoldingsRecords(holdingsRecords);
                            holdingsList.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsResponse.withJsonOK(holdingsList)));
                        } else {
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsResponse.withPlainInternalServerError(reply.cause().getMessage())));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsResponse.withPlainInternalServerError(e.getMessage())));
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsResponse.withPlainInternalServerError(e.getMessage())));
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.GetHoldingsStorageHoldingsResponse.withPlainInternalServerError(e.getMessage())));
    }
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) HoldingsRecord(org.folio.rest.jaxrs.model.HoldingsRecord) HoldingsRecords(org.folio.rest.jaxrs.model.HoldingsRecords) 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)

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