Search in sources :

Example 16 with PostgresClient

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

the class HoldingsStorageAPI method postHoldingsStorageHoldings.

@Override
public void postHoldingsStorageHoldings(@DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, HoldingsRecord 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(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withPlainBadRequest("ID must be a UUID"));
                    }
                }
                postgresClient.save(HOLDINGS_RECORD_TABLE, entity.getId(), entity, reply -> {
                    try {
                        if (reply.succeeded()) {
                            OutStream stream = new OutStream();
                            stream.setData(entity);
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withJsonCreated(reply.result(), stream)));
                        } else {
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withPlainBadRequest(reply.cause().getMessage())));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withPlainInternalServerError(e.getMessage())));
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withPlainInternalServerError(e.getMessage())));
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withPlainInternalServerError(e.getMessage())));
    }
}
Also used : PostgresClient(org.folio.rest.persist.PostgresClient) OutStream(org.folio.rest.tools.utils.OutStream)

Example 17 with PostgresClient

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

the class IdentifierTypeAPI method deleteIdentifierTypesByIdentifierTypeId.

@Validate
@Override
public void deleteIdentifierTypesByIdentifierTypeId(String identifierTypeId, 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(IDENTIFIER_TYPE_TABLE, identifierTypeId, 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(DeleteIdentifierTypesByIdentifierTypeIdResponse.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(DeleteIdentifierTypesByIdentifierTypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    asyncResultHandler.handle(Future.succeededFuture(DeleteIdentifierTypesByIdentifierTypeIdResponse.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 18 with PostgresClient

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

the class InstanceStorageAPI method deleteInstanceStorageInstancesByInstanceId.

@Override
public void deleteInstanceStorageInstancesByInstanceId(@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));
        Criteria a = new Criteria();
        a.addField("'id'");
        a.setOperation("=");
        a.setValue(instanceId);
        Criterion criterion = new Criterion(a);
        vertxContext.runOnContext(v -> {
            try {
                postgresClient.delete("instance", criterion, reply -> {
                    if (reply.succeeded()) {
                        asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceStorageInstancesByInstanceIdResponse.withNoContent()));
                    } else {
                        asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(reply.cause().getMessage())));
                    }
                });
            } catch (Exception e) {
                asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
            }
        });
    } catch (Exception e) {
        asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
    }
}
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)

Example 19 with PostgresClient

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

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

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