Search in sources :

Example 41 with Validate

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

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

the class MaterialTypeAPI method getMaterialTypesByMaterialtypeId.

@Validate
@Override
public void getMaterialTypesByMaterialtypeId(String materialtypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.calculateTenantId(okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT));
            Criterion c = new Criterion(new Criteria().addField(idFieldName).setJSONB(false).setOperation("=").setValue("'" + materialtypeId + "'"));
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(MATERIAL_TYPE_TABLE, Mtype.class, c, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        @SuppressWarnings("unchecked") List<Mtype> userGroup = (List<Mtype>) reply.result().getResults();
                        if (userGroup.isEmpty()) {
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withPlainNotFound(materialtypeId)));
                        } else {
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withJsonOK(userGroup.get(0))));
                        }
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        if (isInvalidUUID(reply.cause().getMessage())) {
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withPlainNotFound(materialtypeId)));
                        } else {
                            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                        }
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
        }
    });
}
Also used : Mtype(org.folio.rest.jaxrs.model.Mtype) Criterion(org.folio.rest.persist.Criteria.Criterion) List(java.util.List) Criteria(org.folio.rest.persist.Criteria.Criteria) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 43 with Validate

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

the class MaterialTypeAPI method getMaterialTypes.

@Validate
@Override
public void getMaterialTypes(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/material-types
     */
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.calculateTenantId(okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT));
            CQLWrapper cql = getCQL(query, limit, offset);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(MATERIAL_TYPE_TABLE, Mtype.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        Mtypes mtypes = new Mtypes();
                        @SuppressWarnings("unchecked") List<Mtype> mtype = (List<Mtype>) reply.result().getResults();
                        mtypes.setMtypes(mtype);
                        mtypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesResponse.withJsonOK(mtypes)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String message = messages.getMessage(lang, MessageConsts.InternalServerError);
            if (e.getCause() != null && e.getCause().getClass().getSimpleName().endsWith("CQLParseException")) {
                message = " CQL parse error " + e.getLocalizedMessage();
            }
            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : Mtype(org.folio.rest.jaxrs.model.Mtype) List(java.util.List) Mtypes(org.folio.rest.jaxrs.model.Mtypes) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 44 with Validate

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

the class LoanTypeAPI method getLoanTypes.

@Validate
@Override
public void getLoanTypes(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/loan-types
     */
    vertxContext.runOnContext(v -> {
        try {
            CQLWrapper cql = getCQL(query, limit, offset);
            getPostgresClient(vertxContext, okapiHeaders).get(LOAN_TYPE_TABLE, Loantype.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        Loantypes loantypes = new Loantypes();
                        @SuppressWarnings("unchecked") List<Loantype> loantype = (List<Loantype>) reply.result().getResults();
                        loantypes.setLoantypes(loantype);
                        loantypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetLoanTypesResponse.withJsonOK(loantypes)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetLoanTypesResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetLoanTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String message = messages.getMessage(lang, MessageConsts.InternalServerError);
            if (e.getCause() instanceof CQLParseException) {
                message = " CQL parse error " + e.getLocalizedMessage();
            }
            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetLoanTypesResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : Loantypes(org.folio.rest.jaxrs.model.Loantypes) List(java.util.List) Loantype(org.folio.rest.jaxrs.model.Loantype) CQLParseException(org.z3950.zing.cql.CQLParseException) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 45 with Validate

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

the class ClassificationTypeAPI method getClassificationTypes.

@Validate
@Override
public void getClassificationTypes(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/classification-types
     */
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            CQLWrapper cql = getCQL(query, limit, offset);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CLASSIFICATION_TYPE_TABLE, ClassificationType.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        ClassificationTypes instanceTypes = new ClassificationTypes();
                        @SuppressWarnings("unchecked") List<ClassificationType> instanceType = (List<ClassificationType>) reply.result().getResults();
                        instanceTypes.setClassificationTypes(instanceType);
                        instanceTypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetClassificationTypesResponse.withJsonOK(instanceTypes)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetClassificationTypesResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetClassificationTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String message = messages.getMessage(lang, MessageConsts.InternalServerError);
            if (e.getCause() instanceof CQLParseException) {
                message = " CQL parse error " + e.getLocalizedMessage();
            }
            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetClassificationTypesResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : ClassificationTypes(org.folio.rest.jaxrs.model.ClassificationTypes) List(java.util.List) ClassificationType(org.folio.rest.jaxrs.model.ClassificationType) CQLParseException(org.z3950.zing.cql.CQLParseException) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) 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