use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.
the class LocationAPI method getLocations.
// Note, this is the way to get rid of unnecessary try-catch blocks. Use the
// same everywhere!
@Override
public void getLocations(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
String tenantId = getTenant(okapiHeaders);
CQLWrapper cql;
try {
cql = getCQL(query, limit, offset, LOCATION_TABLE);
} catch (FieldException e) {
String message = logAndSaveError(e);
asyncResultHandler.handle(Future.succeededFuture(GetLocationsResponse.withPlainBadRequest(message)));
return;
}
PostgresClient.getInstance(vertxContext.owner(), tenantId).get(LOCATION_TABLE, Location.class, new String[] { "*" }, cql, true, true, reply -> {
// netbeans, please indent here!
if (reply.failed()) {
String message = logAndSaveError(reply.cause());
asyncResultHandler.handle(Future.succeededFuture(GetLocationsResponse.withPlainBadRequest(message)));
} else {
Locations shelfLocations = new Locations();
List<Location> shelfLocationsList = (List<Location>) reply.result().getResults();
shelfLocations.setLocations(shelfLocationsList);
shelfLocations.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
asyncResultHandler.handle(Future.succeededFuture(GetLocationsResponse.withJsonOK(shelfLocations)));
}
});
}
use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.
the class LocationUnitAPI method getLocationUnitsInstitutions.
@Override
public void getLocationUnitsInstitutions(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
String tenantId = getTenant(okapiHeaders);
CQLWrapper cql;
try {
cql = getCQL(query, limit, offset, INSTITUTION_TABLE);
} catch (Exception e) {
String message = logAndSaveError(e);
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsInstitutionsResponse.withPlainInternalServerError(message)));
return;
}
PostgresClient.getInstance(vertxContext.owner(), tenantId).get(INSTITUTION_TABLE, Locinst.class, new String[] { "*" }, cql, true, true, reply -> {
if (reply.failed()) {
String message = logAndSaveError(reply.cause());
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsInstitutionsResponse.withPlainBadRequest(message)));
} else {
Locinsts insts = new Locinsts();
List<Locinst> items = (List<Locinst>) reply.result().getResults();
insts.setLocinsts(items);
insts.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsInstitutionsResponse.withJsonOK(insts)));
}
});
}
use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.
the class LocationUnitAPI method getLocationUnitsLibraries.
@Override
public void getLocationUnitsLibraries(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
String tenantId = getTenant(okapiHeaders);
CQLWrapper cql;
try {
cql = getCQL(query, limit, offset, LIBRARY_TABLE);
} catch (Exception e) {
String message = logAndSaveError(e);
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsLibrariesResponse.withPlainInternalServerError(message)));
return;
}
PostgresClient.getInstance(vertxContext.owner(), tenantId).get(LIBRARY_TABLE, Loclib.class, new String[] { "*" }, cql, true, true, reply -> {
if (reply.failed()) {
String message = logAndSaveError(reply.cause());
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsLibrariesResponse.withPlainBadRequest(message)));
} else {
Loclibs lib = new Loclibs();
List<Loclib> items = (List<Loclib>) reply.result().getResults();
lib.setLoclibs(items);
lib.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsLibrariesResponse.withJsonOK(lib)));
}
});
}
use of org.folio.rest.persist.cql.CQLWrapper 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())));
}
}
use of org.folio.rest.persist.cql.CQLWrapper 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())));
}
}
Aggregations