Skip to content

Commit

Permalink
Fix et amélioration des attributions de façade et de département aux …
Browse files Browse the repository at this point in the history
…env_actions (#593)

Resolve #592
  • Loading branch information
VincentAntoine authored Jun 9, 2023
2 parents 6dbf37b + 3b5fbdd commit 4065f71
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ data class EnvActionControlProperties(
val vehicleType: VehicleTypeEnum? = null,
val infractions: List<InfractionEntity>? = listOf(),
) {
fun toEnvActionControlEntity(id: UUID, actionStartDateTimeUtc: ZonedDateTime?, actionEndDateTimeUtc: ZonedDateTime?, geom: Geometry?) = EnvActionControlEntity(
fun toEnvActionControlEntity(id: UUID, actionStartDateTimeUtc: ZonedDateTime?, actionEndDateTimeUtc: ZonedDateTime?, facade: String?, department: String?, geom: Geometry?) = EnvActionControlEntity(
id = id,
actionStartDateTimeUtc = actionStartDateTimeUtc,
actionEndDateTimeUtc = actionEndDateTimeUtc,
facade = facade,
department = department,
geom = geom,
themes = themes,
observations = observations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ data class EnvActionSurveillanceProperties(
val observations: String? = null,
val coverMissionZone: Boolean? = null,
) {
fun toEnvActionSurveillanceEntity(id: UUID, actionStartDateTimeUtc: ZonedDateTime?, actionEndDateTimeUtc: ZonedDateTime?, geom: Geometry?) = EnvActionSurveillanceEntity(
fun toEnvActionSurveillanceEntity(id: UUID, actionStartDateTimeUtc: ZonedDateTime?, actionEndDateTimeUtc: ZonedDateTime?, facade: String?, department: String?, geom: Geometry?) = EnvActionSurveillanceEntity(
id = id,
actionStartDateTimeUtc = actionStartDateTimeUtc,
actionEndDateTimeUtc = actionEndDateTimeUtc,
facade = facade,
department = department,
geom = geom,
themes = themes,
observations = observations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import java.util.UUID
object EnvActionMapper {
private const val jsonbNullString = "null"

fun getEnvActionEntityFromJSON(mapper: ObjectMapper, id: UUID, actionStartDateTimeUtc: ZonedDateTime?, actionEndDateTimeUtc: ZonedDateTime?, geom: Geometry?, actionType: ActionTypeEnum, value: String?): EnvActionEntity {
fun getEnvActionEntityFromJSON(mapper: ObjectMapper, id: UUID, actionStartDateTimeUtc: ZonedDateTime?, actionEndDateTimeUtc: ZonedDateTime?, geom: Geometry?, actionType: ActionTypeEnum, facade: String?, department: String?, value: String?): EnvActionEntity {
return try {
if (!value.isNullOrEmpty() && value != jsonbNullString) {
when (actionType) {
ActionTypeEnum.SURVEILLANCE -> mapper.readValue(value, EnvActionSurveillanceProperties::class.java).toEnvActionSurveillanceEntity(id, actionStartDateTimeUtc, actionEndDateTimeUtc, geom)
ActionTypeEnum.CONTROL -> mapper.readValue(value, EnvActionControlProperties::class.java).toEnvActionControlEntity(id, actionStartDateTimeUtc, actionEndDateTimeUtc, geom)
ActionTypeEnum.SURVEILLANCE -> mapper.readValue(value, EnvActionSurveillanceProperties::class.java).toEnvActionSurveillanceEntity(id, actionStartDateTimeUtc, actionEndDateTimeUtc, facade, department, geom)
ActionTypeEnum.CONTROL -> mapper.readValue(value, EnvActionControlProperties::class.java).toEnvActionControlEntity(id, actionStartDateTimeUtc, actionEndDateTimeUtc, facade, department, geom)
ActionTypeEnum.NOTE -> mapper.readValue(value, EnvActionNoteProperties::class.java).toEnvActionNoteEntity(id, actionStartDateTimeUtc, actionEndDateTimeUtc)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ class CreateOrUpdateMission(
when (it.actionType) {
ActionTypeEnum.CONTROL -> {
(it as EnvActionControlEntity).copy(
facade = it.geom?.let { geom -> facadeRepository.findFacadeFromGeometry(geom) },
department= it.geom?.let { geom -> departmentRepository.findDepartmentFromGeometry(geom) }
facade = (it.geom ?: mission.geom)?.let { geom -> facadeRepository.findFacadeFromGeometry(geom) },
department= (it.geom ?: mission.geom)?.let { geom -> departmentRepository.findDepartmentFromGeometry(geom) }
)
}
ActionTypeEnum.SURVEILLANCE -> {
(it as EnvActionSurveillanceEntity).copy(
facade = it.geom?.let { geom -> facadeRepository.findFacadeFromGeometry(geom) },
department= it.geom?.let { geom -> departmentRepository.findDepartmentFromGeometry(geom) }
val surveillance = it as EnvActionSurveillanceEntity
/*
When coverMissionZone is true, use mission geometry in priority, fall back to action geometry.
When coverMissionZone is not true, prioritize the other way around.
Ideally the fallbacks should not be needed, but if coverMissionZone is true and the mission geom
is null, or if coverMissionZone is false and the action geom is null, then rather that nothing,
better use the geometry that is available, if any.
*/
val geometry = if (surveillance.coverMissionZone == true) (mission.geom ?: surveillance.geom) else (surveillance.geom ?: mission.geom)
surveillance.copy(
facade = geometry?.let { geom -> facadeRepository.findFacadeFromGeometry(geom) },
department = geometry?.let { geom -> departmentRepository.findDepartmentFromGeometry(geom) }
)
}
ActionTypeEnum.NOTE -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ data class EnvActionModel(
actionEndDateTime?.atZone(UTC),
geom,
actionType,
facade,
department,
value,
)
}
Expand All @@ -89,6 +91,8 @@ data class EnvActionModel(
actionType = action.actionType,
actionStartDateTime = action.actionStartDateTimeUtc?.toInstant(),
actionEndDateTime = action.actionEndDateTimeUtc?.toInstant(),
facade = action.facade,
department = action.department,
value = EnvActionMapper.envActionEntityToJSON(mapper, action),
mission = mission,
geom = action.geom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@ package fr.gouv.cacem.monitorenv.infrastructure.database.repositories

import fr.gouv.cacem.monitorenv.domain.entities.controlResources.ControlResourceEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlResources.ControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.entities.missions.ActionTargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.missions.EnvActionControlEntity
import fr.gouv.cacem.monitorenv.domain.entities.missions.EnvActionNoteEntity
import fr.gouv.cacem.monitorenv.domain.entities.missions.EnvActionSurveillanceEntity
import fr.gouv.cacem.monitorenv.domain.entities.missions.FormalNoticeEnum
import fr.gouv.cacem.monitorenv.domain.entities.missions.InfractionEntity
import fr.gouv.cacem.monitorenv.domain.entities.missions.InfractionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.missions.MissionEntity
import fr.gouv.cacem.monitorenv.domain.entities.missions.MissionSourceEnum
import fr.gouv.cacem.monitorenv.domain.entities.missions.MissionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.missions.ThemeEntity
import fr.gouv.cacem.monitorenv.domain.entities.missions.VehicleTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.missions.VesselSizeEnum
import fr.gouv.cacem.monitorenv.domain.entities.missions.VesselTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.missions.*
import fr.gouv.cacem.monitorenv.domain.exceptions.ControlResourceOrUnitNotFoundException
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.catchThrowable
import org.junit.jupiter.api.Test
import org.locationtech.jts.geom.MultiPoint
import org.locationtech.jts.geom.MultiPolygon
import org.locationtech.jts.io.WKTReader
import org.springframework.beans.factory.annotation.Autowired
Expand Down Expand Up @@ -49,6 +37,16 @@ class JpaMissionRepositoryITests : AbstractDBTests() {

assertThat(existingMissions).hasSize(21)

val wktReader = WKTReader()

val multipolygonString = "MULTIPOLYGON(((-2.7335 47.6078, -2.7335 47.8452, -3.6297 47.8452, -3.6297 47.6078, -2.7335 47.6078)))"
val polygon = wktReader.read(multipolygonString) as MultiPolygon

val multipointString = "MULTIPOINT((49.354105 -0.427455))"
val point = wktReader.read(multipointString) as MultiPoint

val noteObservations = "Quelqu'un aurait vu quelque chose quelque part à un certain moment."

val newMission = MissionEntity(
missionTypes = listOf(MissionTypeEnum.SEA),
startDateTimeUtc = ZonedDateTime.parse("2022-01-15T04:50:09Z"),
Expand All @@ -57,6 +55,24 @@ class JpaMissionRepositoryITests : AbstractDBTests() {
missionSource = MissionSourceEnum.MONITORENV,
hasMissionOrder = false,
isUnderJdp = false,
envActions = listOf(
EnvActionControlEntity(
id = UUID.fromString("33310163-4e22-4d3d-b585-dac4431eb4b5"),
facade = "Facade 1",
department = "Department 1",
geom = point,
),
EnvActionSurveillanceEntity(
id = UUID.fromString("a6c4bd17-eb45-4504-ab15-7a18ea714a10"),
facade = "Facade 2",
department = "Department 2",
geom = polygon
),
EnvActionNoteEntity(
id = UUID.fromString("126ded89-2dc0-4c77-9bf2-49f86b9a71a1"),
observations = noteObservations
)
),
controlUnits = listOf(
ControlUnitEntity(
id = 10006,
Expand All @@ -79,6 +95,12 @@ class JpaMissionRepositoryITests : AbstractDBTests() {
assertThat(newMissionCreated.controlUnits.first().resources).hasSize(1)
assertThat(newMissionCreated.controlUnits.first().resources.first().id).isEqualTo(8)
assertThat(newMissionCreated.controlUnits.first().resources.first().name).isEqualTo("PAM Jeanne Barret")
assertThat(newMissionCreated.envActions).hasSize(3)
assertThat(newMissionCreated.envActions?.first()?.facade).isEqualTo("Facade 1")
assertThat(newMissionCreated.envActions?.first()?.department).isEqualTo("Department 1")
assertThat(newMissionCreated.envActions?.get(1)?.facade).isEqualTo("Facade 2")
assertThat(newMissionCreated.envActions?.get(1)?.department).isEqualTo("Department 2")
assertThat((newMissionCreated.envActions?.get(2) as EnvActionNoteEntity).observations).isEqualTo(noteObservations)

val missions = jpaMissionRepository.findAllMissions(
startedAfter = ZonedDateTime.parse("2022-01-01T10:54:00Z").toInstant(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ WITH departments_intersection_areas AS (
JOIN missions
ON missions.id = env_actions.mission_id
LEFT JOIN departments_areas
ON ST_Intersects(ST_MakeValid(env_actions.geom), departments_areas.geometry)
WHERE missions.mission_source = 'MONITORENV'
ON ST_Intersects(
ST_MakeValid(
CASE WHEN env_actions.action_type = 'SURVEILLANCE' AND env_actions.value->>'cover_mission_zone' = 'true' THEN COALESCE(missions.geom, env_actions.geom)
ELSE COALESCE(env_actions.geom, missions.geom) END
),
departments_areas.geometry
)
WHERE missions.mission_source IN ('MONITORENV', 'MONITORFISH')
GROUP BY env_actions.id, departments_areas.insee_dep
),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ WITH facades_intersection_areas AS (
JOIN missions
ON missions.id = env_actions.mission_id
LEFT JOIN facade_areas_subdivided
ON ST_Intersects(ST_MakeValid(env_actions.geom), facade_areas_subdivided.geometry)
WHERE missions.mission_source = 'MONITORENV'
ON ST_Intersects(
ST_MakeValid(
CASE WHEN env_actions.action_type = 'SURVEILLANCE' AND env_actions.value->>'cover_mission_zone' = 'true' THEN COALESCE(missions.geom, env_actions.geom)
ELSE COALESCE(env_actions.geom, missions.geom) END
),
facade_areas_subdivided.geometry
)
WHERE missions.mission_source IN ('MONITORENV', 'MONITORFISH')
GROUP BY env_actions.id, facade_areas_subdivided.facade
),

Expand Down

0 comments on commit 4065f71

Please sign in to comment.