Search in sources :

Example 1 with Mtype

use of org.folio.rest.jaxrs.model.Mtype 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 2 with Mtype

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

the class MaterialTypeAPI method getMaterialTypes.

@Validate
@Override
public void getMaterialTypes(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/material-types
     */
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.calculateTenantId(okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT));
            CQLWrapper cql = getCQL(query, limit, offset);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(MATERIAL_TYPE_TABLE, Mtype.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        Mtypes mtypes = new Mtypes();
                        @SuppressWarnings("unchecked") List<Mtype> mtype = (List<Mtype>) reply.result().getResults();
                        mtypes.setMtypes(mtype);
                        mtypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesResponse.withJsonOK(mtypes)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String message = messages.getMessage(lang, MessageConsts.InternalServerError);
            if (e.getCause() != null && e.getCause().getClass().getSimpleName().endsWith("CQLParseException")) {
                message = " CQL parse error " + e.getLocalizedMessage();
            }
            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : Mtype(org.folio.rest.jaxrs.model.Mtype) List(java.util.List) Mtypes(org.folio.rest.jaxrs.model.Mtypes) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) 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 Mtype (org.folio.rest.jaxrs.model.Mtype)2 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)2 Mtypes (org.folio.rest.jaxrs.model.Mtypes)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