1313import java .util .Comparator ;
1414import java .util .EnumMap ;
1515import java .util .HashSet ;
16+ import java .util .LinkedHashMap ;
1617import java .util .List ;
18+ import java .util .Map ;
1719import java .util .Optional ;
1820import java .util .Set ;
1921import java .util .regex .Matcher ;
@@ -45,7 +47,7 @@ private DataVerifier() {
4547 * @param variablesMap VariablesMap containing definitions of each variable
4648 */
4749 public static void verifySurveyUnits (List <SurveyUnitModel > surveyUnitModelsList , VariablesMap variablesMap ){
48- List <SurveyUnitModel > surveyUnitModelsListForced = new ArrayList <>(); // Created FORCED SU models
50+ List <SurveyUnitModel > surveyUnitModelsListFormatted = new ArrayList <>(); // Created FORCED SU models
4951
5052 for (String interrogationId : getInterrogationIds (surveyUnitModelsList )) { // For each id of the list
5153 List <SurveyUnitModel > srcSurveyUnitModelsOfInterrogationId = surveyUnitModelsList .stream ().filter (element -> element .getInterrogationId ().equals (interrogationId )).toList ();
@@ -58,34 +60,34 @@ public static void verifySurveyUnits(List<SurveyUnitModel> surveyUnitModelsList,
5860
5961 //Create FORCED if any corrected variable
6062 if (!correctedCollectedVariables .isEmpty () || !correctedExternalVariables .isEmpty ()){
61- SurveyUnitModel newForcedSurveyUnitModel = createForcedSurveyUnitModel (surveyUnitModelsList , interrogationId , correctedCollectedVariables , correctedExternalVariables );
62- surveyUnitModelsListForced .add (newForcedSurveyUnitModel );
63+ SurveyUnitModel newFormattedSurveyUnitModel = createFormattedSurveyUnitModel (surveyUnitModelsList , interrogationId , correctedCollectedVariables , correctedExternalVariables );
64+ surveyUnitModelsListFormatted .add (newFormattedSurveyUnitModel );
6365 }
6466 }
65- surveyUnitModelsList .addAll (surveyUnitModelsListForced );
67+ surveyUnitModelsList .addAll (surveyUnitModelsListFormatted );
6668 }
6769
68- private static SurveyUnitModel createForcedSurveyUnitModel (
70+ private static SurveyUnitModel createFormattedSurveyUnitModel (
6971 List <SurveyUnitModel > surveyUnitModelsList ,
7072 String interrogationId ,
7173 List <VariableModel > correctedCollectedVariables ,
7274 List <VariableModel > correctedExternalVariables
7375 ) {
7476 SurveyUnitModel sampleSurveyUnitModel = surveyUnitModelsList .stream ().filter (element -> element .getInterrogationId ().equals (interrogationId )).toList ().getFirst ();
75- SurveyUnitModel newForcedSurveyUnitModel = SurveyUnitModel .builder ()
77+ SurveyUnitModel newFormattedSurveyUnitModel = SurveyUnitModel .builder ()
7678 .questionnaireId (sampleSurveyUnitModel .getQuestionnaireId ())
7779 .campaignId (sampleSurveyUnitModel .getCampaignId ())
7880 .interrogationId (interrogationId )
79- .state (DataState .FORCED )
81+ .state (DataState .FORMATTED )
8082 .mode (sampleSurveyUnitModel .getMode ())
81- .recordDate (LocalDateTime .now ())
83+ .recordDate (LocalDateTime .now (). plusSeconds ( 1 )) // Add 1 second to avoid same recordDate as COLLECTED
8284 .fileDate (sampleSurveyUnitModel .getFileDate ())
8385 .collectedVariables (new ArrayList <>())
8486 .externalVariables (new ArrayList <>())
8587 .build ();
8688
8789 for (VariableModel correctedCollectedVariable : correctedCollectedVariables ){
88- newForcedSurveyUnitModel .getCollectedVariables ().add (
90+ newFormattedSurveyUnitModel .getCollectedVariables ().add (
8991 VariableModel .builder ()
9092 .varId (correctedCollectedVariable .varId ())
9193 .value (correctedCollectedVariable .value ())
@@ -97,7 +99,7 @@ private static SurveyUnitModel createForcedSurveyUnitModel(
9799 }
98100
99101 for (VariableModel correctedExternalVariable : correctedExternalVariables ){
100- newForcedSurveyUnitModel .getExternalVariables ().add (
102+ newFormattedSurveyUnitModel .getExternalVariables ().add (
101103 VariableModel .builder ()
102104 .varId (correctedExternalVariable .varId ())
103105 .value (correctedExternalVariable .value ())
@@ -107,7 +109,7 @@ private static SurveyUnitModel createForcedSurveyUnitModel(
107109 .build ()
108110 );
109111 }
110- return newForcedSurveyUnitModel ;
112+ return newFormattedSurveyUnitModel ;
111113 }
112114
113115 /**
@@ -132,7 +134,8 @@ private static Set<String> getInterrogationIds(List<SurveyUnitModel> surveyUnitM
132134 * @param correctedCollectedVariables FORCED document variables
133135 */
134136 private static void collectedVariablesManagement (List <SurveyUnitModel > srcSurveyUnitModelsOfInterrogationId , VariablesMap variablesMap , List <VariableModel > correctedCollectedVariables ){
135- Set <String > variableNames = new HashSet <>();
137+ Map <String ,List <Integer >> variableIterations = new LinkedHashMap <>();
138+
136139 List <VariableModel > variablesToVerify = new ArrayList <>();
137140
138141 //Sort from more priority to less
@@ -141,10 +144,7 @@ private static void collectedVariablesManagement(List<SurveyUnitModel> srcSurvey
141144 //Get more priority variables to verify
142145 for (SurveyUnitModel srcSurveyUnitModel : sortedSurveyUnitModels ){
143146 for (VariableModel collectedVariable : srcSurveyUnitModel .getCollectedVariables ()){
144- if (!variableNames .contains (collectedVariable .varId ())){
145- variableNames .add (collectedVariable .varId ());
146- variablesToVerify .add (collectedVariable );
147- }
147+ addIteration (collectedVariable , variableIterations , variablesToVerify );
148148 }
149149 }
150150
@@ -164,6 +164,27 @@ private static void collectedVariablesManagement(List<SurveyUnitModel> srcSurvey
164164 }
165165 }
166166
167+ private static void addIteration (VariableModel variableToCheck , Map <String , List <Integer >> variableIterations , List <VariableModel > variablesToVerify ) {
168+ String varIdToCheck = variableToCheck .varId ();
169+ Integer iterationToCheck = variableToCheck .iteration ();
170+
171+ if (!variableIterations .containsKey (varIdToCheck )
172+ || !variableIterations .get (varIdToCheck ).contains (iterationToCheck )){
173+ List <Integer > iterations = variableIterations .containsKey (varIdToCheck ) ?
174+ variableIterations .get (varIdToCheck )
175+ : new ArrayList <>();
176+ if (!iterations .contains (iterationToCheck )){
177+ iterations .add (iterationToCheck );
178+ }
179+ variableIterations .put (
180+ varIdToCheck ,
181+ iterations
182+ );
183+
184+ variablesToVerify .add (variableToCheck );
185+ }
186+ }
187+
167188 private static VariableModel verifyVariable (VariableModel variableModel , fr .insee .bpm .metadata .model .Variable variableDefinition ) {
168189 if (isParseError (variableModel .value (), variableDefinition .getType ())){
169190 return VariableModel .builder ()
@@ -178,7 +199,7 @@ private static VariableModel verifyVariable(VariableModel variableModel, fr.inse
178199 }
179200
180201 private static void externalVariablesManagement (List <SurveyUnitModel > srcSuModels , VariablesMap variablesMap , List <VariableModel > correctedExternalVariables ) {
181- //COLLECTED only
202+ //External variables are in COLLECTED documents only
182203 Optional <SurveyUnitModel > surveyUnitModelOptional = srcSuModels .stream ().filter (
183204 surveyUnitModel -> surveyUnitModel .getState ().equals (DataState .COLLECTED )
184205 ).findFirst ();
0 commit comments