Search in sources :

Example 21 with Validate

use of org.folio.rest.annotations.Validate 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 22 with Validate

use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.

the class MaterialTypeAPI method postMaterialTypes.

@Validate
@Override
public void postMaterialTypes(String lang, Mtype entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    vertxContext.runOnContext(v -> {
        try {
            String id = UUID.randomUUID().toString();
            if (entity.getId() == null) {
                entity.setId(id);
            } else {
                id = entity.getId();
            }
            String tenantId = TenantTool.calculateTenantId(okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT));
            PostgresClient.getInstance(vertxContext.owner(), tenantId).save(MATERIAL_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(PostMaterialTypesResponse.withJsonCreated(LOCATION_PREFIX + ret, stream)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        if (isDuplicate(reply.cause().getMessage())) {
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PostMaterialTypesResponse.withJsonUnprocessableEntity(org.folio.rest.tools.utils.ValidationHelper.createValidationErrorMessage("name", entity.getName(), "Material Type exists"))));
                        } else {
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PostMaterialTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                        }
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PostMaterialTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PostMaterialTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
        }
    });
}
Also used : OutStream(org.folio.rest.tools.utils.OutStream) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 23 with Validate

use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.

the class MaterialTypeAPI method deleteMaterialTypesByMaterialtypeId.

@Validate
@Override
public void deleteMaterialTypesByMaterialtypeId(String materialtypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    vertxContext.runOnContext(v -> {
        String tenantId = TenantTool.calculateTenantId(okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT));
        try {
            Item item = new Item();
            item.setMaterialTypeId(materialtypeId);
            /**
             * check if any item is using this material type *
             */
            try {
                PostgresClient.getInstance(vertxContext.owner(), tenantId).get(ItemStorageAPI.ITEM_TABLE, item, new String[] { idFieldName }, true, false, 0, 1, replyHandler -> {
                    if (replyHandler.succeeded()) {
                        List<Item> mtypeList = (List<Item>) replyHandler.result().getResults();
                        if (mtypeList.size() > 0) {
                            String message = "Can not delete material type, " + materialtypeId + ". " + mtypeList.size() + " items associated with it";
                            log.error(message);
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainBadRequest(message)));
                            return;
                        } else {
                            log.info("Attemping delete of unused material type, " + materialtypeId);
                        }
                        try {
                            PostgresClient.getInstance(vertxContext.owner(), tenantId).delete(MATERIAL_TYPE_TABLE, materialtypeId, reply -> {
                                try {
                                    if (reply.succeeded()) {
                                        if (reply.result().getUpdated() == 1) {
                                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withNoContent()));
                                        } else {
                                            log.error(messages.getMessage(lang, MessageConsts.DeletedCountError, 1, reply.result().getUpdated()));
                                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainNotFound(messages.getMessage(lang, MessageConsts.DeletedCountError, 1, reply.result().getUpdated()))));
                                        }
                                    } else {
                                        log.error(reply.cause().getMessage(), reply.cause());
                                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                                    }
                                } catch (Exception e) {
                                    log.error(e.getMessage(), e);
                                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                                }
                            });
                        } catch (Exception e) {
                            log.error(e.getMessage(), e);
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                        }
                    } else {
                        log.error(replyHandler.cause().getMessage(), replyHandler.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                    }
                });
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
        }
    });
}
Also used : Item(org.folio.rest.jaxrs.model.Item) List(java.util.List) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 24 with Validate

use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.

the class LoanTypeAPI method getLoanTypesByLoantypeId.

@Validate
@Override
public void getLoanTypesByLoantypeId(String loantypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    vertxContext.runOnContext(v -> {
        try {
            Criterion c = new Criterion(new Criteria().addField(idFieldName).setJSONB(false).setOperation("=").setValue("'" + loantypeId + "'"));
            getPostgresClient(vertxContext, okapiHeaders).get(LOAN_TYPE_TABLE, Loantype.class, c, true, reply -> {
                try {
                    if (reply.failed()) {
                        String msg = PgExceptionUtil.badRequestMessage(reply.cause());
                        if (msg == null) {
                            internalServerErrorDuringGetById(reply.cause(), lang, asyncResultHandler);
                            return;
                        }
                        log.info(msg);
                        asyncResultHandler.handle(Future.succeededFuture(GetLoanTypesByLoantypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    @SuppressWarnings("unchecked") List<Loantype> userGroup = (List<Loantype>) reply.result().getResults();
                    if (userGroup.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetLoanTypesByLoantypeIdResponse.withPlainNotFound(loantypeId)));
                    } else {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetLoanTypesByLoantypeIdResponse.withJsonOK(userGroup.get(0))));
                    }
                } catch (Exception e) {
                    internalServerErrorDuringGetById(e, lang, asyncResultHandler);
                }
            });
        } catch (Exception e) {
            internalServerErrorDuringGetById(e, lang, asyncResultHandler);
        }
    });
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) List(java.util.List) Criteria(org.folio.rest.persist.Criteria.Criteria) Loantype(org.folio.rest.jaxrs.model.Loantype) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 25 with Validate

use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.

the class LoanTypeAPI method postLoanTypes.

@Validate
@Override
public void postLoanTypes(String lang, Loantype 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);
            }
            getPostgresClient(vertxContext, okapiHeaders).save(LOAN_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(PostLoanTypesResponse.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(PostLoanTypesResponse.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

Validate (org.folio.rest.annotations.Validate)66 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)42 CQLParseException (org.z3950.zing.cql.CQLParseException)32 List (java.util.List)27 OutStream (org.folio.rest.tools.utils.OutStream)27 Criteria (org.folio.rest.persist.Criteria.Criteria)16 Criterion (org.folio.rest.persist.Criteria.Criterion)16 PostgresClient (org.folio.rest.persist.PostgresClient)16 CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)13 JsonObject (io.vertx.core.json.JsonObject)4 Map (java.util.Map)4 Response (javax.ws.rs.core.Response)4 TenantTool (org.folio.rest.tools.utils.TenantTool)4 InputStream (java.io.InputStream)3 BodyPart (javax.mail.BodyPart)3 io.vertx.core (io.vertx.core)2 AsyncResult (io.vertx.core.AsyncResult)2 Context (io.vertx.core.Context)2 Handler (io.vertx.core.Handler)2 Logger (io.vertx.core.logging.Logger)2