Search in sources :

Example 1 with Location

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

the class LocationAPI method getLocationsById.

@Override
public void getLocationsById(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(LOCATION_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(GetLocationsByIdResponse.withPlainInternalServerError(message)));
        return;
    }
    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(GetLocationsByIdResponse.withPlainInternalServerError(message)));
        } else {
            List<Location> locationList = (List<Location>) getReply.result().getResults();
            if (locationList.isEmpty()) {
                asyncResultHandler.handle(Future.succeededFuture(GetLocationsByIdResponse.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(GetLocationsByIdResponse.withPlainInternalServerError(message)));
            } else {
                asyncResultHandler.handle(Future.succeededFuture(GetLocationsByIdResponse.withJsonOK(locationList.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) Location(org.folio.rest.jaxrs.model.Location)

Example 2 with Location

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

the class LocationAPI method getLocations.

// Note, this is the way to get rid of unnecessary try-catch blocks. Use the
// same everywhere!
@Override
public void getLocations(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
    String tenantId = getTenant(okapiHeaders);
    CQLWrapper cql;
    try {
        cql = getCQL(query, limit, offset, LOCATION_TABLE);
    } catch (FieldException e) {
        String message = logAndSaveError(e);
        asyncResultHandler.handle(Future.succeededFuture(GetLocationsResponse.withPlainBadRequest(message)));
        return;
    }
    PostgresClient.getInstance(vertxContext.owner(), tenantId).get(LOCATION_TABLE, Location.class, new String[] { "*" }, cql, true, true, reply -> {
        // netbeans, please indent here!
        if (reply.failed()) {
            String message = logAndSaveError(reply.cause());
            asyncResultHandler.handle(Future.succeededFuture(GetLocationsResponse.withPlainBadRequest(message)));
        } else {
            Locations shelfLocations = new Locations();
            List<Location> shelfLocationsList = (List<Location>) reply.result().getResults();
            shelfLocations.setLocations(shelfLocationsList);
            shelfLocations.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
            asyncResultHandler.handle(Future.succeededFuture(GetLocationsResponse.withJsonOK(shelfLocations)));
        }
    });
}
Also used : FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Locations(org.folio.rest.jaxrs.model.Locations) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Location(org.folio.rest.jaxrs.model.Location)

Example 3 with Location

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

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

List (java.util.List)4 Location (org.folio.rest.jaxrs.model.Location)4 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)4 ArrayList (java.util.ArrayList)2 NotImplementedException (org.apache.commons.lang.NotImplementedException)2 Shelflocation (org.folio.rest.jaxrs.model.Shelflocation)2 Criteria (org.folio.rest.persist.Criteria.Criteria)2 Criterion (org.folio.rest.persist.Criteria.Criterion)2 CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)2 Locations (org.folio.rest.jaxrs.model.Locations)1 Shelflocations (org.folio.rest.jaxrs.model.Shelflocations)1