Skip to content

Commit

Permalink
#218 Updated EVO, Updated referencing update
Browse files Browse the repository at this point in the history
  • Loading branch information
loreV committed Jun 6, 2022
1 parent 25fbb4c commit e59b7fb
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public TrailResponse updateTrailStatus(@RequestBody TrailDto trailDto) {
errors.addAll(generalValidator.validateUpdateTrail(trailDto.getId()));

if (errors.isEmpty()) {
List<TrailDto> updatedTrail = trailImporterManager.switchToStatus(trailDto);
List<TrailDto> updatedTrail = trailService.switchToStatus(trailDto);
return trailResponseHelper.constructResponse(emptySet(), updatedTrail,
updatedTrail.size(), ZERO, ONE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public Document mapToDocument(PlaceRef object) {
.append(PlaceRef.PLACE_ID, object.getPlaceId())
.append(PlaceRef.ENCOUNTERED_TRAIL_IDS, object.getEncounteredTrailIds())
.append(PlaceRef.COORDINATES, coordinatesMapper.mapToDocument(object.getCoordinates()))
.append(PlaceRef.IS_DYNAMIC, coordinatesMapper.mapToDocument(object.getCoordinates()));
.append(PlaceRef.IS_DYNAMIC, object.isDynamicCrossway());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.sc.data.entity.mapper;

import org.bson.Document;
import org.sc.data.model.Trail;
import org.springframework.stereotype.Component;

@Component
public class TrailCodeMapper implements Mapper<String> {

@Override
public String mapToObject(Document document) {
return document.getString(Trail.CODE);
}

@Override
public Document mapToDocument(String object) {
throw new IllegalStateException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public class TrailPreviewMapper implements Mapper<TrailPreview> {
@Autowired
public TrailPreviewMapper(final PlaceRefMapper placeMapper,
final FileDetailsMapper fileDetailsMapper,
final CycloMapper cycloMapper,
final PlaceRefMapper placeRefMapper) {
final CycloMapper cycloMapper) {
this.placeMapper = placeMapper;
this.fileDetailsMapper = fileDetailsMapper;
this.cycloMapper = cycloMapper;
Expand Down
39 changes: 38 additions & 1 deletion backend/src/main/java/org/sc/data/repository/TrailDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class TrailDAO {
private final Mapper<Trail> trailMapper;
private final StatusFilterHelper statusFilterHelper;
private final SelectiveArgumentMapper<Trail> trailLevelMapper;
private final Mapper<String> trailCodeMapper;
private final Mapper<TrailPreview> trailPreviewMapper;
private final Mapper<TrailMapping> trailMappingMapper;
private final LinkedMediaMapper linkedMediaMapper;
Expand All @@ -65,8 +66,10 @@ public TrailDAO(final DataSource dataSource,
final LinkedMediaMapper linkedMediaMapper,
final TrailPreviewMapper trailPreviewMapper,
final PlaceRefMapper placeRefMapper,
final CycloMapper cycloMapper) {
final CycloMapper cycloMapper,
final TrailCodeMapper trailCodeMapper) {
this.collection = dataSource.getDB().getCollection(Trail.COLLECTION_NAME);

this.trailMapper = trailMapper;
this.statusFilterHelper = statusFilterHelper;
this.trailLevelMapper = trailLevelMapper;
Expand All @@ -75,6 +78,7 @@ public TrailDAO(final DataSource dataSource,
this.trailPreviewMapper = trailPreviewMapper;
this.placeRefMapper = placeRefMapper;
this.cycloMapper = cycloMapper;
this.trailCodeMapper = trailCodeMapper;
}

public List<Trail> getTrails(int skip, int limit,
Expand Down Expand Up @@ -111,6 +115,10 @@ public List<Trail> getTrailById(final String id,
return toTrailsList(collection.find(new Document(Trail.ID, id)), trailSimplifierLevel);
}

public List<String> getCodesById(final List<String> id) {
return toTrailCodeList(collection.find(new Document($_IN, new Document(Trail.ID, id))));
}

public List<Trail> getTrailByPlaceId(final String id,
final int page,
final int limit,
Expand Down Expand Up @@ -319,6 +327,11 @@ private List<Trail> toTrailsList(final Iterable<Document> documents,
.map(t -> trailLevelMapper.mapToObject(t, trailSimplifierLevel)).collect(toList());
}

private List<String> toTrailCodeList(final Iterable<Document> documents) {
return StreamSupport.stream(documents.spliterator(), false)
.map(trailCodeMapper::mapToObject).collect(toList());
}

@NotNull
private FindIterable<Document> foundTrailsWithinSquare(final CoordinatesRectangle geoSquare,
final int skip,
Expand Down Expand Up @@ -389,6 +402,30 @@ public List<Trail> update(@NotNull Trail trail) {
return getTrailById(trail.getId(), TrailSimplifierLevel.LOW);
}

@NotNull
public List<Trail> updateTrailNamePlaceReference(final String trailId,
final String placeId,
final String placeName) {
collection.updateOne(
new Document(Trail.ID, trailId)
.append(Trail.START_POS + DOT + PlaceRef.PLACE_ID, placeId),
new Document($_SET,
new Document(Trail.START_POS + DOT + PlaceRef.NAME, placeName)));
collection.updateOne(
new Document(Trail.ID, trailId)
.append(Trail.FINAL_POS + DOT + PlaceRef.PLACE_ID, placeId),
new Document($_SET,
new Document(Trail.FINAL_POS + DOT + PlaceRef.NAME, placeName)));
collection.updateOne(
new Document(Trail.ID, trailId)
.append(Trail.LOCATIONS + DOT + PlaceRef.PLACE_ID, placeId),
new Document($_SET,
new Document(Trail.LOCATIONS + DOLLAR + PlaceRef.NAME, placeName)));

return getTrailById(trailId, TrailSimplifierLevel.LOW);
}


public List<TrailMapping> getByStartEndPoint(final double startLatitude, final double startLongitude,
final double endLatitude, final double endLongitude) {
final FindIterable<Document> documents = collection.find(
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main/java/org/sc/job/ResourceUpdaterJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ResourceUpdaterJob {
private static final String STARTING_COMPRESSION_JOB = "Going to run reosurce updater job (batch size: %s)...";
private static final String DONE_COMPRESSION_JOB = "Done with resource updater job.";

private ResourceService resourceService;
private final ResourceService resourceService;
private final int imageBatchSize;

@Autowired
Expand All @@ -28,7 +28,7 @@ public ResourceUpdaterJob(final ResourceService resourceService,
this.imageBatchSize = appProperties.getJobImageBatchSize();
}

@Scheduled(cron = "0 */1 * * * *")
@Scheduled(cron = "*/10 * * * * *")
public void doRegenerateResources() {
LOGGER.trace(format(STARTING_COMPRESSION_JOB, imageBatchSize));
resourceService.execute();
Expand Down
11 changes: 10 additions & 1 deletion backend/src/main/java/org/sc/manager/TrailManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class TrailManager @Autowired constructor(
private val trailMapper: TrailMapper,
private val linkedMediaMapper: LinkedMediaMapper,
private val placeRefMapper: PlaceRefMapper,
private val coordinatesMapper: CoordinatesMapper,
private val trailIntersectionMapper: TrailIntersectionMapper,
private val trailMappingMapper: TrailMappingMapper,
private val trailPlacesAligner: TrailPlacesAligner,
Expand All @@ -42,6 +41,9 @@ class TrailManager @Autowired constructor(
fun getById(id: String, level: TrailSimplifierLevel): List<TrailDto> =
trailDAO.getTrailById(id, level).map { trailMapper.map(it) }

fun getByIds(id: String, level: TrailSimplifierLevel): List<TrailDto> =
trailDAO.getTrailById(id, level).map { trailMapper.map(it) }

fun getByPlaceRefId(code: String, page: Int, limit: Int, level: TrailSimplifierLevel): List<TrailDto> =
trailDAO.getTrailByPlaceId(code, page, limit, level).map { trailMapper.map(it) }

Expand All @@ -66,6 +68,10 @@ class TrailManager @Autowired constructor(
return trailDAO.update(trail).map { trailMapper.map(it) };
}

fun updateTrailPlaceNamesReference(trailId: String, placeId: String, placeName: String): List<TrailDto> {
return trailDAO.updateTrailNamePlaceReference(trailId, placeId, placeName).map { trailMapper.map(it) };
}

fun linkMedia(id: String, linkedMediaRequest: LinkedMediaDto): List<TrailDto> {
val linkMedia = linkedMediaMapper.map(linkedMediaRequest)
val result = trailDAO.linkMedia(id, linkMedia)
Expand Down Expand Up @@ -177,6 +183,8 @@ class TrailManager @Autowired constructor(
}
}

fun getCodesByTrailIds(ids: List<String>) = trailDAO.getCodesById(ids);

private fun getTrailIntersection(coordinates: List<Coordinates2D>, trail: Trail): TrailIntersectionDto {
val coordinates2D = GeoCalculator.getIntersectionPointsBetweenSegments(
coordinates, trail.geoLineString
Expand All @@ -199,5 +207,6 @@ class TrailManager @Autowired constructor(
private fun getPreviewById(id: String): List<TrailPreview> =
trailDAO.getTrailPreviewById(id);


}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.sc.manager.TrailManager
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.lang.IllegalStateException

@Component
class PlacesTrailSyncProcessor @Autowired constructor(private val trailManager: TrailManager,
Expand All @@ -17,7 +18,8 @@ class PlacesTrailSyncProcessor @Autowired constructor(private val trailManager:
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 ->
Expand All @@ -27,7 +29,25 @@ class PlacesTrailSyncProcessor @Autowired constructor(private val trailManager:
trailManager.linkTrailToPlace(encounteredTrailNotTrailSaved, it)
}
}

if(it.isDynamicCrossway) {
updateCrosswayNameWithTrailsPassingCodes(it.placeId)
}
}
}

private fun updateCrosswayNameWithTrailsPassingCodes(placeId: String) {
val placeList = placeManager.getById(placeId)
if(placeList.isEmpty()) throw IllegalStateException("Cannot update place name of a not-existing location")
val place = placeList.first()
val placeCrossingTrailsNames =
trailManager.getCodesByTrailIds(place.crossingTrailIds).joinToString(", ")
val updatedName = "Crocevia $placeCrossingTrailsNames";
logger.info("Updating place with id ${place.id} with previous name='${place.name}' with new name='$updatedName'")
place.name = updatedName
placeManager.update(place)
// update trails places references
place.crossingTrailIds.forEach { trailManager.updateTrailPlaceNamesReference(it, placeId, updatedName)}
}

}
5 changes: 3 additions & 2 deletions backend/src/main/java/org/sc/service/PlaceService.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.sc.service

import org.sc.manager.PlaceManager
import org.sc.manager.TrailManager
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class PlaceService @Autowired constructor(private val placeManager: PlaceManager) {

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


}
9 changes: 6 additions & 3 deletions backend/src/main/java/org/sc/service/TrailImporterService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,10 @@ class TrailImporterService @Autowired constructor(
}

val created = placeManager.create(PlaceDto(null, it.name, "",
emptyList(), emptyList(), listOf(it.coordinates), it.encounteredTrailIds,
it.isDynamicCrossway, RecordDetailsDto(Date(),
emptyList(), emptyList(), listOf(it.coordinates),
it.encounteredTrailIds,
it.isDynamicCrossway,
RecordDetailsDto(Date(),
authHelper.username,
authHelper.instance,
authHelper.realm))).first()
Expand All @@ -289,7 +291,8 @@ class TrailImporterService @Autowired constructor(
}
}

private fun placeSubmittedMatchingSubmittedOnes(otherPlacesBeingSaved: List<PlaceDto>, it: PlaceRefDto): List<PlaceDto> =
private fun placeSubmittedMatchingSubmittedOnes(otherPlacesBeingSaved: List<PlaceDto>,
it: PlaceRefDto): List<PlaceDto> =
otherPlacesBeingSaved
.filter { otherPlaceSubmitted -> otherPlaceSubmitted.name == it.name }
.filter { otherPlaceSubmitted ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.sc.integration;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sc.common.rest.PlaceDto;
import org.sc.common.rest.TrailImportDto;
import org.sc.common.rest.response.PlaceResponse;
import org.sc.common.rest.response.TrailResponse;
import org.sc.configuration.DataSource;
import org.sc.controller.PlaceController;
import org.sc.controller.TrailController;
import org.sc.controller.admin.AdminPlaceController;
import org.sc.controller.admin.AdminTrailController;
import org.sc.controller.admin.AdminTrailImporterController;
import org.sc.processor.TrailSimplifierLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class CrosswayIntegrationTest extends ImportTrailIT {

public static final String LEVEL = TrailSimplifierLevel.FULL.toString();
public static final int TWELVE_SECONDS = 12000;

@Autowired
private DataSource dataSource;

@Autowired private AdminPlaceController adminPlaceController;
@Autowired private PlaceController placeController;
@Autowired private AdminTrailController adminTrailController;
@Autowired private TrailController trailController;

private PlaceResponse addedPlace;
private TrailResponse importedTrail;

@Before
public void setUp() {
IntegrationUtils.clearCollections(dataSource);
importedTrail = adminTrailController.importTrail(TrailImportRestIntegrationTest.createThreePointsTrailImportWithNoCrossways(adminPlaceController));

addedPlace = adminPlaceController.create(getPlaceWithCrosswayTrail(Arrays.asList(
importedTrail.getContent().stream().findFirst().get().getId()
)));
}

@Test
public void shouldCheckOneAutoUpdateCrosswayNameWithOneTrailCrossingPlace() {
PlaceResponse placeResponse = placeController.get(addedPlace.getContent().get(0).getId());
final PlaceDto result = placeResponse.getContent().stream().findFirst().get();

assertThat(result.getName()).isEqualTo(PLACE_NAME);
}


private PlaceDto getPlaceWithCrosswayTrail(List<String> trailIdsCrossingPlace) {
return new PlaceDto(null, PLACE_NAME, PLACE_EXPECTED_DESCRIPTION,
TAGS, Collections.emptyList(),
Collections.singletonList(INTERMEDIATE_COORDINATES_DTO), trailIdsCrossingPlace,
true, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void shouldRetrieveItBack() {
assertThat(addedPlace.getContent().get(0)).isEqualTo(returnedPlaceDto);
}


@Test
public void shouldRetrieveAndModifyItBack() {
PlaceResponse placeResponse = placeController.get(addedPlace.getContent().get(0).getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public class TrailImportRestIntegrationTest extends ImportTrailIT {
public static final String REALM = "S&C";
public static final String INSTANCE_ID = "BOLOGNA_1";


public static final FileDetailsDto IMPORTED_FILE_DETAILS = new FileDetailsDto(EXPECTED_DATE, USER_ADMIN, INSTANCE_ID, REALM, ANY_FILENAME, ANY_FILENAME, USER_ADMIN);

public static List<PlaceRefDto> LOCATION_REFS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class TrailIntersectionsRestIntegrationTest {

public static final int ANY_OFFICIAL_ETA = 15;
public static final String ANY_MAINTAINING_SECTION = "CAI Bologna";
public static final String MAINTAINING_SECTION = "CAI Bologna";

@Autowired
DataSource dataSource;
Expand Down Expand Up @@ -260,7 +261,7 @@ public void shallCreatTrailThenAddAnotherOneWithStartingPlaceMatchingCrossway()
Arrays.asList(startPlaceCrocevia, endPlaceMonteBaducco),
crosswaySingleton, TrailClassification.E, "Italy",
trailRawDto.getCoordinates(),
"CAI Bologna",
MAINTAINING_SECTION,
false, "Ovest", Collections.emptyList(),
new Date(), trailRawDto.getFileDetails(), TrailStatus.PUBLIC);

Expand Down
3 changes: 2 additions & 1 deletion evo/01_#218.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
db.getCollection("core.Place").updateMany({}, {$set : { "isDynamic": false}} )
db.getCollection("core.Place").updateMany({}, {$set : { "isDynamic": false}} )
db.getCollection("core.Trail").updateMany({}, {$set : { "locations.$[].isDynamic": false}} )

0 comments on commit e59b7fb

Please sign in to comment.