Search in sources :

Example 1 with BodyError

use of org.onebusaway.nextbus.model.nextbus.BodyError in project onebusaway-application-modules by camsys.

the class PredictionsForMultiStopsAction method getModel.

public Body<Predictions> getModel() {
    Body<Predictions> body = new Body<Predictions>();
    if (isValid(body)) {
        String serviceUrl = getServiceUrl() + agencyId + PREDICTIONS_COMMAND + "?";
        String routeStopIds = getStopParams();
        String uri = serviceUrl + routeStopIds + "format=" + REQUEST_TYPE;
        try {
            int timeout = _configUtil.getHttpTimeoutSeconds();
            JsonArray predictionsJson = _httpUtil.getJsonObject(uri, timeout).getAsJsonArray("predictions");
            Type listType = new TypeToken<List<Predictions>>() {
            }.getType();
            List<Predictions> predictions = new Gson().fromJson(predictionsJson, listType);
            modifyJSONObject(predictions);
            body.getResponse().addAll(predictions);
        } catch (Exception e) {
            body.getErrors().add(new BodyError("No valid results found."));
        }
    }
    return body;
}
Also used : Gson(com.google.gson.Gson) ServiceException(org.onebusaway.exceptions.ServiceException) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) BodyError(org.onebusaway.nextbus.model.nextbus.BodyError) Predictions(org.onebusaway.nextbus.model.transiTime.Predictions) ArrayList(java.util.ArrayList) List(java.util.List) Body(org.onebusaway.nextbus.model.nextbus.Body)

Example 2 with BodyError

use of org.onebusaway.nextbus.model.nextbus.BodyError in project onebusaway-application-modules by camsys.

the class AgencyListAction method getModel.

@Override
public Body<Agency> getModel() {
    Body<Agency> body = new Body<Agency>();
    try {
        List<AgencyWithCoverageBean> agencies = _service.getAgenciesWithCoverage();
        for (AgencyWithCoverageBean agencyBean : agencies) {
            Agency agency = new Agency();
            agency.setTag(agencyBean.getAgency().getId());
            agency.setTitle(agencyBean.getAgency().getName());
            agency.setRegionTitle(agencyBean.getAgency().getName());
            body.getResponse().add(agency);
        }
    } catch (ServiceException e) {
        body.getErrors().add(new BodyError(ErrorMsg.SERVICE_ERROR.getDescription()));
    }
    return body;
}
Also used : Agency(org.onebusaway.nextbus.model.nextbus.Agency) ServiceException(org.onebusaway.exceptions.ServiceException) BodyError(org.onebusaway.nextbus.model.nextbus.BodyError) AgencyWithCoverageBean(org.onebusaway.transit_data.model.AgencyWithCoverageBean) Body(org.onebusaway.nextbus.model.nextbus.Body)

Example 3 with BodyError

use of org.onebusaway.nextbus.model.nextbus.BodyError in project onebusaway-application-modules by camsys.

the class NextBusApiBase method processRouteIds.

protected <E> boolean processRouteIds(String routeVal, List<AgencyAndId> routeIds, List<String> agencyIds, Body<E> body, boolean handleErrors) {
    if (routeVal != null) {
        try {
            AgencyAndId routeId = AgencyAndIdLibrary.convertFromString(routeVal);
            if (this.isValidRoute(routeId))
                routeIds.add(routeId);
        } catch (IllegalStateException e) {
            for (String agency : agencyIds) {
                AgencyAndId routeId = new AgencyAndId(agency, routeVal);
                if (this.isValidRoute(routeId)) {
                    routeIds.add(routeId);
                }
            }
        }
        if (handleErrors && routeIds.size() == 0) {
            String agencyId = agencyIds.get(0);
            body.getErrors().add(new BodyError(ErrorMsg.ROUTE_UNAVAILABLE.getDescription(), agencyId, routeVal));
            return false;
        }
    } else {
        if (handleErrors) {
            body.getErrors().add(new BodyError(ErrorMsg.ROUTE_NULL.getDescription(), agencyIds.get(0)));
        }
        return false;
    }
    return true;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BodyError(org.onebusaway.nextbus.model.nextbus.BodyError)

Example 4 with BodyError

use of org.onebusaway.nextbus.model.nextbus.BodyError in project onebusaway-application-modules by camsys.

the class PredictionsAction method isValid.

private boolean isValid(Body body, List<AgencyAndId> stopIds, List<AgencyAndId> routeIds) {
    if (!isValidAgency(body, agencyId))
        return false;
    List<String> agencies = new ArrayList<String>();
    agencies.add(agencyId);
    if (!processStopIds(stopId, stopIds, agencies, body))
        return false;
    StopBean stopBean = getCachedStopBean(stopIds.get(0).toString());
    if (routeTag == null) {
        for (RouteBean routeBean : stopBean.getRoutes()) {
            routeIds.add(AgencyAndId.convertFromString(routeBean.getId()));
        }
    } else {
        if (!processRouteIds(routeTag, routeIds, agencies, body))
            return false;
        boolean stopServesRoute = false;
        for (RouteBean routeBean : stopBean.getRoutes()) {
            if (routeIds.contains(AgencyAndId.convertFromString(routeBean.getId())))
                stopServesRoute = true;
        }
        if (!stopServesRoute) {
            body.getErrors().add(new BodyError(ErrorMsg.ROUTE_UNAVAILABLE.getDescription(), agencyId, routeTag));
            return false;
        }
    }
    return true;
}
Also used : RouteBean(org.onebusaway.transit_data.model.RouteBean) BodyError(org.onebusaway.nextbus.model.nextbus.BodyError) ArrayList(java.util.ArrayList) StopBean(org.onebusaway.transit_data.model.StopBean)

Example 5 with BodyError

use of org.onebusaway.nextbus.model.nextbus.BodyError in project onebusaway-application-modules by camsys.

the class BodySerializer method serialize.

@Override
public void serialize(Body<T> value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
    List<T> response = value.getResponse();
    List<BodyError> errors = value.getErrors();
    String copyright = value.getCopyright();
    LastTime lastTime = value.getLastTime();
    gen.writeStartObject();
    // Copyright
    if (StringUtils.isNotBlank(copyright))
        gen.writeStringField("copyright", copyright);
    // Error
    if (errors != null && errors.size() > 0) {
        for (BodyError error : errors) error.setContent(error.getContent().replace("\\", ""));
        gen.writeObjectField("Error", errors);
    }
    // Response
    if (response != null && response.size() > 0) {
        JsonRootName jsonRootName = response.get(0).getClass().getAnnotation(JsonRootName.class);
        if (jsonRootName != null)
            gen.writeObjectField(jsonRootName.value(), response);
        else
            gen.writeObjectField("response", response);
    }
    // LastTime
    if (lastTime != null)
        gen.writeObjectField("lastTime", lastTime);
    gen.writeEndObject();
}
Also used : BodyError(org.onebusaway.nextbus.model.nextbus.BodyError) LastTime(org.onebusaway.nextbus.model.nextbus.LastTime) JsonRootName(com.fasterxml.jackson.annotation.JsonRootName)

Aggregations

BodyError (org.onebusaway.nextbus.model.nextbus.BodyError)9 ArrayList (java.util.ArrayList)3 ServiceException (org.onebusaway.exceptions.ServiceException)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 Body (org.onebusaway.nextbus.model.nextbus.Body)3 Gson (com.google.gson.Gson)2 JsonArray (com.google.gson.JsonArray)2 Type (java.lang.reflect.Type)2 List (java.util.List)2 Predictions (org.onebusaway.nextbus.model.transiTime.Predictions)2 RouteBean (org.onebusaway.transit_data.model.RouteBean)2 StopBean (org.onebusaway.transit_data.model.StopBean)2 JsonRootName (com.fasterxml.jackson.annotation.JsonRootName)1 Agency (org.onebusaway.nextbus.model.nextbus.Agency)1 LastTime (org.onebusaway.nextbus.model.nextbus.LastTime)1 AgencyWithCoverageBean (org.onebusaway.transit_data.model.AgencyWithCoverageBean)1