Search in sources :

Example 1 with Shelflocation

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

the class ShelfLocationAPI method getShelfLocationsById.

/**
 * Get a new-kind of Location object, and convert it to old-style
 * shelf-location.
 */
@Override
public void getShelfLocationsById(String id, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    try {
        String tenantId = getTenant(okapiHeaders);
        Criteria criteria = new Criteria(LOCATION_SCHEMA_PATH);
        criteria.addField(ID_FIELD_NAME);
        criteria.setOperation("=");
        criteria.setValue(id);
        Criterion criterion = new Criterion(criteria);
        PostgresClient.getInstance(vertxContext.owner(), tenantId).get(LOCATION_TABLE, Location.class, criterion, true, false, getReply -> {
            if (getReply.failed()) {
                String message = logAndSaveError(getReply.cause());
                asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsByIdResponse.withPlainInternalServerError(message)));
            } else {
                List<Location> locationList = (List<Location>) getReply.result().getResults();
                if (locationList.size() < 1) {
                    asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsByIdResponse.withPlainNotFound(messages.getMessage(lang, MessageConsts.ObjectDoesNotExist))));
                } else if (locationList.size() > 1) {
                    String message = "Multiple locations found with the same id";
                    logger.error(message);
                    asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsByIdResponse.withPlainInternalServerError(message)));
                } else {
                    Location loc = locationList.get(0);
                    Shelflocation sl = new Shelflocation();
                    sl.setId(loc.getId());
                    sl.setName(loc.getName());
                    asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsByIdResponse.withJsonOK(sl)));
                }
            }
        });
    } catch (Exception e) {
        String message = logAndSaveError(e);
        asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsByIdResponse.withPlainInternalServerError(message)));
    }
}
Also used : Criterion(org.folio.rest.persist.Criteria.Criterion) ArrayList(java.util.ArrayList) List(java.util.List) Criteria(org.folio.rest.persist.Criteria.Criteria) NotImplementedException(org.apache.commons.lang.NotImplementedException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Location(org.folio.rest.jaxrs.model.Location) Shelflocation(org.folio.rest.jaxrs.model.Shelflocation)

Example 2 with Shelflocation

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

the class ShelfLocationAPI method getShelfLocations.

/**
 * Get a list of the new locations, and fake old kind of shelf-locations out
 * of them.
 */
@Override
public void getShelfLocations(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    try {
        String tenantId = getTenant(okapiHeaders);
        CQLWrapper cql = getCQL(query, limit, offset, LocationAPI.LOCATION_TABLE);
        PostgresClient.getInstance(vertxContext.owner(), tenantId).get(LocationAPI.LOCATION_TABLE, Location.class, new String[] { "*" }, cql, true, true, reply -> {
            try {
                if (reply.failed()) {
                    String message = logAndSaveError(reply.cause());
                    asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsResponse.withPlainBadRequest(message)));
                } else {
                    Shelflocations shelfLocations = new Shelflocations();
                    List<Location> locationsList = (List<Location>) reply.result().getResults();
                    List<Shelflocation> shelfLocationsList = new ArrayList<>(locationsList.size());
                    for (Location loc : locationsList) {
                        Shelflocation sl = new Shelflocation();
                        sl.setId(loc.getId());
                        sl.setName(loc.getName());
                        shelfLocationsList.add(sl);
                    }
                    shelfLocations.setShelflocations(shelfLocationsList);
                    shelfLocations.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                    asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsResponse.withJsonOK(shelfLocations)));
                }
            } catch (Exception e) {
                String message = logAndSaveError(e);
                asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsResponse.withPlainInternalServerError(message)));
            }
        });
    } catch (Exception e) {
        String message = logAndSaveError(e);
        asyncResultHandler.handle(Future.succeededFuture(GetShelfLocationsResponse.withPlainInternalServerError(message)));
    }
}
Also used : Shelflocations(org.folio.rest.jaxrs.model.Shelflocations) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) NotImplementedException(org.apache.commons.lang.NotImplementedException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Location(org.folio.rest.jaxrs.model.Location) Shelflocation(org.folio.rest.jaxrs.model.Shelflocation)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 NotImplementedException (org.apache.commons.lang.NotImplementedException)2 Location (org.folio.rest.jaxrs.model.Location)2 Shelflocation (org.folio.rest.jaxrs.model.Shelflocation)2 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)2 Shelflocations (org.folio.rest.jaxrs.model.Shelflocations)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