|
| 1 | +package com.conveyal.gtfs.validator; |
| 2 | + |
| 3 | +import com.conveyal.gtfs.error.NewGTFSError; |
| 4 | +import com.conveyal.gtfs.error.SQLErrorStorage; |
| 5 | +import com.conveyal.gtfs.loader.Feed; |
| 6 | +import com.conveyal.gtfs.loader.Table; |
| 7 | +import com.conveyal.gtfs.model.Stop; |
| 8 | +import com.google.common.collect.HashMultimap; |
| 9 | +import com.google.common.collect.Multimap; |
| 10 | +import com.google.common.collect.Sets; |
| 11 | + |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.HashSet; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +import static com.conveyal.gtfs.error.NewGTFSErrorType.REFERENTIAL_INTEGRITY; |
| 17 | + |
| 18 | +/** |
| 19 | + * Find stop#parent_station values that reference non-existent stop_ids. Unfortunately, we cannot perform this check |
| 20 | + * while the stops.txt table is being loaded because we do not yet have the full set of stop_ids available to check |
| 21 | + * parent_station values against. |
| 22 | + */ |
| 23 | +public class ParentStationValidator extends FeedValidator { |
| 24 | + |
| 25 | + public ParentStationValidator(Feed feed, SQLErrorStorage errorStorage) { |
| 26 | + super(feed, errorStorage); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void validate () { |
| 31 | + Multimap<String, Stop> stopsForParentStations = HashMultimap.create(); |
| 32 | + Set<String> stopIds = new HashSet<>(); |
| 33 | + for (Stop stop : feed.stops) { |
| 34 | + // Collect all stop_ids found in feed. |
| 35 | + stopIds.add(stop.stop_id); |
| 36 | + // Collect all non-null parent_station values. |
| 37 | + if (stop.parent_station != null) { |
| 38 | + stopsForParentStations.put(stop.parent_station, stop); |
| 39 | + } |
| 40 | + } |
| 41 | + // Find parent_station values that do not reference a valid stop_id from feed. |
| 42 | + Sets.SetView<String> badReferences = Sets.difference(stopsForParentStations.keySet(), stopIds); |
| 43 | + for (String parentStation : badReferences) { |
| 44 | + // For any bad parent_station ref (this could be more than one stop), add an error to the error storage. |
| 45 | + Collection<Stop> stops = stopsForParentStations.get(parentStation); |
| 46 | + for (Stop stop : stops) { |
| 47 | + registerError( |
| 48 | + NewGTFSError |
| 49 | + .forLine(Table.STOPS, stop.id, REFERENTIAL_INTEGRITY, parentStation) |
| 50 | + .setEntityId(stop.stop_id) |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments