Skip to content

Commit

Permalink
Refactor CreateOrUpdateMissionWithAttachedReporting
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Oct 19, 2023
1 parent 9d0c622 commit 97e6e7d
Show file tree
Hide file tree
Showing 38 changed files with 248 additions and 383 deletions.
Binary file added backend/.gradle/8.4/checksums/checksums.lock
Binary file not shown.
Binary file not shown.
Binary file added backend/.gradle/8.4/checksums/sha1-checksums.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file added backend/.gradle/8.4/fileChanges/last-build.bin
Binary file not shown.
Binary file added backend/.gradle/8.4/fileHashes/fileHashes.bin
Binary file not shown.
Binary file added backend/.gradle/8.4/fileHashes/fileHashes.lock
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
2 changes: 2 additions & 0 deletions backend/.gradle/buildOutputCleanup/cache.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Wed Oct 18 10:21:17 CEST 2023
gradle.version=8.4
Binary file not shown.
Binary file added backend/.gradle/file-system.probe
Binary file not shown.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import org.springframework.data.domain.Pageable
import java.time.Instant

interface IMissionRepository {
fun findById(missionId: Int): MissionDTO
fun findAll(
fun findFullMissionById(missionId: Int): MissionDTO
fun findById(missionId: Int): MissionEntity
fun findAllFullMissions(
startedAfter: Instant,
startedBefore: Instant?,
missionTypes: List<String>?,
Expand All @@ -17,7 +18,15 @@ interface IMissionRepository {
seaFronts: List<String>?,
pageable: Pageable,
): List<MissionDTO>

fun findAll(
startedAfter: Instant,
startedBefore: Instant?,
missionTypes: List<String>?,
missionStatuses: List<String>?,
missionSources: List<MissionSourceEnum>? = null,
seaFronts: List<String>?,
pageable: Pageable,
): List<MissionEntity>
fun save(mission: MissionEntity): MissionDTO
fun delete(missionId: Int)
fun count(): Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionContr
import fr.gouv.cacem.monitorenv.domain.repositories.IDepartmentAreaRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IFacadeAreasRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO

@UseCase
class CreateOrUpdateMission(
Expand All @@ -22,8 +21,7 @@ class CreateOrUpdateMission(
@Throws(IllegalArgumentException::class)
fun execute(
mission: MissionEntity?,
attachedReportingIds: List<Int>? = null,
): MissionDTO {
): MissionEntity {
require(mission != null) {
"No mission to create or update"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,36 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.missions

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.mission.*
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.ActionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionNoteEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionSurveillanceEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.EnvActionControlEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IDepartmentAreaRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IFacadeAreasRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IReportingRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.inputs.missions.EnvActionAttachedToReportingIds

@UseCase
class CreateOrUpdateMissionWithAttachedReporting(
private val departmentRepository: IDepartmentAreaRepository,
private val facadeRepository: IFacadeAreasRepository,
private val createOrUpdateMission: CreateOrUpdateMission,
private val missionRepository: IMissionRepository,
private val reportingRepository: IReportingRepository,

) {
@Throws(IllegalArgumentException::class)
fun execute(
mission: MissionEntity?,
attachedReportingIds: List<Int>? = null,
attachedReportingIds: List<Int>,
envActionsAttachedToReportingIds: List<EnvActionAttachedToReportingIds>,

): MissionDTO {
require(mission != null) {
"No mission to create or update"
val savedMission = createOrUpdateMission.execute(mission)
require(savedMission.id != null) {
"The mission id is null"
}
val envActions = mission.envActions?.map {
when (it.actionType) {
ActionTypeEnum.CONTROL -> {
(it as EnvActionControlEntity).copy(
facade = (
it.geom
?: mission.geom
)?.let { geom -> facadeRepository.findFacadeFromGeometry(geom) },
department = (
it.geom
?: mission.geom
)?.let { geom -> departmentRepository.findDepartmentFromGeometry(geom) },
)
}

ActionTypeEnum.SURVEILLANCE -> {
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) },
)
}
reportingRepository.attachReportingsToMission(attachedReportingIds, savedMission.id)

ActionTypeEnum.NOTE -> {
(it as EnvActionNoteEntity).copy()
}
}
envActionsAttachedToReportingIds.forEach {
reportingRepository.attachEnvActionsToReportings(it.first, it.second)
}

var facade: String? = null

if (mission.geom != null) {
facade = facadeRepository.findFacadeFromGeometry(mission.geom)
}

val missionToSave = mission.copy(
facade = facade,
envActions = envActions,
)
val savedMission = missionRepository.save(missionToSave)

if (savedMission.mission.id == null) {
throw IllegalArgumentException("Mission id is null")
}

if (attachedReportingIds != null) {
reportingRepository.attachReportingsToMission(attachedReportingIds, savedMission.mission.id)
}

if (envActionsAttachedToReportingIds != null) {
envActionsAttachedToReportingIds.forEach {
reportingRepository.attachEnvActionsToReportings(it.first, it.second)
}
}

return missionRepository.findById(savedMission.mission.id)
return missionRepository.findFullMissionById(savedMission.id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import org.slf4j.LoggerFactory
import java.time.ZonedDateTime

@UseCase
class GetEngagedControlUnits(private val getMissions: GetMissions) {
class GetEngagedControlUnits(private val getFullMissions: GetFullMissions) {
private val logger = LoggerFactory.getLogger(GetEngagedControlUnits::class.java)

fun execute(): List<LegacyControlUnitEntity> {
val openedMissions = getMissions.execute(
val openedMissions = getFullMissions.execute(
startedAfterDateTime = ZonedDateTime.now().minusMonths(2),
startedBeforeDateTime = null,
missionSources = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@file:Suppress("ktlint:standard:package-name")

package fr.gouv.cacem.monitorenv.domain.use_cases.missions

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO

@UseCase
class GetFullMissionById(private val missionRepository: IMissionRepository) {
fun execute(missionId: Int): MissionDTO {
return missionRepository.findFullMissionById(missionId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@file:Suppress("ktlint:standard:package-name")

package fr.gouv.cacem.monitorenv.domain.use_cases.missions

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionSourceEnum
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO
import org.slf4j.LoggerFactory
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import java.time.ZonedDateTime

@UseCase
class GetFullMissions(private val missionRepository: IMissionRepository) {
private val logger = LoggerFactory.getLogger(GetFullMissions::class.java)

fun execute(
startedAfterDateTime: ZonedDateTime?,
startedBeforeDateTime: ZonedDateTime?,
missionSources: List<MissionSourceEnum>?,
missionTypes: List<String>?,
missionStatuses: List<String>?,
pageNumber: Int?,
pageSize: Int?,
seaFronts: List<String>?,
): List<MissionDTO> {
val missions = missionRepository.findAllFullMissions(
startedAfter = startedAfterDateTime?.toInstant() ?: ZonedDateTime.now().minusDays(30).toInstant(),
startedBefore = startedBeforeDateTime?.toInstant(),
missionSources = missionSources ?: listOf(MissionSourceEnum.MONITORENV, MissionSourceEnum.MONITORFISH),
missionTypes = missionTypes,
missionStatuses = missionStatuses,
seaFronts = seaFronts,
pageable = if (pageNumber != null && pageSize != null) {
PageRequest.of(
pageNumber,
pageSize,
)
} else {
Pageable.unpaged()
},
)

logger.info("Found ${missions.size} mission(s)")

return missions
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.missions

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO

@UseCase
class GetMissionById(private val missionRepository: IMissionRepository) {
fun execute(missionId: Int): MissionDTO {
fun execute(missionId: Int): MissionEntity {
return missionRepository.findById(missionId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.missions

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionSourceEnum
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO
import org.slf4j.LoggerFactory
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
Expand All @@ -24,7 +24,7 @@ class GetMissions(private val missionRepository: IMissionRepository) {
pageNumber: Int?,
pageSize: Int?,
seaFronts: List<String>?,
): List<MissionDTO> {
): List<MissionEntity> {
val missions = missionRepository.findAll(
startedAfter = startedAfterDateTime?.toInstant() ?: ZonedDateTime.now().minusDays(30).toInstant(),
startedBefore = startedBeforeDateTime?.toInstant(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data class CreateOrUpdateMissionDataInput(
val envActions: List<MissionEnvActionDataInput>? = null,
val hasMissionOrder: Boolean? = false,
val isUnderJdp: Boolean? = false,
val attachedReportingIds: List<Int>? = null,
val attachedReportingIds: List<Int>,
) {
fun toMissionEntity(): MissionEntity {
val hasMissionOrder = this.hasMissionOrder ?: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.outputs

import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.LegacyControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionSourceEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionEntity
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO
import org.locationtech.jts.geom.MultiPolygon
import java.time.ZonedDateTime

Expand All @@ -28,29 +28,29 @@ data class MissionDataOutput(
val isGeometryComputedFromControls: Boolean,
) {
companion object {
fun fromMissionDTO(dto: MissionDTO): MissionDataOutput {
requireNotNull(dto.mission.id) {
fun fromMissionEntity(mission: MissionEntity): MissionDataOutput {
requireNotNull(mission.id) {
"a mission must have an id"
}

return MissionDataOutput(
id = dto.mission.id,
missionTypes = dto.mission.missionTypes,
controlUnits = dto.mission.controlUnits,
openBy = dto.mission.openBy,
closedBy = dto.mission.closedBy,
observationsCacem = dto.mission.observationsCacem,
observationsCnsp = dto.mission.observationsCnsp,
facade = dto.mission.facade,
geom = dto.mission.geom,
startDateTimeUtc = dto.mission.startDateTimeUtc,
endDateTimeUtc = dto.mission.endDateTimeUtc,
envActions = dto.mission.envActions,
missionSource = dto.mission.missionSource,
isClosed = dto.mission.isClosed,
hasMissionOrder = dto.mission.hasMissionOrder,
isUnderJdp = dto.mission.isUnderJdp,
isGeometryComputedFromControls = dto.mission.isGeometryComputedFromControls,
id = mission.id,
missionTypes = mission.missionTypes,
controlUnits = mission.controlUnits,
openBy = mission.openBy,
closedBy = mission.closedBy,
observationsCacem = mission.observationsCacem,
observationsCnsp = mission.observationsCnsp,
facade = mission.facade,
geom = mission.geom,
startDateTimeUtc = mission.startDateTimeUtc,
endDateTimeUtc = mission.endDateTimeUtc,
envActions = mission.envActions,
missionSource = mission.missionSource,
isClosed = mission.isClosed,
hasMissionOrder = mission.hasMissionOrder,
isUnderJdp = mission.isUnderJdp,
isGeometryComputedFromControls = mission.isGeometryComputedFromControls,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import java.time.ZonedDateTime
@Tag(description = "API Missions", name = "Missions")
class MissionsController(
private val createOrUpdateMissionWithAttachedReporting: CreateOrUpdateMissionWithAttachedReporting,
private val getMissions: GetMissions,
private val getMissionById: GetMissionById,
private val getFullMissions: GetFullMissions,
private val getFullMissionById: GetFullMissionById,
private val deleteMission: DeleteMission,
private val getEngagedControlUnits: GetEngagedControlUnits,
) {
Expand Down Expand Up @@ -55,7 +55,7 @@ class MissionsController(
@RequestParam(name = "seaFronts", required = false)
seaFronts: List<String>?,
): List<MissionsDataOutput> {
val missions = getMissions.execute(
val missions = getFullMissions.execute(
startedAfterDateTime = startedAfterDateTime,
startedBeforeDateTime = startedBeforeDateTime,
missionSources = missionSources,
Expand Down Expand Up @@ -89,7 +89,7 @@ class MissionsController(
@PathVariable(name = "missionId")
missionId: Int,
): MissionDataOutput {
val mission = getMissionById.execute(missionId = missionId)
val mission = getFullMissionById.execute(missionId = missionId)

return MissionDataOutput.fromMissionDTO(mission)
}
Expand Down
Loading

0 comments on commit 97e6e7d

Please sign in to comment.