Search in sources :

Example 26 with Criterion

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

the class LocationAPI method deleteLocationsById.

@Override
public void deleteLocationsById(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;
    }
    locationInUse(id, tenantId, vertxContext).setHandler(res -> {
        if (res.failed()) {
            String message = logAndSaveError(res.cause());
            DeleteLocationsByIdResponse.withPlainInternalServerError(message);
        } else {
            if (res.result()) {
                asyncResultHandler.handle(Future.succeededFuture(DeleteLocationsByIdResponse.withPlainBadRequest("Cannot delete location, as it is in use")));
            } else {
                PostgresClient.getInstance(vertxContext.owner(), tenantId).delete(LOCATION_TABLE, criterion, deleteReply -> {
                    if (deleteReply.failed()) {
                        logAndSaveError(deleteReply.cause());
                        asyncResultHandler.handle(Future.succeededFuture(DeleteLocationsByIdResponse.withPlainNotFound("Not found")));
                    } else {
                        asyncResultHandler.handle(Future.succeededFuture(DeleteLocationsByIdResponse.withNoContent()));
                    }
                });
            }
        }
    });
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) Criteria(org.folio.rest.persist.Criteria.Criteria) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Example 27 with Criterion

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

the class LocationAPI method putLocationsById.

@Override
public void putLocationsById(String id, String lang, Location entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
    if (!id.equals(entity.getId())) {
        String message = "Illegal operation: id cannot be changed";
        asyncResultHandler.handle(Future.succeededFuture(PutLocationsByIdResponse.withPlainBadRequest(message)));
        return;
    }
    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(PutLocationsByIdResponse.withPlainInternalServerError(message)));
        return;
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).update(LOCATION_TABLE, entity, criterion, false, updateReply -> {
        if (updateReply.failed()) {
            String message = logAndSaveError(updateReply.cause());
            asyncResultHandler.handle(Future.succeededFuture(PutLocationsByIdResponse.withPlainInternalServerError(message)));
        } else {
            if (updateReply.result().getUpdated() == 0) {
                asyncResultHandler.handle(Future.succeededFuture(PutLocationsByIdResponse.withPlainNotFound("Not found")));
            // Not found
            } else {
                asyncResultHandler.handle(Future.succeededFuture(PutLocationsByIdResponse.withNoContent()));
            }
        }
    });
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) Criteria(org.folio.rest.persist.Criteria.Criteria) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Example 28 with Criterion

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

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

the class ContributorTypeAPI method getContributorTypesByContributorTypeId.

@Validate
@Override
public void getContributorTypesByContributorTypeId(String contributorTypeId, 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("'" + contributorTypeId + "'"));
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CONTRIBUTOR_TYPE_TABLE, ContributorType.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(GetContributorTypesByContributorTypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    @SuppressWarnings("unchecked") List<ContributorType> contributorType = (List<ContributorType>) reply.result().getResults();
                    if (contributorType.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withPlainNotFound(contributorTypeId)));
                    } else {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withJsonOK(contributorType.get(0))));
                    }
                } catch (Exception e) {
                    internalServerErrorDuringGetById(e, lang, asyncResultHandler);
                }
            });
        } catch (Exception e) {
            internalServerErrorDuringGetById(e, lang, asyncResultHandler);
        }
    });
}
Also used : ContributorType(org.folio.rest.jaxrs.model.ContributorType) Criterion(org.folio.rest.persist.Criteria.Criterion) List(java.util.List) Criteria(org.folio.rest.persist.Criteria.Criteria) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 30 with Criterion

use of org.folio.rest.persist.Criteria.Criterion in project raml-module-builder by folio-org.

the class JobAPI method getJobsJobconfsByJobconfsId.

@Validate
@Override
public void getJobsJobconfsByJobconfsId(String jobconfsId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    System.out.println("sending... getJobsJobconfsByJobconfsId");
    try {
        Criteria c = new Criteria();
        c.addField("_id");
        c.setOperation("=");
        c.setValue(jobconfsId);
        c.setJSONB(false);
        vertxContext.runOnContext(v -> {
            try {
                PostgresClient.getInstance(vertxContext.owner()).get(RTFConsts.JOB_CONF_COLLECTION, JobConf.class, new Criterion(c), true, reply -> {
                    @SuppressWarnings("unchecked") List<JobConf> confs = (List<JobConf>) reply.result().getResults();
                    if (confs.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withPlainNotFound("JobConf " + messages.getMessage(lang, "10008"))));
                        return;
                    }
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withJsonOK(confs.get(0))));
                });
            } catch (Exception e) {
                log.error(e);
                asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
            }
        });
    } catch (Exception e) {
        log.error(e);
        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetJobsJobconfsByJobconfsIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
    }
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) List(java.util.List) Criteria(org.folio.rest.persist.Criteria.Criteria) JobConf(org.folio.rest.jaxrs.model.JobConf) 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