Skip to content

Commit 8202ea4

Browse files
committed
Filter out dead runs while importing timetables.
Resolves HSLdevcom/jore4#1596
1 parent 3778312 commit 8202ea4

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

src/main/kotlin/fi/hsl/jore4/hastus/data/hastus/TripRecord.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package fi.hsl.jore4.hastus.data.hastus
2020
* @property tripType2 Duplicated(?) type of the trip. 0 = normal, 1 = pull out, 2 = pull in, 3 = transfer
2121
* @property note Special note. Value 'p' means it's a friday trip
2222
* @property note2 Reserved for extra information
23-
* @property direction Direction. 1 = outbound, 2 = inbound
23+
* @property direction Direction. 1 = outbound, 2 = inbound, null = dead run
2424
* @property vehicleType HSL vehicle type id
2525
* @property isVehicleTypeMandatory Is vehicle type mandatory
2626
* @property isBackupTrip Is this a backup trip
@@ -45,7 +45,7 @@ data class TripRecord(
4545
val tripType2: String, // TODO This field appears to be a duplicate
4646
val note: String,
4747
val note2: String,
48-
val direction: Int,
48+
val direction: Int?,
4949
val vehicleType: Int,
5050
val isVehicleTypeMandatory: Boolean,
5151
val isBackupTrip: Boolean,
@@ -70,7 +70,7 @@ data class TripRecord(
7070
tripType2 = elements[15],
7171
note = elements[16],
7272
note2 = elements[17],
73-
direction = parseToInt(elements[18]),
73+
direction = elements[18].trim().toIntOrNull(),
7474
vehicleType = parseToInt(elements[19]),
7575
isVehicleTypeMandatory = parseToBoolean(elements[20]),
7676
isBackupTrip = parseToBoolean(elements[21]),

src/main/kotlin/fi/hsl/jore4/hastus/service/importing/ConversionsFromHastus.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ object ConversionsFromHastus {
321321
return when (trip.direction) {
322322
1 -> JoreRouteDirection.OUTBOUND
323323
2 -> JoreRouteDirection.INBOUND
324+
null -> throw IllegalArgumentException("Null route direction encountered (dead run)")
324325
else -> throw IllegalArgumentException("Unknown Hastus route direction: ${trip.direction}")
325326
}
326327
}

src/main/kotlin/fi/hsl/jore4/hastus/service/importing/ImportService.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ImportService(private val graphQLServiceFactory: GraphQLServiceFactory) {
2424
csv: String,
2525
hasuraHeaders: Map<String, String>
2626
): UUID? {
27-
val hastusItems: List<IHastusData> = READER.parseCsv(csv)
27+
val hastusItems: List<IHastusData> = getHastusDataItems(csv)
2828
val graphQLService: GraphQLService = graphQLServiceFactory.createForSession(hasuraHeaders)
2929

3030
val hastusBookingRecord: BookingRecord = hastusItems.filterIsInstance<BookingRecord>().first()
@@ -63,6 +63,8 @@ class ImportService(private val graphQLServiceFactory: GraphQLServiceFactory) {
6363
return graphQLService.persistVehicleScheduleFrame(vehicleScheduleFrame, selectedJourneyPatternRefs)
6464
}
6565

66+
private fun getHastusDataItems(csv: String): List<IHastusData> = filterOutDeadRunItems(READER.parseCsv(csv))
67+
6668
companion object {
6769

6870
private val READER = CsvReader(";")
@@ -189,5 +191,31 @@ class ImportService(private val graphQLServiceFactory: GraphQLServiceFactory) {
189191
throw exception
190192
}
191193
}
194+
195+
private fun filterOutDeadRunItems(hastusItems: List<IHastusData>): List<IHastusData> {
196+
val internalNumbersOfDeadRuns: List<String> = hastusItems
197+
.filterIsInstance<TripRecord>()
198+
.filter { trip ->
199+
// Null direction denotes a dead run which is not imported to Jore4.
200+
trip.direction == null
201+
}
202+
.map {
203+
it.tripInternalNumber.also {
204+
LOGGER.info {
205+
"Found a dead run trip with internal number $it. " +
206+
"Discarding the trip and the related stop points."
207+
}
208+
}
209+
}
210+
211+
return hastusItems
212+
.filter { item ->
213+
when (item) {
214+
is TripRecord -> item.tripInternalNumber !in internalNumbersOfDeadRuns
215+
is TripStopRecord -> item.tripInternalNumber !in internalNumbersOfDeadRuns
216+
else -> true
217+
}
218+
}
219+
}
192220
}
193221
}

0 commit comments

Comments
 (0)