-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSurveyUnitService.java
More file actions
1013 lines (878 loc) · 47.1 KB
/
Copy pathSurveyUnitService.java
File metadata and controls
1013 lines (878 loc) · 47.1 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package fr.insee.genesis.domain.service.surveyunit;
import fr.insee.bpm.exceptions.MetadataParserException;
import fr.insee.bpm.metadata.model.VariableType;
import fr.insee.bpm.metadata.model.VariablesMap;
import fr.insee.bpm.metadata.reader.ddi.DDIReader;
import fr.insee.bpm.metadata.reader.lunatic.LunaticReader;
import fr.insee.genesis.Constants;
import fr.insee.genesis.controller.adapter.LunaticXmlAdapter;
import fr.insee.genesis.controller.dto.*;
import fr.insee.genesis.controller.rest.responses.ApiError;
import fr.insee.genesis.controller.rest.responses.ResponseController;
import fr.insee.genesis.controller.services.MetadataService;
import fr.insee.genesis.controller.sources.xml.LunaticXmlCampaign;
import fr.insee.genesis.controller.sources.xml.LunaticXmlDataParser;
import fr.insee.genesis.controller.sources.xml.LunaticXmlDataSequentialParser;
import fr.insee.genesis.controller.sources.xml.LunaticXmlSurveyUnit;
import fr.insee.genesis.controller.utils.AuthUtils;
import fr.insee.genesis.controller.utils.ControllerUtils;
import fr.insee.genesis.controller.utils.DataTransformer;
import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
import fr.insee.genesis.domain.model.surveyunit.DataState;
import fr.insee.genesis.domain.model.surveyunit.InterrogationId;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.domain.model.surveyunit.VarIdScopeTuple;
import fr.insee.genesis.domain.model.surveyunit.VariableModel;
import fr.insee.genesis.domain.ports.api.DataProcessingContextApiPort;
import fr.insee.genesis.domain.ports.api.SurveyUnitApiPort;
import fr.insee.genesis.domain.ports.spi.SurveyUnitPersistencePort;
import fr.insee.genesis.domain.utils.GroupUtils;
import fr.insee.genesis.exceptions.GenesisError;
import fr.insee.genesis.exceptions.GenesisException;
import fr.insee.genesis.exceptions.NoDataException;
import fr.insee.genesis.infrastructure.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import java.io.*;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
@Slf4j
public class SurveyUnitService implements SurveyUnitApiPort {
public static final String PATH_FORMAT = "%s/%s";
@Qualifier("surveyUnitMongoAdapter")
private final SurveyUnitPersistencePort surveyUnitPersistencePort;
private final MetadataService metadataService;
private final FileUtils fileUtils;
private final DataProcessingContextApiPort contextService;
private final SurveyUnitQualityService surveyUnitQualityService;
private final ControllerUtils controllerUtils;
private final AuthUtils authUtils;
@Autowired
public SurveyUnitService(SurveyUnitPersistencePort surveyUnitPersistencePort,
MetadataService metadataService,
FileUtils fileUtils,
DataProcessingContextApiPort contextService,
SurveyUnitQualityService surveyUnitQualityService,
ControllerUtils controllerUtils,
AuthUtils authUtils) {
this.surveyUnitPersistencePort = surveyUnitPersistencePort;
this.metadataService = metadataService;
this.fileUtils = fileUtils;
this.contextService = contextService;
this.surveyUnitQualityService = surveyUnitQualityService;
this.controllerUtils = controllerUtils;
this.authUtils = authUtils;
}
@Override
public void saveSurveyUnits(List<SurveyUnitModel> surveyUnitModels) {
surveyUnitPersistencePort.saveAll(surveyUnitModels);
}
@Override
public List<SurveyUnitModel> findByIdsInterrogationAndQuestionnaire(String interrogationId, String questionnaireId) {
return surveyUnitPersistencePort.findByIds(interrogationId, questionnaireId);
}
@Override
public List<SurveyUnitModel> findByInterrogationId(String interrogationId) {
return surveyUnitPersistencePort.findByInterrogationId(interrogationId);
}
@Override
public Stream<SurveyUnitModel> findByQuestionnaireId(String questionnaireId) {
return surveyUnitPersistencePort.findByQuestionnaireId(questionnaireId);
}
/**
* In this method we want to get the latest update for each variable of a survey unit
* But we need to separate the updates by mode
* So we will calculate the latest state for a given collection mode
* @param interrogationId : Survey unit id
* @param questionnaireId : Questionnaire id
* @return the latest update for each variable of a survey unit
*/
@Override
public List<SurveyUnitModel> findLatestByIdAndByQuestionnaireId(String interrogationId, String questionnaireId) {
List<SurveyUnitModel> latestUpdatesbyVariables = new ArrayList<>();
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findByIds(interrogationId, questionnaireId);
List<Mode> modes = getDistinctsModes(surveyUnitModels);
modes.forEach(mode ->{
List<SurveyUnitModel> suByMode = surveyUnitModels.stream()
.filter(surveyUnitModel -> surveyUnitModel.getMode().equals(mode))
.sorted((o1, o2) -> o2.getRecordDate().compareTo(o1.getRecordDate())) //Sorting update by date (latest updates first by date of upload in database)
.toList();
//We had all the variables of the oldest update
latestUpdatesbyVariables.add(suByMode.getFirst());
//We keep the name of already added variables to skip them in older updates
List<VarIdScopeTuple> addedVariables = new ArrayList<>();
SurveyUnitModel latestUpdate = suByMode.getFirst();
if(latestUpdate.getCollectedVariables() == null){
latestUpdate.setCollectedVariables(new ArrayList<>());
}
if(latestUpdate.getExternalVariables() == null){
latestUpdate.setExternalVariables(new ArrayList<>());
}
latestUpdate.getCollectedVariables().forEach(colVar -> addedVariables.add(new VarIdScopeTuple(colVar.varId(),
colVar.scope(), colVar.iteration())));
latestUpdate.getExternalVariables().forEach(extVar -> addedVariables.add(new VarIdScopeTuple(extVar.varId(), extVar.scope(), extVar.iteration())));
suByMode.forEach(surveyUnitModel -> {
List<VariableModel> collectedVariablesToKeep = new ArrayList<>();
List<VariableModel> externalVariablesToKeep = new ArrayList<>();
// We iterate over the variables of the update and add them to the list if they are not already added
if (surveyUnitModel.getCollectedVariables() != null) {
surveyUnitModel.getCollectedVariables().stream()
.filter(colVar -> !addedVariables.contains(new VarIdScopeTuple(colVar.varId(), colVar.scope()
, colVar.iteration())))
.forEach(colVar -> {
collectedVariablesToKeep.add(colVar);
addedVariables.add(new VarIdScopeTuple(colVar.varId(), colVar.scope(), colVar.iteration()));
});
}
if (surveyUnitModel.getExternalVariables() != null){
surveyUnitModel.getExternalVariables().stream()
.filter(extVar -> !addedVariables.contains(new VarIdScopeTuple(extVar.varId(), extVar.scope(),
extVar.iteration())))
.forEach(extVar -> {
externalVariablesToKeep.add(extVar);
addedVariables.add(new VarIdScopeTuple(extVar.varId(), extVar.scope(), extVar.iteration()));
});
}
// If there are new variables, we add the update to the list of latest updates
if (!collectedVariablesToKeep.isEmpty() || !externalVariablesToKeep.isEmpty()){
surveyUnitModel.setCollectedVariables(collectedVariablesToKeep);
surveyUnitModel.setExternalVariables(externalVariablesToKeep);
latestUpdatesbyVariables.add(surveyUnitModel);
}
});
});
return latestUpdatesbyVariables;
}
//========= OPTIMISATIONS PERFS (START) ==========
/**
* @author Adrien Marchal
*/
@Override
public List<List<SurveyUnitModel>> findLatestByIdAndByQuestionnaireIdAndModeOrdered(String questionnaireId, String mode,
List<InterrogationId> interrogationIds) {
//return object
List<List<SurveyUnitModel>> listLatestUpdatesbyVariables = new ArrayList<>();
//1) QUERY
// => conversion of "List<InterrogationId>" -> "List<String>" for query using lamda
List<String> queryInParam = interrogationIds.stream().map(InterrogationId::getInterrogationId).toList();
//Get !!!all versions!!! of a set of "interrogationIds"
List<SurveyUnitModel> allResponsesVersionsSet = surveyUnitPersistencePort.findBySetOfIdsAndQuestionnaireIdAndMode(questionnaireId, mode, queryInParam);
//2) FILTER BY interrogationId AND ORDER BY DATE (MOST RECENT FIRST, oldest last)
interrogationIds.forEach(interrogationId -> {
List<SurveyUnitModel> allResponsesVersionsForSingleInterrId = allResponsesVersionsSet.stream()
.filter(surveyUnitModel -> surveyUnitModel.getInterrogationId().equals(interrogationId.getInterrogationId()))
.sorted((o1, o2) -> o2.getRecordDate().compareTo(o1.getRecordDate())) //Sorting update by date (latest updates first by date of upload in database)
.toList();
List<SurveyUnitModel> latestUpdates = extractLatestUpdates(allResponsesVersionsForSingleInterrId);
//3) add to result (: keep same existing process)
listLatestUpdatesbyVariables.add(latestUpdates);
});
return listLatestUpdatesbyVariables;
}
private List<SurveyUnitModel> extractLatestUpdates(List<SurveyUnitModel> allResponsesVersionsForSingleInterrId) {
//return
List<SurveyUnitModel> latestUpdatesbyVariables = new ArrayList<>();
//We keep the name of already added variables to skip them in older updates
List<VarIdScopeTuple> addedVariables = new ArrayList<>();
//No useless process if there is only ONE or NONE element (performances optimisations)
if(allResponsesVersionsForSingleInterrId.size() <= 1) {
//We add all the variables of the LATEST update
latestUpdatesbyVariables.add(allResponsesVersionsForSingleInterrId.getFirst());
return latestUpdatesbyVariables;
}
//ELSE -> CASE WHERE THERE ARE MORE THAN ONE VERSION!
allResponsesVersionsForSingleInterrId.forEach(surveyUnitModel -> {
List<VariableModel> collectedVariablesToKeep = new ArrayList<>();
List<VariableModel> externalVariablesToKeep = new ArrayList<>();
// We iterate over the variables of the update and add them to the list if they are not already added
if (surveyUnitModel.getCollectedVariables() != null) {
surveyUnitModel.getCollectedVariables().stream()
.filter(colVar -> !addedVariables.contains(new VarIdScopeTuple(colVar.varId(), colVar.scope(), colVar.iteration())))
.forEach(colVar -> {
collectedVariablesToKeep.add(colVar);
addedVariables.add(new VarIdScopeTuple(colVar.varId(), colVar.scope(), colVar.iteration()));
});
}
if (surveyUnitModel.getExternalVariables() != null){
surveyUnitModel.getExternalVariables().stream()
.filter(extVar -> !addedVariables.contains(new VarIdScopeTuple(extVar.varId(), extVar.scope(), extVar.iteration())))
.forEach(extVar -> {
externalVariablesToKeep.add(extVar);
addedVariables.add(new VarIdScopeTuple(extVar.varId(), extVar.scope(), extVar.iteration()));
});
}
// If there are new variables, we add the update to the list of latest updates
if (!collectedVariablesToKeep.isEmpty() || !externalVariablesToKeep.isEmpty()){
surveyUnitModel.setCollectedVariables(collectedVariablesToKeep);
surveyUnitModel.setExternalVariables(externalVariablesToKeep);
latestUpdatesbyVariables.add(surveyUnitModel);
}
});
return latestUpdatesbyVariables;
}
//========= OPTIMISATIONS PERFS (END) ==========
@Override
public SurveyUnitDto findLatestValuesByStateByIdAndByQuestionnaireId(String interrogationId,
String questionnaireId) {
SurveyUnitDto surveyUnitDto = SurveyUnitDto.builder()
.interrogationId(interrogationId)
.collectedVariables(new ArrayList<>())
.externalVariables(new ArrayList<>())
.build();
//Extract variables
Map<VarIdScopeTuple, VariableDto> collectedVariableMap = new HashMap<>();
Map<VarIdScopeTuple, VariableDto> externalVariableMap = new HashMap<>();
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findByIds(interrogationId, questionnaireId);
List<Mode> modes = getDistinctsModes(surveyUnitModels);
modes.forEach(mode -> {
List<SurveyUnitModel> suByMode = surveyUnitModels.stream()
.filter(surveyUnitModel -> surveyUnitModel.getMode().equals(mode))
.sorted((o1, o2) -> o2.getRecordDate().compareTo(o1.getRecordDate())) //Sorting update by date (latest updates first by date of upload in database)
.toList();
VariablesMap variablesMap = null;
for(SurveyUnitModel surveyUnitModel : suByMode){
if(variablesMap == null){
variablesMap = metadataService.readMetadatas(
surveyUnitModel.getCampaignId(),
surveyUnitModel.getMode().getModeName(),
fileUtils,
new ArrayList<>());
}
extractVariables(surveyUnitModel, collectedVariableMap,
externalVariableMap, variablesMap);
}
});
collectedVariableMap.keySet().forEach(variableTuple -> surveyUnitDto.getCollectedVariables().add(collectedVariableMap.get(variableTuple)));
externalVariableMap.keySet().forEach(variableName -> surveyUnitDto.getExternalVariables().add(externalVariableMap.get(variableName)));
return surveyUnitDto;
}
@Override
public List<InterrogationId> findDistinctInterrogationIdsByQuestionnaireId(String questionnaireId) {
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findInterrogationIdsByQuestionnaireId(questionnaireId);
List<InterrogationId> suIds = new ArrayList<>();
surveyUnitModels.forEach(surveyUnitModel -> suIds.add(new InterrogationId(surveyUnitModel.getInterrogationId())));
return suIds.stream().distinct().toList();
}
//============ OPTIMISATIONS PERFS (START) ============
/**
* @author Adrien Marchal
* Calculations made to establish the data a worker will be responsible of, among the whole data to be processed.
* (needed for distributed process / horizontal scaling)
*/
@Override
public List<InterrogationId> findDistinctPageableInterrogationIdsByQuestionnaireId(String questionnaireId, long totalSize,
long blockSize, long page) {
long calculatedTotalSize;
if(totalSize == 0) {
calculatedTotalSize = countInterrogationIdsByQuestionnaireId(questionnaireId);
} else {
calculatedTotalSize = totalSize;
}
//Check arguments
long skip = page * blockSize;
if(page < 0 || skip > calculatedTotalSize) {
//return empty list
return List.of();
}
long calculatedBlockSize = (skip + blockSize) > calculatedTotalSize ? calculatedTotalSize - skip : blockSize;
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findPageableInterrogationIdsByQuestionnaireId(questionnaireId, skip, calculatedBlockSize);
List<InterrogationId> suIds = new ArrayList<>();
surveyUnitModels.forEach(surveyUnitModel -> suIds.add(new InterrogationId(surveyUnitModel.getInterrogationId())));
return suIds.stream().distinct().toList();
}
/**
* @author Adrien Marchal
*/
@Override
public long countInterrogationIdsByQuestionnaireId(String questionnaireId) {
return surveyUnitPersistencePort.countInterrogationIdsByQuestionnaireId(questionnaireId);
}
//=========== OPTIMISATIONS PERFS (END) =============
@Override
public List<SurveyUnitModel> findInterrogationIdsAndModesByQuestionnaireId(String questionnaireId) {
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findInterrogationIdsByQuestionnaireId(questionnaireId);
return surveyUnitModels.stream().distinct().toList();
}
@Override
public List<Mode> findModesByQuestionnaireId(String questionnaireId) {
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findInterrogationIdsByQuestionnaireId(questionnaireId);
List<Mode> sources = new ArrayList<>();
surveyUnitModels.forEach(surveyUnitModel -> sources.add(surveyUnitModel.getMode()));
return sources.stream().distinct().toList();
}
@Override
public List<Mode> findModesByCampaignId(String campaignId) {
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findInterrogationIdsByCampaignId(campaignId);
List<Mode> sources = new ArrayList<>();
surveyUnitModels.forEach(surveyUnitModel -> sources.add(surveyUnitModel.getMode()));
return sources.stream().distinct().toList();
}
//========= OPTIMISATIONS PERFS (START) ==========
@Override
public List<Mode> findModesByQuestionnaireIdV2(String questionnaireId) {
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findModesByQuestionnaireIdV2(questionnaireId);
List<Mode> sources = new ArrayList<>();
surveyUnitModels.forEach(surveyUnitModel -> sources.add(surveyUnitModel.getMode()));
return sources.stream().distinct().toList();
}
@Override
public List<Mode> findModesByCampaignIdV2(String campaignId) {
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findModesByCampaignIdV2(campaignId);
List<Mode> sources = new ArrayList<>();
surveyUnitModels.forEach(surveyUnitModel -> sources.add(surveyUnitModel.getMode()));
return sources.stream().distinct().toList();
}
//========= OPTIMISATIONS PERFS (END) ==========
@Override
public Long deleteByQuestionnaireId(String questionnaireId) {
return surveyUnitPersistencePort.deleteByQuestionnaireId(questionnaireId);
}
@Override
public long countResponses() {
return surveyUnitPersistencePort.count();
}
@Override
public Set<String> findQuestionnaireIdsByCampaignId(String campaignId) {
return surveyUnitPersistencePort.findQuestionnaireIdsByCampaignId(campaignId);
}
//========= OPTIMISATIONS PERFS (START) ==========
/**
* @author Adrien Marchal
*/
@Override
public Set<String> findQuestionnaireIdsByCampaignIdV2(String campaignId) {
return surveyUnitPersistencePort.findQuestionnaireIdsByCampaignIdV2(campaignId);
}
//========= OPTIMISATIONS PERFS (END) ==========
@Override
public Set<String> findDistinctCampaignIds() {
return surveyUnitPersistencePort.findDistinctCampaignIds();
}
@Override
public List<CampaignWithQuestionnaire> findCampaignsWithQuestionnaires() {
List<CampaignWithQuestionnaire> campaignsWithQuestionnaireList = new ArrayList<>();
for(String campaignId : findDistinctCampaignIds()){
Set<String> questionnaires = findQuestionnaireIdsByCampaignId(campaignId);
campaignsWithQuestionnaireList.add(new CampaignWithQuestionnaire(campaignId,questionnaires));
}
return campaignsWithQuestionnaireList;
}
@Override
public long countResponsesByCampaignId(String campaignId){
return surveyUnitPersistencePort.countByCampaignId(campaignId);
}
@Override
public Set<String> findDistinctQuestionnaireIds() {
return surveyUnitPersistencePort.findDistinctQuestionnaireIds();
}
@Override
public List<QuestionnaireWithCampaign> findQuestionnairesWithCampaigns() {
List<QuestionnaireWithCampaign> questionnaireWithCampaignList = new ArrayList<>();
for(String questionnaireId : findDistinctQuestionnaireIds()){
Set<String> campaigns = surveyUnitPersistencePort.findCampaignIdsByQuestionnaireId(questionnaireId);
questionnaireWithCampaignList.add(new QuestionnaireWithCampaign(
questionnaireId,
campaigns)
);
}
return questionnaireWithCampaignList;
}
@Override
public List<SurveyUnitModel> parseEditedVariables(
SurveyUnitInputDto surveyUnitInputDto,
String userIdentifier,
VariablesMap variablesMap
) throws GenesisException {
List<DataState> statesReceived = surveyUnitInputDto.getCollectedVariables().stream()
.map(colVar -> colVar.getVariableStateInputDto().getState())
.distinct()
.toList();
if (statesReceived.contains(DataState.COLLECTED)){
throw new GenesisException(400,"You can not persist in database a new value with the state COLLECTED");
}
List<SurveyUnitModel> surveyUnitModels = new ArrayList<>();
for (DataState state : statesReceived){
SurveyUnitModel surveyUnitModel = SurveyUnitModel.builder()
.campaignId(surveyUnitInputDto.getCampaignId())
.mode(surveyUnitInputDto.getMode())
.questionnaireId(surveyUnitInputDto.getQuestionnaireId())
.interrogationId(surveyUnitInputDto.getInterrogationId())
.state(state)
.recordDate(LocalDateTime.now())
.collectedVariables(new ArrayList<>())
.externalVariables(new ArrayList<>())
.modifiedBy(userIdentifier)
.build();
//Keep only variable dtos who has the corresponding state
List<VariableInputDto> editedCollectedVariables = surveyUnitInputDto.getCollectedVariables().stream()
.filter(colVar -> colVar.getVariableStateInputDto().getState() == state).toList();
//Collected variables management
for(VariableInputDto editedVariableDto : editedCollectedVariables){
VariableModel collectedVariable = VariableModel.builder()
.varId(editedVariableDto.getVariableName())
.value(editedVariableDto.getVariableStateInputDto().getValue() == null ?
null
: editedVariableDto.getVariableStateInputDto().getValue().toString()
)
.parentId(GroupUtils.getParentGroupName(editedVariableDto.getVariableName(), variablesMap))
.scope(variablesMap.getVariable(editedVariableDto.getVariableName()).getGroupName())
.iteration(editedVariableDto.getIteration())
.build();
surveyUnitModel.getCollectedVariables().add(collectedVariable);
}
surveyUnitModels.add(surveyUnitModel);
}
return surveyUnitModels;
}
@Override
public String findQuestionnaireIdByInterrogationId(String interrogationId) throws GenesisException {
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findByInterrogationId(interrogationId);
if (surveyUnitModels.isEmpty()){
throw new GenesisException(404,String.format("The interrogationId %s is not in database",interrogationId));
}
Set<String> questionnaireIds = new HashSet<>();
for(SurveyUnitModel surveyUnitModel : surveyUnitModels){
questionnaireIds.add(surveyUnitModel.getQuestionnaireId());
}
if(questionnaireIds.size() > 1){
throw new GenesisException(207,String.format("Multiple questionnaires for %s :%n%s",
interrogationId,
String.join("\n", questionnaireIds)
));
}
return questionnaireIds.iterator().next(); //Return first (and supposed only) element of set
}
@Override
public Set<String> findCampaignIdsFrom(SurveyUnitInputDto dto) {
List<SurveyUnitModel> responses = findByIdsInterrogationAndQuestionnaire(
dto.getInterrogationId(),
dto.getQuestionnaireId()
);
return responses.stream()
.map(SurveyUnitModel::getCampaignId)
.collect(Collectors.toSet());
}
//Utils
private static List<Mode> getDistinctsModes(List<SurveyUnitModel> surveyUnitModels) {
List<Mode> sources = new ArrayList<>();
surveyUnitModels.forEach(surveyUnitModel -> sources.add(surveyUnitModel.getMode()));
return sources.stream().distinct().toList();
}
/**
* Extract collected variables from a model class to a variable map
* @param surveyUnitModel survey unit model
* @param collectedVariableMap Collected variable DTO map to populate
* @param externalVariableMap External variable DTO map to populate
*/
private void extractVariables(SurveyUnitModel surveyUnitModel,
Map<VarIdScopeTuple, VariableDto> collectedVariableMap,
Map<VarIdScopeTuple, VariableDto> externalVariableMap,
VariablesMap variablesMap
) {
if(surveyUnitModel.getCollectedVariables() == null){
surveyUnitModel.setCollectedVariables(new ArrayList<>());
}
for (VariableModel collectedVariable : surveyUnitModel.getCollectedVariables()) {
VarIdScopeTuple loopIdTuple = new VarIdScopeTuple(collectedVariable.varId(), collectedVariable.scope(),
collectedVariable.iteration());
VariableDto variableDto = collectedVariableMap.get(loopIdTuple);
//Create variable into map if not exists
if (variableDto == null) {
variableDto = VariableDto.builder()
.variableName(collectedVariable.varId())
.scope(collectedVariable.scope())
.iteration(collectedVariable.iteration())
.variableStateDtoList(new ArrayList<>())
.build();
collectedVariableMap.put(loopIdTuple, variableDto);
}
//Extract variable state
if (isMostRecentForSameState(surveyUnitModel, variableDto)) {
variableDto.getVariableStateDtoList().add(
VariableStateDto.builder()
.state(surveyUnitModel.getState())
.active(isLastVariableState(surveyUnitModel, variableDto))
.value(getValueWithType(
collectedVariable.varId(),
collectedVariable.value(),
variablesMap))
.date(surveyUnitModel.getRecordDate())
.build()
);
}
}
if(surveyUnitModel.getExternalVariables() == null){
surveyUnitModel.setExternalVariables(new ArrayList<>());
}
for(VariableModel externalVariable : surveyUnitModel.getExternalVariables()){
VarIdScopeTuple loopIdTuple = new VarIdScopeTuple(externalVariable.varId(), externalVariable.scope(), externalVariable.iteration());
VariableDto variableDto = externalVariableMap.get(loopIdTuple);
//Create variable into map if not exists
if(variableDto == null){
variableDto = VariableDto.builder()
.variableName(externalVariable.varId())
.scope(externalVariable.scope())
.iteration(externalVariable.iteration())
.variableStateDtoList(new ArrayList<>())
.build();
externalVariableMap.put(loopIdTuple, variableDto);
}
//Extract variable state
if(isMostRecentForSameState(surveyUnitModel, variableDto)){
variableDto.getVariableStateDtoList().add(
VariableStateDto.builder()
.state(surveyUnitModel.getState())
.active(isLastVariableState(surveyUnitModel, variableDto))
.value(getValueWithType(
externalVariable.varId(),
externalVariable.value(),
variablesMap))
.date(surveyUnitModel.getRecordDate())
.build()
);
}
}
}
private Object getValueWithType(String variableName, String value, VariablesMap variablesMap) {
if(!variablesMap.hasVariable(variableName)){
log.warn("Variable {} not found in variableMap", variableName);
return value;
}
if(value == null) return null;
if(value.isEmpty()) return value;
VariableType variableType = variablesMap.getVariable(variableName).getType();
try {
switch (variableType) {
case INTEGER -> {
return Integer.parseInt(value);
}
case BOOLEAN -> {
return Boolean.parseBoolean(value);
}
case NUMBER -> {
return Double.parseDouble(value);
}
default -> {
return value;
}
}
}catch (NumberFormatException e){
log.warn("Invalid value {} for variable {}, expected type {}", value, variableName, variableType);
return value;
}
}
/**
* Check if there is any other more recent variable value for a same state in Variable DTO
* @param surveyUnitModel model containing variable
* @param variableDto DTO to check in
* @return true if there's no more recent variable for the same state
*/
private boolean isMostRecentForSameState(SurveyUnitModel surveyUnitModel, VariableDto variableDto) {
List<VariableStateDto> variableStatesSameState = variableDto.getVariableStateDtoList().stream().filter(
variableStateDto -> variableStateDto.getState().equals(surveyUnitModel.getState())
)
.sorted((o1, o2) -> o2.getDate().compareTo(o1.getDate()))
.toList();
if(variableStatesSameState.isEmpty()){
//Variable doesn't contain state
return true;
}
LocalDateTime mostRecentStateDateTime = variableStatesSameState.getFirst().getDate();
return mostRecentStateDateTime.isBefore(surveyUnitModel.getRecordDate());
}
/**
* Check if model is more recent that any variable state in variable DTO regardless of state
* Used for active variable
* @param surveyUnitModel model used to compare
* @param variableDto variable to check
* @return false if there is any variable state that comes from a more recent model already
*/
private boolean isLastVariableState(SurveyUnitModel surveyUnitModel, VariableDto variableDto) {
for(VariableStateDto variableStateDTO : variableDto.getVariableStateDtoList()){
if(variableStateDTO.getDate().isAfter(surveyUnitModel.getRecordDate())){
return false;
}
variableStateDTO.setActive(false);
}
return true;
}
@Override
public void saveResponsesFromXmlFile(String xmlFile, String metadataFilePath, Mode modeSpecified, boolean withDDI) throws Exception {
VariablesMap variablesMap;
if(withDDI) {
//Parse DDI
log.info("Try to read DDI file : {}", metadataFilePath);
try {
variablesMap =
DDIReader.getMetadataFromDDI(Path.of(metadataFilePath).toFile().toURI().toURL().toString(),
new FileInputStream(metadataFilePath)).getVariables();
} catch (MetadataParserException e) {
throw new MetadataParserException(e.getMessage());
} catch (FileNotFoundException e) {
throw new FileNotFoundException(e.getMessage());
} catch(MalformedURLException e) {
throw new MalformedURLException(e.getMessage());
}
}else{
//Parse Lunatic
log.info("Try to read lunatic file : {}", metadataFilePath);
variablesMap = LunaticReader.getMetadataFromLunatic(new FileInputStream(metadataFilePath)).getVariables();
}
log.info("Try to read Xml file : {}", xmlFile);
Path filepath = Paths.get(xmlFile);
try {
if (getFileSizeInMB(filepath) <= Constants.MAX_FILE_SIZE_UNTIL_SEQUENTIAL) {
processXmlFileWithMemory(filepath, modeSpecified, variablesMap);
return;
}
processXmlFileSequentially(filepath, modeSpecified, variablesMap);
} catch(IOException e) {
throw new IOException(e.getMessage());
} catch(ParserConfigurationException e) {
throw new ParserConfigurationException(e.getMessage());
} catch(SAXException e) {
throw new SAXException(e.getMessage());
} catch(XMLStreamException e) {
throw new XMLStreamException(e.getMessage());
} catch(GenesisException e) {
throw new GenesisException(e.getStatus(), e.getMessage());
}
}
@Override
public boolean saveResponsesFromXmlCampaignFolder(String campaignName, Mode modeSpecified) throws Exception {
List<GenesisError> errors = new ArrayList<>();
boolean isAnyDataSaved = false;
log.info("Try to import XML data for campaign: {}", campaignName);
List<Mode> modesList = controllerUtils.getModesList(campaignName, modeSpecified);
for (Mode currentMode : modesList) {
try {
processCampaignWithMode(campaignName, currentMode, errors, null);
isAnyDataSaved = true;
}catch (NoDataException nde){
//Don't stop if NoDataError thrown
log.warn(nde.getMessage());
}catch (Exception e){
log.error(ResponseController.CAMPAIGN_ERROR, campaignName, e.toString());
throw new Exception(e.getMessage());
}
}
if (!errors.isEmpty()){
throw new GenesisException(500, errors.getFirst().getMessage());
}
return isAnyDataSaved;
}
@Override
public void saveResponsesFromAllCampaignFolders() throws NoDataException, GenesisException {
List<GenesisError> errors = new ArrayList<>();
List<File> campaignFolders = fileUtils.listAllSpecsFolders();
if (campaignFolders.isEmpty()) {
throw new NoDataException("No campaign to save");
}
for (File campaignFolder: campaignFolders) {
String campaignName = campaignFolder.getName();
log.info("Try to import data for campaign: {}", campaignName);
try {
List<Mode> modesList = controllerUtils.getModesList(campaignName, null); //modeSpecified null = all modes
for (Mode currentMode : modesList) {
processCampaignWithMode(campaignName, currentMode, errors, Constants.DIFFERENTIAL_DATA_FOLDER_NAME);
}
}catch (NoDataException nde){
log.warn(nde.getMessage());
}
catch (Exception e) {
log.warn(ResponseController.CAMPAIGN_ERROR, campaignName, e.toString());
errors.add(new GenesisError(e.getMessage()));
}
}
if (!errors.isEmpty()){
throw new GenesisException(209, "Data saved with " + errors.size() + " errors");
}
}
@Override
public SurveyUnitQualityToolDto findResponsesByInterrogationAndQuestionnaireLatestStates(String interrogationId, String questionnaireId) throws GenesisException {
//Check context
DataProcessingContextModel dataProcessingContextModel =
contextService.getContext(interrogationId);
if(dataProcessingContextModel == null || !dataProcessingContextModel.isWithReview()){
throw new GenesisException(403, new ApiError("Review is disabled for that partition").message());
}
SurveyUnitDto response = findLatestValuesByStateByIdAndByQuestionnaireId(interrogationId, questionnaireId);
return DataTransformer.transformSurveyUnitDto(response);
}
@Override
public void saveEditedVariables(SurveyUnitInputDto surveyUnitInputDto) throws GenesisException {
log.debug("Received in save edited : {}",surveyUnitInputDto.toString());
//Code quality : we need to put all that logic out of this controller
//Parse metadata
//Try to look for DDI first, if no DDI found looks for lunatic components
List<GenesisError> errors = new ArrayList<>();
//We need to retrieve campaignId
Set<String> campaignIds = findCampaignIdsFrom(surveyUnitInputDto);
if (campaignIds.size() != 1){
throw new GenesisException(500, "Impossible to assign one campaignId to that response");
}
// If the size is equal to 1 we get this campaignId
String campaignId = campaignIds.iterator().next();
surveyUnitInputDto.setCampaignId(campaignId);
VariablesMap variablesMap = metadataService.readMetadatas(surveyUnitInputDto.getCampaignId(),
surveyUnitInputDto.getMode().getModeName(), fileUtils, errors);
if(variablesMap == null){
log.warn("Can't find DDI, trying with lunatic...");
variablesMap = metadataService.readMetadatas(surveyUnitInputDto.getCampaignId(),
surveyUnitInputDto.getMode().getModeName(), fileUtils, errors);
if(variablesMap == null){
throw new GenesisException(404, errors.getLast().getMessage());
}
}
//Check if input edited variables are in metadatas
List<String> absentCollectedVariableNames =
surveyUnitQualityService.checkVariablesPresentInMetadata(surveyUnitInputDto.getCollectedVariables(),
variablesMap);
if (!absentCollectedVariableNames.isEmpty()) {
String absentVariables = String.join("\n", absentCollectedVariableNames);
//400 = Bad Request
throw new GenesisException(400, String.format("The following variables are absent in metadatas : %n%s", absentVariables));
}
//Fetch user identifier from OIDC token
String userIdentifier = authUtils.getIDEP();
//Create surveyUnitModel for each STATE received (Quality tool could send variables with another STATE than EDITED)
List<SurveyUnitModel> surveyUnitModels;
try{
surveyUnitModels = parseEditedVariables(
surveyUnitInputDto,
userIdentifier,
variablesMap
);
}catch (GenesisException e){
throw new GenesisException(e.getStatus(), e.getMessage());
}
//Check data with dataverifier (might create a FORCED document)
surveyUnitQualityService.verifySurveyUnits(surveyUnitModels, variablesMap);
//Save documents
saveSurveyUnits(surveyUnitModels);
}
//Utilities
/**
* Process a campaign with a specific mode
* @param campaignName name of campaign
* @param mode mode of collected data
* @param errors error list to fill
*/
private void processCampaignWithMode(String campaignName, Mode mode, List<GenesisError> errors, String rootDataFolder)
throws IOException, ParserConfigurationException, SAXException, XMLStreamException, NoDataException, GenesisException {
log.info("Starting data import for mode: {}", mode.getModeName());
String dataFolder = rootDataFolder == null ?
fileUtils.getDataFolder(campaignName, mode.getFolder(), null)
: fileUtils.getDataFolder(campaignName, mode.getFolder(), rootDataFolder);
List<String> dataFiles = fileUtils.listFiles(dataFolder);
log.info("Number of files to load in folder {} : {}", dataFolder, dataFiles.size());
if (dataFiles.isEmpty()) {
throw new NoDataException("No data file found in folder %s".formatted(dataFolder));
}
VariablesMap variablesMap = metadataService.readMetadatas(campaignName, mode.getModeName(), fileUtils, errors);
if (variablesMap == null){
return;
}
//For each XML data file
for (String fileName : dataFiles.stream().filter(s -> s.endsWith(".xml")).toList()) {
processOneXmlFileForCampaign(campaignName, mode, fileName, dataFolder, variablesMap);
}
//Create context if not exist
if(contextService.getContextByPartitionId(campaignName) == null){
contextService.saveContext(campaignName, false);
}
}
private void processOneXmlFileForCampaign(String campaignName,
Mode mode,
String fileName,
String dataFolder,
VariablesMap variablesMap) throws IOException, ParserConfigurationException, SAXException, XMLStreamException, GenesisException {
String filepathString = String.format(PATH_FORMAT, dataFolder, fileName);
Path filepath = Paths.get(filepathString);
//Check if file not in done folder, delete if true
if(isDataFileInDoneFolder(filepath, campaignName, mode.getFolder())){
log.warn("File {} already exists in DONE folder ! Deleting...", fileName);
Files.deleteIfExists(filepath);
return;
}
//Read file
log.info("Try to read Xml file : {}", fileName);
try {
if (getFileSizeInMB(filepath) <= Constants.MAX_FILE_SIZE_UNTIL_SEQUENTIAL) {
processXmlFileWithMemory(filepath, mode, variablesMap);
} else {
processXmlFileSequentially(filepath, mode, variablesMap);
}
log.debug("File {} saved", fileName);
//If no exception has been thrown at this step, all is good then : we can move the file!
fileUtils.moveDataFile(campaignName, mode.getFolder(), filepath);
} catch(GenesisException e) {
log.error("Error {} on file {} : {}", e.getStatus(), fileName, e.getMessage());
throw new GenesisException(e.getStatus(), e.getMessage());
}
}
private static long getFileSizeInMB(Path filepath) {
return filepath.toFile().length() / 1024 / 1024;
}
private boolean isDataFileInDoneFolder(Path filepath, String campaignName, String modeFolder) {
return Path.of(fileUtils.getDoneFolder(campaignName, modeFolder)).resolve(filepath.getFileName()).toFile().exists();
}
private void processXmlFileWithMemory(Path filepath, Mode modeSpecified, VariablesMap variablesMap) throws IOException, ParserConfigurationException, SAXException, GenesisException {
LunaticXmlCampaign campaign;
// DOM method
LunaticXmlDataParser parser = new LunaticXmlDataParser();
try {
campaign = parser.parseDataFile(filepath);
} catch (GenesisException e) {
log.error(e.toString());
throw new GenesisException(e.getStatus(), e.getMessage());
}
List<SurveyUnitModel> surveyUnitModels = new ArrayList<>();
for (LunaticXmlSurveyUnit su : campaign.getSurveyUnits()) {
surveyUnitModels.addAll(LunaticXmlAdapter.convert(su, variablesMap, campaign.getCampaignId(), modeSpecified));
}
surveyUnitQualityService.verifySurveyUnits(surveyUnitModels, variablesMap);
log.debug("Saving {} survey units updates", surveyUnitModels.size());
saveSurveyUnits(surveyUnitModels);
log.debug("Survey units updates saved");
log.info("File {} processed with {} survey units", filepath.getFileName(), surveyUnitModels.size());
}
private void processXmlFileSequentially(Path filepath, Mode modeSpecified, VariablesMap variablesMap) throws IOException, XMLStreamException, GenesisException {
LunaticXmlCampaign campaign;
//Sequential method
log.warn("File size > {} MB! Parsing XML file using sequential method...", Constants.MAX_FILE_SIZE_UNTIL_SEQUENTIAL);
try (final InputStream stream = new FileInputStream(filepath.toFile())) {
LunaticXmlDataSequentialParser parser = new LunaticXmlDataSequentialParser(filepath, stream);
int suCount = 0;
campaign = parser.getCampaign();
LunaticXmlSurveyUnit su = parser.readNextSurveyUnit();
contextService.saveContext(campaign.getCampaignId(), false);
while (su != null) {
List<SurveyUnitModel> surveyUnitModels = new ArrayList<>(LunaticXmlAdapter.convert(su, variablesMap, campaign.getCampaignId(), modeSpecified));