Skip to content

Commit

Permalink
#218 Changed evo script, Extracted trail-places populate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
loreV committed May 25, 2022
1 parent a949c56 commit 25fbb4c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 65 deletions.
33 changes: 33 additions & 0 deletions backend/src/main/java/org/sc/processor/PlacesTrailSyncProcessor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.sc.processor

import org.sc.common.rest.TrailDto
import org.sc.manager.PlaceManager
import org.sc.manager.TrailManager
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class PlacesTrailSyncProcessor @Autowired constructor(private val trailManager: TrailManager,
private val placeManager: PlaceManager) {

private val logger = LoggerFactory.getLogger(javaClass)

fun populatePlacesWithTrailData(trailSaved: TrailDto) {
trailSaved.locations.map {
logger.info("Connecting place with Id '${it.placeId}' to newly created trail with Id '${trailSaved.id}'")
trailManager.linkTrailToPlace(trailSaved.id, it)
// Reload place
val updatedPlace = placeManager.getById(it.placeId).first()
updatedPlace.crossingTrailIds.filter { encounteredTrail -> encounteredTrail.equals(trailSaved.id) }
.forEach { encounteredTrailNotTrailSaved ->
run {
logger.info("Ensuring also place with Id '${it.placeId}' " +
"to other existing trail with Id '${encounteredTrailNotTrailSaved}'")
trailManager.linkTrailToPlace(encounteredTrailNotTrailSaved, it)
}
}
}
}

}
83 changes: 22 additions & 61 deletions backend/src/main/java/org/sc/service/TrailImporterService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import org.sc.manager.TrailFileManager
import org.sc.manager.TrailManager
import org.sc.manager.regeneration.RegenerationActionType
import org.sc.manager.regeneration.RegenerationEntryType
import org.sc.processor.DistanceProcessor
import org.sc.processor.TrailSimplifier
import org.sc.processor.TrailSimplifierLevel
import org.sc.processor.TrailsStatsCalculator
import org.sc.processor.*
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
Expand All @@ -32,6 +29,7 @@ class TrailImporterService @Autowired constructor(
private val trailFileManager: TrailFileManager,
private val placeManager: PlaceManager,
private val resourceManager: ResourceManager,
private val placesTrailSyncProcessor: PlacesTrailSyncProcessor,
private val trailsStatsCalculator: TrailsStatsCalculator,
private val trailDatasetVersionDao: TrailDatasetVersionDao,
private val coordinatesMapper: CoordinatesMapper,
Expand Down Expand Up @@ -69,31 +67,22 @@ class TrailImporterService @Autowired constructor(

val createdOn = Date()

// Create places in case they did not exist
val authHelper = authFacade.authHelper

logger.info("Creating or retrieving places for trail import...")
logger.info("Creating or retrieving crossway places for trail import...")
val trailCrosswaysFromLocations: List<PlaceDto> = getLocationFromPlaceRef(
listOf(), importingTrail.crossways, authHelper)

logger.info("Creating or retrieving other places for trail import...")
val placesLocations: List<PlaceDto> = getLocationFromPlaceRef(
trailCrosswaysFromLocations,
importingTrail.locations,
authHelper)

val otherPlacesRefs = placesLocations.map {
PlaceRef(it.name,
coordinatesMapper.map(it.coordinates.last()), it.id, it.crossingTrailIds, false)
}
val trailCrosswaysFromLocationsRefs = trailCrosswaysFromLocations.map {
PlaceRef(it.name,
coordinatesMapper.map(it.coordinates.last()), it.id, it.crossingTrailIds, false)
}
logger.info("Mapping retrieved places to refs")
val locationsSet = getDistinctPlacesRefs(placesLocations, trailCrosswaysFromLocations)

val locationsSet = otherPlacesRefs.plus(trailCrosswaysFromLocationsRefs)
.distinctBy { it.placeId }

logger.info("Reordering places in memory...")
logger.info("Reordering places refs in memory...")
val placesInOrder: List<PlaceRef> =
trailPlacesAligner.sortLocationsByTrailCoordinates(
coordinates,
Expand Down Expand Up @@ -162,7 +151,7 @@ class TrailImporterService @Autowired constructor(

val trailSaved = savedTrailAsList.first()
logger.info("Linking places to trail...")
populatePlacesWithTrailData(trailSaved)
placesTrailSyncProcessor.populatePlacesWithTrailData(trailSaved)

logger.info("Generating static resources for trail...")
val targetPlaces = trailSaved.locations.flatMap { placeManager.getById(it.placeId) }
Expand All @@ -186,21 +175,21 @@ class TrailImporterService @Autowired constructor(
return savedTrailAsList
}

private fun populatePlacesWithTrailData(trailSaved: TrailDto) {
trailSaved.locations.map {
logger.info("Connecting place with Id '${it.placeId}' to newly created trail with Id '${trailSaved.id}'")
trailsManager.linkTrailToPlace(trailSaved.id, it)
// Reload place
val updatedPlace = placeManager.getById(it.placeId).first()
updatedPlace.crossingTrailIds.filter { encounteredTrail -> encounteredTrail.equals(trailSaved.id) }
.forEach { encounteredTrailNotTrailSaved ->
run {
logger.info("Ensuring also place with Id '${it.placeId}' " +
"to other existing trail with Id '${encounteredTrailNotTrailSaved}'")
trailsManager.linkTrailToPlace(encounteredTrailNotTrailSaved, it)
}
}
private fun getDistinctPlacesRefs(placesLocations: List<PlaceDto>,
trailCrosswaysFromLocations: List<PlaceDto>): List<PlaceRef> {

val otherPlacesRefs = placesLocations.map {
PlaceRef(it.name,
coordinatesMapper.map(it.coordinates.last()), it.id, it.crossingTrailIds, false)
}

val trailCrosswaysFromLocationsRefs = trailCrosswaysFromLocations.map {
PlaceRef(it.name,
coordinatesMapper.map(it.coordinates.last()), it.id, it.crossingTrailIds, false)
}

return otherPlacesRefs.plus(trailCrosswaysFromLocationsRefs)
.distinctBy { it.placeId }
}

fun mappingMatchingTrail(targetTrailRaw: TrailRawDto): List<TrailMappingDto> {
Expand Down Expand Up @@ -262,34 +251,6 @@ class TrailImporterService @Autowired constructor(
return update
}

fun switchToStatus(trailDto: TrailDto): List<TrailDto> {
val trailToUpdate = trailsManager.getById(trailDto.id, TrailSimplifierLevel.LOW).first()

if (trailDto.status == trailToUpdate.status) {
logger.info("Did not change status to trail ${trailDto.id}")
return trailsManager.update(trailMapper.map(trailToUpdate))
}
// Turn PUBLIC -> DRAFT
if (isSwitchingToDraft(trailDto, trailToUpdate)) {
logger.info("""Trail ${trailToUpdate.code} -> ${TrailStatus.DRAFT}""")
trailsManager.propagateChangesToTrails(trailDto.id)
trailDto.locations.forEach{
placeManager.unlinkTrailFromPlace(it.placeId, trailDto.id, it.coordinates)
}
// DRAFT -> PUBLIC
} else {
logger.info("""Trail ${trailToUpdate.code} -> ${TrailStatus.PUBLIC}""")
populatePlacesWithTrailData(trailDto)
}
trailToUpdate.status = trailDto.status
return trailsManager.update(trailMapper.map(trailToUpdate))
}

private fun isSwitchingToDraft(
trailDto: TrailDto,
trailToUpdate: TrailDto
) = trailDto.status == TrailStatus.DRAFT &&
trailToUpdate.status == TrailStatus.PUBLIC

private fun getLocationFromPlaceRef(otherPlacesBeingSaved: List<PlaceDto>,
elements: List<PlaceRefDto>,
Expand Down
42 changes: 39 additions & 3 deletions backend/src/main/java/org/sc/service/TrailService.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package org.sc.service

import org.sc.common.rest.TrailDto
import org.sc.data.mapper.TrailMapper
import org.sc.data.model.TrailStatus
import org.sc.manager.*
import org.sc.processor.PlacesTrailSyncProcessor
import org.sc.processor.TrailSimplifierLevel
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.lang.IllegalStateException
import java.util.logging.Logger

@Service
class TrailService @Autowired constructor(private val trailManager: TrailManager,
private val maintenanceManager: MaintenanceManager,
private val accessibilityNotificationManager: AccessibilityNotificationManager,
private val placeManager: PlaceManager,
private val poiManager: PoiManager) {
private val placesTrailSyncProcessor: PlacesTrailSyncProcessor,
private val poiManager: PoiManager,
private val trailMapper: TrailMapper) {

private val logger = Logger.getLogger(TrailManager::class.java.name)

fun deleteById(id: String): List<TrailDto> {
val deletedTrails = trailManager.deleteById(id)
if(deletedTrails.isEmpty()) throw IllegalStateException()
if (deletedTrails.isEmpty()) throw IllegalStateException()
val deletedTrail = deletedTrails.first()
maintenanceManager.deleteByTrailId(id)
accessibilityNotificationManager.deleteByTrailId(id)
Expand All @@ -27,4 +32,35 @@ class TrailService @Autowired constructor(private val trailManager: TrailManager
logger.info("Purge deleting trail $id")
return deletedTrails
}

fun switchToStatus(trailDto: TrailDto): List<TrailDto> {
val trailToUpdate = trailManager.getById(trailDto.id, TrailSimplifierLevel.LOW).first()

if (trailDto.status == trailToUpdate.status) {
logger.info("Did not change status to trail ${trailDto.id}")
return trailManager.update(trailMapper.map(trailToUpdate))
}
// Turn PUBLIC -> DRAFT
if (isSwitchingToDraft(trailDto, trailToUpdate)) {
logger.info("""Trail ${trailToUpdate.code} -> ${TrailStatus.DRAFT}""")
trailManager.propagateChangesToTrails(trailDto.id)
trailDto.locations.forEach {
placeManager.unlinkTrailFromPlace(it.placeId, trailDto.id, it.coordinates)
}
// DRAFT -> PUBLIC
} else {
logger.info("""Trail ${trailToUpdate.code} -> ${TrailStatus.PUBLIC}""")
placesTrailSyncProcessor.populatePlacesWithTrailData(trailDto)
}
trailToUpdate.status = trailDto.status
return trailManager.update(trailMapper.map(trailToUpdate))
}

private fun isSwitchingToDraft(
trailDto: TrailDto,
trailToUpdate: TrailDto
) = trailDto.status == TrailStatus.DRAFT &&
trailToUpdate.status == TrailStatus.PUBLIC


}
2 changes: 1 addition & 1 deletion evo/01_#218.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
db.getCollection("core.Place").updateMany({}, {$set : { "isDynamic": true}} )
db.getCollection("core.Place").updateMany({}, {$set : { "isDynamic": false}} )

0 comments on commit 25fbb4c

Please sign in to comment.