Search in sources :

Example 16 with CQLWrapper

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())));
    }
}
Also used : Instance(org.folio.rest.jaxrs.model.Instance) PostgresClient(org.folio.rest.persist.PostgresClient) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Example 17 with CQLWrapper

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())));
    }
}
Also used : Instances(org.folio.rest.jaxrs.model.Instances) Instance(org.folio.rest.jaxrs.model.Instance) PostgresClient(org.folio.rest.persist.PostgresClient) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Example 18 with CQLWrapper

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)));
        }
    });
}
Also used : List(java.util.List) InstanceTypes(org.folio.rest.jaxrs.model.InstanceTypes) InstanceType(org.folio.rest.jaxrs.model.InstanceType) CQLParseException(org.z3950.zing.cql.CQLParseException) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 19 with CQLWrapper

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())));
    }
}
Also used : PostgresClient(org.folio.rest.persist.PostgresClient) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 20 with CQLWrapper

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")));
        }
    }
}
Also used : PostgresClient(org.folio.rest.persist.PostgresClient) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Aggregations

CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)32 List (java.util.List)24 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)22 Validate (org.folio.rest.annotations.Validate)12 CQL2PgJSON (org.z3950.zing.cql.cql2pgjson.CQL2PgJSON)12 PostgresClient (org.folio.rest.persist.PostgresClient)9 CQLParseException (org.z3950.zing.cql.CQLParseException)8 Test (org.junit.Test)6 Limit (org.folio.rest.persist.Criteria.Limit)4 Offset (org.folio.rest.persist.Criteria.Offset)4 HoldingsRecord (org.folio.rest.jaxrs.model.HoldingsRecord)3 Instance (org.folio.rest.jaxrs.model.Instance)3 ArrayList (java.util.ArrayList)2 OutStream (org.folio.rest.tools.utils.OutStream)2 io.vertx.core (io.vertx.core)1 Logger (io.vertx.core.logging.Logger)1 LoggerFactory (io.vertx.core.logging.LoggerFactory)1 Async (io.vertx.ext.unit.Async)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1