Search in sources :

Example 6 with PostgresClient

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

the class InstanceStorageAPI method putInstanceStorageInstancesByInstanceId.

@Override
public void putInstanceStorageInstancesByInstanceId(@NotNull String instanceId, @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 {
                String[] fieldList = { "*" };
                CQLWrapper cql = handleCQL(String.format("id==%s", instanceId), 1, 0);
                postgresClient.get(tableName, Instance.class, fieldList, cql, true, false, reply -> {
                    if (reply.succeeded()) {
                        List<Instance> instancesList = (List<Instance>) reply.result().getResults();
                        if (instancesList.size() == 1) {
                            try {
                                postgresClient.update(tableName, entity, entity.getId(), update -> {
                                    try {
                                        if (update.succeeded()) {
                                            OutStream stream = new OutStream();
                                            stream.setData(entity);
                                            asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withNoContent()));
                                        } else {
                                            asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(update.cause().getMessage())));
                                        }
                                    } catch (Exception e) {
                                        asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
                                    }
                                });
                            } catch (Exception e) {
                                asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
                            }
                        } else {
                            try {
                                postgresClient.save(tableName, entity.getId(), entity, save -> {
                                    try {
                                        if (save.succeeded()) {
                                            OutStream stream = new OutStream();
                                            stream.setData(entity);
                                            asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withNoContent()));
                                        } else {
                                            asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(save.cause().getMessage())));
                                        }
                                    } catch (Exception e) {
                                        asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
                                    }
                                });
                            } catch (Exception e) {
                                asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
                            }
                        }
                    } else {
                        asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(reply.cause().getMessage())));
                    }
                });
            } catch (Exception e) {
                asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
            }
        });
    } catch (Exception e) {
        asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
    }
}
Also used : Instance(org.folio.rest.jaxrs.model.Instance) 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)

Example 7 with PostgresClient

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

the class ItemStorageAPI method deleteItemStorageItemsByItemId.

@Validate
@Override
public void deleteItemStorageItemsByItemId(@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));
        Criteria a = new Criteria();
        a.addField("'id'");
        a.setOperation("=");
        a.setValue(itemId);
        Criterion criterion = new Criterion(a);
        vertxContext.runOnContext(v -> {
            try {
                postgresClient.delete("item", criterion, reply -> {
                    if (reply.succeeded()) {
                        asyncResultHandler.handle(Future.succeededFuture(DeleteItemStorageItemsByItemIdResponse.withNoContent()));
                    } else {
                        asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsByItemIdResponse.withPlainInternalServerError("Error")));
                    }
                });
            } catch (Exception e) {
                asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsByItemIdResponse.withPlainInternalServerError("Error")));
            }
        });
    } catch (Exception e) {
        asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsByItemIdResponse.withPlainInternalServerError("Error")));
    }
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) PostgresClient(org.folio.rest.persist.PostgresClient) Criteria(org.folio.rest.persist.Criteria.Criteria) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 8 with PostgresClient

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

the class ItemStorageAPI method deleteItemStorageItems.

@Validate
@Override
public void deleteItemStorageItems(@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 -> {
            PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
            postgresClient.mutate(String.format("TRUNCATE TABLE %s_%s.item", tenantId, "mod_inventory_storage"), reply -> {
                if (reply.succeeded()) {
                    asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsResponse.noContent().build()));
                } else {
                    asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsResponse.withPlainInternalServerError(reply.cause().getMessage())));
                }
            });
        });
    } catch (Exception e) {
        asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
    }
}
Also used : PostgresClient(org.folio.rest.persist.PostgresClient) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 9 with PostgresClient

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

the class LoanTypeAPI method deleteLoanTypesByLoantypeId.

@Validate
@Override
public void deleteLoanTypesByLoantypeId(String loantypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    vertxContext.runOnContext(v -> {
        try {
            PostgresClient postgres = getPostgresClient(vertxContext, okapiHeaders);
            postgres.delete(LOAN_TYPE_TABLE, loantypeId, 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(DeleteLoanTypesByLoantypeIdResponse.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(DeleteLoanTypesByLoantypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    asyncResultHandler.handle(Future.succeededFuture(DeleteLoanTypesByLoantypeIdResponse.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 10 with PostgresClient

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

the class ContributorNameTypeAPI method deleteContributorNameTypesByContributorNameTypeId.

@Validate
@Override
public void deleteContributorNameTypesByContributorNameTypeId(String ContributorNameTypeId, 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(CONTRIBUTOR_NAME_TYPE_TABLE, ContributorNameTypeId, 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(DeleteContributorNameTypesByContributorNameTypeIdResponse.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(DeleteContributorNameTypesByContributorNameTypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    asyncResultHandler.handle(Future.succeededFuture(DeleteContributorNameTypesByContributorNameTypeIdResponse.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)

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