diff --git a/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsExporter.java b/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsExporter.java index 61f6bb8cf..cd3b25aef 100644 --- a/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsExporter.java +++ b/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsExporter.java @@ -30,6 +30,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -67,6 +68,17 @@ public JdbcGtfsExporter(String feedId, String outFile, DataSource dataSource, bo this.fromEditor = fromEditor; } + /** + * Utility method to check if an exception uses a specific service. + */ + public Boolean exceptionInvolvesService(ScheduleException ex, String serviceId) { + return ( + ex.addedService.contains(serviceId) || + ex.removedService.contains(serviceId) || + ex.customSchedule.contains(serviceId) + ); + } + /** * Export primary entity tables as well as Pattern and PatternStops tables. * @@ -135,7 +147,10 @@ public FeedLoadResult exportTables() { for (Calendar cal : calendars) { Service service = new Service(cal.service_id); service.calendar = cal; - for (ScheduleException ex : exceptions) { + for (ScheduleException ex : exceptions.stream() + .filter(ex -> exceptionInvolvesService(ex, cal.service_id)) + .collect(Collectors.toList()) + ) { if (ex.exemplar.equals(ScheduleException.ExemplarServiceDescriptor.SWAP) && (!ex.addedService.contains(cal.service_id) && !ex.removedService.contains(cal.service_id))) { // Skip swap exception if cal is not referenced by added or removed service. @@ -153,7 +168,7 @@ public FeedLoadResult exportTables() { calendarDate.date = date; calendarDate.service_id = cal.service_id; calendarDate.exception_type = ex.serviceRunsOn(cal) ? 1 : 2; - LOG.info("Adding exception {} (type={}) for calendar {} on date {}", ex.name, calendarDate.exception_type, cal.service_id, date.toString()); + LOG.info("Adding exception {} (type={}) for calendar {} on date {}", ex.name, calendarDate.exception_type, cal.service_id, date); if (service.calendar_dates.containsKey(date)) throw new IllegalArgumentException("Duplicate schedule exceptions on " + date.toString());