Search in sources :

Example 6 with BodyError

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

the class BodyErrorConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    BodyError error = new BodyError();
    error.setContent(reader.getValue());
    error.setShouldRetry(Boolean.parseBoolean(reader.getAttribute("shouldRetry")));
    return error;
}
Also used : BodyError(org.onebusaway.nextbus.model.nextbus.BodyError)

Example 7 with BodyError

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

the class PredictionsForMultiStopsAction method isValid.

private boolean isValid(Body<Predictions> body) {
    if (!isValidAgency(body, agencyId)) {
        return false;
    }
    if (stops == null) {
        body.getErrors().add(new BodyError(ErrorMsg.STOP_STOPS_NULL.getDescription()));
        return false;
    }
    for (String stop : stops) {
        String[] stopArray = stop.split("\\|");
        if (stopArray.length < 2) {
            String error = "The stop " + stop + " is invalid because it did not contain a route, optional dir, and stop tag";
            body.getErrors().add(new BodyError(error));
            return false;
        }
        try {
            String stopId = _tdsMappingService.getStopIdFromStopCode(stopArray[1]);
            StopBean stopBean;
            try {
                stopBean = _transitDataService.getStop(stopId);
            } catch (ServiceException se) {
                // The user didn't provide an agency id in stopId, so use provided
                // agency
                String stopIdVal = new AgencyAndId(agencyId, stopId).toString();
                stopBean = _transitDataService.getStop(stopIdVal);
            }
            boolean routeExists = false;
            for (RouteBean routeBean : stopBean.getRoutes()) {
                if (routeBean.getId().equals(_tdsMappingService.getRouteIdFromShortName(stopArray[0]))) {
                    routeExists = true;
                    break;
                }
            }
            if (!routeExists) {
                String error = "For agency=" + getA() + " route r=" + stopArray[0] + " is not currently available. It might be initializing still.";
                body.getErrors().add(new BodyError(error));
                return false;
            }
            String routeTag = getIdNoAgency(_tdsMappingService.getRouteIdFromShortName(stopArray[0]));
            String routeStop = getIdNoAgency(_tdsMappingService.getStopIdFromStopCode(stopArray[1]));
            mappedStops.add(routeTag + "|" + routeStop);
        } catch (ServiceException se) {
            String error = "For agency=" + getA() + " stop s=" + stopArray[1] + " is on none of the directions for r=" + stopArray[0] + " so cannot determine which stop to provide data for.";
            body.getErrors().add(new BodyError(error));
            return false;
        }
    }
    return true;
}
Also used : RouteBean(org.onebusaway.transit_data.model.RouteBean) BodyError(org.onebusaway.nextbus.model.nextbus.BodyError) ServiceException(org.onebusaway.exceptions.ServiceException) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) StopBean(org.onebusaway.transit_data.model.StopBean)

Example 8 with BodyError

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

the class PredictionsAction method getModel.

public Body<Predictions> getModel() {
    Body<Predictions> body = new Body<Predictions>();
    List<AgencyAndId> stopIds = new ArrayList<AgencyAndId>();
    List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
    if (isValid(body, stopIds, routeIds)) {
        String serviceUrl = getServiceUrl() + agencyId + PREDICTIONS_COMMAND + "?";
        String routeStop = "";
        for (AgencyAndId routeId : routeIds) {
            routeStop += "rs=" + getIdNoAgency(routeId.toString()) + "|" + getIdNoAgency(stopId) + "&";
        }
        String uri = serviceUrl + routeStop + "format=" + REQUEST_TYPE;
        _log.info(uri);
        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."));
            _log.error(e.getMessage());
        }
    }
    return body;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) 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 9 with BodyError

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

the class BodyErrorConverter method marshal.

@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
    BodyError error = (BodyError) source;
    writer.addAttribute("shouldRetry", Boolean.toString(error.isShouldRetry()));
    writer.setValue(error.getContent());
}
Also used : BodyError(org.onebusaway.nextbus.model.nextbus.BodyError)

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