Search in sources :

Example 6 with Criterion

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

the class LocationUnitAPI method getLocationUnitsLibrariesById.

@Override
public void getLocationUnitsLibrariesById(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.GetLocationUnitsLibrariesByIdResponse.withPlainInternalServerError(message)));
        return;
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).get(LIBRARY_TABLE, Loclib.class, criterion, true, false, getReply -> {
        if (getReply.failed()) {
            String message = logAndSaveError(getReply.cause());
            asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsLibrariesByIdResponse.withPlainInternalServerError(message)));
        } else {
            List<Loclib> items = (List<Loclib>) getReply.result().getResults();
            if (items.isEmpty()) {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsLibrariesByIdResponse.withPlainNotFound(messages.getMessage(lang, MessageConsts.ObjectDoesNotExist))));
            } else if (items.size() > 1) {
                String message = "Multiple locations found with the same id";
                logger.error(message);
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsLibrariesByIdResponse.withPlainInternalServerError(message)));
            } else {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsLibrariesByIdResponse.withJsonOK(items.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) Loclib(org.folio.rest.jaxrs.model.Loclib)

Example 7 with Criterion

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

the class LocationUnitAPI method putLocationUnitsCampusesById.

@Override
public void putLocationUnitsCampusesById(String id, String lang, Loccamp entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    if (!id.equals(entity.getId())) {
        String message = "Illegal operation: id cannot be changed";
        asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PutLocationUnitsCampusesByIdResponse.withPlainBadRequest(message)));
        return;
    }
    String tenantId = getTenant(okapiHeaders);
    Criterion criterion;
    try {
        Criteria criteria = new Criteria(CAMP_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.PutLocationUnitsCampusesByIdResponse.withPlainInternalServerError(message)));
        return;
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).update(CAMPUS_TABLE, entity, criterion, false, updateReply -> {
        if (updateReply.failed()) {
            String message = logAndSaveError(updateReply.cause());
            asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PutLocationUnitsCampusesByIdResponse.withPlainInternalServerError(message)));
        } else {
            if (updateReply.result().getUpdated() == 0) {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PutLocationUnitsCampusesByIdResponse.withPlainNotFound("Campus not found")));
            } else {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PutLocationUnitsCampusesByIdResponse.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 8 with Criterion

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

the class LocationUnitAPI method getLocationUnitsCampusesById.

@Override
public void getLocationUnitsCampusesById(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(CAMP_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.GetLocationUnitsCampusesByIdResponse.withPlainInternalServerError(message)));
        return;
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CAMPUS_TABLE, Loccamp.class, criterion, true, false, getReply -> {
        if (getReply.failed()) {
            String message = logAndSaveError(getReply.cause());
            asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsCampusesByIdResponse.withPlainInternalServerError(message)));
        } else {
            List<Loccamp> items = (List<Loccamp>) getReply.result().getResults();
            if (items.isEmpty()) {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsCampusesByIdResponse.withPlainNotFound(messages.getMessage(lang, MessageConsts.ObjectDoesNotExist))));
            } else if (items.size() > 1) {
                String message = "Multiple campuses found with the same id";
                logger.error(message);
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsCampusesByIdResponse.withPlainInternalServerError(message)));
            } else {
                asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.GetLocationUnitsCampusesByIdResponse.withJsonOK(items.get(0))));
            }
        }
    });
}
Also used : Loccamp(org.folio.rest.jaxrs.model.Loccamp) 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 9 with Criterion

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

the class PlatformAPI method getPlatformsByPlatformId.

@Validate
@Override
public void getPlatformsByPlatformId(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(PLATFORM_TABLE, Platform.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(PlatformsResource.GetPlatformsByPlatformIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    @SuppressWarnings("unchecked") List<Platform> instanceType = (List<Platform>) reply.result().getResults();
                    if (instanceType.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PlatformsResource.GetPlatformsByPlatformIdResponse.withPlainNotFound(instanceTypeId)));
                    } else {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PlatformsResource.GetPlatformsByPlatformIdResponse.withJsonOK(instanceType.get(0))));
                    }
                } catch (Exception e) {
                    internalServerErrorDuringGetById(e, lang, asyncResultHandler);
                }
            });
        } catch (Exception e) {
            internalServerErrorDuringGetById(e, lang, asyncResultHandler);
        }
    });
}
Also used : Platform(org.folio.rest.jaxrs.model.Platform) 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 10 with Criterion

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

the class ItemStorageAPI method deleteItemStorageItemsByItemId.

@Validate
@Override
public void deleteItemStorageItemsByItemId(@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));
        Criteria a = new Criteria();
        a.addField("'id'");
        a.setOperation("=");
        a.setValue(itemId);
        Criterion criterion = new Criterion(a);
        vertxContext.runOnContext(v -> {
            try {
                postgresClient.delete("item", criterion, reply -> {
                    if (reply.succeeded()) {
                        asyncResultHandler.handle(Future.succeededFuture(DeleteItemStorageItemsByItemIdResponse.withNoContent()));
                    } else {
                        asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsByItemIdResponse.withPlainInternalServerError("Error")));
                    }
                });
            } catch (Exception e) {
                asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsByItemIdResponse.withPlainInternalServerError("Error")));
            }
        });
    } catch (Exception e) {
        asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsByItemIdResponse.withPlainInternalServerError("Error")));
    }
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) PostgresClient(org.folio.rest.persist.PostgresClient) Criteria(org.folio.rest.persist.Criteria.Criteria) 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