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..4c41f256a59 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 @@ -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));