diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/mapper/BahnStationMapper.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/mapper/BahnStationMapper.kt index 404993e..4515731 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/mapper/BahnStationMapper.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/mapper/BahnStationMapper.kt @@ -33,8 +33,7 @@ interface BahnStationMapper { return Station( StationId.of(NonEmptyString(countryCode + "-" + db.eva.value.toString())), CountryCode(NonEmptyString(countryCode)), - db.name, - listOf() + db.name ) } } \ No newline at end of file diff --git a/module-model/src/main/kotlin/de/twomartens/timetable/model/common/Platform.kt b/module-model/src/main/kotlin/de/twomartens/timetable/model/common/Platform.kt index 7734597..fff0ead 100644 --- a/module-model/src/main/kotlin/de/twomartens/timetable/model/common/Platform.kt +++ b/module-model/src/main/kotlin/de/twomartens/timetable/model/common/Platform.kt @@ -2,5 +2,16 @@ package de.twomartens.timetable.model.common data class Platform( var name: String, - var sections: MutableList
-) +) { + private var sectionsInternal: MutableSet
= mutableSetOf() + + val sections: Set
get() = sectionsInternal + + fun addSection(section: Section) { + sectionsInternal.add(section) + } + + fun addAllSections(sections: Collection
) { + sectionsInternal.addAll(sections) + } +} diff --git a/module-model/src/main/kotlin/de/twomartens/timetable/model/db/Station.kt b/module-model/src/main/kotlin/de/twomartens/timetable/model/db/Station.kt index db93762..29615ca 100644 --- a/module-model/src/main/kotlin/de/twomartens/timetable/model/db/Station.kt +++ b/module-model/src/main/kotlin/de/twomartens/timetable/model/db/Station.kt @@ -18,7 +18,6 @@ data class Station( var stationId: StationId, var countryCode: CountryCode, var name: NonEmptyString, - var platforms: List ) { @Id lateinit var id: ObjectId @@ -28,4 +27,24 @@ data class Station( @LastModifiedDate lateinit var lastModified: Instant + + private var platformsInternal: MutableMap = mutableMapOf() + + val platforms: List get() = platformsInternal.values.toList() + + fun addPlatform(platform: Platform?) { + if (platform == null) { + return + } + + if (!platformsInternal.containsKey(platform.name)) { + platformsInternal[platform.name] = platform + } else { + platformsInternal[platform.name]?.addAllSections(platform.sections) + } + } + + fun addAllPlatforms(platforms: Collection) { + platforms.forEach { addPlatform(it) } + } } diff --git a/module-model/src/main/kotlin/de/twomartens/timetable/model/mapper/StationMapper.kt b/module-model/src/main/kotlin/de/twomartens/timetable/model/mapper/StationMapper.kt index 4ef640a..f4e6fca 100644 --- a/module-model/src/main/kotlin/de/twomartens/timetable/model/mapper/StationMapper.kt +++ b/module-model/src/main/kotlin/de/twomartens/timetable/model/mapper/StationMapper.kt @@ -16,12 +16,13 @@ interface StationMapper { @Mapping(target = "created", ignore = true) @Mapping(target = "lastModified", ignore = true) fun mapToDB(countryCode: CountryCode, dto: Station): de.twomartens.timetable.model.db.Station { - return de.twomartens.timetable.model.db.Station( + val station = de.twomartens.timetable.model.db.Station( StationId.of(NonEmptyString(countryCode.countryCode.value + "-" + dto.id)), countryCode, - NonEmptyString(dto.name), - dto.platforms + NonEmptyString(dto.name) ) + station.addAllPlatforms(dto.platforms) + return station } fun mapStationsToDto(stations: List): List diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/service/BatchProcessTimetableConfiguration.kt b/module-server/src/main/kotlin/de/twomartens/timetable/service/BatchProcessTimetableConfiguration.kt index f31e9ff..0489019 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/service/BatchProcessTimetableConfiguration.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/service/BatchProcessTimetableConfiguration.kt @@ -2,14 +2,14 @@ package de.twomartens.timetable.service import de.twomartens.timetable.bahnApi.model.db.BahnTimetable import de.twomartens.timetable.bahnApi.repository.BahnTimetableRepository -import de.twomartens.timetable.model.common.ServiceId -import de.twomartens.timetable.model.common.TimetableId -import de.twomartens.timetable.model.common.UserId +import de.twomartens.timetable.model.common.* import de.twomartens.timetable.model.db.Service import de.twomartens.timetable.model.db.ServiceRepository import de.twomartens.timetable.model.db.ServiceStop import de.twomartens.timetable.model.db.Timetable import de.twomartens.timetable.model.dto.TimetableState +import de.twomartens.timetable.model.repository.StationRepository +import de.twomartens.timetable.route.TswRouteRepository import de.twomartens.timetable.timetable.TimetableRepository import de.twomartens.timetable.types.NonEmptyString import org.springframework.batch.core.Job @@ -112,6 +112,9 @@ class BatchProcessTimetableConfiguration { transactionManager: PlatformTransactionManager, stopStorageRequestRepository: StopStorageRequestRepository, serviceRepository: ServiceRepository, + stationRepository: StationRepository, + timetableRepository: TimetableRepository, + routeRepository: TswRouteRepository, @Value("#{jobParameters}") jobParameters: JobParameters): Step { val userId = UserId.of(NonEmptyString(jobParameters.getString("userId")!!)) val timetableId = TimetableId.of(NonEmptyString(jobParameters.getString("timetableId")!!)) @@ -125,7 +128,6 @@ class BatchProcessTimetableConfiguration { .saveState(true) .build() - val processor = ItemProcessor { val serviceId = ServiceId.of(NonEmptyString(it.trainNumber)) val serviceExists = serviceRepository.existsServiceByUserIdAndTimetableIdAndServiceId( @@ -136,6 +138,18 @@ class BatchProcessTimetableConfiguration { } else { Service(userId, timetableId, serviceId) } + + val timetable = timetableRepository.findByUserIdAndTimetableId(userId, timetableId)!! + val route = routeRepository.findByUserIdAndRouteId(userId, timetable.routeId)!! + val station = stationRepository.findByCountryCodeAndStationId( + route.country.code, + StationId.of(NonEmptyString(route.country.code + "-" + it.eva)))!! + val arrivalPlatform = buildPlatform(it.plannedArrivalPlatform) + station.addPlatform(arrivalPlatform) + val departurePlatform = buildPlatform(it.plannedDeparturePlatform) + station.addPlatform(departurePlatform) + stationRepository.save(station) // yes, this violates the pure function principle + service.addStop(ServiceStop( it.eva, it.plannedArrivalTime, @@ -192,4 +206,28 @@ class BatchProcessTimetableConfiguration { .writer(writer) .build() } + + private fun buildPlatform(platformString: String?): Platform? { + if (platformString == null) { + return null + } + + val platformFormat = Regex("(\\d+)(?:\\s+([\\w-]+))?") + val matchResult = platformFormat.matchEntire(platformString) ?: return null + val track = matchResult.groups["track"]?.value ?: return null + val platform = Platform(track) + val sectionsString = matchResult.groups["sections"]?.value ?: return platform + val sectionRangeEndpoints = sectionsString.split("-") + val sections = if (sectionRangeEndpoints.size == 1) { + setOf(Section.of(NonEmptyString(sectionRangeEndpoints[0]))) + } else { + (sectionRangeEndpoints[0].toCharArray()[0].. + sectionRangeEndpoints[1].toCharArray()[0]).map { + Section.of(NonEmptyString(it.toString())) + } + } + platform.addAllSections(sections) + + return platform + } } \ No newline at end of file