Search in sources :

Example 21 with OutStream

use of org.folio.rest.tools.utils.OutStream in project mod-inventory-storage by folio-org.

the class LocationUnitAPI method postLocationUnitsInstitutions.

@Override
public void postLocationUnitsInstitutions(String lang, Locinst entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
    String tenantId = getTenant(okapiHeaders);
    String id = entity.getId();
    if (id == null) {
        id = UUID.randomUUID().toString();
        entity.setId(id);
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).save(INSTITUTION_TABLE, id, entity, reply -> {
        if (reply.failed()) {
            String message = logAndSaveError(reply.cause());
            if (isDuplicate(message)) {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PostLocationUnitsInstitutionsResponse.withJsonUnprocessableEntity(ValidationHelper.createValidationErrorMessage("locinst", entity.getId(), "Institution already exists"))));
            } else {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PostLocationUnitsInstitutionsResponse.withPlainInternalServerError(message)));
            }
        } else {
            Object responseObject = reply.result();
            entity.setId((String) responseObject);
            OutStream stream = new OutStream();
            stream.setData(entity);
            asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PostLocationUnitsInstitutionsResponse.withJsonCreated(URL_PREFIX + responseObject, stream)));
        }
    });
}
Also used : OutStream(org.folio.rest.tools.utils.OutStream)

Example 22 with OutStream

use of org.folio.rest.tools.utils.OutStream 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 23 with OutStream

use of org.folio.rest.tools.utils.OutStream in project mod-inventory-storage by folio-org.

the class InstanceFormatAPI method postInstanceFormats.

@Validate
@Override
public void postInstanceFormats(String lang, InstanceFormat entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    vertxContext.runOnContext(v -> {
        try {
            String id = entity.getId();
            if (id == null) {
                id = UUID.randomUUID().toString();
                entity.setId(id);
            }
            String tenantId = TenantTool.tenantId(okapiHeaders);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).save(INSTANCE_FORMAT_TABLE, id, entity, reply -> {
                try {
                    if (reply.succeeded()) {
                        Object ret = reply.result();
                        entity.setId((String) ret);
                        OutStream stream = new OutStream();
                        stream.setData(entity);
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PostInstanceFormatsResponse.withJsonCreated(LOCATION_PREFIX + ret, stream)));
                    } else {
                        String msg = PgExceptionUtil.badRequestMessage(reply.cause());
                        if (msg == null) {
                            internalServerErrorDuringPost(reply.cause(), lang, asyncResultHandler);
                            return;
                        }
                        log.info(msg);
                        asyncResultHandler.handle(Future.succeededFuture(PostInstanceFormatsResponse.withPlainBadRequest(msg)));
                    }
                } catch (Exception e) {
                    internalServerErrorDuringPost(e, lang, asyncResultHandler);
                }
            });
        } catch (Exception e) {
            internalServerErrorDuringPost(e, lang, asyncResultHandler);
        }
    });
}
Also used : OutStream(org.folio.rest.tools.utils.OutStream) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 24 with OutStream

use of org.folio.rest.tools.utils.OutStream 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 25 with OutStream

use of org.folio.rest.tools.utils.OutStream 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

OutStream (org.folio.rest.tools.utils.OutStream)40 Validate (org.folio.rest.annotations.Validate)26 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)13 CQLParseException (org.z3950.zing.cql.CQLParseException)8 PostgresClient (org.folio.rest.persist.PostgresClient)7 List (java.util.List)5 JsonObject (io.vertx.core.json.JsonObject)4 Response (javax.ws.rs.core.Response)4 CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)4 Map (java.util.Map)3 Limit (org.folio.rest.persist.Criteria.Limit)3 Offset (org.folio.rest.persist.Criteria.Offset)3 TenantTool (org.folio.rest.tools.utils.TenantTool)3 CQL2PgJSON (org.z3950.zing.cql.cql2pgjson.CQL2PgJSON)3 io.vertx.core (io.vertx.core)2 AsyncResult (io.vertx.core.AsyncResult)2 JsonArray (io.vertx.core.json.JsonArray)2 Logger (io.vertx.core.logging.Logger)2 LoggerFactory (io.vertx.core.logging.LoggerFactory)2 InputStream (java.io.InputStream)2