Search in sources :

Example 21 with CQLWrapper

use of org.folio.rest.persist.cql.CQLWrapper 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)

Example 22 with CQLWrapper

use of org.folio.rest.persist.cql.CQLWrapper 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 23 with CQLWrapper

use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.

the class ShelfLocationAPI method getShelfLocations.

/**
 * Get a list of the new locations, and fake old kind of shelf-locations out
 * of them.
 */
@Override
public void getShelfLocations(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    try {
        String tenantId = getTenant(okapiHeaders);
        CQLWrapper cql = getCQL(query, limit, offset, LocationAPI.LOCATION_TABLE);
        PostgresClient.getInstance(vertxContext.owner(), tenantId).get(LocationAPI.LOCATION_TABLE, Location.class, new String[] { "*" }, cql, true, true, reply -> {
            try {
                if (reply.failed()) {
                    String message = logAndSaveError(reply.cause());
                    asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsResponse.withPlainBadRequest(message)));
                } else {
                    Shelflocations shelfLocations = new Shelflocations();
                    List<Location> locationsList = (List<Location>) reply.result().getResults();
                    List<Shelflocation> shelfLocationsList = new ArrayList<>(locationsList.size());
                    for (Location loc : locationsList) {
                        Shelflocation sl = new Shelflocation();
                        sl.setId(loc.getId());
                        sl.setName(loc.getName());
                        shelfLocationsList.add(sl);
                    }
                    shelfLocations.setShelflocations(shelfLocationsList);
                    shelfLocations.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                    asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsResponse.withJsonOK(shelfLocations)));
                }
            } catch (Exception e) {
                String message = logAndSaveError(e);
                asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsResponse.withPlainInternalServerError(message)));
            }
        });
    } catch (Exception e) {
        String message = logAndSaveError(e);
        asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsResponse.withPlainInternalServerError(message)));
    }
}
Also used : Shelflocations(org.folio.rest.jaxrs.model.Shelflocations) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) NotImplementedException(org.apache.commons.lang.NotImplementedException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Location(org.folio.rest.jaxrs.model.Location) Shelflocation(org.folio.rest.jaxrs.model.Shelflocation)

Example 24 with CQLWrapper

use of org.folio.rest.persist.cql.CQLWrapper 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 25 with CQLWrapper

use of org.folio.rest.persist.cql.CQLWrapper 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

CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)32 List (java.util.List)24 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)22 Validate (org.folio.rest.annotations.Validate)12 CQL2PgJSON (org.z3950.zing.cql.cql2pgjson.CQL2PgJSON)12 PostgresClient (org.folio.rest.persist.PostgresClient)9 CQLParseException (org.z3950.zing.cql.CQLParseException)8 Test (org.junit.Test)6 Limit (org.folio.rest.persist.Criteria.Limit)4 Offset (org.folio.rest.persist.Criteria.Offset)4 HoldingsRecord (org.folio.rest.jaxrs.model.HoldingsRecord)3 Instance (org.folio.rest.jaxrs.model.Instance)3 ArrayList (java.util.ArrayList)2 OutStream (org.folio.rest.tools.utils.OutStream)2 io.vertx.core (io.vertx.core)1 Logger (io.vertx.core.logging.Logger)1 LoggerFactory (io.vertx.core.logging.LoggerFactory)1 Async (io.vertx.ext.unit.Async)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1