-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessMissionActionControlEnvTarget.kt
156 lines (142 loc) · 6.17 KB
/
ProcessMissionActionControlEnvTarget.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package fr.gouv.dgampa.rapportnav.domain.use_cases.mission.control.v2
import fr.gouv.dgampa.rapportnav.config.UseCase
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.control.*
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.control.IControlAdministrativeRepository
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.control.IControlGensDeMerRepository
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.control.IControlNavigationRepository
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.control.IControlSecurityRepository
import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.adapters.v2.ActionControlInput
import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.adapters.v2.InfractionInput2
import java.util.*
@UseCase
class ProcessMissionActionControlEnvTarget(
private val controlSecurityRepo: IControlSecurityRepository,
private val controlNavigationRepo: IControlNavigationRepository,
private val controlGensDeMerRepo: IControlGensDeMerRepository,
private val controlAdministrativeRepo: IControlAdministrativeRepository
) {
fun execute(infraction: InfractionInput2, controls: ActionControlInput): String? {
val controlType = infraction.controlType?.let { ControlType.valueOf(it) }
val id = when (controlType) {
ControlType.SECURITY -> controls.controlSecurity?.id
?: processControlSecurity(
actionId = infraction.actionId!!,
missionId = infraction.missionId!!
).id
ControlType.GENS_DE_MER -> controls.controlGensDeMer?.id ?: processControlGensDeMer(
actionId = infraction.actionId!!,
missionId = infraction.missionId!!
).id
ControlType.NAVIGATION -> controls.controlNavigation?.id ?: processControlNavigation(
actionId = infraction.actionId!!,
missionId = infraction.missionId!!
).id
ControlType.ADMINISTRATIVE -> controls.controlAdministrative?.id ?: processControlAdministrative(
actionId = infraction.actionId!!,
missionId = infraction.missionId!!
).id
null -> null
}
return id?.toString()
}
private inline fun <T : BaseControlEntity> processControl(
missionId: Int,
actionId: String,
saveControl: (T) -> T,
findControlByActionId: (String) -> T?,
createControl: (UUID, String, Int) -> T,
): T {
val existingControl = findControlByActionId(actionId)
if (existingControl?.id != null) return existingControl
val control = createControl(UUID.randomUUID(), actionId, missionId)
return saveControl(control)
}
private fun processControlSecurity(actionId: String, missionId: Int): ControlSecurityEntity {
return processControl(
actionId = actionId,
missionId = missionId,
createControl = { id: UUID, s: String, i: Int ->
ControlSecurityEntity(
id = id,
missionId = i,
actionControlId = s,
amountOfControls = 1
)
},
saveControl = { controlSecurityRepo.save(it).toControlSecurityEntity() },
findControlByActionId = {
if (controlSecurityRepo.existsByActionControlId(it)) {
controlSecurityRepo.findByActionControlId(it).toControlSecurityEntity()
} else {
null
}
},
)
}
private fun processControlGensDeMer(actionId: String, missionId: Int): ControlGensDeMerEntity {
return processControl(
actionId = actionId,
missionId = missionId,
createControl = { id: UUID, s: String, i: Int ->
ControlGensDeMerEntity(
id = id,
missionId = i,
actionControlId = s,
amountOfControls = 1
)
},
saveControl = { controlGensDeMerRepo.save(it).toControlGensDeMerEntity() },
findControlByActionId = {
if (controlGensDeMerRepo.existsByActionControlId(it)) {
controlGensDeMerRepo.findByActionControlId(it).toControlGensDeMerEntity()
} else {
null
}
},
)
}
private fun processControlNavigation(actionId: String, missionId: Int): ControlNavigationEntity {
return processControl(
actionId = actionId,
missionId = missionId,
createControl = { id: UUID, s: String, i: Int ->
ControlNavigationEntity(
id = id,
missionId = i,
actionControlId = s,
amountOfControls = 1
)
},
saveControl = { controlNavigationRepo.save(it).toControlNavigationEntity() },
findControlByActionId = {
if (controlNavigationRepo.existsByActionControlId(it)) {
controlNavigationRepo.findByActionControlId(it).toControlNavigationEntity()
} else {
null
}
},
)
}
private fun processControlAdministrative(actionId: String, missionId: Int): ControlAdministrativeEntity {
return processControl(
actionId = actionId,
missionId = missionId,
createControl = { id: UUID, s: String, i: Int ->
ControlAdministrativeEntity(
id = id,
missionId = i,
actionControlId = s,
amountOfControls = 1
)
},
saveControl = { controlAdministrativeRepo.save(it).toControlAdministrativeEntity() },
findControlByActionId = {
if (controlAdministrativeRepo.existsByActionControlId(it)) {
controlAdministrativeRepo.findByActionControlId(it).toControlAdministrativeEntity()
} else {
null
}
},
)
}
}