package org.folio.rest.impl; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.folio.rest.jaxrs.model.PurchaseOrderCollection; import org.folio.rest.persist.PostgresClient; import org.folio.rest.persist.Criteria.Limit; import org.folio.rest.persist.Criteria.Offset; import org.folio.rest.persist.cql.CQLWrapper; import org.folio.rest.persist.facets.FacetField; import org.z3950.zing.cql.cql2pgjson.CQL2PgJSON; import org.z3950.zing.cql.cql2pgjson.FieldException; import io.vertx.core.Vertx; import io.vertx.core.json.JsonObject; public class SearchAndFilterPOC { public static void main(String args[]) throws Exception { PostgresClient.setConfigFilePath("/tmp/db.conf"); String tenant = "diku"; String table = "testview"; String searchField = "metadata"; String returnField = "jsonb"; String idField = "id"; boolean returnCount = true; boolean returnId = false; boolean setId = false; List facets = null; //provided by request //String query = "cql.allRecords=1 sortBy po_number/sort.descending"; String query = "payment_status==Awaiting Payment AND workflow_status==Open AND order_type==One-Time sortBy created/sort.descending"; int limit = 20; int offset = 0; CQLWrapper cql = getCQL(query, table, searchField) .setLimit(new Limit(limit)) .setOffset(new Offset(offset)); String distinctOn = searchField + "->>'po_number'," + searchField + "->>'created'"; PostgresClient client = PostgresClient.getInstance(Vertx.vertx(), tenant); client.setIdField(idField); client.get(table, org.folio.rest.jaxrs.model.PurchaseOrder.class, returnField, cql.toString(), returnCount, returnId, setId, facets, distinctOn, reply -> { try { if (reply.succeeded()) { PurchaseOrderCollection collection = new PurchaseOrderCollection(); collection.setTotalRecords(reply.result().getResultInfo().getTotalRecords()); collection.setPurchaseOrders(reply.result().getResults()); System.out.println(JsonObject.mapFrom(collection).encodePrettily()); System.out.println(collection.getPurchaseOrders().size()); } else { throw new Exception(reply.cause()); } } catch (Exception e) { e.printStackTrace(); } finally { PostgresClient.closeAllClients(); System.exit(0); } }); } private static CQLWrapper getCQL(String query, String tableName, String column) throws FieldException { CQL2PgJSON cql2pgJson = new CQL2PgJSON(tableName + "." + column); return new CQLWrapper(cql2pgJson, query); } }