|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC, MobilityData IO |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.mobilitydata.gtfsvalidator.validator; |
| 17 | + |
| 18 | +import javax.inject.Inject; |
| 19 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; |
| 20 | +import org.mobilitydata.gtfsvalidator.notice.ForeignKeyViolationNotice; |
| 21 | +import org.mobilitydata.gtfsvalidator.notice.MissingRequiredFieldNotice; |
| 22 | +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; |
| 23 | +import org.mobilitydata.gtfsvalidator.table.*; |
| 24 | + |
| 25 | +/** |
| 26 | + * Validates GtfsFareLegJoinRule entities |
| 27 | + * |
| 28 | + * <p>Generated notices: |
| 29 | + * |
| 30 | + * <ul> |
| 31 | + * <li>{@link ForeignKeyViolationNotice} |
| 32 | + * <li>{@link MissingRequiredFieldNotice} |
| 33 | + * </ul> |
| 34 | + */ |
| 35 | +@GtfsValidator |
| 36 | +public class FareLegJoinRuleValidator extends FileValidator { |
| 37 | + GtfsNetworkTableContainer networkTable; |
| 38 | + GtfsRouteTableContainer routeTable; |
| 39 | + GtfsFareLegJoinRuleTableContainer fareLegJoinRuleTable; |
| 40 | + |
| 41 | + @Inject |
| 42 | + public FareLegJoinRuleValidator( |
| 43 | + GtfsNetworkTableContainer networkTable, |
| 44 | + GtfsRouteTableContainer routeTable, |
| 45 | + GtfsFareLegJoinRuleTableContainer fareLegJoinRuleTable) { |
| 46 | + this.networkTable = networkTable; |
| 47 | + this.routeTable = routeTable; |
| 48 | + this.fareLegJoinRuleTable = fareLegJoinRuleTable; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void validate(NoticeContainer noticeContainer) { |
| 53 | + for (GtfsFareLegJoinRule entity : fareLegJoinRuleTable.getEntities()) { |
| 54 | + validate(entity, noticeContainer); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public void validate(GtfsFareLegJoinRule entity, NoticeContainer noticeContainer) { |
| 59 | + // Validate foreign key reference - from_network_id references a network or route |
| 60 | + if (!networkTable.byNetworkId(entity.fromNetworkId()).isPresent() |
| 61 | + && routeTable.byNetworkId(entity.fromNetworkId()).isEmpty()) { |
| 62 | + noticeContainer.addValidationNotice( |
| 63 | + new ForeignKeyViolationNotice( |
| 64 | + GtfsFareLegJoinRule.FILENAME, |
| 65 | + GtfsFareLegJoinRule.FROM_NETWORK_ID_FIELD_NAME, |
| 66 | + GtfsRoute.FILENAME + " or " + GtfsNetwork.FILENAME, |
| 67 | + GtfsNetwork.NETWORK_ID_FIELD_NAME, |
| 68 | + entity.fromNetworkId(), |
| 69 | + entity.csvRowNumber())); |
| 70 | + } |
| 71 | + |
| 72 | + // Validate foreign key reference - to_network_id references a network or route |
| 73 | + if (!networkTable.byNetworkId(entity.toNetworkId()).isPresent() |
| 74 | + && routeTable.byNetworkId(entity.toNetworkId()).isEmpty()) { |
| 75 | + noticeContainer.addValidationNotice( |
| 76 | + new ForeignKeyViolationNotice( |
| 77 | + GtfsFareLegJoinRule.FILENAME, |
| 78 | + GtfsFareLegJoinRule.TO_NETWORK_ID_FIELD_NAME, |
| 79 | + GtfsRoute.FILENAME + " or " + GtfsNetwork.FILENAME, |
| 80 | + GtfsNetwork.NETWORK_ID_FIELD_NAME, |
| 81 | + entity.toNetworkId(), |
| 82 | + entity.csvRowNumber())); |
| 83 | + } |
| 84 | + |
| 85 | + // Validate conditionally required fields - from_stop_id and to_stop_id |
| 86 | + if (entity.hasFromStopId() && !entity.hasToStopId()) { |
| 87 | + noticeContainer.addValidationNotice( |
| 88 | + new MissingRequiredFieldNotice( |
| 89 | + GtfsFareLegJoinRule.FILENAME, |
| 90 | + entity.csvRowNumber(), |
| 91 | + GtfsFareLegJoinRule.TO_STOP_ID_FIELD_NAME)); |
| 92 | + } else if (entity.hasToStopId() && !entity.hasFromStopId()) { |
| 93 | + noticeContainer.addValidationNotice( |
| 94 | + new MissingRequiredFieldNotice( |
| 95 | + GtfsFareLegJoinRule.FILENAME, |
| 96 | + entity.csvRowNumber(), |
| 97 | + GtfsFareLegJoinRule.FROM_STOP_ID_FIELD_NAME)); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments