Search in sources :

Example 16 with Criterion

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

the class ContributorNameTypeAPI method getContributorNameTypesByContributorNameTypeId.

@Validate
@Override
public void getContributorNameTypesByContributorNameTypeId(String ContributorNameTypeId, 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("'" + ContributorNameTypeId + "'"));
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CONTRIBUTOR_NAME_TYPE_TABLE, ContributorNameType.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(GetContributorNameTypesByContributorNameTypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    @SuppressWarnings("unchecked") List<ContributorNameType> ContributorNameType = (List<ContributorNameType>) reply.result().getResults();
                    if (ContributorNameType.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorNameTypesByContributorNameTypeIdResponse.withPlainNotFound(ContributorNameTypeId)));
                    } else {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorNameTypesByContributorNameTypeIdResponse.withJsonOK(ContributorNameType.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) ContributorNameType(org.folio.rest.jaxrs.model.ContributorNameType) 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 17 with Criterion

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

the class LocationUnitAPI method putLocationUnitsInstitutionsById.

@Override
public void putLocationUnitsInstitutionsById(String id, String lang, Locinst 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(LocationUnitsResource.PutLocationUnitsInstitutionsByIdResponse.withPlainBadRequest(message)));
        return;
    }
    String tenantId = getTenant(okapiHeaders);
    Criterion criterion;
    try {
        Criteria criteria = new Criteria(INST_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(LocationUnitsResource.PutLocationUnitsInstitutionsByIdResponse.withPlainInternalServerError(message)));
        return;
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).update(INSTITUTION_TABLE, entity, criterion, false, updateReply -> {
        if (updateReply.failed()) {
            String message = logAndSaveError(updateReply.cause());
            asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PutLocationUnitsInstitutionsByIdResponse.withPlainInternalServerError(message)));
        } else {
            if (updateReply.result().getUpdated() == 0) {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PutLocationUnitsInstitutionsByIdResponse.withPlainNotFound("Institution not found")));
            } else {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PutLocationUnitsInstitutionsByIdResponse.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 18 with Criterion

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

the class LocationUnitAPI method deleteLocationUnitsLibrariesById.

@Override
public void deleteLocationUnitsLibrariesById(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(LIB_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(LocationUnitsResource.DeleteLocationUnitsLibrariesByIdResponse.withPlainInternalServerError(message)));
        return;
    }
    libInUse(id, tenantId, vertxContext).setHandler(res -> {
        if (res.failed()) {
            String message = logAndSaveError(res.cause());
            LocationUnitsResource.DeleteLocationUnitsLibrariesByIdResponse.withPlainInternalServerError(message);
        } else {
            if (res.result()) {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.DeleteLocationUnitsLibrariesByIdResponse.withPlainBadRequest("Cannot delete library, as it is in use")));
            } else {
                PostgresClient.getInstance(vertxContext.owner(), tenantId).delete(LIBRARY_TABLE, criterion, deleteReply -> {
                    if (deleteReply.failed()) {
                        logAndSaveError(deleteReply.cause());
                        asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.DeleteLocationUnitsLibrariesByIdResponse.withPlainNotFound("Library not found")));
                    } else {
                        asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.DeleteLocationUnitsLibrariesByIdResponse.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 19 with Criterion

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

the class LocationUnitAPI method putLocationUnitsLibrariesById.

@Override
public void putLocationUnitsLibrariesById(String id, String lang, Loclib 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(LocationUnitsResource.PutLocationUnitsLibrariesByIdResponse.withPlainBadRequest(message)));
        return;
    }
    String tenantId = getTenant(okapiHeaders);
    Criterion criterion;
    try {
        Criteria criteria = new Criteria(LIB_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(LocationUnitsResource.PutLocationUnitsLibrariesByIdResponse.withPlainInternalServerError(message)));
        return;
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).update(LIBRARY_TABLE, entity, criterion, false, updateReply -> {
        if (updateReply.failed()) {
            String message = logAndSaveError(updateReply.cause());
            asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PutLocationUnitsLibrariesByIdResponse.withPlainInternalServerError(message)));
        } else {
            if (updateReply.result().getUpdated() == 0) {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PutLocationUnitsLibrariesByIdResponse.withPlainNotFound("Library not found")));
            } else {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PutLocationUnitsLibrariesByIdResponse.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 20 with Criterion

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

the class LocationUnitAPI method getLocationUnitsInstitutionsById.

@Override
public void getLocationUnitsInstitutionsById(String id, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
    Criterion criterion;
    String tenantId = getTenant(okapiHeaders);
    try {
        Criteria criteria = new Criteria(INST_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(LocationUnitsResource.GetLocationUnitsInstitutionsByIdResponse.withPlainInternalServerError(message)));
        return;
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).get(INSTITUTION_TABLE, Locinst.class, criterion, true, false, getReply -> {
        if (getReply.failed()) {
            String message = logAndSaveError(getReply.cause());
            asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsInstitutionsByIdResponse.withPlainInternalServerError(message)));
        } else {
            List<Locinst> instlist = (List<Locinst>) getReply.result().getResults();
            if (instlist.isEmpty()) {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsInstitutionsByIdResponse.withPlainNotFound(messages.getMessage(lang, MessageConsts.ObjectDoesNotExist))));
            // We can safely ignore the case that we have more than one with
            // the same id, RMB has a primary key on ID, will not allow it
            } else {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsInstitutionsByIdResponse.withJsonOK(instlist.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) Locinst(org.folio.rest.jaxrs.model.Locinst)

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