Search in sources :

Example 11 with Criterion

use of org.folio.rest.persist.Criteria.Criterion in project mod-inventory-storage by folio-org.

the class ItemStorageAPI method getShelfLocation.

private Future<Location> getShelfLocation(Vertx vertx, String tenantId, String locationId) {
    Future<Location> future = Future.future();
    try {
        Criteria crit = new Criteria(LocationAPI.LOCATION_SCHEMA_PATH);
        crit.addField(LocationAPI.ID_FIELD_NAME);
        crit.setOperation("=");
        crit.setValue(locationId);
        PostgresClient.getInstance(vertx, tenantId).get(LocationAPI.LOCATION_TABLE, Location.class, new Criterion(crit), true, false, getReply -> {
            if (getReply.failed()) {
                future.fail(getReply.cause());
            } else {
                List<Location> locationList = (List<Location>) getReply.result().getResults();
                if (locationList.size() < 1) {
                    future.fail("No location found");
                } else {
                    future.complete(locationList.get(0));
                }
            }
        });
    } catch (Exception e) {
        future.fail(e);
    }
    return future;
}
Also used : 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)

Example 12 with Criterion

use of org.folio.rest.persist.Criteria.Criterion in project mod-inventory-storage by folio-org.

the class LocationAPI method getLocationsById.

@Override
public void getLocationsById(String id, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
    String tenantId = getTenant(okapiHeaders);
    Criterion criterion;
    try {
        Criteria criteria = new Criteria(LOCATION_SCHEMA_PATH);
        criteria.addField(ID_FIELD_NAME);
        criteria.setOperation("=");
        criteria.setValue(id);
        criterion = new Criterion(criteria);
    } catch (Exception e) {
        String message = logAndSaveError(e);
        asyncResultHandler.handle(Future.succeededFuture(GetLocationsByIdResponse.withPlainInternalServerError(message)));
        return;
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).get(LOCATION_TABLE, Location.class, criterion, true, false, getReply -> {
        if (getReply.failed()) {
            String message = logAndSaveError(getReply.cause());
            asyncResultHandler.handle(Future.succeededFuture(GetLocationsByIdResponse.withPlainInternalServerError(message)));
        } else {
            List<Location> locationList = (List<Location>) getReply.result().getResults();
            if (locationList.isEmpty()) {
                asyncResultHandler.handle(Future.succeededFuture(GetLocationsByIdResponse.withPlainNotFound(messages.getMessage(lang, MessageConsts.ObjectDoesNotExist))));
            } else if (locationList.size() > 1) {
                String message = "Multiple locations found with the same id";
                logger.error(message);
                asyncResultHandler.handle(Future.succeededFuture(GetLocationsByIdResponse.withPlainInternalServerError(message)));
            } else {
                asyncResultHandler.handle(Future.succeededFuture(GetLocationsByIdResponse.withJsonOK(locationList.get(0))));
            }
        }
    });
}
Also used : 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) Location(org.folio.rest.jaxrs.model.Location)

Example 13 with Criterion

use of org.folio.rest.persist.Criteria.Criterion in project mod-inventory-storage by folio-org.

the class ShelfLocationAPI method getShelfLocationsById.

/**
 * Get a new-kind of Location object, and convert it to old-style
 * shelf-location.
 */
@Override
public void getShelfLocationsById(String id, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    try {
        String tenantId = getTenant(okapiHeaders);
        Criteria criteria = new Criteria(LOCATION_SCHEMA_PATH);
        criteria.addField(ID_FIELD_NAME);
        criteria.setOperation("=");
        criteria.setValue(id);
        Criterion criterion = new Criterion(criteria);
        PostgresClient.getInstance(vertxContext.owner(), tenantId).get(LOCATION_TABLE, Location.class, criterion, true, false, getReply -> {
            if (getReply.failed()) {
                String message = logAndSaveError(getReply.cause());
                asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsByIdResponse.withPlainInternalServerError(message)));
            } else {
                List<Location> locationList = (List<Location>) getReply.result().getResults();
                if (locationList.size() < 1) {
                    asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsByIdResponse.withPlainNotFound(messages.getMessage(lang, MessageConsts.ObjectDoesNotExist))));
                } else if (locationList.size() > 1) {
                    String message = "Multiple locations found with the same id";
                    logger.error(message);
                    asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsByIdResponse.withPlainInternalServerError(message)));
                } else {
                    Location loc = locationList.get(0);
                    Shelflocation sl = new Shelflocation();
                    sl.setId(loc.getId());
                    sl.setName(loc.getName());
                    asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsByIdResponse.withJsonOK(sl)));
                }
            }
        });
    } catch (Exception e) {
        String message = logAndSaveError(e);
        asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsByIdResponse.withPlainInternalServerError(message)));
    }
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) ArrayList(java.util.ArrayList) List(java.util.List) Criteria(org.folio.rest.persist.Criteria.Criteria) 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 14 with Criterion

use of org.folio.rest.persist.Criteria.Criterion 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 15 with Criterion

use of org.folio.rest.persist.Criteria.Criterion in project mod-inventory-storage by folio-org.

the class ClassificationTypeAPI method getClassificationTypesByClassificationTypeId.

@Validate
@Override
public void getClassificationTypesByClassificationTypeId(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);
            Criterion c = new Criterion(new Criteria().addField(idFieldName).setJSONB(false).setOperation("=").setValue("'" + instanceTypeId + "'"));
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CLASSIFICATION_TYPE_TABLE, ClassificationType.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(GetClassificationTypesByClassificationTypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    @SuppressWarnings("unchecked") List<ClassificationType> instanceType = (List<ClassificationType>) reply.result().getResults();
                    if (instanceType.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetClassificationTypesByClassificationTypeIdResponse.withPlainNotFound(instanceTypeId)));
                    } else {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetClassificationTypesByClassificationTypeIdResponse.withJsonOK(instanceType.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) ClassificationType(org.folio.rest.jaxrs.model.ClassificationType) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Aggregations

Criterion (org.folio.rest.persist.Criteria.Criterion)32 Criteria (org.folio.rest.persist.Criteria.Criteria)31 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)26 List (java.util.List)18 Validate (org.folio.rest.annotations.Validate)14 CQLParseException (org.z3950.zing.cql.CQLParseException)8 PostgresClient (org.folio.rest.persist.PostgresClient)3 IOException (java.io.IOException)2 JobConf (org.folio.rest.jaxrs.model.JobConf)2 Location (org.folio.rest.jaxrs.model.Location)2 Limit (org.folio.rest.persist.Criteria.Limit)2 Offset (org.folio.rest.persist.Criteria.Offset)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)1 SQLConnection (io.vertx.ext.sql.SQLConnection)1 Async (io.vertx.ext.unit.Async)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 NotImplementedException (org.apache.commons.lang.NotImplementedException)1