Search in sources :

Example 6 with ServiceException

use of org.onebusaway.exceptions.ServiceException in project onebusaway-application-modules by camsys.

the class FederatedServiceMethodInvocationHandlerFactory method getHandlerForMethod.

public FederatedServiceMethodInvocationHandler getHandlerForMethod(Method method) {
    FederatedByAgencyIdMethod byAgency = method.getAnnotation(FederatedByAgencyIdMethod.class);
    if (byAgency != null)
        return new FederatedByAgencyIdMethodInvocationHandlerImpl(method, byAgency.argument(), byAgency.propertyExpression());
    FederatedByAnyEntityIdMethod byAnyEntityId = method.getAnnotation(FederatedByAnyEntityIdMethod.class);
    if (byAnyEntityId != null)
        return new FederatedByAnyEntityIdMethodInvocationHandlerImpl(method, byAnyEntityId.argument(), byAnyEntityId.properties(), byAnyEntityId.agencyIdProperties());
    FederatedByEntityIdMethod ann0 = method.getAnnotation(FederatedByEntityIdMethod.class);
    if (ann0 != null)
        return new FederatedByEntityIdMethodInvocationHandlerImpl(method, ann0.argument(), ann0.propertyExpression());
    FederatedByEntityIdsMethod ann1 = method.getAnnotation(FederatedByEntityIdsMethod.class);
    if (ann1 != null)
        return new FederatedByEntityIdsMethodInvocationHandlerImpl(ann1.argument());
    FederatedByBoundsMethod ann2 = method.getAnnotation(FederatedByBoundsMethod.class);
    if (ann2 != null)
        return new FederatedByBoundsMethodInvocationHandlerImpl(ann2.lat1Argument(), ann2.lon1Argument(), ann2.lat2Argument(), ann2.lon2Argument());
    FederatedByLocationMethod ann3 = method.getAnnotation(FederatedByLocationMethod.class);
    if (ann3 != null)
        return new FederatedByLocationMethodInvocationHandlerImpl(ann3.latArgument(), ann3.lonArgument());
    FederatedByAggregateMethod ann4 = method.getAnnotation(FederatedByAggregateMethod.class);
    if (ann4 != null) {
        EMethodAggregationType aggregationType = getAggregationTypeForMethod(method);
        return new FederatedByAggregateMethodInvocationHandlerImpl(aggregationType);
    }
    FederatedByCoordinateBoundsMethod ann5 = method.getAnnotation(FederatedByCoordinateBoundsMethod.class);
    if (ann5 != null)
        return new FederatedByCoordinateBoundsMethodInvocationHandlerImpl(method, ann5.argument(), ann5.propertyExpression());
    FederatedByCoordinatePointsMethod ann6 = method.getAnnotation(FederatedByCoordinatePointsMethod.class);
    if (ann6 != null) {
        int[] argumentIndices = ann6.arguments();
        String[] expressions = ann6.propertyExpressions();
        if (expressions.length == 0)
            expressions = new String[argumentIndices.length];
        return new FederatedByCoordinatePointsMethodInvocationHandlerImpl(method, argumentIndices, expressions);
    }
    FederatedByCustomMethod ann7 = method.getAnnotation(FederatedByCustomMethod.class);
    if (ann7 != null) {
        Class<? extends FederatedServiceMethodInvocationHandler> handlerClass = ann7.handler();
        try {
            return handlerClass.newInstance();
        } catch (Exception ex) {
            throw new ServiceException("error creating FederatedServiceMethodInvocationHandler of type " + handlerClass, ex);
        }
    }
    throw new IllegalArgumentException("No FederatedService method annotation for method: " + method);
}
Also used : ServiceException(org.onebusaway.exceptions.ServiceException) ServiceException(org.onebusaway.exceptions.ServiceException)

Example 7 with ServiceException

use of org.onebusaway.exceptions.ServiceException in project onebusaway-application-modules by camsys.

the class PreCacheTask method run.

@Override
public void run() {
    // Clear all existing cache elements
    for (String cacheName : _cacheManager.getCacheNames()) {
        Cache cache = _cacheManager.getCache(cacheName);
        cache.removeAll();
    }
    try {
        List<AgencyWithCoverageBean> agenciesWithCoverage = _service.getAgenciesWithCoverage();
        for (AgencyWithCoverageBean agencyWithCoverage : agenciesWithCoverage) {
            AgencyBean agency = agencyWithCoverage.getAgency();
            System.out.println("agency=" + agency.getId());
            ListBean<String> stopIds = _service.getStopIdsForAgencyId(agency.getId());
            for (String stopId : stopIds.getList()) {
                System.out.println("  stop=" + stopId);
                _service.getStop(stopId);
            }
            ListBean<String> routeIds = _service.getRouteIdsForAgencyId(agency.getId());
            for (String routeId : routeIds.getList()) {
                System.out.println("  route=" + routeId);
                _service.getStopsForRoute(routeId);
            }
        }
        Set<AgencyAndId> shapeIds = new HashSet<AgencyAndId>();
        for (TripEntry trip : _transitGraphDao.getAllTrips()) {
            AgencyAndId shapeId = trip.getShapeId();
            if (shapeId != null && shapeId.hasValues())
                shapeIds.add(shapeId);
        }
        for (AgencyAndId shapeId : shapeIds) {
            System.out.println("shape=" + shapeId);
            _service.getShapeForId(AgencyAndIdLibrary.convertToString(shapeId));
        }
    } catch (ServiceException ex) {
        _log.error("service exception", ex);
    }
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ServiceException(org.onebusaway.exceptions.ServiceException) AgencyWithCoverageBean(org.onebusaway.transit_data.model.AgencyWithCoverageBean) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) Cache(net.sf.ehcache.Cache) AgencyBean(org.onebusaway.transit_data.model.AgencyBean) HashSet(java.util.HashSet)

Example 8 with ServiceException

use of org.onebusaway.exceptions.ServiceException 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 9 with ServiceException

use of org.onebusaway.exceptions.ServiceException in project onebusaway-application-modules by camsys.

the class StopsBeanServiceImpl method getStopsByName.

@Override
public StopsBean getStopsByName(String stopName) throws ServiceException {
    List<StopBean> stopBeans = new ArrayList<StopBean>();
    SearchResult<AgencyAndId> results = null;
    try {
        results = _searchService.searchForStopsByName(stopName, MAX_STOPS, NAME_MIN_SCORE);
        for (AgencyAndId aid : results.getResultsByTopScore()) {
            StopBean stopBean = _stopBeanService.getStopForId(aid);
            if (stopBean != null) {
                stopBeans.add(stopBean);
            }
        }
    } catch (Exception e) {
        _log.error("search failed!", e);
        // simply return no results, the search was not understood
        return new StopsBean();
    }
    if (results == null) {
        return new StopsBean();
    }
    return constructResult(stopBeans, results.size() == MAX_STOPS);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) StopBean(org.onebusaway.transit_data.model.StopBean) InvalidArgumentServiceException(org.onebusaway.exceptions.InvalidArgumentServiceException) ParseException(org.apache.lucene.queryParser.ParseException) ServiceException(org.onebusaway.exceptions.ServiceException) NoSuchAgencyServiceException(org.onebusaway.exceptions.NoSuchAgencyServiceException) IOException(java.io.IOException) StopsBean(org.onebusaway.transit_data.model.StopsBean)

Example 10 with ServiceException

use of org.onebusaway.exceptions.ServiceException in project onebusaway-application-modules by camsys.

the class StopsBeanServiceImpl method getStopsByBoundsAndQuery.

private StopsBean getStopsByBoundsAndQuery(SearchQueryBean queryBean) throws ServiceException {
    CoordinateBounds bounds = queryBean.getBounds();
    String query = queryBean.getQuery();
    int maxCount = queryBean.getMaxCount();
    CoordinatePoint center = SphericalGeometryLibrary.getCenterOfBounds(bounds);
    SearchResult<AgencyAndId> stops;
    try {
        stops = _searchService.searchForStopsByCode(query, 10, MIN_SCORE);
    } catch (ParseException e) {
        throw new InvalidArgumentServiceException("query", "queryParseError");
    } catch (IOException e) {
        _log.error("error executing stop search: query=" + query, e);
        e.printStackTrace();
        throw new ServiceException();
    }
    Min<StopBean> closest = new Min<StopBean>();
    List<StopBean> stopBeans = new ArrayList<StopBean>();
    for (AgencyAndId aid : stops.getResults()) {
        StopBean stopBean = _stopBeanService.getStopForId(aid);
        if (bounds.contains(stopBean.getLat(), stopBean.getLon()))
            stopBeans.add(stopBean);
        double distance = SphericalGeometryLibrary.distance(center.getLat(), center.getLon(), stopBean.getLat(), stopBean.getLon());
        closest.add(distance, stopBean);
    }
    boolean limitExceeded = BeanServiceSupport.checkLimitExceeded(stopBeans, maxCount);
    // If nothing was found in range, add the closest result
    if (stopBeans.isEmpty() && !closest.isEmpty())
        stopBeans.add(closest.getMinElement());
    return constructResult(stopBeans, limitExceeded);
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) Min(org.onebusaway.collections.Min) InvalidArgumentServiceException(org.onebusaway.exceptions.InvalidArgumentServiceException) ServiceException(org.onebusaway.exceptions.ServiceException) NoSuchAgencyServiceException(org.onebusaway.exceptions.NoSuchAgencyServiceException) InvalidArgumentServiceException(org.onebusaway.exceptions.InvalidArgumentServiceException) StopBean(org.onebusaway.transit_data.model.StopBean) ParseException(org.apache.lucene.queryParser.ParseException) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Aggregations

ServiceException (org.onebusaway.exceptions.ServiceException)10 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)5 StopBean (org.onebusaway.transit_data.model.StopBean)4 ArrayList (java.util.ArrayList)3 InvalidArgumentServiceException (org.onebusaway.exceptions.InvalidArgumentServiceException)3 NoSuchAgencyServiceException (org.onebusaway.exceptions.NoSuchAgencyServiceException)3 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 ParseException (org.apache.lucene.queryParser.ParseException)2 BodyError (org.onebusaway.nextbus.model.nextbus.BodyError)2 AgencyWithCoverageBean (org.onebusaway.transit_data.model.AgencyWithCoverageBean)2 Cache (net.sf.ehcache.Cache)1 Min (org.onebusaway.collections.Min)1 NoSuchTripServiceException (org.onebusaway.exceptions.NoSuchTripServiceException)1 OutOfServiceAreaServiceException (org.onebusaway.exceptions.OutOfServiceAreaServiceException)1 FederatedService (org.onebusaway.federations.FederatedService)1 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)1 Agency (org.onebusaway.nextbus.model.nextbus.Agency)1 Body (org.onebusaway.nextbus.model.nextbus.Body)1