|
| 1 | +package org.mobilitydata.gtfsvalidator.validator; |
| 2 | + |
| 3 | +import javax.annotation.Nullable; |
| 4 | +import javax.inject.Inject; |
| 5 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice; |
| 6 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice.FileRefs; |
| 7 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; |
| 8 | +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; |
| 9 | +import org.mobilitydata.gtfsvalidator.notice.SeverityLevel; |
| 10 | +import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; |
| 11 | +import org.mobilitydata.gtfsvalidator.table.*; |
| 12 | + |
| 13 | +@GtfsValidator |
| 14 | +public class PickupBookingRuleIdValidator extends FileValidator { |
| 15 | + private final GtfsStopTimeTableContainer stopTimeTable; |
| 16 | + private final GtfsBookingRulesTableContainer bookingRulesTable; |
| 17 | + |
| 18 | + @Inject |
| 19 | + public PickupBookingRuleIdValidator( |
| 20 | + GtfsStopTimeTableContainer stopTimeTable, GtfsBookingRulesTableContainer bookingRulesTable) { |
| 21 | + this.stopTimeTable = stopTimeTable; |
| 22 | + this.bookingRulesTable = bookingRulesTable; |
| 23 | + } |
| 24 | + |
| 25 | + public void validate(GtfsStopTime entity, NoticeContainer noticeContainer) { |
| 26 | + if (entity.hasPickupType() |
| 27 | + && entity.pickupType() == GtfsPickupDropOff.MUST_PHONE |
| 28 | + && !entity.hasPickupBookingRuleId()) { |
| 29 | + noticeContainer.addValidationNotice( |
| 30 | + new MissingPickupDropOffBookingRuleIdNotice( |
| 31 | + entity.csvRowNumber(), |
| 32 | + entity.pickupType(), |
| 33 | + entity.hasDropOffType() ? entity.dropOffType() : null)); |
| 34 | + } |
| 35 | + if (entity.hasDropOffType() |
| 36 | + && entity.dropOffType() == GtfsPickupDropOff.MUST_PHONE |
| 37 | + && !entity.hasDropOffBookingRuleId()) { |
| 38 | + noticeContainer.addValidationNotice( |
| 39 | + new MissingPickupDropOffBookingRuleIdNotice( |
| 40 | + entity.csvRowNumber(), |
| 41 | + entity.hasPickupType() ? entity.pickupType() : null, |
| 42 | + entity.dropOffType())); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void validate(NoticeContainer noticeContainer) { |
| 48 | + for (GtfsStopTime stopTime : stopTimeTable.getEntities()) { |
| 49 | + validate(stopTime, noticeContainer); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public boolean shouldCallValidate() { |
| 55 | + return bookingRulesTable != null |
| 56 | + && !bookingRulesTable.isMissingFile() |
| 57 | + && stopTimeTable != null |
| 58 | + && !stopTimeTable.isMissingFile() |
| 59 | + && (stopTimeTable.hasColumn(GtfsStopTime.PICKUP_TYPE_FIELD_NAME) |
| 60 | + || stopTimeTable.hasColumn(GtfsStopTime.DROP_OFF_TYPE_FIELD_NAME)); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * `pickup_booking_rule_id` is recommended when `pickup_type=2` and `drop_off_booking_rule_id` is |
| 65 | + * recommended when `drop_off_type=2` |
| 66 | + */ |
| 67 | + @GtfsValidationNotice( |
| 68 | + severity = SeverityLevel.WARNING, |
| 69 | + files = @FileRefs(GtfsStopTimeSchema.class)) |
| 70 | + static class MissingPickupDropOffBookingRuleIdNotice extends ValidationNotice { |
| 71 | + /** The row number of the faulty record in `stop_times.txt` */ |
| 72 | + private final int csvRowNumber; |
| 73 | + |
| 74 | + /** The pickup type of the faulty record. */ |
| 75 | + @Nullable private final GtfsPickupDropOff pickupType; |
| 76 | + |
| 77 | + /** The drop-off type of the faulty record. */ |
| 78 | + @Nullable private final GtfsPickupDropOff dropOffType; |
| 79 | + |
| 80 | + public MissingPickupDropOffBookingRuleIdNotice( |
| 81 | + int csvRowNumber, GtfsPickupDropOff pickupType, GtfsPickupDropOff dropOffType) { |
| 82 | + this.csvRowNumber = csvRowNumber; |
| 83 | + this.pickupType = pickupType; |
| 84 | + this.dropOffType = dropOffType; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments