Search in sources :

Example 1 with InstanceFormat

use of org.folio.rest.jaxrs.model.InstanceFormat in project mod-inventory-storage by folio-org.

the class InstanceFormatAPI method getInstanceFormats.

@Validate
@Override
public void getInstanceFormats(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/instance-formats
     */
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            CQLWrapper cql = getCQL(query, limit, offset);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(INSTANCE_FORMAT_TABLE, InstanceFormat.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        InstanceFormats instanceFormats = new InstanceFormats();
                        @SuppressWarnings("unchecked") List<InstanceFormat> instanceFormat = (List<InstanceFormat>) reply.result().getResults();
                        instanceFormats.setInstanceFormats(instanceFormat);
                        instanceFormats.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceFormatsResponse.withJsonOK(instanceFormats)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceFormatsResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceFormatsResponse.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(GetInstanceFormatsResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : InstanceFormats(org.folio.rest.jaxrs.model.InstanceFormats) List(java.util.List) 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) InstanceFormat(org.folio.rest.jaxrs.model.InstanceFormat) Validate(org.folio.rest.annotations.Validate)

Example 2 with InstanceFormat

use of org.folio.rest.jaxrs.model.InstanceFormat in project mod-inventory-storage by folio-org.

the class InstanceFormatAPI method getInstanceFormatsByInstanceFormatId.

@Validate
@Override
public void getInstanceFormatsByInstanceFormatId(String instanceFormatId, 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("'" + instanceFormatId + "'"));
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(INSTANCE_FORMAT_TABLE, InstanceFormat.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(GetInstanceFormatsByInstanceFormatIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    @SuppressWarnings("unchecked") List<InstanceFormat> instanceFormat = (List<InstanceFormat>) reply.result().getResults();
                    if (instanceFormat.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceFormatsByInstanceFormatIdResponse.withPlainNotFound(instanceFormatId)));
                    } else {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceFormatsByInstanceFormatIdResponse.withJsonOK(instanceFormat.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) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) InstanceFormat(org.folio.rest.jaxrs.model.InstanceFormat) Validate(org.folio.rest.annotations.Validate)

Aggregations

List (java.util.List)2 Validate (org.folio.rest.annotations.Validate)2 InstanceFormat (org.folio.rest.jaxrs.model.InstanceFormat)2 CQLParseException (org.z3950.zing.cql.CQLParseException)2 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)2 InstanceFormats (org.folio.rest.jaxrs.model.InstanceFormats)1 Criteria (org.folio.rest.persist.Criteria.Criteria)1 Criterion (org.folio.rest.persist.Criteria.Criterion)1 CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)1