-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataProcessingContextService.java
More file actions
345 lines (312 loc) · 16.3 KB
/
DataProcessingContextService.java
File metadata and controls
345 lines (312 loc) · 16.3 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package fr.insee.genesis.domain.service.context;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import fr.insee.genesis.Constants;
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.document.context.DataProcessingContextDocument;
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.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
import java.util.ArrayList;
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;
}
/**
* @Deprecated Use collectionInstrumentId instead
*/
@Override
@Deprecated(forRemoval = true)
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 saveContextByCollectionInstrumentId(String collectionInstrumentId, Boolean withReview) throws GenesisException {
DataProcessingContextModel dataProcessingContextModel = dataProcessingContextPersistancePort.findByCollectionInstrumentId(collectionInstrumentId);
if(dataProcessingContextModel == null){
//Create if not exist
dataProcessingContextModel = DataProcessingContextModel.builder()
.collectionInstrumentId(collectionInstrumentId)
.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));
}
/**
* @Deprecated Will be replaced by Bangles V1
*/
@Override
@Deprecated
public void saveKraftwerkExecutionScheduleByCollectionInstrumentId(String collectionInstrumentId, ServiceToCall serviceToCall, String frequency, LocalDateTime startDate, LocalDateTime endDate, TrustParameters trustParameters) throws GenesisException {
DataProcessingContextModel dataProcessingContextModel = dataProcessingContextPersistancePort.findByCollectionInstrumentId(collectionInstrumentId);
if(dataProcessingContextModel == null){
//Create if not exist
dataProcessingContextModel = DataProcessingContextModel.builder()
.collectionInstrumentId(collectionInstrumentId)
.withReview(false)
.kraftwerkExecutionScheduleList(new ArrayList<>())
.build();
}
dataProcessingContextModel.getKraftwerkExecutionScheduleList().add(
new KraftwerkExecutionSchedule(frequency,
serviceToCall,
startDate,
endDate,
trustParameters
)
);
dataProcessingContextPersistancePort.save(DataProcessingContextMapper.INSTANCE.modelToDocument(dataProcessingContextModel));
}
@Deprecated(forRemoval = true)
@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));
}
/**
* @Deprecated Will be replaced by Bangles V1
*/
@Override
@Deprecated
public void updateLastExecutionDateByCollectionInstrumentId(String collectionInstrumentId, LocalDateTime newDate) throws GenesisException {
DataProcessingContextModel dataProcessingContextModel = dataProcessingContextPersistancePort.findByCollectionInstrumentId(collectionInstrumentId);
if (dataProcessingContextModel == null) {
throw new GenesisException(404, NOT_FOUND_MESSAGE);
}
dataProcessingContextModel.setLastExecution(newDate);
dataProcessingContextPersistancePort.save(DataProcessingContextMapper.INSTANCE.modelToDocument(dataProcessingContextModel));
}
/**
* @Deprecated Will be replaced by Bangles V1 + partitionId is obsolete
*/
@Override
@Deprecated(forRemoval = true)
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));
}
/**
* @Deprecated Will be replaced by Bangles V1
*/
@Override
@Deprecated
public void deleteSchedulesByCollectionInstrumentId(String collectionInstrumentId) throws GenesisException {
DataProcessingContextModel dataProcessingContextModel =
dataProcessingContextPersistancePort.findByCollectionInstrumentId(collectionInstrumentId);
if (dataProcessingContextModel == null) {
throw new GenesisException(404, NOT_FOUND_MESSAGE);
}
dataProcessingContextModel.setKraftwerkExecutionScheduleList(new ArrayList<>());
dataProcessingContextPersistancePort.save(DataProcessingContextMapper.INSTANCE.modelToDocument(dataProcessingContextModel));
}
/**
* @Deprecated Will be replaced by Bangles V1
*/
@Override
@Deprecated
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;
}
/**
* @Deprecated Will be replaced by Bangles V1
*/
@Override
@Deprecated
public void deleteExpiredSchedules(String logFolder) throws GenesisException {
List<DataProcessingContextModel> dataProcessingContextModels =
DataProcessingContextMapper.INSTANCE.listDocumentToListModel(dataProcessingContextPersistancePort.findAll());
for(DataProcessingContextModel context : dataProcessingContextModels){
try {
List<KraftwerkExecutionSchedule> deletedKraftwerkExecutionSchedules = dataProcessingContextPersistancePort.removeExpiredSchedules(context);
//Save in JSON log
if(!deletedKraftwerkExecutionSchedules.isEmpty()) {
String scheduleName = context.getCollectionInstrumentId()==null ?
context.getPartitionId() : context.getCollectionInstrumentId();
Path jsonLogPath = Path.of(logFolder, Constants.SCHEDULE_ARCHIVE_FOLDER_NAME,
scheduleName + ".json");
ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
objectMapper.registerModule(new JavaTimeModule());
String jsonToWrite = objectMapper.writeValueAsString(deletedKraftwerkExecutionSchedules);
if(Files.exists(jsonLogPath)){
//Remove last ] and append survey
StringBuilder content = new StringBuilder(Files.readString(jsonLogPath));
content.setCharAt(content.length()-1, ',');
content.append(jsonToWrite, 1, jsonToWrite.length()-1);
content.append(']');
Files.write(jsonLogPath, content.toString().getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
}else {
Files.createDirectories(jsonLogPath.getParent());
Files.write(jsonLogPath, jsonToWrite.getBytes());
}
}
} catch (IOException e) {
String name = context.getCollectionInstrumentId()!=null?context.getCollectionInstrumentId() :context.getPartitionId();
throw new GenesisException(500,String.format("An error occured trying to delete expired schedules for %s",name));
}
}
}
@Override
public long countContexts() {
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> collectionInstrumentIds = new HashSet<>();
for (SurveyUnitModel su : surveyUnitModels){
if (su.getCollectionInstrumentId() != null){
collectionInstrumentIds.add(su.getCollectionInstrumentId());
}
}
if(collectionInstrumentIds.size() > 1){
throw new GenesisException(500,"Multiple collection instruments for interrogation %s".formatted(interrogationId));
}
if(collectionInstrumentIds.isEmpty()){
throw new GenesisException(404,"No collection instrument found for interrogation %s".formatted(interrogationId));
}
return dataProcessingContextPersistancePort.findByCollectionInstrumentId(collectionInstrumentIds.stream().toList().getFirst());
}
@Override
public DataProcessingContextModel getContextByCollectionInstrumentId(String collectionInstrumentId){
return dataProcessingContextPersistancePort.findByCollectionInstrumentId(collectionInstrumentId);
}
/**
* @deprecated PartitionId is obsolete since modele filiere
*/
@Override
@Deprecated(forRemoval = true)
public List<String> getPartitionIds(boolean withReview){
List<String> partitionIds = new ArrayList<>();
for(DataProcessingContextModel dataProcessingContextModel
: DataProcessingContextMapper.INSTANCE.listDocumentToListModel(
dataProcessingContextPersistancePort.findAllByReview(withReview)
)){
partitionIds.add(dataProcessingContextModel.getPartitionId());
}
return partitionIds;
}
@Override
public List<String> getCollectionInstrumentIds(boolean withReview) {
List<String> collectionInstrumentIds = new ArrayList<>();
for(DataProcessingContextModel dataProcessingContextModel
: DataProcessingContextMapper.INSTANCE.listDocumentToListModel(
dataProcessingContextPersistancePort.findAllByReview(withReview)
)){
if(dataProcessingContextModel.getCollectionInstrumentId() != null) {
collectionInstrumentIds.add(dataProcessingContextModel.getCollectionInstrumentId());
}
}
return collectionInstrumentIds;
}
/**
* @deprecated PartitionId is obsolete since modele filiere
*/
@Deprecated(forRemoval = true)
@Override
public boolean getReviewByPartitionId(String partitionId) throws GenesisException {
DataProcessingContextDocument dataProcessingContextDocument =
dataProcessingContextPersistancePort.findByPartitionId(partitionId);
if(dataProcessingContextDocument == null){
throw new GenesisException(404, "Data processing context not found");
}
return DataProcessingContextMapper.INSTANCE.documentToModel(
dataProcessingContextPersistancePort.findByPartitionId(partitionId)
).isWithReview();
}
@Override
public boolean getReviewByCollectionInstrumentId(String collectionInstrumentId) throws GenesisException {
DataProcessingContextModel dataProcessingContextModel =
dataProcessingContextPersistancePort.findByCollectionInstrumentId(collectionInstrumentId);
if(dataProcessingContextModel == null){
throw new GenesisException(404, "Data processing context not found");
}
return dataProcessingContextModel.isWithReview();
}
}