diff --git a/application/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/TripImpl.java b/application/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/TripImpl.java index 2266da23f63..d4b4b68c738 100644 --- a/application/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/TripImpl.java +++ b/application/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/TripImpl.java @@ -104,12 +104,12 @@ public DataFetcher> alerts() { .anyMatch(entity -> ( entity instanceof EntitySelector.StopAndRoute stopAndRoute && - stopAndRoute.routeId().equals(getRoute(environment).getId()) + stopAndRoute.routeId().equals(getRoute(environment).getId()) ) || - ( - entity instanceof EntitySelector.StopAndTrip stopAndTrip && - stopAndTrip.tripId().equals(getSource(environment).getId()) - ) + ( + entity instanceof EntitySelector.StopAndTrip stopAndTrip && + stopAndTrip.tripId().equals(getSource(environment).getId()) + ) ) ) .toList() @@ -143,7 +143,7 @@ public DataFetcher arrivalStoptime() { } } - TripPattern tripPattern = getTripPattern(environment); + TripPattern tripPattern = getTripPattern(environment, serviceDate); if (tripPattern == null) { return null; } @@ -176,7 +176,7 @@ public DataFetcher departureStoptime() { } } - TripPattern tripPattern = getTripPattern(environment); + TripPattern tripPattern = getTripPattern(environment, serviceDate); if (tripPattern == null) { return null; } @@ -301,7 +301,7 @@ public DataFetcher> stoptimesForDate() { ? ServiceDateUtils.parseString(args.getGraphQLServiceDate()) : LocalDate.now(timeZone); - TripPattern tripPattern = transitService.getPatternForTrip(trip, serviceDate); + TripPattern tripPattern = getTripPattern(environment, serviceDate); // no matching pattern found if (tripPattern == null) { return List.of(); @@ -376,6 +376,15 @@ private TripPattern getTripPattern(DataFetchingEnvironment environment) { return getTransitService(environment).getPatternForTrip(environment.getSource()); } + private TripPattern getTripPattern( + DataFetchingEnvironment environment, + @Nullable LocalDate date + ) { + return date == null + ? getTripPattern(environment) + : getTransitService(environment).getPatternForTrip(environment.getSource(), date); + } + private TransitService getTransitService(DataFetchingEnvironment environment) { return environment.getContext().transitService(); } @@ -389,17 +398,28 @@ private TripTimeOnDate getStoptimeAtIndex( @Nullable LocalDate serviceDate, int stopIndex ) { - var tripPattern = getTripPattern(environment); + var tripPattern = getTripPattern(environment, serviceDate); + var transitService = getTransitService(environment); + var timetable = serviceDate != null + ? transitService.getTimetableForTripPattern(tripPattern, serviceDate) + : tripPattern.getScheduledTimetable(); + if (timetable == null) { + return null; + } + + var tripTimes = timetable.getTripTimes(getSource(environment)); + if (tripTimes == null) { + return null; + } + return new TripTimeOnDate( - tripPattern.getScheduledTimetable().getTripTimes(getSource(environment)), + tripTimes, stopIndex, tripPattern, serviceDate, serviceDate == null ? null - : ServiceDateUtils - .asStartOfService(serviceDate, getTransitService(environment).getTimeZone()) - .toInstant() + : ServiceDateUtils.asStartOfService(serviceDate, transitService.getTimeZone()).toInstant() ); } diff --git a/application/src/main/java/org/opentripplanner/model/TripTimeOnDate.java b/application/src/main/java/org/opentripplanner/model/TripTimeOnDate.java index 028e91c0fc2..ef663d9bd06 100644 --- a/application/src/main/java/org/opentripplanner/model/TripTimeOnDate.java +++ b/application/src/main/java/org/opentripplanner/model/TripTimeOnDate.java @@ -63,9 +63,15 @@ public TripTimeOnDate( /** * Must pass in both Timetable and Trip, because TripTimes do not have a reference to * StopPatterns. + * + * @return null if the trip does not exist in the timetable */ + @Nullable public static List fromTripTimes(Timetable table, Trip trip) { TripTimes times = table.getTripTimes(trip); + if (times == null) { + return null; + } List out = new ArrayList<>(); for (int i = 0; i < times.getNumStops(); ++i) { out.add(new TripTimeOnDate(times, i, table.getPattern())); @@ -78,7 +84,10 @@ public static List fromTripTimes(Timetable table, Trip trip) { * StopPatterns. * * @param serviceDate service day to set, if null none is set + * @return null if the trip does not exist in the timetable */ + + @Nullable public static List fromTripTimes( Timetable table, Trip trip, @@ -86,6 +95,9 @@ public static List fromTripTimes( Instant midnight ) { TripTimes times = table.getTripTimes(trip); + if (times == null) { + return null; + } List out = new ArrayList<>(); for (int i = 0; i < times.getNumStops(); ++i) { out.add(new TripTimeOnDate(times, i, table.getPattern(), serviceDate, midnight));