use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.
the class InstanceStorageAPI method getInstanceStorageInstancesByInstanceId.
@Override
public void getInstanceStorageInstancesByInstanceId(@NotNull String instanceId, @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));
String[] fieldList = { "*" };
CQLWrapper cql = handleCQL(String.format("id==%s", instanceId), 1, 0);
log.info(String.format("SQL generated from CQL: %s", cql.toString()));
vertxContext.runOnContext(v -> {
try {
postgresClient.get(tableName, Instance.class, fieldList, cql, true, false, reply -> {
try {
if (reply.succeeded()) {
List<Instance> instanceList = (List<Instance>) reply.result().getResults();
if (instanceList.size() == 1) {
Instance instance = instanceList.get(0);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesByInstanceIdResponse.withJsonOK(instance)));
} else {
asyncResultHandler.handle(Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesByInstanceIdResponse.withPlainNotFound("Not Found")));
}
} else {
asyncResultHandler.handle(Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.
the class InstanceStorageAPI method getInstanceStorageInstances.
@Override
public void getInstanceStorageInstances(@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 = { "*" };
CQLWrapper cql = handleCQL(query, limit, offset);
log.info(String.format("SQL generated from CQL: %s", cql.toString()));
postgresClient.get(tableName, Instance.class, fieldList, cql, true, false, reply -> {
try {
if (reply.succeeded()) {
List<Instance> instances = (List<Instance>) reply.result().getResults();
Instances instanceList = new Instances();
instanceList.setInstances(instances);
instanceList.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesResponse.withJsonOK(instanceList)));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.GetInstanceStorageInstancesResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.
the class InstanceTypeAPI method getInstanceTypes.
@Validate
@Override
public void getInstanceTypes(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
/**
* http://host:port/instance-types
*/
vertxContext.runOnContext(v -> {
try {
String tenantId = TenantTool.tenantId(okapiHeaders);
CQLWrapper cql = getCQL(query, limit, offset);
PostgresClient.getInstance(vertxContext.owner(), tenantId).get(INSTANCE_TYPE_TABLE, InstanceType.class, new String[] { "*" }, cql, true, true, reply -> {
try {
if (reply.succeeded()) {
InstanceTypes instanceTypes = new InstanceTypes();
@SuppressWarnings("unchecked") List<InstanceType> instanceType = (List<InstanceType>) reply.result().getResults();
instanceTypes.setInstanceTypes(instanceType);
instanceTypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceTypesResponse.withJsonOK(instanceTypes)));
} else {
log.error(reply.cause().getMessage(), reply.cause());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceTypesResponse.withPlainBadRequest(reply.cause().getMessage())));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceTypesResponse.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(GetInstanceTypesResponse.withPlainInternalServerError(message)));
}
});
}
use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.
the class ItemStorageAPI method getItemStorageItemsByItemId.
@Validate
@Override
public void getItemStorageItemsByItemId(@PathParam("itemId") @NotNull String itemId, @QueryParam("lang") @DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, java.util.Map<String, String> okapiHeaders, io.vertx.core.Handler<io.vertx.core.AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
String tenantId = okapiHeaders.get(TENANT_HEADER);
try {
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
String[] fieldList = { "*" };
String query = String.format("id==%s", itemId);
CQLWrapper cql = getCQL(query, 1, 0);
log.info(String.format("SQL generated from CQL: %s", cql.toString()));
vertxContext.runOnContext(v -> {
try {
postgresClient.get(getTableName(query), Item.class, fieldList, cql, true, false, reply -> {
try {
if (reply.succeeded()) {
List<Item> itemList = (List<Item>) reply.result().getResults();
if (itemList.size() == 1) {
Item item = itemList.get(0);
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withJsonOK(item)));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainNotFound("Not Found")));
}
} else {
Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(reply.cause().getMessage()));
}
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.
the class ItemStorageAPI method getItemStorageItems.
@Validate
@Override
public void getItemStorageItems(int offset, int limit, String query, 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 = { "*" };
CQLWrapper cql = getCQL(query, limit, offset);
log.info(String.format("SQL generated by CQL query (%s): %s", query, cql.toString()));
postgresClient.get(getTableName(query), Item.class, fieldList, cql, true, false, reply -> {
try {
if (reply.succeeded()) {
List<Item> items = (List<Item>) reply.result().getResults();
Items itemList = new Items();
itemList.setItems(items);
itemList.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withJsonOK(itemList)));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
} catch (Exception e) {
if (e.getCause() != null && e.getCause().getClass().getSimpleName().contains("CQLParseException")) {
asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainBadRequest("CQL Parsing Error for '" + query + "': " + e.getLocalizedMessage())));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError("Error")));
}
}
});
} catch (IllegalStateException e) {
asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainInternalServerError("CQL State Error for '" + query + "': " + e.getLocalizedMessage())));
} catch (Exception e) {
if (e.getCause() != null && e.getCause().getClass().getSimpleName().contains("CQLParseException")) {
asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainBadRequest("CQL Parsing Error for '" + query + "': " + e.getLocalizedMessage())));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError("Error")));
}
}
});
} catch (Exception e) {
if (e.getCause() != null && e.getCause().getClass().getSimpleName().contains("CQLParseException")) {
asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainBadRequest("CQL Parsing Error for '" + query + "': " + e.getLocalizedMessage())));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError("Error")));
}
}
}
Aggregations