Search in sources :

Example 1 with InstanceType

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

the class InstanceTypeAPI method getInstanceTypesByInstanceTypeId.

@Validate
@Override
public void getInstanceTypesByInstanceTypeId(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(INSTANCE_TYPE_TABLE, InstanceType.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(GetInstanceTypesByInstanceTypeIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    @SuppressWarnings("unchecked") List<InstanceType> instanceType = (List<InstanceType>) reply.result().getResults();
                    if (instanceType.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceTypesByInstanceTypeIdResponse.withPlainNotFound(instanceTypeId)));
                    } else {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceTypesByInstanceTypeIdResponse.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) InstanceType(org.folio.rest.jaxrs.model.InstanceType) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 2 with InstanceType

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

the class InstanceTypeAPI method getInstanceTypes.

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

Aggregations

List (java.util.List)2 Validate (org.folio.rest.annotations.Validate)2 InstanceType (org.folio.rest.jaxrs.model.InstanceType)2 CQLParseException (org.z3950.zing.cql.CQLParseException)2 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)2 InstanceTypes (org.folio.rest.jaxrs.model.InstanceTypes)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