Skip to content

Commit

Permalink
refacto envActionDataOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
thoomasbro authored and claire2212 committed Oct 20, 2023
1 parent 2153184 commit c9ee998
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.inputs.missions.
@UseCase
class CreateOrUpdateMissionWithAttachedReporting(
private val departmentRepository: IDepartmentAreaRepository,
private val missionRepository: IMissionRepository,
private val facadeRepository: IFacadeAreasRepository,
private val missionRepository: IMissionRepository,
private val reportingRepository: IReportingRepository,

) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.mission
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.LegacyControlUnitEntity
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 @@ -20,7 +19,7 @@ data class MissionDataOutput(
val geom: MultiPolygon? = null,
val startDateTimeUtc: ZonedDateTime,
val endDateTimeUtc: ZonedDateTime? = null,
val envActions: List<EnvActionEntity>? = null,
val envActions: List<MissionEnvActionDataOutput>? = null,
val missionSource: MissionSourceEnum,
val isClosed: Boolean,
val hasMissionOrder: Boolean,
Expand Down Expand Up @@ -48,7 +47,7 @@ data class MissionDataOutput(
geom = dto.mission.geom,
startDateTimeUtc = dto.mission.startDateTimeUtc,
endDateTimeUtc = dto.mission.endDateTimeUtc,
envActions = dto.mission.envActions,
envActions = dto.mission.envActions?.map { MissionEnvActionDataOutput.fromEnvActionEntity(it) },
missionSource = dto.mission.missionSource,
isClosed = dto.mission.isClosed,
hasMissionOrder = dto.mission.hasMissionOrder,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.missions

data class MissionEnvActionDataOutput()
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.ActionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionEntity
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 org.locationtech.jts.geom.Geometry
import java.time.ZonedDateTime
import java.util.UUID

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "actionType",
visible = true,
)
@JsonSubTypes(
JsonSubTypes.Type(EnvActionControlEntity::class, name = "CONTROL"),
JsonSubTypes.Type(EnvActionSurveillanceEntity::class, name = "SURVEILLANCE"),
JsonSubTypes.Type(EnvActionNoteEntity::class, name = "NOTE"),
)
data class MissionEnvActionDataOutput(
val id: UUID,
val actionType: ActionTypeEnum,
val actionStartDateTimeUtc: ZonedDateTime? = null,
val actionEndDateTimeUtc: ZonedDateTime? = null,
val department: String? = null,
val facade: String? = null,
val geom: Geometry? = null,
) {
companion object {
fun fromEnvActionEntity(envActionEntity: EnvActionEntity) = MissionEnvActionDataOutput(
id = envActionEntity.id,
actionType = envActionEntity.actionType,
actionStartDateTimeUtc = envActionEntity.actionStartDateTimeUtc,
actionEndDateTimeUtc = envActionEntity.actionEndDateTimeUtc,
department = envActionEntity.department,
facade = envActionEntity.facade,
geom = envActionEntity.geom,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ data class EnvActionModel(
@Column(name = "is_safety_equipment_and_standards_compliance_control")
val isSafetyEquipmentAndStandardsComplianceControl: Boolean? = null,
@Column(name = "is_seafarers_control") val isSeafarersControl: Boolean? = null,
@OneToMany(fetch = FetchType.EAGER, mappedBy = "attachedEnvActionId")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "attachedEnvAction")
@JsonManagedReference
val attachedReporting: List<ReportingModel>? = null,
val attachedReporting: List<ReportingModel>? = listOf(),
) {

fun toActionEntity(mapper: ObjectMapper): EnvActionEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import org.n52.jackson.datatype.jts.GeometryDeserializer
import org.n52.jackson.datatype.jts.GeometrySerializer
import java.time.Instant
import java.time.ZoneOffset.UTC
import java.util.UUID

@Entity
@Table(name = "reportings")
Expand All @@ -45,6 +44,7 @@ data class ReportingModel(
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
val id: Int? = null,

@Generated(GenerationTime.INSERT)
@Column(
name = "reporting_id",
Expand All @@ -54,62 +54,100 @@ data class ReportingModel(
insertable = false,
)
val reportingId: Long? = null,

@Column(name = "source_type", columnDefinition = "reportings_source_type")
@Enumerated(EnumType.STRING)
@Type(PostgreSQLEnumType::class)
val sourceType: SourceTypeEnum? = null,

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "semaphore_id", nullable = true)
@JsonBackReference
val semaphore: SemaphoreModel? = null,

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "control_unit_id", nullable = true)
@JsonBackReference
val controlUnit: ControlUnitModel? = null,
@Column(name = "source_name") val sourceName: String? = null,
@Column(name = "source_name")
val sourceName: String? = null,

@Column(name = "target_type", columnDefinition = "reportings_target_type")
@Enumerated(EnumType.STRING)
@Type(PostgreSQLEnumType::class)
val targetType: TargetTypeEnum? = null,

@Column(name = "vehicle_type", columnDefinition = "reportings_vehicle_type")
@Enumerated(EnumType.STRING)
@Type(PostgreSQLEnumType::class)
val vehicleType: VehicleTypeEnum? = null,

@Column(name = "target_details", columnDefinition = "jsonb")
@Type(JsonBinaryType::class)
val targetDetails: List<TargetDetailsEntity>? = listOf(),

@JsonSerialize(using = GeometrySerializer::class)
@JsonDeserialize(contentUsing = GeometryDeserializer::class)
@Column(name = "geom")
val geom: Geometry? = null,
@Column(name = "sea_front") val seaFront: String? = null,
@Column(name = "description") val description: String? = null,

@Column(name = "sea_front")
val seaFront: String? = null,

@Column(name = "description")
val description: String? = null,

@Column(name = "report_type", columnDefinition = "reportings_report_type")
@Enumerated(EnumType.STRING)
@Type(PostgreSQLEnumType::class)
val reportType: ReportingTypeEnum? = null,
@Column(name = "theme") val theme: String? = null,

@Column(name = "theme")
val theme: String? = null,

@Column(name = "sub_themes")
@Type(ListArrayType::class)
val subThemes: List<String>? = listOf(),
@Column(name = "action_taken") val actionTaken: String? = null,
@Column(name = "is_control_required") val isControlRequired: Boolean? = null,
@Column(name = "has_no_unit_available") val hasNoUnitAvailable: Boolean? = null,
@Column(name = "created_at") val createdAt: Instant,
@Column(name = "validity_time") val validityTime: Int? = null,
@Column(name = "is_archived", nullable = false) val isArchived: Boolean,
@Column(name = "is_deleted", nullable = false) val isDeleted: Boolean,
@Column(name = "open_by") val openBy: String? = null,

@Column(name = "action_taken")
val actionTaken: String? = null,

@Column(name = "is_control_required")
val isControlRequired: Boolean? = null,

@Column(name = "has_no_unit_available")
val hasNoUnitAvailable: Boolean? = null,

@Column(name = "created_at")
val createdAt: Instant,

@Column(name = "validity_time")
val validityTime: Int? = null,

@Column(name = "is_archived", nullable = false)
val isArchived: Boolean,

@Column(name = "is_deleted", nullable = false)
val isDeleted: Boolean,

@Column(name = "open_by")
val openBy: String? = null,

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "mission_id", nullable = true)
@JsonBackReference
val mission: MissionModel? = null,
@Column(name = "attached_to_mission_at_utc") val attachedToMissionAtUtc: Instant? = null,

@Column(name = "attached_to_mission_at_utc")
val attachedToMissionAtUtc: Instant? = null,

@Column(name = "detached_from_mission_at_utc")
val detachedFromMissionAtUtc: Instant? = null,

@JdbcType(UUIDJdbcType::class)
@Column(name = "attached_env_action_id", columnDefinition = "uuid")
val attachedEnvActionId: UUID? = null,
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "attached_env_action_id", columnDefinition = "uuid", referencedColumnName = "id")
val attachedEnvAction: EnvActionModel? = null,
) {

fun toReporting() =
Expand Down Expand Up @@ -140,7 +178,7 @@ data class ReportingModel(
missionId = mission?.id,
attachedToMissionAtUtc = attachedToMissionAtUtc?.atZone(UTC),
detachedFromMissionAtUtc = detachedFromMissionAtUtc?.atZone(UTC),
attachedEnvActionId = attachedEnvActionId,
attachedEnvActionId = attachedEnvAction?.id,
)
fun toReportingDTO(objectMapper: ObjectMapper) =
ReportingDTO(
Expand Down Expand Up @@ -182,6 +220,7 @@ data class ReportingModel(
semaphoreReference: SemaphoreModel?,
controlUnitReference: ControlUnitModel?,
missionReference: MissionModel?,
envActionReference: EnvActionModel?,
) =
ReportingModel(
id = reporting.id,
Expand Down Expand Up @@ -210,7 +249,7 @@ data class ReportingModel(
mission = missionReference,
attachedToMissionAtUtc = reporting.attachedToMissionAtUtc?.toInstant(),
detachedFromMissionAtUtc = reporting.detachedFromMissionAtUtc?.toInstant(),
attachedEnvActionId = reporting.attachedEnvActionId,
attachedEnvAction = envActionReference,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fr.gouv.cacem.monitorenv.domain.repositories.IReportingRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingDTO
import fr.gouv.cacem.monitorenv.infrastructure.database.model.ReportingModel
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlUnitRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBEnvActionRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBMissionRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBReportingRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBSemaphoreRepository
Expand All @@ -27,6 +28,7 @@ class JpaReportingRepository(
private val dbMissionRepository: IDBMissionRepository,
private val dbSemaphoreRepository: IDBSemaphoreRepository,
private val dbControlUnitRepository: IDBControlUnitRepository,
private val dbEnvActionRepository: IDBEnvActionRepository,
private val mapper: ObjectMapper,
) : IReportingRepository {

Expand Down Expand Up @@ -103,12 +105,22 @@ class JpaReportingRepository(
} else {
null
}
val envActionReference =
if (reporting.attachedEnvActionId != null) {
dbEnvActionRepository.getReferenceById(
reporting.attachedEnvActionId,
)
} else {
null
}

val reportingModel =
ReportingModel.fromReportingEntity(
reporting = reporting,
semaphoreReference = semaphoreReference,
controlUnitReference = controlUnitReference,
missionReference = missionReference,
envActionReference = envActionReference,
)
dbReportingRepository.saveAndFlush(reportingModel).toReportingDTO(mapper)
} catch (e: JpaObjectRetrievalFailureException) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces

import fr.gouv.cacem.monitorenv.infrastructure.database.model.EnvActionModel
import org.springframework.data.jpa.repository.JpaRepository
import java.util.UUID

interface IDBEnvActionRepository : JpaRepository<EnvActionModel, UUID>
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ interface IDBReportingRepository : JpaRepository<ReportingModel, Int> {
)
fun attachReportingsToMission(reportingIds: List<Int>, missionId: Int)

// ['envActionUUID' ,[reportingIds]]
// [['uu1', [1,2]]]

// reporting_id, attached_env_action_id
// 1, 'uu1'
// 2, 'uu2'
// 3, null

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(
value =
Expand Down
Loading

0 comments on commit c9ee998

Please sign in to comment.