-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataVerifier.java
More file actions
282 lines (255 loc) · 12.6 KB
/
Copy pathDataVerifier.java
File metadata and controls
282 lines (255 loc) · 12.6 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
package fr.insee.genesis.domain.utils;
import fr.insee.bpm.metadata.model.VariableType;
import fr.insee.bpm.metadata.model.VariablesMap;
import fr.insee.genesis.Constants;
import fr.insee.genesis.domain.model.surveyunit.DataState;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.domain.model.surveyunit.VariableModel;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
@UtilityClass
public class DataVerifier {
//DataStates priority
static final EnumMap<DataState,Integer> dataStatesPriority = new EnumMap<>(DataState.class);
static {
dataStatesPriority.put(DataState.INPUTED, 1);
dataStatesPriority.put(DataState.EDITED, 2);
dataStatesPriority.put(DataState.FORCED, 3);
dataStatesPriority.put(DataState.COLLECTED, 4);
dataStatesPriority.put(DataState.PREVIOUS, 5);
}
/**
* Verify data format in all surveyUnits models
* If there is at least 1 incorrect variable for a survey unit, a new SurveyUnitModel is created with "FORCED"
* status
* The new surveyUnits are added to the list
* @param surveyUnitModelsList list of SurveyUnitModels to verify
* @param variablesMap VariablesMap containing definitions of each variable
*/
public static void verifySurveyUnits(List<SurveyUnitModel> surveyUnitModelsList, VariablesMap variablesMap){
List<SurveyUnitModel> surveyUnitModelsListFormatted = new ArrayList<>(); // Created FORCED SU models
for(String interrogationId : getInterrogationIds(surveyUnitModelsList)) { // For each id of the list
List<SurveyUnitModel> srcSurveyUnitModelsOfInterrogationId = surveyUnitModelsList.stream().filter(element -> element.getInterrogationId().equals(interrogationId)).toList();
List<VariableModel> correctedCollectedVariables = new ArrayList<>();
List<VariableModel> correctedExternalVariables = new ArrayList<>();
//Get corrected variables
collectedVariablesManagement(srcSurveyUnitModelsOfInterrogationId, variablesMap, correctedCollectedVariables);
externalVariablesManagement(srcSurveyUnitModelsOfInterrogationId, variablesMap, correctedExternalVariables);
//Create FORCED if any corrected variable
if(!correctedCollectedVariables.isEmpty() || !correctedExternalVariables.isEmpty()){
SurveyUnitModel newFormattedSurveyUnitModel = createFormattedSurveyUnitModel(surveyUnitModelsList, interrogationId, correctedCollectedVariables, correctedExternalVariables);
surveyUnitModelsListFormatted.add(newFormattedSurveyUnitModel);
}
}
surveyUnitModelsList.addAll(surveyUnitModelsListFormatted);
}
private static SurveyUnitModel createFormattedSurveyUnitModel(
List<SurveyUnitModel> surveyUnitModelsList,
String interrogationId,
List<VariableModel> correctedCollectedVariables,
List<VariableModel> correctedExternalVariables
) {
SurveyUnitModel sampleSurveyUnitModel = surveyUnitModelsList.stream().filter(element -> element.getInterrogationId().equals(interrogationId)).toList().getFirst();
SurveyUnitModel newFormattedSurveyUnitModel = SurveyUnitModel.builder()
.collectionInstrumentId(sampleSurveyUnitModel.getCollectionInstrumentId())
.interrogationId(interrogationId)
.usualSurveyUnitId(sampleSurveyUnitModel.getUsualSurveyUnitId())
.state(DataState.FORMATTED)
.mode(sampleSurveyUnitModel.getMode())
.recordDate(Instant.now().plusSeconds(1)) // Add 1 second to avoid same recordDate as COLLECTED
.rawRecordDate(sampleSurveyUnitModel.getRawRecordDate())
.collectedVariables(new ArrayList<>())
.externalVariables(new ArrayList<>())
.build();
for(VariableModel correctedCollectedVariable : correctedCollectedVariables){
newFormattedSurveyUnitModel.getCollectedVariables().add(
VariableModel.builder()
.varId(correctedCollectedVariable.varId())
.value(correctedCollectedVariable.value())
.scope(correctedCollectedVariable.scope())
.iteration(correctedCollectedVariable.iteration())
.parentId(correctedCollectedVariable.parentId())
.build()
);
}
for(VariableModel correctedExternalVariable : correctedExternalVariables){
newFormattedSurveyUnitModel.getExternalVariables().add(
VariableModel.builder()
.varId(correctedExternalVariable.varId())
.value(correctedExternalVariable.value())
.scope(correctedExternalVariable.scope())
.iteration(correctedExternalVariable.iteration())
.parentId(correctedExternalVariable.parentId())
.build()
);
}
return newFormattedSurveyUnitModel;
}
/**
* Fetch individual interrogationIds of variable from the list
* @param surveyUnitModelsList source list
* @return a set of interrogationIds
*/
private static Set<String> getInterrogationIds(List<SurveyUnitModel> surveyUnitModelsList) {
Set<String> interrogationIds = new HashSet<>();
for(SurveyUnitModel surveyUnitModel : surveyUnitModelsList){
interrogationIds.add(surveyUnitModel.getInterrogationId());
}
return interrogationIds;
}
/**
* Adds the collected variables for the FORCED document
* @param srcSurveyUnitModelsOfInterrogationId source Survey Unit documents associated with InterrogationId
* @param variablesMap variables definitions
* @param correctedCollectedVariables FORCED document variables
*/
private static void collectedVariablesManagement(List<SurveyUnitModel> srcSurveyUnitModelsOfInterrogationId, VariablesMap variablesMap, List<VariableModel> correctedCollectedVariables){
Map<String,List<Integer>> variableIterations = new LinkedHashMap<>();
List<VariableStateTuple> variablesToVerify = new ArrayList<>();
//Sort from more priority to less
List<SurveyUnitModel> sortedSurveyUnitModels = srcSurveyUnitModelsOfInterrogationId.stream().sorted(Comparator.comparing(surveyUnitModel -> dataStatesPriority.get(surveyUnitModel.getState()))).toList();
//Get more priority variables to verify
for(SurveyUnitModel srcSurveyUnitModel : sortedSurveyUnitModels){
for(VariableModel collectedVariable : srcSurveyUnitModel.getCollectedVariables()){
addIteration(collectedVariable, variableIterations, variablesToVerify, srcSurveyUnitModel.getState());
}
}
//Verify variables
for(VariableStateTuple collectedVariableToVerify : variablesToVerify){
if(variablesMap.hasVariable(collectedVariableToVerify.variableModel().varId()))
{
VariableModel correctedCollectedVariable = verifyVariable(
collectedVariableToVerify.variableModel(),
variablesMap.getVariable(collectedVariableToVerify.variableModel().varId()),
collectedVariableToVerify.dataState()
);
if(correctedCollectedVariable != null){
correctedCollectedVariables.add(correctedCollectedVariable);
}
}
}
}
private static void addIteration(VariableModel variableToCheck,
Map<String, List<Integer>> variableIterations,
List<VariableStateTuple> variablesToVerify,
DataState dataState
) {
String varIdToCheck = variableToCheck.varId();
Integer iterationToCheck = variableToCheck.iteration();
if(!variableIterations.containsKey(varIdToCheck)
|| !variableIterations.get(varIdToCheck).contains(iterationToCheck)){
List<Integer> iterations = variableIterations.containsKey(varIdToCheck) ?
variableIterations.get(varIdToCheck)
: new ArrayList<>();
if(!iterations.contains(iterationToCheck)){
iterations.add(iterationToCheck);
}
variableIterations.put(
varIdToCheck,
iterations
);
variablesToVerify.add(new VariableStateTuple(variableToCheck, dataState));
}
}
private static VariableModel verifyVariable(
VariableModel variableModel,
fr.insee.bpm.metadata.model.Variable variableDefinition,
DataState dataState
) {
if(isParseError(variableModel.value(), variableDefinition.getType(),dataState)){
return VariableModel.builder()
.varId(variableModel.varId())
.value("")
.scope(variableModel.scope())
.iteration(variableModel.iteration())
.parentId(variableModel.parentId())
.build();
}
return null;
}
private static void externalVariablesManagement(List<SurveyUnitModel> srcSuModels, VariablesMap variablesMap, List<VariableModel> correctedExternalVariables) {
//External variables are in COLLECTED documents only
Optional<SurveyUnitModel> surveyUnitModelOptional = srcSuModels.stream().filter(
surveyUnitModel -> surveyUnitModel.getState().equals(DataState.COLLECTED)
).findFirst();
//Verify variables
if(surveyUnitModelOptional.isPresent()){
DataState state = surveyUnitModelOptional.get().getState();
for(VariableModel externalVariable: surveyUnitModelOptional.get().getExternalVariables()){
if(variablesMap.hasVariable(externalVariable.varId())) {
VariableModel correctedExternalVariable = verifyVariable(
externalVariable,
variablesMap.getVariable(externalVariable.varId()),
state
);
if (correctedExternalVariable != null) {
correctedExternalVariables.add(correctedExternalVariable);
}
}
}
}
}
/**
* Use the correct parser and try to parse
* @param value value to verify
* @param type type of the variable
* @param state state of the data where the variable is contained in
* @return true if the value is not conform to the variable type
*/
private static boolean isParseError(String value, VariableType type, DataState state){
//Allow null values
if(value == null){
return !(state.equals(DataState.EDITED)
|| state.equals(DataState.FORCED)
|| state.equals(DataState.FORMATTED)); //Return false if datastate one of those
}
switch(type){
case BOOLEAN:
if(!value.equals("true")
&&!value.equals("false")
&&!value.isEmpty()) {
return true;
}
break;
case DATE:
Pattern pattern = Pattern.compile(Constants.DATE_REGEX);
Matcher matcher = pattern.matcher(value);
if(!matcher.find()){
// We only monitor parsing date errors, so we always return false
log.debug("Can't parse date {}", value);
return false;
}
break;
case INTEGER:
try{
Long.parseLong(value);
}catch (NumberFormatException e){
return true;
}
break;
case NUMBER:
try{
Double.parseDouble(value);
}catch (NumberFormatException e){
return true;
}
break;
default:
break;
}
return false;
}
}