|
| 1 | +package org.mobilitydata.gtfsvalidator.validator; |
| 2 | + |
| 3 | +import javax.inject.Inject; |
| 4 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice; |
| 5 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; |
| 6 | +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; |
| 7 | +import org.mobilitydata.gtfsvalidator.notice.SeverityLevel; |
| 8 | +import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; |
| 9 | +import org.mobilitydata.gtfsvalidator.table.GtfsCalendar; |
| 10 | +import org.mobilitydata.gtfsvalidator.table.GtfsCalendarSchema; |
| 11 | +import org.mobilitydata.gtfsvalidator.table.GtfsCalendarService; |
| 12 | +import org.mobilitydata.gtfsvalidator.table.GtfsCalendarTableContainer; |
| 13 | + |
| 14 | +@GtfsValidator |
| 15 | +public class ServiceHasNoActiveDayOfTheWeekValidator extends FileValidator { |
| 16 | + private final GtfsCalendarTableContainer calendarTable; |
| 17 | + |
| 18 | + @Inject |
| 19 | + ServiceHasNoActiveDayOfTheWeekValidator(GtfsCalendarTableContainer calendarTable) { |
| 20 | + this.calendarTable = calendarTable; |
| 21 | + } |
| 22 | + |
| 23 | + private boolean hasNoActiveDayOfTheWeek(GtfsCalendar calendar) { |
| 24 | + return calendar.monday() == GtfsCalendarService.NOT_AVAILABLE |
| 25 | + && calendar.tuesday() == GtfsCalendarService.NOT_AVAILABLE |
| 26 | + && calendar.wednesday() == GtfsCalendarService.NOT_AVAILABLE |
| 27 | + && calendar.thursday() == GtfsCalendarService.NOT_AVAILABLE |
| 28 | + && calendar.friday() == GtfsCalendarService.NOT_AVAILABLE |
| 29 | + && calendar.saturday() == GtfsCalendarService.NOT_AVAILABLE |
| 30 | + && calendar.sunday() == GtfsCalendarService.NOT_AVAILABLE; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void validate(NoticeContainer noticeContainer) { |
| 35 | + calendarTable.getEntities().stream() |
| 36 | + .filter(calendar -> calendar.hasServiceId() && hasNoActiveDayOfTheWeek(calendar)) |
| 37 | + .forEach( |
| 38 | + calendar -> |
| 39 | + noticeContainer.addValidationNotice( |
| 40 | + new ServiceHasNoActiveDayOfTheWeekNotice( |
| 41 | + calendar.csvRowNumber(), calendar.serviceId()))); |
| 42 | + } |
| 43 | + |
| 44 | + /** A service is not valid for any day of the week. */ |
| 45 | + @GtfsValidationNotice( |
| 46 | + severity = SeverityLevel.WARNING, |
| 47 | + files = @GtfsValidationNotice.FileRefs({GtfsCalendarSchema.class})) |
| 48 | + static class ServiceHasNoActiveDayOfTheWeekNotice extends ValidationNotice { |
| 49 | + /** The row number in calendar.txt where the error occurs. */ |
| 50 | + private final int csvRowNumber; |
| 51 | + |
| 52 | + /** The service_id field value. */ |
| 53 | + private final String serviceId; |
| 54 | + |
| 55 | + ServiceHasNoActiveDayOfTheWeekNotice(int csvRowNumber, String serviceId) { |
| 56 | + this.csvRowNumber = csvRowNumber; |
| 57 | + this.serviceId = serviceId; |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments