Search in sources :

Example 36 with Validate

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

the class InstanceTypeAPI method deleteInstanceTypesByInstanceTypeId.

@Validate
@Override
public void deleteInstanceTypesByInstanceTypeId(String instanceTypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            PostgresClient postgres = PostgresClient.getInstance(vertxContext.owner(), tenantId);
            postgres.delete(INSTANCE_TYPE_TABLE, instanceTypeId, reply -> {
                try {
                    if (reply.failed()) {
                        String msg = PgExceptionUtil.badRequestMessage(reply.cause());
                        if (msg == null) {
                            internalServerErrorDuringDelete(reply.cause(), lang, asyncResultHandler);
                            return;
                        }
                        log.info(msg);
                        asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceTypesByInstanceTypeIdResponse.withPlainBadRequest(msg)));
                        return;
                    }
                    int updated = reply.result().getUpdated();
                    if (updated != 1) {
                        String msg = messages.getMessage(lang, MessageConsts.DeletedCountError, 1, updated);
                        log.error(msg);
                        asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceTypesByInstanceTypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceTypesByInstanceTypeIdResponse.withNoContent()));
                } catch (Exception e) {
                    internalServerErrorDuringDelete(e, lang, asyncResultHandler);
                }
            });
        } catch (Exception e) {
            internalServerErrorDuringDelete(e, lang, asyncResultHandler);
        }
    });
}
Also used : PostgresClient(org.folio.rest.persist.PostgresClient) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 37 with Validate

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

the class InstanceTypeAPI method getInstanceTypes.

@Validate
@Override
public void getInstanceTypes(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/instance-types
     */
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            CQLWrapper cql = getCQL(query, limit, offset);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(INSTANCE_TYPE_TABLE, InstanceType.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        InstanceTypes instanceTypes = new InstanceTypes();
                        @SuppressWarnings("unchecked") List<InstanceType> instanceType = (List<InstanceType>) reply.result().getResults();
                        instanceTypes.setInstanceTypes(instanceType);
                        instanceTypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceTypesResponse.withJsonOK(instanceTypes)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceTypesResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceTypesResponse.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(GetInstanceTypesResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : List(java.util.List) InstanceTypes(org.folio.rest.jaxrs.model.InstanceTypes) InstanceType(org.folio.rest.jaxrs.model.InstanceType) 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 38 with Validate

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

the class ItemStorageAPI method getItemStorageItemsByItemId.

@Validate
@Override
public void getItemStorageItemsByItemId(@PathParam("itemId") @NotNull String itemId, @QueryParam("lang") @DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, 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));
        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()));
        vertxContext.runOnContext(v -> {
            try {
                postgresClient.get(getTableName(query), Item.class, fieldList, cql, true, false, reply -> {
                    try {
                        if (reply.succeeded()) {
                            List<Item> itemList = (List<Item>) reply.result().getResults();
                            if (itemList.size() == 1) {
                                Item item = itemList.get(0);
                                asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withJsonOK(item)));
                            } else {
                                asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainNotFound("Not Found")));
                            }
                        } else {
                            Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(reply.cause().getMessage()));
                        }
                    } catch (Exception e) {
                        asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
                    }
                });
            } catch (Exception e) {
                asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
            }
        });
    } catch (Exception e) {
        asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
    }
}
Also used : PostgresClient(org.folio.rest.persist.PostgresClient) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 39 with Validate

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

the class ItemStorageAPI method getItemStorageItems.

@Validate
@Override
public void getItemStorageItems(int offset, int limit, String query, 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 = getCQL(query, limit, offset);
                log.info(String.format("SQL generated by CQL query (%s): %s", query, cql.toString()));
                postgresClient.get(getTableName(query), Item.class, fieldList, cql, true, false, reply -> {
                    try {
                        if (reply.succeeded()) {
                            List<Item> items = (List<Item>) reply.result().getResults();
                            Items itemList = new Items();
                            itemList.setItems(items);
                            itemList.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                            asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withJsonOK(itemList)));
                        } else {
                            asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError(reply.cause().getMessage())));
                        }
                    } catch (Exception e) {
                        if (e.getCause() != null && e.getCause().getClass().getSimpleName().contains("CQLParseException")) {
                            asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainBadRequest("CQL Parsing Error for '" + query + "': " + e.getLocalizedMessage())));
                        } else {
                            asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError("Error")));
                        }
                    }
                });
            } catch (IllegalStateException e) {
                asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainInternalServerError("CQL State Error for '" + query + "': " + e.getLocalizedMessage())));
            } catch (Exception e) {
                if (e.getCause() != null && e.getCause().getClass().getSimpleName().contains("CQLParseException")) {
                    asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainBadRequest("CQL Parsing Error for '" + query + "': " + e.getLocalizedMessage())));
                } else {
                    asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError("Error")));
                }
            }
        });
    } catch (Exception e) {
        if (e.getCause() != null && e.getCause().getClass().getSimpleName().contains("CQLParseException")) {
            asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainBadRequest("CQL Parsing Error for '" + query + "': " + e.getLocalizedMessage())));
        } else {
            asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError("Error")));
        }
    }
}
Also used : PostgresClient(org.folio.rest.persist.PostgresClient) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 40 with Validate

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

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