use of org.folio.rest.jaxrs.model.ResultInfo in project raml-module-builder by folio-org.
the class PostgresClient method processResult.
private Results processResult(io.vertx.ext.sql.ResultSet rs, Class<?> clazz, boolean count, boolean setId) {
long start = System.nanoTime();
Object[] ret = new Object[2];
List<Object> list = new ArrayList<>();
List<JsonObject> tempList = rs.getRows();
List<String> columnNames = rs.getColumnNames();
int columnNamesCount = columnNames.size();
Map<String, org.folio.rest.jaxrs.model.Facet> rInfo = new HashMap<>();
// this is incorrect in facet queries which add a row per facet value
int rowCount = rs.getNumRows();
boolean countSet = false;
if (rowCount > 0 && count) {
// if facet query, this wont set the count as it doesnt have a count column at this location,
Object firstColFirstVal = rs.getResults().get(0).getValue(0);
if (null != firstColFirstVal && "Integer".equals(firstColFirstVal.getClass().getSimpleName())) {
// regular query with count requested since count is the first column for each record
rowCount = rs.getResults().get(0).getInteger(0);
}
}
/* an exception to having the jsonb column get mapped to the corresponding clazz is a case where the
* clazz has an jsonb field, for example an audit class which contains a field called
* jsonb - meaning it encapsulates the real object for example for auditing purposes
* (contains the jsonb object as well as some other fields). In such a
* case, do not map the clazz to the content of the jsonb - but rather set the jsonb named field of the clazz
* with the jsonb column value */
boolean isAuditFlavored = false;
try {
clazz.getField(DEFAULT_JSONB_FIELD_NAME);
isAuditFlavored = true;
} catch (NoSuchFieldException nse) {
if (log.isDebugEnabled()) {
log.debug("non audit table, no " + DEFAULT_JSONB_FIELD_NAME + " found in json");
}
}
int facetEntriesInResultSet = 0;
for (int i = 0; i < tempList.size(); i++) {
try {
Object jo = tempList.get(i).getValue(DEFAULT_JSONB_FIELD_NAME);
Object id = tempList.get(i).getValue(idField);
Object o = null;
if (!isAuditFlavored && jo != null) {
try {
// is this a facet entry - if so process it, otherwise will throw an exception
// and continue trying to map to the pojos
o = mapper.readValue(jo.toString(), org.folio.rest.jaxrs.model.Facet.class);
org.folio.rest.jaxrs.model.Facet facet = rInfo.get(((org.folio.rest.jaxrs.model.Facet) o).getType());
if (facet == null) {
rInfo.put(((org.folio.rest.jaxrs.model.Facet) o).getType(), (org.folio.rest.jaxrs.model.Facet) o);
} else {
facet.getFacetValues().add(((org.folio.rest.jaxrs.model.Facet) o).getFacetValues().get(0));
}
facetEntriesInResultSet = facetEntriesInResultSet + 1;
continue;
} catch (Exception e) {
try {
o = mapper.readValue(jo.toString(), clazz);
} catch (UnrecognizedPropertyException e1) {
// this is a facet query , and this is the count entry {"count": 11}
rowCount = new JsonObject(tempList.get(i).getString("jsonb")).getInteger("count");
continue;
}
}
} else {
o = clazz.newInstance();
}
/* attempt to populate jsonb object with values from external columns - for example:
* if there is an update_date column in the record - try to populate a field updateDate in the
* jsonb object - this allows to use the DB for things like triggers to populate the update_date
* automatically, but still push them into the jsonb object - the json schema must declare this field
* as well - also support the audit mode descrbed above.
* NOTE that the query must request any field it wants to get populated into the jsonb obj*/
for (int j = 0; j < columnNamesCount; j++) {
/* if(columnNames.get(j).equals("count") && !countSet){
//check if this is reachable
rowCount = tempList.get(i).getLong(columnNames.get(j)).intValue();
}*/
if ((isAuditFlavored || !columnNames.get(j).equals(DEFAULT_JSONB_FIELD_NAME)) && !columnNames.get(j).equals(idField)) {
try {
Method[] m = o.getClass().getMethods();
for (int k = 0; k < m.length; k++) {
if (m[k].getName().equals(columnNametoCamelCaseWithset(columnNames.get(j)))) {
o.getClass().getMethod(columnNametoCamelCaseWithset(columnNames.get(j)), m[k].getParameterTypes()).invoke(o, new Object[] { tempList.get(i).getValue(columnNames.get(j)) });
}
}
} catch (Exception e) {
log.warn("Unable to populate field " + columnNametoCamelCaseWithset(columnNames.get(j)) + " for object of type " + clazz.getName());
}
}
}
if (setId) {
o.getClass().getMethod(columnNametoCamelCaseWithset(idField), new Class[] { String.class }).invoke(o, new String[] { id.toString() });
}
list.add(o);
} catch (Exception e) {
log.error(e.getMessage(), e);
list.add(null);
}
}
ResultInfo rn = new ResultInfo();
rInfo.forEach((k, v) -> {
rn.getFacets().add(v);
});
rn.setTotalRecords(rowCount);
Results r = new Results();
r.setResults(list);
r.setResultInfo(rn);
long end = System.nanoTime();
StatsTracker.addStatElement(STATS_KEY + ".processResult", (end - start));
if (log.isDebugEnabled()) {
log.debug("timer: process results (ns) " + (end - start));
}
return r;
}
use of org.folio.rest.jaxrs.model.ResultInfo in project raml-module-builder by folio-org.
the class PostgresClientIT method getCQLWrapperNoFacets.
@Test
public void getCQLWrapperNoFacets(TestContext context) throws FieldException {
final String tableDefiniton = "id UUID PRIMARY KEY , jsonb JSONB NOT NULL, distinct_test_field TEXT";
createTableWithPoLines(context, MOCK_POLINES_TABLE, tableDefiniton);
List<FacetField> facets = null;
CQL2PgJSON cql2pgJson = new CQL2PgJSON("jsonb");
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, null, handler -> {
context.assertTrue(handler.succeeded());
ResultInfo resultInfo = handler.result().getResultInfo();
context.assertEquals(6, resultInfo.getTotalRecords());
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(0, retFacets.size());
async.complete();
});
async.awaitSuccess();
}
String distinctOn = "jsonb->>'order_format'";
List<FacetField> emptyFacets = new ArrayList<FacetField>();
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, emptyFacets, distinctOn, handler -> {
context.assertTrue(handler.succeeded());
ResultInfo resultInfo = handler.result().getResultInfo();
context.assertEquals(4, resultInfo.getTotalRecords());
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(0, retFacets.size());
async.complete();
});
async.awaitSuccess();
}
{
Async async = context.async();
CQLWrapper cqlWrapperNull = null;
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapperNull, true, true, facets, null, handler -> {
context.assertTrue(handler.succeeded());
ResultInfo resultInfo = handler.result().getResultInfo();
context.assertEquals(6, resultInfo.getTotalRecords());
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(0, retFacets.size());
async.complete();
});
async.awaitSuccess();
}
{
Async async = context.async();
CQLWrapper cqlWrapperNull = null;
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapperNull, true, true, facets, distinctOn, handler -> {
context.assertTrue(handler.succeeded());
ResultInfo resultInfo = handler.result().getResultInfo();
context.assertEquals(4, resultInfo.getTotalRecords());
try {
List<Object> objs = handler.result().getResults();
context.assertEquals(4, objs.size());
} catch (Exception ex) {
context.fail(ex);
}
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(0, retFacets.size());
async.complete();
});
async.awaitSuccess();
}
}
use of org.folio.rest.jaxrs.model.ResultInfo in project raml-module-builder by folio-org.
the class PostgresClientIT method streamGetResultException.
@Test
public void streamGetResultException(TestContext context) {
createTableWithPoLines(context);
ResultInfo resultInfo = new ResultInfo();
context.assertNotNull(vertx);
RowStream<Row> sqlRowStream = new MySQLRowStream();
StringBuilder events = new StringBuilder();
Async async = context.async();
PostgresClientStreamResult<Object> streamResult = new PostgresClientStreamResult(resultInfo);
Transaction transaction = null;
postgresClient.doStreamRowResults(sqlRowStream, Object.class, transaction, new QueryHelper("table_name"), streamResult, context.asyncAssertSuccess(sr -> {
sr.handler(streamHandler -> {
events.append("[handler]");
});
sr.endHandler(x -> {
events.append("[endHandler]");
throw new NullPointerException("null");
});
sr.exceptionHandler(x -> {
events.append("[exception]");
context.assertEquals("SQLRowStream exception", x.getMessage());
async.complete();
});
}));
async.await(1000);
context.assertEquals("[exception]", events.toString());
}
use of org.folio.rest.jaxrs.model.ResultInfo in project raml-module-builder by folio-org.
the class PostgresClientIT method getCQLWrapperJsonbField.
@Test
public void getCQLWrapperJsonbField(TestContext context) throws FieldException {
final String tableDefiniton = "id UUID PRIMARY KEY , jsonb JSONB NOT NULL, distinct_test_field TEXT";
List<FacetField> facets = null;
createTableWithPoLines(context, MOCK_POLINES_TABLE, tableDefiniton);
CQL2PgJSON cql2pgJson = new CQL2PgJSON(MOCK_POLINES_TABLE + ".jsonb");
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "jsonb", cqlWrapper, true, true, facets, null, handler -> {
context.assertTrue(handler.succeeded());
ResultInfo resultInfo = handler.result().getResultInfo();
context.assertEquals(6, resultInfo.getTotalRecords());
async.complete();
});
async.awaitSuccess();
}
String distinctOn = "jsonb->>'order_format'";
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "jsonb", cqlWrapper, true, true, facets, distinctOn, handler -> {
context.assertTrue(handler.succeeded());
ResultInfo resultInfo = handler.result().getResultInfo();
context.assertEquals(4, resultInfo.getTotalRecords());
async.complete();
});
async.awaitSuccess();
}
}
use of org.folio.rest.jaxrs.model.ResultInfo in project raml-module-builder by folio-org.
the class PostgresClientIT method getCQLWrapperWithFacets.
@Test
public void getCQLWrapperWithFacets(TestContext context) throws FieldException {
final String tableDefiniton = "id UUID PRIMARY KEY , jsonb JSONB NOT NULL, distinct_test_field TEXT";
createTableWithPoLines(context, MOCK_POLINES_TABLE, tableDefiniton);
CQL2PgJSON cql2pgJson = new CQL2PgJSON("jsonb");
List<FacetField> facets = List.of(new FacetField("jsonb->>'edition'"));
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, null, handler -> {
context.assertTrue(handler.succeeded());
ResultInfo resultInfo = handler.result().getResultInfo();
context.assertEquals(6, resultInfo.getTotalRecords());
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(1, retFacets.size());
async.complete();
});
async.awaitSuccess();
}
String distinctOn = "jsonb->>'order_format'";
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, distinctOn, handler -> {
context.assertTrue(handler.succeeded());
ResultInfo resultInfo = handler.result().getResultInfo();
context.assertEquals(4, resultInfo.getTotalRecords());
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(1, retFacets.size());
async.complete();
});
async.awaitSuccess();
}
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "order_format==Other");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, distinctOn, context.asyncAssertSuccess(res -> {
ResultInfo resultInfo = res.getResultInfo();
context.assertEquals(1, resultInfo.getTotalRecords());
List<Object> objs = res.getResults();
ObjectMapper mapper = new ObjectMapper();
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(1, retFacets.size());
context.assertEquals("edition", retFacets.get(0).getType());
context.assertEquals(1, retFacets.get(0).getFacetValues().get(0).getCount());
context.assertEquals("First edition", retFacets.get(0).getFacetValues().get(0).getValue().toString());
context.assertEquals(1, objs.size());
try {
context.assertEquals("70fb4e66-cdf1-11e8-a8d5-f2801f1b9fd1", new JsonObject(mapper.writeValueAsString(objs.get(0))).getString("id"));
} catch (JsonProcessingException e) {
context.fail(e);
}
async.complete();
}));
async.awaitSuccess();
}
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "order_format==Other");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Poline.class, "*", cqlWrapper, true, true, facets, distinctOn, context.asyncAssertSuccess(res -> {
ResultInfo resultInfo = res.getResultInfo();
context.assertEquals(1, resultInfo.getTotalRecords());
List<Poline> objs = res.getResults();
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(1, retFacets.size());
context.assertEquals("edition", retFacets.get(0).getType());
context.assertEquals(1, retFacets.get(0).getFacetValues().get(0).getCount());
context.assertEquals("First edition", retFacets.get(0).getFacetValues().get(0).getValue().toString());
context.assertEquals(1, objs.size());
context.assertEquals("70fb4e66-cdf1-11e8-a8d5-f2801f1b9fd1", objs.get(0).getId());
async.complete();
}));
async.awaitSuccess();
}
}
Aggregations