-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataProcessingContextService.java
More file actions
175 lines (155 loc) · 7.97 KB
/
Copy pathDataProcessingContextService.java
File metadata and controls
175 lines (155 loc) · 7.97 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package fr.insee.genesis.domain.service.context;
import fr.insee.genesis.controller.dto.ScheduleDto;
import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionSchedule;
import fr.insee.genesis.domain.model.context.schedule.ServiceToCall;
import fr.insee.genesis.domain.model.context.schedule.TrustParameters;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.domain.ports.api.DataProcessingContextApiPort;
import fr.insee.genesis.domain.ports.spi.DataProcessingContextPersistancePort;
import fr.insee.genesis.domain.ports.spi.SurveyUnitPersistencePort;
import fr.insee.genesis.exceptions.GenesisException;
import fr.insee.genesis.infrastructure.mappers.DataProcessingContextMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Service
@Slf4j
public class DataProcessingContextService implements DataProcessingContextApiPort {
public static final String NOT_FOUND_MESSAGE = "Context not found";
private final DataProcessingContextPersistancePort dataProcessingContextPersistancePort;
private final SurveyUnitPersistencePort surveyUnitPersistencePort;
@Autowired
public DataProcessingContextService(DataProcessingContextPersistancePort dataProcessingContextPersistancePort,
SurveyUnitPersistencePort surveyUnitPersistencePort) {
this.dataProcessingContextPersistancePort = dataProcessingContextPersistancePort;
this.surveyUnitPersistencePort = surveyUnitPersistencePort;
}
@Override
public void saveContext(String partitionId, Boolean withReview) throws GenesisException {
DataProcessingContextModel dataProcessingContextModel =
DataProcessingContextMapper.INSTANCE.documentToModel(dataProcessingContextPersistancePort.findByPartitionId(partitionId));
if(dataProcessingContextModel == null){
//Create if not exist
dataProcessingContextModel = DataProcessingContextModel.builder()
.partitionId(partitionId)
.kraftwerkExecutionScheduleList(new ArrayList<>())
.build();
}
dataProcessingContextModel.setWithReview(withReview);
dataProcessingContextPersistancePort.save(DataProcessingContextMapper.INSTANCE.modelToDocument(dataProcessingContextModel));
}
@Override
public void saveKraftwerkExecutionSchedule(String partitionId,
ServiceToCall serviceToCall,
String frequency,
LocalDateTime startDate,
LocalDateTime endDate,
TrustParameters trustParameters) throws GenesisException {
DataProcessingContextModel dataProcessingContextModel =
DataProcessingContextMapper.INSTANCE.documentToModel(dataProcessingContextPersistancePort.findByPartitionId(partitionId));
if(dataProcessingContextModel == null){
//Create if not exist
dataProcessingContextModel = DataProcessingContextModel.builder()
.partitionId(partitionId)
.withReview(false)
.kraftwerkExecutionScheduleList(new ArrayList<>())
.build();
}
dataProcessingContextModel.getKraftwerkExecutionScheduleList().add(
new KraftwerkExecutionSchedule(frequency,
serviceToCall,
startDate,
endDate,
trustParameters
)
);
dataProcessingContextPersistancePort.save(DataProcessingContextMapper.INSTANCE.modelToDocument(dataProcessingContextModel));
}
@Override
public void updateLastExecutionDate(String partitionId, LocalDateTime newDate) throws GenesisException {
DataProcessingContextModel dataProcessingContextModel =
DataProcessingContextMapper.INSTANCE.documentToModel(
dataProcessingContextPersistancePort.findByPartitionId(partitionId)
);
if (dataProcessingContextModel == null) {
throw new GenesisException(404, NOT_FOUND_MESSAGE);
}
dataProcessingContextModel.setLastExecution(newDate);
dataProcessingContextPersistancePort.save(DataProcessingContextMapper.INSTANCE.modelToDocument(dataProcessingContextModel));
}
@Override
public void deleteSchedules(String partitionId) throws GenesisException {
DataProcessingContextModel dataProcessingContextModel =
DataProcessingContextMapper.INSTANCE.documentToModel(
dataProcessingContextPersistancePort.findByPartitionId(partitionId)
);
if (dataProcessingContextModel == null) {
throw new GenesisException(404, NOT_FOUND_MESSAGE);
}
dataProcessingContextModel.setKraftwerkExecutionScheduleList(new ArrayList<>());
dataProcessingContextPersistancePort.save(DataProcessingContextMapper.INSTANCE.modelToDocument(dataProcessingContextModel));
}
@Override
public List<ScheduleDto> getAllSchedules() {
List<ScheduleDto> scheduleDtos = new ArrayList<>();
List<DataProcessingContextModel> dataProcessingContextModels =
DataProcessingContextMapper.INSTANCE.listDocumentToListModel(dataProcessingContextPersistancePort.findAll());
dataProcessingContextModels.forEach(
model -> scheduleDtos.add(model.toScheduleDto())
);
return scheduleDtos;
}
@Override
public List<KraftwerkExecutionSchedule> deleteExpiredSchedules(String partitionId) throws GenesisException {
DataProcessingContextModel dataProcessingContextModel =
DataProcessingContextMapper.INSTANCE.documentToModel(
dataProcessingContextPersistancePort.findByPartitionId(partitionId)
);
if (dataProcessingContextModel == null) {
throw new GenesisException(404, NOT_FOUND_MESSAGE);
}
return new ArrayList<>(
dataProcessingContextPersistancePort.removeExpiredSchedules(dataProcessingContextModel)
);
}
@Override
public long countSchedules() {
return dataProcessingContextPersistancePort.count();
}
@Override
public DataProcessingContextModel getContext(String interrogationId) throws GenesisException {
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findByInterrogationId(interrogationId);
if(surveyUnitModels.isEmpty()){
throw new GenesisException(404,"No interrogation in database with id %s".formatted(interrogationId));
}
Set<String> partitionIds = new HashSet<>();
surveyUnitModels.forEach(
surveyUnitModel -> partitionIds.add(surveyUnitModel.getCampaignId())
);
if(partitionIds.isEmpty()){
return null;
}
if(partitionIds.size() > 1){
throw new GenesisException(500,"Multiple partitions for interrogation %s %n%s".formatted(
interrogationId,
Arrays.toString(partitionIds.toArray())
));
}
return DataProcessingContextMapper.INSTANCE.documentToModel(
dataProcessingContextPersistancePort.findByPartitionId(partitionIds.stream().toList().getFirst())
);
}
@Override
public DataProcessingContextModel getContextByPartitionId(String partitionId){
return DataProcessingContextMapper.INSTANCE.documentToModel(
dataProcessingContextPersistancePort.findByPartitionId(partitionId)
);
}
}