Skip to content

Commit

Permalink
feat: add comments and control units and rename briefing to dashboard…
Browse files Browse the repository at this point in the history
…_datas
  • Loading branch information
maximeperrault committed Oct 8, 2024
1 parent 554ec8a commit 077d70c
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ data class DashboardEntity(
val amps: List<Int>,
val vigilanceAreas: List<Int>,
val regulatoryAreas: List<Int>,
val controlUnits: List<Int>,
val inseeCode: String?,
val comments: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class DashboardDataInput(
val regulatoryAreas: List<Int>,
val amps: List<Int>,
val vigilanceAreas: List<Int>,
val controlUnits: List<Int>,
val comments: String?,
val inseeCode: String?,
) {
fun toDashboardEntity(): DashboardEntity {
Expand All @@ -24,6 +26,8 @@ class DashboardDataInput(
amps = amps,
vigilanceAreas = vigilanceAreas,
inseeCode = inseeCode,
comments = comments,
controlUnits = controlUnits,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class DashboardDataOutput(
val regulatoryAreas: List<Int>,
val amps: List<Int>,
val vigilanceAreas: List<Int>,
val controlUnits: List<Int>,
val inseeCode: String?,
val comments: String?,
) {
companion object {
fun fromDashboardEntity(dashboardEntity: DashboardEntity): DashboardDataOutput {
Expand All @@ -24,7 +26,9 @@ class DashboardDataOutput(
amps = dashboardEntity.amps,
regulatoryAreas = dashboardEntity.regulatoryAreas,
vigilanceAreas = dashboardEntity.vigilanceAreas,
controlUnits = dashboardEntity.controlUnits,
inseeCode = dashboardEntity.inseeCode,
comments = dashboardEntity.comments,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import jakarta.persistence.Table
import java.util.UUID

@Entity
@Table(name = "briefing")
data class BriefingModel(
@Table(name = "dashboard_datas")
data class DashboardDatasModel(
@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.UUID)
Expand All @@ -35,6 +35,9 @@ data class BriefingModel(
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "regulations_cacem_id")
val regulatoryAreaModel: RegulatoryAreaModel?,
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "control_unit_id")
val controlUnitModel: ControlUnitModel?,
@Column(name = "insee_code")
val inseeCode: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,49 @@ data class DashboardModel(
val id: UUID?,
val name: String,
val geom: Geometry,
val comments: String?,
@OneToMany(
mappedBy = "dashboard",
fetch = FetchType.LAZY,
cascade = [CascadeType.ALL],
)
val briefings: MutableList<BriefingModel>,
val dashboardDatas: MutableList<DashboardDatasModel>,
) {
fun toDashboardEntity(): DashboardEntity {
val amps: MutableList<Int> = mutableListOf()
val regulatoryAreas: MutableList<Int> = mutableListOf()
val vigilanceAreas: MutableList<Int> = mutableListOf()
val reportings: MutableList<Int> = mutableListOf()
val controlUnits: MutableList<Int> = mutableListOf()
var inseeCode: String? = null
briefings.forEach { briefing ->
briefing.amp.let {
dashboardDatas.forEach { datas ->
datas.amp.let {
if (it?.id != null) {
amps.add(it.id)
}
}
briefing.regulatoryAreaModel.let {
datas.regulatoryAreaModel.let {
if (it?.id != null) {
regulatoryAreas.add(it.id)
}
}
briefing.vigilanceAreaModel.let {
datas.vigilanceAreaModel.let {
if (it?.id != null) {
vigilanceAreas.add(it.id)
}
}
briefing.reportingModel.let {
datas.reportingModel.let {
if (it?.id != null) {
reportings.add(it.id)
}
}
if (briefing.inseeCode != null) {
inseeCode = briefing.inseeCode
datas.controlUnitModel.let {
if (it?.id != null) {
controlUnits.add(it.id)
}
}
if (datas.inseeCode != null) {
inseeCode = datas.inseeCode
}
}
return DashboardEntity(
Expand All @@ -69,27 +76,30 @@ data class DashboardModel(
regulatoryAreas = regulatoryAreas,
vigilanceAreas = vigilanceAreas,
reportings = reportings,
controlUnits = controlUnits,
comments = comments,
)
}

fun addBriefing(briefing: BriefingModel) {
briefing.dashboard = this
briefings.add(briefing)
fun addBriefing(dashboardDatasModel: DashboardDatasModel) {
dashboardDatasModel.dashboard = this
this.dashboardDatas.add(dashboardDatasModel)
}

companion object {
fun fromDashboardEntity(
dashboardEntity: DashboardEntity,
briefings: List<BriefingModel>,
dashboardDatasModels: List<DashboardDatasModel>,
): DashboardModel {
val dashboardModel =
DashboardModel(
id = dashboardEntity.id,
name = dashboardEntity.name,
geom = dashboardEntity.geom,
briefings = mutableListOf(),
comments = dashboardEntity.comments,
dashboardDatas = mutableListOf(),
)
briefings.forEach {
dashboardDatasModels.forEach {
dashboardModel.addBriefing(it)
}
return dashboardModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package fr.gouv.cacem.monitorenv.infrastructure.database.repositories

import fr.gouv.cacem.monitorenv.domain.entities.dashboard.DashboardEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IDashboardRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.model.BriefingModel
import fr.gouv.cacem.monitorenv.infrastructure.database.model.DashboardDatasModel
import fr.gouv.cacem.monitorenv.infrastructure.database.model.DashboardModel.Companion.fromDashboardEntity
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBAMPRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBBriefingRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlUnitRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBDashboardRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBRegulatoryAreaRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBReportingRepository
Expand All @@ -18,6 +19,7 @@ class JpaDashboardRepository(
private val dashboardRepository: IDBDashboardRepository,
private val briefingRepository: IDBBriefingRepository,
private val ampRepository: IDBAMPRepository,
private val controlUnitRepository: IDBControlUnitRepository,
private val regulatoryAreaRepository: IDBRegulatoryAreaRepository,
private val reportingRepository: IDBReportingRepository,
private val vigilanceAreaRepository: IDBVigilanceAreaRepository,
Expand All @@ -26,106 +28,132 @@ class JpaDashboardRepository(
@Transactional
override fun save(dashboard: DashboardEntity): DashboardEntity {
dashboard.id?.let { briefingRepository.deleteAllByDashboardId(dashboardId = it) }
val briefingsToSave: MutableList<BriefingModel> = mutableListOf()
val briefingsToSave: MutableList<DashboardDatasModel> = mutableListOf()
addAmps(dashboard, briefingsToSave)
addInseeCode(dashboard, briefingsToSave)
addReportings(dashboard, briefingsToSave)
addVigilanceAreas(dashboard, briefingsToSave)
addRegulatoryAreas(dashboard, briefingsToSave)
addControlUnits(dashboard, briefingsToSave)
val dashboardModel = dashboardRepository.save(fromDashboardEntity(dashboard, briefingsToSave))
return dashboardModel.toDashboardEntity()
}

private fun addRegulatoryAreas(
dashboard: DashboardEntity,
briefingsToSave: MutableList<BriefingModel>,
briefingsToSave: MutableList<DashboardDatasModel>,
) {
dashboard.regulatoryAreas.forEach {
briefingsToSave.add(
BriefingModel(
DashboardDatasModel(
id = null,
dashboard = null,
amp = null,
inseeCode = null,
vigilanceAreaModel = null,
reportingModel = null,
regulatoryAreaModel = regulatoryAreaRepository.getReferenceById(it),
controlUnitModel = null,
),
)
}
}

private fun addVigilanceAreas(
dashboard: DashboardEntity,
briefingsToSave: MutableList<BriefingModel>,
briefingsToSave: MutableList<DashboardDatasModel>,
) {
dashboard.vigilanceAreas.forEach {
briefingsToSave.add(
BriefingModel(
DashboardDatasModel(
id = null,
dashboard = null,
amp = null,
inseeCode = null,
vigilanceAreaModel = vigilanceAreaRepository.getReferenceById(it),
reportingModel = null,
regulatoryAreaModel = null,
controlUnitModel = null,
),
)
}
}

private fun addReportings(
dashboard: DashboardEntity,
briefingsToSave: MutableList<BriefingModel>,
briefingsToSave: MutableList<DashboardDatasModel>,
) {
dashboard.reportings.forEach {
briefingsToSave.add(
BriefingModel(
DashboardDatasModel(
id = null,
dashboard = null,
amp = null,
inseeCode = null,
vigilanceAreaModel = null,
reportingModel = reportingRepository.getReferenceById(it),
regulatoryAreaModel = null,
controlUnitModel = null,
),
)
}
}

private fun addInseeCode(
dashboard: DashboardEntity,
briefingsToSave: MutableList<BriefingModel>,
briefingsToSave: MutableList<DashboardDatasModel>,
) {
dashboard.inseeCode?.let {
briefingsToSave.add(
BriefingModel(
DashboardDatasModel(
id = null,
dashboard = null,
amp = null,
inseeCode = it,
vigilanceAreaModel = null,
reportingModel = null,
regulatoryAreaModel = null,
controlUnitModel = null,
),
)
}
}

private fun addAmps(
dashboard: DashboardEntity,
briefingsToSave: MutableList<BriefingModel>,
briefingsToSave: MutableList<DashboardDatasModel>,
) {
dashboard.amps.forEach {
briefingsToSave.add(
BriefingModel(
DashboardDatasModel(
id = null,
dashboard = null,
amp = ampRepository.getReferenceById(it),
inseeCode = null,
vigilanceAreaModel = null,
reportingModel = null,
regulatoryAreaModel = null,
controlUnitModel = null,
),
)
}
}

private fun addControlUnits(
dashboard: DashboardEntity,
briefingsToSave: MutableList<DashboardDatasModel>,
) {
dashboard.controlUnits.forEach {
briefingsToSave.add(
DashboardDatasModel(
id = null,
dashboard = null,
amp = null,
inseeCode = null,
vigilanceAreaModel = null,
reportingModel = null,
regulatoryAreaModel = null,
controlUnitModel = controlUnitRepository.getReferenceById(it),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces

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

interface IDBBriefingRepository : JpaRepository<BriefingModel, UUID> {
interface IDBBriefingRepository : JpaRepository<DashboardDatasModel, UUID> {
fun deleteAllByDashboardId(dashboardId: UUID)
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
CREATE TABLE dashboard
(
id uuid PRIMARY KEY,
name VARCHAR(255) NOT NULL,
geom geometry(geometry, 4326) NOT NULL
id uuid PRIMARY KEY,
name VARCHAR(255) NOT NULL,
geom geometry(geometry, 4326) NOT NULL,
comments VARCHAR(255)
);

CREATE TABLE briefing
CREATE TABLE dashboard_datas
(
id uuid PRIMARY KEY,
dashboard_id uuid NOT NULL,
reportings_id integer,
amp_cacem_id integer,
regulations_cacem_id integer,
vigilance_area_id integer,
control_units_id integer,
control_unit_id integer,
--department code has 3 char max : https://www.insee.fr/fr/information/7766585
insee_code varchar(3),
CONSTRAINT fk_dashboard_id FOREIGN KEY (dashboard_id) REFERENCES dashboard (id),
CONSTRAINT fk_reportings FOREIGN KEY (reportings_id) REFERENCES reportings (id),
CONSTRAINT fk_regulatory_cacem FOREIGN KEY (regulations_cacem_id) REFERENCES regulations_cacem (id),
CONSTRAINT fk_vigilance_area FOREIGN KEY (vigilance_area_id) REFERENCES vigilance_areas (id),
CONSTRAINT fk_control_units FOREIGN KEY (control_units_id) REFERENCES control_units (id)
CONSTRAINT fk_control_units FOREIGN KEY (control_unit_id) REFERENCES control_units (id),
CONSTRAINT fk_amp_cacem FOREIGN KEY (amp_cacem_id) REFERENCES amp_cacem (id)
);


Loading

0 comments on commit 077d70c

Please sign in to comment.