-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessMissionActionControl.kt
98 lines (89 loc) · 4.34 KB
/
ProcessMissionActionControl.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
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.entities.mission.nav.control.v2.ActionControlEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.v2.MissionActionEntity
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 java.util.*
@UseCase
class ProcessMissionActionControl(
private val controlSecurityRepo: IControlSecurityRepository,
private val controlNavigationRepo: IControlNavigationRepository,
private val controlGensDeMerRepo: IControlGensDeMerRepository,
private val controlAdministrativeRepo: IControlAdministrativeRepository
) {
fun execute(action: MissionActionEntity): ActionControlEntity {
val controlSecurity = processControlSecurity(action.getActionId(), control = action.controlSecurity)
val controlGensDeMer = processControlGensDeMer(action.getActionId(), control = action.controlGensDeMer)
val controlNavigation = processControlNavigation(action.getActionId(), control = action.controlNavigation)
val controlAdministrative =
processControlAdministrative(action.getActionId(), control = action.controlAdministrative)
return ActionControlEntity(
controlSecurity = controlSecurity,
controlGensDeMer = controlGensDeMer,
controlNavigation = controlNavigation,
controlAdministrative = controlAdministrative
)
}
private inline fun <T : BaseControlEntity> processControl(
actionId: String,
control: T?,
findByActionId: (String) -> T?,
saveControl: (T) -> T
): UUID? {
val existingControl = findByActionId(actionId)
if (control != null && !control.equals(existingControl)) {
return saveControl(control).id
}
return existingControl?.id
}
private fun processControlSecurity(actionId: String, control: ControlSecurityEntity?): ControlSecurityEntity? {
val id = processControl(
actionId = actionId,
control = control,
saveControl = { controlSecurityRepo.save(it).toControlSecurityEntity() },
findByActionId = { controlSecurityRepo.findByActionControlId(it)?.toControlSecurityEntity() },
)
if (id != null && control != null) control.id = id
return control
}
private fun processControlGensDeMer(actionId: String, control: ControlGensDeMerEntity?): ControlGensDeMerEntity? {
val id = processControl(
actionId = actionId,
control = control,
saveControl = { controlGensDeMerRepo.save(it).toControlGensDeMerEntity() },
findByActionId = { controlGensDeMerRepo.findByActionControlId(it)?.toControlGensDeMerEntity() },
)
if (id != null && control != null) control.id = id
return control
}
private fun processControlNavigation(
actionId: String,
control: ControlNavigationEntity?
): ControlNavigationEntity? {
val id = processControl(
actionId = actionId,
control = control,
saveControl = { controlNavigationRepo.save(it).toControlNavigationEntity() },
findByActionId = { controlNavigationRepo.findByActionControlId(it)?.toControlNavigationEntity() },
)
if (id != null && control != null) control.id = id
return control
}
private fun processControlAdministrative(
actionId: String,
control: ControlAdministrativeEntity?
): ControlAdministrativeEntity? {
val id = processControl(
actionId = actionId,
control = control,
saveControl = { controlAdministrativeRepo.save(it).toControlAdministrativeEntity() },
findByActionId = { controlAdministrativeRepo.findByActionControlId(it).toControlAdministrativeEntity() },
)
if (id != null && control != null) control.id = id
return control
}
}