-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLunaticXmlAdapter.java
More file actions
170 lines (149 loc) · 7.44 KB
/
Copy pathLunaticXmlAdapter.java
File metadata and controls
170 lines (149 loc) · 7.44 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
package fr.insee.genesis.controller.adapter;
import fr.insee.bpm.metadata.model.VariablesMap;
import fr.insee.genesis.controller.sources.xml.LunaticXmlCollectedData;
import fr.insee.genesis.controller.sources.xml.LunaticXmlOtherData;
import fr.insee.genesis.controller.sources.xml.LunaticXmlSurveyUnit;
import fr.insee.genesis.controller.sources.xml.ValueType;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.domain.utils.GroupUtils;
import fr.insee.genesis.domain.model.surveyunit.DataState;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.domain.model.surveyunit.VariableModel;
import lombok.experimental.UtilityClass;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@UtilityClass
public class LunaticXmlAdapter {
/**
* Convert a Lunatic XML survey unit into a genesis survey unit model
* @param su Lunatic XML survey unit to convert
* @param variablesMap variable definitions (used for loops)
* @param campaignId survey id
* @return Genesis SurveyUnitModels for each data state
*/
public static List<SurveyUnitModel> convert(LunaticXmlSurveyUnit su, VariablesMap variablesMap, String campaignId, Mode mode){
//Get COLLECTED Data and external variables
List<SurveyUnitModel> surveyUnitModelList = new ArrayList<>();
SurveyUnitModel surveyUnitModel = getStateDataFromSurveyUnit(su, variablesMap, DataState.COLLECTED, mode);
getExternalDataFromSurveyUnit(su, surveyUnitModel, variablesMap);
surveyUnitModelList.add(surveyUnitModel);
//Get data from other states
SurveyUnitModel editedSurveyUnitModel = getStateDataFromSurveyUnit(su, variablesMap, DataState.EDITED,mode);
if(editedSurveyUnitModel != null){
surveyUnitModelList.add(editedSurveyUnitModel);
}
SurveyUnitModel inputedSurveyUnitModel = getStateDataFromSurveyUnit(su, variablesMap, DataState.INPUTED,mode);
if(inputedSurveyUnitModel != null){
surveyUnitModelList.add(inputedSurveyUnitModel);
}
SurveyUnitModel forcedSurveyUnitModel = getStateDataFromSurveyUnit(su, variablesMap, DataState.FORCED,mode);
if(forcedSurveyUnitModel != null){
surveyUnitModelList.add(forcedSurveyUnitModel);
}
SurveyUnitModel previousSurveyUnitModel = getStateDataFromSurveyUnit(su, variablesMap, DataState.PREVIOUS,mode);
if(previousSurveyUnitModel != null){
surveyUnitModelList.add(previousSurveyUnitModel);
}
return surveyUnitModelList;
}
/**
* Collects data from XML survey unit depending on the data state
* @param su source XML Survey Unit
* @param variablesMap variable definitions (used for loops)
* @param dataState state of the SurveyUnitModel to generate
* @return SurveyUnitModel with a specific state
*/
private static SurveyUnitModel getStateDataFromSurveyUnit(LunaticXmlSurveyUnit su, VariablesMap variablesMap, DataState dataState, Mode mode) {
SurveyUnitModel surveyUnitModel = SurveyUnitModel.builder()
.collectionInstrumentId(su.getQuestionnaireModelId().toUpperCase())
.interrogationId(su.getId())
.state(dataState)
.mode(mode)
.recordDate(LocalDateTime.now())
.fileDate(su.getFileDate())
.build();
return getCollectedDataFromSurveyUnit(su, surveyUnitModel, variablesMap, dataState);
}
/**
* Gets data from a specific state and put it into Model's data
* @param su XML survey unit to extract from
* @param surveyUnitModel Model to aliment
* @param variablesMap variables definitions (used for loops)
* @param dataState data state from XML
* @return the SurveyUnitModel containing data, null if no data and not COLLECTED
*/
private static SurveyUnitModel getCollectedDataFromSurveyUnit(LunaticXmlSurveyUnit su, SurveyUnitModel surveyUnitModel, VariablesMap variablesMap, DataState dataState) {
List<VariableModel> variableModels = new ArrayList<>();
int dataCount = 0;
for (LunaticXmlCollectedData lunaticXmlCollectedData : su.getData().getCollected()){
List<ValueType> valueTypeList;
switch (dataState){
case COLLECTED:
valueTypeList = lunaticXmlCollectedData.getCollected();
break;
case EDITED :
valueTypeList = lunaticXmlCollectedData.getEdited();
break;
case FORCED :
valueTypeList = lunaticXmlCollectedData.getForced();
break;
case INPUTED:
valueTypeList = lunaticXmlCollectedData.getInputed();
break;
case PREVIOUS:
valueTypeList = lunaticXmlCollectedData.getPrevious();
break;
default:
return null;
}
if(valueTypeList == null) {
continue; //Go to next data
}
for (int i = 1; i <= valueTypeList.size(); i++) {
if (valueTypeList.get(i-1).getValue()!=null) {
variableModels.add(VariableModel.builder()
.varId(lunaticXmlCollectedData.getVariableName())
.value(valueTypeList.get(i-1).getValue())
.scope(GroupUtils.getGroupName(lunaticXmlCollectedData.getVariableName(), variablesMap))
.parentId(GroupUtils.getParentGroupName(lunaticXmlCollectedData.getVariableName(), variablesMap))
.iteration(i)
.build());
dataCount++;
}
}
}
surveyUnitModel.setCollectedVariables(variableModels);
//Return null if no data and not COLLECTED
if(dataCount > 0 || dataState.equals(DataState.COLLECTED)){
return surveyUnitModel;
}
return null;
}
/**
* Extract external data from XML survey unit and put it into Model
* @param su XML survey unit
* @param surveyUnitModel Model to aliment
*/
private static void getExternalDataFromSurveyUnit(LunaticXmlSurveyUnit su, SurveyUnitModel surveyUnitModel, VariablesMap variablesMap) {
List<VariableModel> variableModels = new ArrayList<>();
for(LunaticXmlOtherData lunaticXmlExternalData : su.getData().getExternal()){
List<ValueType> valueTypeList = lunaticXmlExternalData.getValues();
if(valueTypeList == null) {
continue; //Go to next data
}
for (int i = 1; i <= valueTypeList.size(); i++) {
if (valueTypeList.get(i-1).getValue()!=null) {
variableModels.add(VariableModel.builder()
.varId(lunaticXmlExternalData.getVariableName())
.value(valueTypeList.get(i-1).getValue())
.scope(GroupUtils.getGroupName(lunaticXmlExternalData.getVariableName(), variablesMap))
.iteration(i)
.parentId(GroupUtils.getParentGroupName(lunaticXmlExternalData.getVariableName(), variablesMap))
.build());
}
}
}
surveyUnitModel.setExternalVariables(variableModels);
}
}