Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Mar 19, 2024
1 parent 157c8c1 commit 51f3b82
Show file tree
Hide file tree
Showing 23 changed files with 506 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ data class LogbookMessage(
val externalReferenceNumber: String? = null,
val ircs: String? = null,
val vesselName: String? = null,
// ISO Alpha-3 country code
val flagState: String? = null,
val imo: String? = null,
val messageType: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ data class PriorNotification(
val types: List<PriorNotificationType>,
val vesselId: Int,
val vesselExternalReferenceNumber: String?,
// ISO Alpha-2 country code
val vesselFlagCountryCode: String?,
val vesselInternalReferenceNumber: String?,
val vesselIrcs: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ data class Vessel(
val ircs: String? = null,
val externalReferenceNumber: String? = null,
val vesselName: String? = null,
// ISO Alpha-2 country code
val flagState: CountryCode,
val width: Double? = null,
val length: Double? = null,
Expand All @@ -37,11 +38,12 @@ data class Vessel(
val underCharter: Boolean? = null,
) {
fun getNationalIdentifier(): String {
val internalReferenceNumberCountryCode = LIKELY_CONTROLLED_COUNTRY_CODES.find { countryAlpha3 ->
internalReferenceNumber?.contains(
countryAlpha3,
) ?: false
}
val internalReferenceNumberCountryCode =
LIKELY_CONTROLLED_COUNTRY_CODES.find { countryAlpha3 ->
internalReferenceNumber?.contains(
countryAlpha3,
) ?: false
}
val identifier = internalReferenceNumber?.replace("${internalReferenceNumberCountryCode}000", "") ?: ""

if (districtCode.isNullOrEmpty()) {
Expand All @@ -52,47 +54,48 @@ data class Vessel(
}
}

val LIKELY_CONTROLLED_COUNTRY_CODES = listOf(
CountryCode.FR.alpha3,
CountryCode.RU.alpha3,
CountryCode.KR.alpha3,
CountryCode.JP.alpha3,
CountryCode.LY.alpha3,
CountryCode.VE.alpha3,
CountryCode.SC.alpha3,
CountryCode.AU.alpha3,
CountryCode.CN.alpha3,
CountryCode.MG.alpha3,
CountryCode.BR.alpha3,
CountryCode.PL.alpha3,
CountryCode.VU.alpha3,
CountryCode.KM.alpha3,
CountryCode.MC.alpha3,
CountryCode.BW.alpha3,
CountryCode.AI.alpha3,
CountryCode.LK.alpha3,
CountryCode.TW.alpha3,
CountryCode.TT.alpha3,
CountryCode.FO.alpha3,
CountryCode.GY.alpha3,
CountryCode.PA.alpha3,
CountryCode.SX.alpha3,
CountryCode.TN.alpha3,
CountryCode.TR.alpha3,
CountryCode.AL.alpha3,
CountryCode.GD.alpha3,
CountryCode.DZ.alpha3,
CountryCode.SX.alpha3,
CountryCode.BE.alpha3,
CountryCode.DK.alpha3,
CountryCode.DE.alpha3,
CountryCode.EE.alpha3,
CountryCode.IE.alpha3,
CountryCode.ES.alpha3,
CountryCode.IT.alpha3,
CountryCode.NL.alpha3,
CountryCode.NO.alpha3,
CountryCode.PT.alpha3,
CountryCode.SE.alpha3,
CountryCode.GB.alpha3,
)
val LIKELY_CONTROLLED_COUNTRY_CODES =
listOf(
CountryCode.FR.alpha3,
CountryCode.RU.alpha3,
CountryCode.KR.alpha3,
CountryCode.JP.alpha3,
CountryCode.LY.alpha3,
CountryCode.VE.alpha3,
CountryCode.SC.alpha3,
CountryCode.AU.alpha3,
CountryCode.CN.alpha3,
CountryCode.MG.alpha3,
CountryCode.BR.alpha3,
CountryCode.PL.alpha3,
CountryCode.VU.alpha3,
CountryCode.KM.alpha3,
CountryCode.MC.alpha3,
CountryCode.BW.alpha3,
CountryCode.AI.alpha3,
CountryCode.LK.alpha3,
CountryCode.TW.alpha3,
CountryCode.TT.alpha3,
CountryCode.FO.alpha3,
CountryCode.GY.alpha3,
CountryCode.PA.alpha3,
CountryCode.SX.alpha3,
CountryCode.TN.alpha3,
CountryCode.TR.alpha3,
CountryCode.AL.alpha3,
CountryCode.GD.alpha3,
CountryCode.DZ.alpha3,
CountryCode.SX.alpha3,
CountryCode.BE.alpha3,
CountryCode.DK.alpha3,
CountryCode.DE.alpha3,
CountryCode.EE.alpha3,
CountryCode.IE.alpha3,
CountryCode.ES.alpha3,
CountryCode.IT.alpha3,
CountryCode.NL.alpha3,
CountryCode.NO.alpha3,
CountryCode.PT.alpha3,
CountryCode.SE.alpha3,
CountryCode.GB.alpha3,
)
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ data class LogbookReportEntity(
val externalReferenceNumber: String? = null,
@Column(name = "vessel_name")
val vesselName: String? = null,
// ISO Alpha-3 country code
@Column(name = "flag_state")
val flagState: String? = null,
@Column(name = "imo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ data class RiskFactorsEntity(
val segmentHighestPriority: String? = null,
@Column(name = "control_priority_level")
val controlPriorityLevel: Double,
// TODO Can't this field be null?
@Column(name = "last_control_datetime_utc")
val lastControlDatetime: ZonedDateTime,
@Column(name = "control_rate_risk_factor")
Expand All @@ -62,34 +63,36 @@ data class RiskFactorsEntity(
@Column(name = "number_vessel_seizures_last_5_years")
val numberVesselSeizuresLastFiveYears: Short,
) : Serializable {

fun toVesselRiskFactor(mapper: ObjectMapper) = VesselRiskFactor(
impactRiskFactor = impactRiskFactor,
probabilityRiskFactor = probabilityRiskFactor,
detectabilityRiskFactor = detectabilityRiskFactor,
riskFactor = riskFactor,
internalReferenceNumber = cfr,
gearOnboard = mapper.readValue(
gearOnboard,
mapper.typeFactory
.constructCollectionType(MutableList::class.java, Gear::class.java),
),
speciesOnboard = mapper.readValue(
speciesOnboard,
mapper.typeFactory
.constructCollectionType(MutableList::class.java, Species::class.java),
),
segments = segments,
controlPriorityLevel = controlPriorityLevel,
segmentHighestImpact = segmentHighestImpact,
segmentHighestPriority = segmentHighestPriority,
lastControlDatetime = lastControlDatetime,
controlRateRiskFactor = controlRateRiskFactor,
numberControlsLastFiveYears = numberControlsLastFiveYears,
numberControlsLastThreeYears = numberControlsLastThreeYears,
numberInfractionsLastFiveYears = numberInfractionsLastFiveYears,
numberGearSeizuresLastFiveYears = numberGearSeizuresLastFiveYears,
numberSpeciesSeizuresLastFiveYears = numberSpeciesSeizuresLastFiveYears,
numberVesselSeizuresLastFiveYears = numberVesselSeizuresLastFiveYears,
)
fun toVesselRiskFactor(mapper: ObjectMapper) =
VesselRiskFactor(
impactRiskFactor = impactRiskFactor,
probabilityRiskFactor = probabilityRiskFactor,
detectabilityRiskFactor = detectabilityRiskFactor,
riskFactor = riskFactor,
internalReferenceNumber = cfr,
gearOnboard =
mapper.readValue(
gearOnboard,
mapper.typeFactory
.constructCollectionType(MutableList::class.java, Gear::class.java),
),
speciesOnboard =
mapper.readValue(
speciesOnboard,
mapper.typeFactory
.constructCollectionType(MutableList::class.java, Species::class.java),
),
segments = segments,
controlPriorityLevel = controlPriorityLevel,
segmentHighestImpact = segmentHighestImpact,
segmentHighestPriority = segmentHighestPriority,
lastControlDatetime = lastControlDatetime,
controlRateRiskFactor = controlRateRiskFactor,
numberControlsLastFiveYears = numberControlsLastFiveYears,
numberControlsLastThreeYears = numberControlsLastThreeYears,
numberInfractionsLastFiveYears = numberInfractionsLastFiveYears,
numberGearSeizuresLastFiveYears = numberGearSeizuresLastFiveYears,
numberSpeciesSeizuresLastFiveYears = numberSpeciesSeizuresLastFiveYears,
numberVesselSeizuresLastFiveYears = numberVesselSeizuresLastFiveYears,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ data class VesselEntity(
val externalReferenceNumber: String? = null,
@Column(name = "vessel_name")
val vesselName: String? = null,
// ISO Alpha-2 country code
@Column(name = "flag_state")
val flagState: String? = null,
@Column(name = "width")
Expand Down Expand Up @@ -62,7 +63,6 @@ data class VesselEntity(
val declaredFishingGears: List<String>? = null,
@Column(name = "nav_licence_expiration_date", columnDefinition = "date")
val navigationLicenceExpirationDate: Date? = null,

@Column(name = "operator_name")
val operatorName: String? = null,
@Column(name = "operator_phones", columnDefinition = "varchar(100)[]")
Expand All @@ -87,44 +87,45 @@ data class VesselEntity(
@Column(name = "under_charter")
val underCharter: Boolean? = null,
) {

fun toVessel() = Vessel(
id = id,
internalReferenceNumber = internalReferenceNumber,
ircs = ircs,
mmsi = mmsi,
externalReferenceNumber = externalReferenceNumber,
vesselName = vesselName,
flagState = flagState?.let {
try {
CountryCode.valueOf(flagState)
} catch (e: IllegalArgumentException) {
logger.warn(e.message)
CountryCode.UNDEFINED
}
} ?: CountryCode.UNDEFINED,
width = width,
length = length,
district = district,
districtCode = districtCode,
gauge = gauge,
registryPort = registryPort,
power = power,
vesselType = vesselType,
sailingCategory = sailingCategory,
sailingType = sailingType,
declaredFishingGears = declaredFishingGears,
navigationLicenceExpirationDate = navigationLicenceExpirationDate,
operatorName = operatorName,
operatorPhones = operatorPhones,
operatorEmail = operatorEmail,
proprietorName = proprietorName,
proprietorPhones = proprietorPhones,
proprietorEmails = proprietorEmails,
vesselPhones = vesselPhones,
vesselEmails = vesselEmails,
underCharter = underCharter,
)
fun toVessel() =
Vessel(
id = id,
internalReferenceNumber = internalReferenceNumber,
ircs = ircs,
mmsi = mmsi,
externalReferenceNumber = externalReferenceNumber,
vesselName = vesselName,
flagState =
flagState?.let {
try {
CountryCode.valueOf(flagState)
} catch (e: IllegalArgumentException) {
logger.warn(e.message)
CountryCode.UNDEFINED
}
} ?: CountryCode.UNDEFINED,
width = width,
length = length,
district = district,
districtCode = districtCode,
gauge = gauge,
registryPort = registryPort,
power = power,
vesselType = vesselType,
sailingCategory = sailingCategory,
sailingType = sailingType,
declaredFishingGears = declaredFishingGears,
navigationLicenceExpirationDate = navigationLicenceExpirationDate,
operatorName = operatorName,
operatorPhones = operatorPhones,
operatorEmail = operatorEmail,
proprietorName = proprietorName,
proprietorPhones = proprietorPhones,
proprietorEmails = proprietorEmails,
vesselPhones = vesselPhones,
vesselEmails = vesselEmails,
underCharter = underCharter,
)

companion object {
private val logger: Logger = LoggerFactory.getLogger(VesselEntity::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class JpaLogbookReportRepository(
return Specification { root, _, criteriaBuilder ->
val vessel: Join<LogbookReportEntity, RiskFactorsEntity> = root.join("vessel")

// TODO Double-check if it's < 12 VS >= 12.
if (isLessThanTwelveMetersVessel) {
criteriaBuilder.lessThan(vessel.get("length"), 12)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,6 @@
insert into risk_factors (cfr,
impact_risk_factor,
probability_risk_factor,
detectability_risk_factor,
risk_factor,
last_logbook_message_datetime_utc,
departure_datetime_utc,
trip_number,
gear_onboard,
species_onboard,
segments,
total_weight_onboard,
control_priority_level,
segment_highest_impact,
segment_highest_priority,
vessel_id,
ircs,
external_immatriculation,
last_control_datetime_utc,
last_control_infraction,
post_control_comments,
number_recent_controls,
control_rate_risk_factor,
infraction_score,
infraction_rate_risk_factor,
number_controls_last_5_years,
number_controls_last_3_years,
number_infractions_last_5_years,
number_gear_seizures_last_5_years,
number_species_seizures_last_5_years,
number_vessel_seizures_last_5_years)
values ('FAK000999999',
2.1,
2,
3,
2.473,
CURRENT_DATE,
CURRENT_DATE,
123456,
'[{"gear": "OTB", "mesh": 70.0, "dimensions": 45.0}]',
'[{ "gear": "OTB","faoZone": "27.8.b","species": "BLI","weight": 13.46 },{ "gear": "OTB","faoZone": "27.8.c","species": "HKE","weight": 235.6 },{ "gear": "OTB","faoZone": "27.8.b","species": "HKE","weight": 235.6 }]',
'{"NWW10", "PEL 03"}',
1235.36,
2.56,
'NWW10',
'PEL 03',
1,
'CALLME',
'',
CURRENT_DATE,
true,
'',
0,
3.8,
NULL,
2.56,
8,
1,
5,
4,
3,
2);
-- /!\ This file is automatically generated by a local script.
-- Do NOT update it directly, update the associated .jsonc file.

INSERT INTO risk_factors (cfr, control_priority_level, control_rate_risk_factor, departure_datetime_utc, detectability_risk_factor, external_immatriculation, impact_risk_factor, infraction_rate_risk_factor, infraction_score, ircs, last_control_datetime_utc, last_control_infraction, last_logbook_message_datetime_utc, number_controls_last_3_years, number_controls_last_5_years, number_gear_seizures_last_5_years, number_infractions_last_5_years, number_recent_controls, number_species_seizures_last_5_years, number_vessel_seizures_last_5_years, post_control_comments, probability_risk_factor, risk_factor, segment_highest_impact, segment_highest_priority, segments, total_weight_onboard, trip_number, vessel_id, gear_onboard, species_onboard) VALUES ('FAK000999999', 2.56, 3.8, CURRENT_DATE, 3, '', 2.1, 2.56, NULL, 'CALLME', CURRENT_DATE, true, CURRENT_DATE, 1, 8, 4, 5, 0, 3, 2, '', 2, 2.473, 'NWW10', 'PEL 03', '{"NWW10", "PEL 03"}', 1235.36, 123456, 1, '[{"gear":"OTB","mesh":70,"dimensions":45}]', '[{"gear":"OTB","faoZone":"27.8.b","species":"BLI","weight":13.46},{"gear":"OTB","faoZone":"27.8.c","species":"HKE","weight":235.6},{"gear":"OTB","faoZone":"27.8.b","species":"HKE","weight":235.6}]');

INSERT INTO risk_factors (cfr, control_priority_level, control_rate_risk_factor, departure_datetime_utc, detectability_risk_factor, external_immatriculation, impact_risk_factor, infraction_rate_risk_factor, infraction_score, ircs, last_control_datetime_utc, last_control_infraction, last_logbook_message_datetime_utc, number_controls_last_3_years, number_controls_last_5_years, number_gear_seizures_last_5_years, number_infractions_last_5_years, number_recent_controls, number_species_seizures_last_5_years, number_vessel_seizures_last_5_years, post_control_comments, probability_risk_factor, risk_factor, segment_highest_impact, segment_highest_priority, segments, total_weight_onboard, trip_number, vessel_id, gear_onboard, species_onboard) VALUES ('CFR101', 4, 5, NOW() - INTERVAL '1 day', 5, '', 4, 3, NULL, 'IRCS101', '2023-01-01T00:00:00', true, NOW(), 0, 0, 4, 5, 0, 3, 2, '', 4, 4, 'NWW10', 'PEL 03', '{"NWW10", "PEL 03"}', 12345.67, 123102, 101, '[{"gear":"OTB","mesh":70,"dimensions":45}]', '[{"gear":"OTB","faoZone":"27.8.b","species":"BLI","weight":13.46},{"gear":"OTB","faoZone":"27.8.c","species":"HKE","weight":235.6},{"gear":"OTB","faoZone":"27.8.b","species":"HKE","weight":235.6}]');
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- /!\ This file is automatically generated by a local script.
-- Do NOT update it directly, update the associated .jsonc file.

INSERT INTO vessels (id, cfr, mmsi, ircs, external_immatriculation, vessel_name, flag_state, length, under_charter) VALUES (101, 'CFR101', 'MMSI101', 'IRCS101', 'EXTIMM101', 'VIVA ESPANA', 'ES', 15, false);
Loading

0 comments on commit 51f3b82

Please sign in to comment.