Search in sources :

Example 1 with Instance

use of org.folio.rest.jaxrs.model.Instance 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 2 with Instance

use of org.folio.rest.jaxrs.model.Instance 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 3 with Instance

use of org.folio.rest.jaxrs.model.Instance 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

List (java.util.List)3 Instance (org.folio.rest.jaxrs.model.Instance)3 PostgresClient (org.folio.rest.persist.PostgresClient)3 CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)3 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)3 Instances (org.folio.rest.jaxrs.model.Instances)1 OutStream (org.folio.rest.tools.utils.OutStream)1