Search in sources :

Example 26 with OutStream

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

the class ItemStorageAPI method postItemStorageItems.

@Validate
@Override
public void postItemStorageItems(@DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, Item 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));
        if (entity.getId() == null) {
            entity.setId(UUID.randomUUID().toString());
        }
        vertxContext.runOnContext(v -> {
            try {
                /**
                 *This should be replaced with a foreign key / cache since a lookup into the MT table
                 * every time an item is inserted is wasteful and slows down the insert process
                 */
                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(ItemStorageResource.PostItemStorageItemsResponse.withPlainBadRequest(message)));
                        return;
                    } else if (res == -1) {
                        asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError("")));
                        return;
                    } else {
                        Future<Location> temporaryLocationFuture;
                        if (entity.getTemporaryLocationId() != null) {
                            temporaryLocationFuture = getShelfLocation(vertxContext.owner(), tenantId, entity.getTemporaryLocationId());
                        } else {
                            temporaryLocationFuture = Future.succeededFuture();
                        }
                        temporaryLocationFuture.setHandler(compRes -> {
                            if (compRes.failed()) {
                                String message = "Attempting to specify non-existent location";
                                log.error(message);
                                asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainBadRequest(message)));
                            } else {
                                try {
                                    postgresClient.save("item", entity.getId(), entity, reply -> {
                                        try {
                                            if (reply.succeeded()) {
                                                OutStream stream = new OutStream();
                                                stream.setData(entity);
                                                asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withJsonCreated(reply.result(), stream)));
                                            } else {
                                                String message = PgExceptionUtil.badRequestMessage(reply.cause());
                                                if (message != null) {
                                                    asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainBadRequest(message)));
                                                } else {
                                                    asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.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())));
            }
        });
    } 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) OutStream(org.folio.rest.tools.utils.OutStream) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 27 with OutStream

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

the class LocationAPI method postLocations.

@Override
public void postLocations(String lang, Location 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(LOCATION_TABLE, id, entity, reply -> {
        if (reply.failed()) {
            String message = logAndSaveError(reply.cause());
            if (message != null && message.contains("duplicate key value violates unique constraint")) {
                asyncResultHandler.handle(Future.succeededFuture(PostLocationsResponse.withJsonUnprocessableEntity(ValidationHelper.createValidationErrorMessage("shelflocation", entity.getId(), "Location already exists"))));
            } else {
                asyncResultHandler.handle(Future.succeededFuture(PostLocationsResponse.withPlainInternalServerError(message)));
            }
        } else {
            Object responseObject = reply.result();
            entity.setId((String) responseObject);
            OutStream stream = new OutStream();
            stream.setData(entity);
            asyncResultHandler.handle(Future.succeededFuture(PostLocationsResponse.withJsonCreated(URL_PREFIX + responseObject, stream)));
        }
    });
}
Also used : OutStream(org.folio.rest.tools.utils.OutStream)

Example 28 with OutStream

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

the class ClassificationTypeAPI method postClassificationTypes.

@Validate
@Override
public void postClassificationTypes(String lang, ClassificationType 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(CLASSIFICATION_TYPE_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(PostClassificationTypesResponse.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(PostClassificationTypesResponse.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 29 with OutStream

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

the class ContributorNameTypeAPI method postContributorNameTypes.

@Validate
@Override
public void postContributorNameTypes(String lang, ContributorNameType 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(CONTRIBUTOR_NAME_TYPE_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(PostContributorNameTypesResponse.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(PostContributorNameTypesResponse.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 30 with OutStream

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

the class ContributorTypeAPI method postContributorTypes.

@Validate
@Override
public void postContributorTypes(String lang, ContributorType 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(CONTRIBUTOR_TYPE_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(PostContributorTypesResponse.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(PostContributorTypesResponse.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)

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