Skip to content

Commit

Permalink
[Tech] Hétérogénéité de minuscules / majuscules sur le pavillon dans …
Browse files Browse the repository at this point in the history
…la table `mission_actions` (#3146)

## Linked issues

- Resolve #2805

----

- [ ] Tests E2E (Cypress)
  • Loading branch information
louptheron authored May 29, 2024
2 parents 1d18e58 + 79a9297 commit 48e9eca
Show file tree
Hide file tree
Showing 48 changed files with 388 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.gouv.cnsp.monitorfish.domain.entities.mission.mission_actions

import com.neovisionaries.i18n.CountryCode
import fr.gouv.cnsp.monitorfish.domain.entities.mission.ControlUnit
import java.time.ZonedDateTime

Expand All @@ -11,7 +12,7 @@ data class MissionAction(
val internalReferenceNumber: String? = null,
val externalReferenceNumber: String? = null,
val ircs: String? = null,
val flagState: String? = null,
val flagState: CountryCode,
val districtCode: String? = null,
val faoAreas: List<String> = listOf(),
val actionType: MissionActionType,
Expand All @@ -35,7 +36,7 @@ data class MissionAction(
val unitWithoutOmegaGauge: Boolean? = null,
val controlQualityComments: String? = null,
val feedbackSheetRequired: Boolean? = null,
val userTrigram: String? = null,
val userTrigram: String,
val segments: List<FleetSegment> = listOf(),
val facade: String? = null,
val longitude: Double? = null,
Expand Down Expand Up @@ -76,6 +77,10 @@ data class MissionAction(
"A control must specify a vessel: the `vesselId` must be given."
}

require(this.userTrigram.isNotEmpty()) {
"A control must specify a user trigram: the `userTrigram` must be given."
}

when (this.actionType) {
MissionActionType.AIR_CONTROL -> checkControlPosition()
MissionActionType.SEA_CONTROL -> checkControlPosition()
Expand All @@ -92,17 +97,11 @@ data class MissionAction(
require(this.latitude != null) {
"A control must specify a position: the `latitude` must be given."
}
require(this.userTrigram != null) {
"A control must specify a user trigram: the `userTrigram` must be given."
}
}

private fun checkControlPort() {
require(this.portLocode != null) {
"A land control must specify a port: the `portLocode` must be given."
}
require(this.userTrigram != null) {
"A control must specify a user trigram: the `userTrigram` must be given."
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.gouv.cnsp.monitorfish.domain.entities.mission.mission_actions.actrep

import com.neovisionaries.i18n.CountryCode

enum class JointDeploymentPlan(private val species: List<FaoZonesAndSpecy>) {
MEDITERRANEAN_AND_EASTERN_ATLANTIC(MEDITERRANEAN_AND_EASTERN_ATLANTIC_SPECIES),
NORTH_SEA(NORTH_SEA_SPECIES),
Expand All @@ -18,7 +20,7 @@ enum class JointDeploymentPlan(private val species: List<FaoZonesAndSpecy>) {
* See "DÉCISION D’EXÉCUTION (UE) 2023/2376 DE LA COMMISSION":
* https://extranet.legipeche.metier.developpement-durable.gouv.fr/fichier/pdf/oj_l_202302376_fr_txt_cle6b198e.pdf?arg=24774&cle=7d14626b709ff7e8c62586bcd8683e7e9fcaa348&file=pdf%2Foj_l_202302376_fr_txt_cle6b198e.pdf
*/
fun isLandControlApplicable(flagState: String?, speciesOnboardCodes: List<String>, tripFaoCodes: List<String>): Boolean {
fun isLandControlApplicable(flagState: CountryCode, speciesOnboardCodes: List<String>, tripFaoCodes: List<String>): Boolean {
val isThirdCountryVessel = EU_THIRD_COUNTRIES.contains(flagState)

val hasSpeciesInJdp = this.species.any { (jdpFaoZones, jdpSpecy) ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.gouv.cnsp.monitorfish.domain.entities.mission.mission_actions.actrep

import com.neovisionaries.i18n.CountryCode

typealias FaoZones = List<String>
typealias FaoZonesAndSpecy = Pair<FaoZones, String>

Expand Down Expand Up @@ -156,7 +158,7 @@ val WESTERN_WATERS_SPECIES: List<FaoZonesAndSpecy> = listOf(
),
)

val EU_THIRD_COUNTRIES = listOf("GB")
val EU_THIRD_COUNTRIES = listOf(CountryCode.GB)

/**
* See species detailed in p.26 ("CARTES DES ESPÈCES SOUMISES A QUOTAS")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.gouv.cnsp.monitorfish.infrastructure.api.input

import com.neovisionaries.i18n.CountryCode
import fr.gouv.cnsp.monitorfish.domain.entities.mission.mission_actions.*
import fr.gouv.cnsp.monitorfish.domain.entities.mission.mission_actions.Completion
import java.time.ZonedDateTime
Expand All @@ -11,7 +12,7 @@ data class AddMissionActionDataInput(
var internalReferenceNumber: String? = null,
var externalReferenceNumber: String? = null,
var ircs: String? = null,
var flagState: String? = null,
var flagState: CountryCode,
var districtCode: String? = null,
var faoAreas: List<String> = listOf(),
var flightGoals: List<FlightGoal> = listOf(),
Expand Down Expand Up @@ -43,7 +44,7 @@ data class AddMissionActionDataInput(
var seizureAndDiversionComments: String? = null,
var otherComments: String? = null,
var gearOnboard: List<GearControl> = listOf(),
var userTrigram: String? = null,
var userTrigram: String,
var speciesOnboard: List<SpeciesControl> = listOf(),
var vesselTargeted: ControlCheck? = null,
var hasSomeGearsSeized: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.gouv.cnsp.monitorfish.infrastructure.api.outputs

import com.neovisionaries.i18n.CountryCode
import fr.gouv.cnsp.monitorfish.domain.entities.mission.ControlUnit
import fr.gouv.cnsp.monitorfish.domain.entities.mission.mission_actions.*
import fr.gouv.cnsp.monitorfish.domain.entities.mission.mission_actions.Completion
Expand All @@ -12,7 +13,7 @@ data class MissionActionDataOutput(
val internalReferenceNumber: String? = null,
val externalReferenceNumber: String? = null,
val ircs: String? = null,
val flagState: String? = null,
val flagState: CountryCode,
val districtCode: String? = null,
val faoAreas: List<String> = listOf(),
val flightGoals: List<FlightGoal> = listOf(),
Expand Down Expand Up @@ -48,7 +49,7 @@ data class MissionActionDataOutput(
val gearOnboard: List<GearControl> = listOf(),
val speciesOnboard: List<SpeciesControl> = listOf(),
val controlUnits: List<ControlUnit> = listOf(),
val userTrigram: String? = null,
val userTrigram: String,
val vesselTargeted: ControlCheck? = null,
val hasSomeGearsSeized: Boolean,
val hasSomeSpeciesSeized: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.gouv.cnsp.monitorfish.infrastructure.api.outputs

import com.neovisionaries.i18n.CountryCode
import fr.gouv.cnsp.monitorfish.domain.entities.facade.Seafront
import fr.gouv.cnsp.monitorfish.domain.entities.logbook.LogbookOperationType
import fr.gouv.cnsp.monitorfish.domain.entities.prior_notification.PriorNotification
Expand Down Expand Up @@ -29,7 +30,7 @@ data class PriorNotificationDataOutput(
val types: List<PriorNotificationTypeDataOutput>,
val vesselId: Int?,
val vesselExternalReferenceNumber: String?,
val vesselFlagCountryCode: String?,
val vesselFlagCountryCode: CountryCode,
val vesselInternalReferenceNumber: String?,
val vesselIrcs: String?,
val vesselLastControlDate: String?,
Expand Down Expand Up @@ -85,7 +86,7 @@ data class PriorNotificationDataOutput(
types,
vesselId = priorNotification.vessel.id,
vesselExternalReferenceNumber = priorNotification.vessel.externalReferenceNumber,
vesselFlagCountryCode = priorNotification.vessel.flagState.toString(),
vesselFlagCountryCode = priorNotification.vessel.flagState,
vesselInternalReferenceNumber = priorNotification.vessel.internalReferenceNumber,
vesselIrcs = priorNotification.vessel.ircs,
vesselLastControlDate = priorNotification.vesselRiskFactor?.lastControlDatetime?.toString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package fr.gouv.cnsp.monitorfish.infrastructure.database.entities

import com.fasterxml.jackson.databind.ObjectMapper
import com.neovisionaries.i18n.CountryCode
import fr.gouv.cnsp.monitorfish.domain.entities.facade.Seafront
import fr.gouv.cnsp.monitorfish.domain.entities.mission.mission_actions.*
import fr.gouv.cnsp.monitorfish.infrastructure.database.entities.converters.CountryCodeConverter
import io.hypersistence.utils.hibernate.type.json.JsonBinaryType
import jakarta.persistence.*
import org.hibernate.annotations.JdbcType
Expand Down Expand Up @@ -31,7 +33,8 @@ class MissionActionEntity(
@Column(name = "ircs")
val ircs: String? = null,
@Column(name = "flag_state")
val flagState: String? = null,
@Convert(converter = CountryCodeConverter::class)
val flagState: CountryCode,
@Column(name = "district_code")
val districtCode: String? = null,
@Column(name = "flight_goals", columnDefinition = "varchar(100)[]")
Expand Down Expand Up @@ -93,7 +96,7 @@ class MissionActionEntity(
@Column(name = "is_from_poseidon")
val isFromPoseidon: Boolean,
@Column(name = "user_trigram")
val userTrigram: String? = null,
val userTrigram: String,
@Type(JsonBinaryType::class)
@Column(name = "segments", columnDefinition = "jsonb")
val segments: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.neovisionaries.i18n.CountryCode
import fr.gouv.cnsp.monitorfish.domain.entities.alerts.PendingAlert
import fr.gouv.cnsp.monitorfish.domain.entities.alerts.type.AlertType
import fr.gouv.cnsp.monitorfish.domain.entities.vessel.VesselIdentifier
import fr.gouv.cnsp.monitorfish.infrastructure.database.entities.converters.CountryCodeConverter
import io.hypersistence.utils.hibernate.type.json.JsonBinaryType
import jakarta.persistence.*
import org.hibernate.annotations.JdbcType
Expand Down Expand Up @@ -35,7 +36,7 @@ data class PendingAlertEntity(
@JdbcType(PostgreSQLEnumJdbcType::class)
val vesselIdentifier: VesselIdentifier,
@Column(name = "flag_state")
@Enumerated(EnumType.STRING)
@Convert(converter = CountryCodeConverter::class)
val flagState: CountryCode,
@Column(name = "creation_date", nullable = false)
val creationDate: ZonedDateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.gouv.cnsp.monitorfish.infrastructure.database.entities
import com.neovisionaries.i18n.CountryCode
import fr.gouv.cnsp.monitorfish.domain.entities.position.Position
import fr.gouv.cnsp.monitorfish.domain.entities.position.PositionType
import fr.gouv.cnsp.monitorfish.infrastructure.database.entities.converters.CountryCodeConverter
import jakarta.persistence.*
import java.time.ZonedDateTime

Expand All @@ -27,8 +28,8 @@ data class PositionEntity(
@Column(name = "vessel_name")
val vesselName: String? = null,
@Column(name = "flag_state")
@Enumerated(EnumType.STRING)
val flagState: CountryCode? = null,
@Convert(converter = CountryCodeConverter::class)
val flagState: CountryCode,
@Column(name = "from_country")
@Enumerated(EnumType.STRING)
val from: CountryCode? = null,
Expand Down Expand Up @@ -92,7 +93,7 @@ data class PositionEntity(
vesselName = position.vesselName,
speed = position.speed,
course = position.course,
flagState = position.flagState,
flagState = position.flagState ?: CountryCode.UNDEFINED,
destination = position.destination,
from = position.from,
tripNumber = position.tripNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fr.gouv.cnsp.monitorfish.domain.entities.reporting.Reporting
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.ReportingType
import fr.gouv.cnsp.monitorfish.domain.entities.vessel.VesselIdentifier
import fr.gouv.cnsp.monitorfish.domain.mappers.ReportingMapper
import fr.gouv.cnsp.monitorfish.infrastructure.database.entities.converters.CountryCodeConverter
import io.hypersistence.utils.hibernate.type.json.JsonBinaryType
import jakarta.persistence.*
import org.hibernate.annotations.JdbcType
Expand Down Expand Up @@ -41,7 +42,7 @@ data class ReportingEntity(
@Column(name = "vessel_identifier", columnDefinition = "vessel_identifier")
val vesselIdentifier: VesselIdentifier? = null,
@Column(name = "flag_state")
@Enumerated(EnumType.STRING)
@Convert(converter = CountryCodeConverter::class)
val flagState: CountryCode,
@Column(name = "creation_date", nullable = false)
val creationDate: ZonedDateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fr.gouv.cnsp.monitorfish.domain.entities.alerts.PendingAlert
import fr.gouv.cnsp.monitorfish.domain.entities.alerts.SilencedAlert
import fr.gouv.cnsp.monitorfish.domain.entities.alerts.type.AlertType
import fr.gouv.cnsp.monitorfish.domain.entities.vessel.VesselIdentifier
import fr.gouv.cnsp.monitorfish.infrastructure.database.entities.converters.CountryCodeConverter
import io.hypersistence.utils.hibernate.type.json.JsonBinaryType
import jakarta.persistence.*
import org.hibernate.annotations.JdbcType
Expand Down Expand Up @@ -36,7 +37,7 @@ data class SilencedAlertEntity(
@Column(name = "vessel_identifier", columnDefinition = "vessel_identifier")
val vesselIdentifier: VesselIdentifier,
@Column(name = "flag_state")
@Enumerated(EnumType.STRING)
@Convert(converter = CountryCodeConverter::class)
val flagState: CountryCode,
@Column(name = "silenced_before_date", nullable = false)
val silencedBeforeDate: ZonedDateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.gouv.cnsp.monitorfish.infrastructure.database.entities

import com.neovisionaries.i18n.CountryCode
import fr.gouv.cnsp.monitorfish.domain.entities.vessel.Vessel
import fr.gouv.cnsp.monitorfish.infrastructure.database.entities.converters.CountryCodeConverter
import jakarta.persistence.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -35,7 +36,8 @@ data class VesselEntity(
val vesselName: String? = null,
// ISO Alpha-2 country code
@Column(name = "flag_state")
val flagState: String? = null,
@Convert(converter = CountryCodeConverter::class)
val flagState: CountryCode,
@Column(name = "width")
val width: Double? = null,
@Column(name = "length")
Expand Down Expand Up @@ -87,14 +89,7 @@ data class VesselEntity(
mmsi = mmsi,
externalReferenceNumber = externalReferenceNumber,
vesselName = vesselName,
flagState = flagState?.let {
try {
CountryCode.valueOf(flagState)
} catch (e: IllegalArgumentException) {
logger.warn(e.message)
CountryCode.UNDEFINED
}
} ?: CountryCode.UNDEFINED,
flagState = flagState,
width = width,
length = length,
district = district,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.gouv.cnsp.monitorfish.infrastructure.database.entities.converters

import com.neovisionaries.i18n.CountryCode
import jakarta.persistence.AttributeConverter
import jakarta.persistence.Converter

@Converter(autoApply = true)
class CountryCodeConverter : AttributeConverter<CountryCode?, String?> {
override fun convertToDatabaseColumn(attribute: CountryCode?): String? {
return attribute?.name
}

override fun convertToEntityAttribute(dbData: String?): CountryCode? {
if (dbData == null) {
return CountryCode.UNDEFINED
}

return try {
CountryCode.valueOf(dbData)
} catch (e: Throwable) {
return CountryCode.UNDEFINED
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ALTER TABLE mission_actions
ALTER COLUMN user_trigram SET NOT NULL;

UPDATE mission_actions
SET flag_state = 'UNDEFINED'
WHERE
flag_state IS NULL
OR flag_state IN ('UNKNOWN', 'X');

UPDATE mission_actions
SET flag_state = upper(flag_state);

ALTER TABLE mission_actions
ALTER COLUMN flag_state SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- We now store `UNDEFINED` from `com.neovisionaries.i18n.CountryCode` library

ALTER TABLE pending_alerts
ALTER COLUMN flag_state SET DATA TYPE varchar(10);

ALTER TABLE silenced_alerts
ALTER COLUMN flag_state SET DATA TYPE varchar(10);

ALTER TABLE reportings
ALTER COLUMN flag_state SET DATA TYPE varchar(10);
Loading

0 comments on commit 48e9eca

Please sign in to comment.