Search in sources :

Example 21 with PostgresClient

use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.

the class InstanceStorageAPI method postInstanceStorageInstances.

@Override
public void postInstanceStorageInstances(@DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, Instance entity, 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 {
                if (entity.getId() == null) {
                    entity.setId(UUID.randomUUID().toString());
                } else {
                    if (isUUID(entity.getId())) {
                        io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withPlainBadRequest("ID must be a UUID"));
                    }
                }
                postgresClient.save("instance", entity.getId(), entity, reply -> {
                    try {
                        if (reply.succeeded()) {
                            OutStream stream = new OutStream();
                            stream.setData(entity);
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withJsonCreated(reply.result(), stream)));
                        } else {
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withPlainBadRequest(reply.cause().getMessage())));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withPlainInternalServerError(e.getMessage())));
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withPlainInternalServerError(e.getMessage())));
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withPlainInternalServerError(e.getMessage())));
    }
}
Also used : PostgresClient(org.folio.rest.persist.PostgresClient) OutStream(org.folio.rest.tools.utils.OutStream) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Example 22 with PostgresClient

use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.

the class InstanceTypeAPI method deleteInstanceTypesByInstanceTypeId.

@Validate
@Override
public void deleteInstanceTypesByInstanceTypeId(String instanceTypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            PostgresClient postgres = PostgresClient.getInstance(vertxContext.owner(), tenantId);
            postgres.delete(INSTANCE_TYPE_TABLE, instanceTypeId, reply -> {
                try {
                    if (reply.failed()) {
                        String msg = PgExceptionUtil.badRequestMessage(reply.cause());
                        if (msg == null) {
                            internalServerErrorDuringDelete(reply.cause(), lang, asyncResultHandler);
                            return;
                        }
                        log.info(msg);
                        asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceTypesByInstanceTypeIdResponse.withPlainBadRequest(msg)));
                        return;
                    }
                    int updated = reply.result().getUpdated();
                    if (updated != 1) {
                        String msg = messages.getMessage(lang, MessageConsts.DeletedCountError, 1, updated);
                        log.error(msg);
                        asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceTypesByInstanceTypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceTypesByInstanceTypeIdResponse.withNoContent()));
                } catch (Exception e) {
                    internalServerErrorDuringDelete(e, lang, asyncResultHandler);
                }
            });
        } catch (Exception e) {
            internalServerErrorDuringDelete(e, lang, asyncResultHandler);
        }
    });
}
Also used : PostgresClient(org.folio.rest.persist.PostgresClient) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 23 with PostgresClient

use of org.folio.rest.persist.PostgresClient 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 24 with PostgresClient

use of org.folio.rest.persist.PostgresClient 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)

Example 25 with PostgresClient

use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.

the class ItemStorageAPI method putItemStorageItemsByItemId.

@Validate
@Override
public void putItemStorageItemsByItemId(@PathParam("itemId") @NotNull String itemId, @QueryParam("lang") @DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, Item entity, 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));
        vertxContext.runOnContext(v -> {
            try {
                getMaterialType(vertxContext.owner(), tenantId, entity, replyHandler -> {
                    int res = replyHandler.result();
                    if (res == 0) {
                        String message = "Can not add " + entity.getMaterialTypeId() + ". Material type not found";
                        log.error(message);
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainBadRequest(message)));
                    } else if (res == -1) {
                        asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError("")));
                    } else {
                        try {
                            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()));
                            postgresClient.get(getTableName(query), Item.class, fieldList, cql, true, false, reply -> {
                                if (reply.succeeded()) {
                                    List<Item> itemList = (List<Item>) reply.result().getResults();
                                    if (itemList.size() == 1) {
                                        try {
                                            postgresClient.update("item", entity, entity.getId(), update -> {
                                                try {
                                                    if (update.succeeded()) {
                                                        OutStream stream = new OutStream();
                                                        stream.setData(entity);
                                                        asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withNoContent()));
                                                    } else {
                                                        String message = PgExceptionUtil.badRequestMessage(update.cause());
                                                        if (message != null) {
                                                            asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainBadRequest(message)));
                                                        } else {
                                                            asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainInternalServerError(update.cause().getMessage())));
                                                        }
                                                    }
                                                } catch (Exception e) {
                                                    asyncResultHandler.handle(Future.succeededFuture(PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
                                                }
                                            });
                                        } catch (Exception e) {
                                            asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
                                        }
                                    } else {
                                        try {
                                            postgresClient.save("item", entity.getId(), entity, save -> {
                                                try {
                                                    if (save.succeeded()) {
                                                        OutStream stream = new OutStream();
                                                        stream.setData(entity);
                                                        asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withNoContent()));
                                                    } else {
                                                        asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainInternalServerError(save.cause().getMessage())));
                                                    }
                                                } catch (Exception e) {
                                                    asyncResultHandler.handle(Future.succeededFuture(PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
                                                }
                                            });
                                        } catch (Exception e) {
                                            asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
                                        }
                                    }
                                } else {
                                    asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainInternalServerError(reply.cause().getMessage())));
                                }
                            });
                        } catch (Exception e) {
                            asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
                        }
                    }
                });
            } catch (Exception e) {
                asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
            }
        });
    } catch (Exception e) {
        asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
    }
}
Also used : PathParam(javax.ws.rs.PathParam) Arrays(java.util.Arrays) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) io.vertx.core(io.vertx.core) Criteria(org.folio.rest.persist.Criteria.Criteria) LoggerFactory(io.vertx.core.logging.LoggerFactory) QueryParam(javax.ws.rs.QueryParam) Limit(org.folio.rest.persist.Criteria.Limit) PgExceptionUtil(org.folio.rest.persist.PgExceptionUtil) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Offset(org.folio.rest.persist.Criteria.Offset) Logger(io.vertx.core.logging.Logger) ItemStorageResource(org.folio.rest.jaxrs.resource.ItemStorageResource) UUID(java.util.UUID) NotNull(javax.validation.constraints.NotNull) Validate(org.folio.rest.annotations.Validate) TenantTool(org.folio.rest.tools.utils.TenantTool) PostgresClient(org.folio.rest.persist.PostgresClient) org.folio.rest.jaxrs.model(org.folio.rest.jaxrs.model) OutStream(org.folio.rest.tools.utils.OutStream) List(java.util.List) Criterion(org.folio.rest.persist.Criteria.Criterion) Response(javax.ws.rs.core.Response) Pattern(javax.validation.constraints.Pattern) CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) PostgresClient(org.folio.rest.persist.PostgresClient) List(java.util.List) OutStream(org.folio.rest.tools.utils.OutStream) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Aggregations

PostgresClient (org.folio.rest.persist.PostgresClient)30 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)22 Validate (org.folio.rest.annotations.Validate)14 List (java.util.List)10 CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)10 CQLParseException (org.z3950.zing.cql.CQLParseException)8 OutStream (org.folio.rest.tools.utils.OutStream)6 Criteria (org.folio.rest.persist.Criteria.Criteria)5 Criterion (org.folio.rest.persist.Criteria.Criterion)5 Limit (org.folio.rest.persist.Criteria.Limit)5 Offset (org.folio.rest.persist.Criteria.Offset)5 CQL2PgJSON (org.z3950.zing.cql.cql2pgjson.CQL2PgJSON)5 HoldingsRecord (org.folio.rest.jaxrs.model.HoldingsRecord)3 Instance (org.folio.rest.jaxrs.model.Instance)3 io.vertx.core (io.vertx.core)2 Logger (io.vertx.core.logging.Logger)2 LoggerFactory (io.vertx.core.logging.LoggerFactory)2 Arrays (java.util.Arrays)2 Map (java.util.Map)2 UUID (java.util.UUID)2