-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataExtractor.js
127 lines (90 loc) · 3.82 KB
/
dataExtractor.js
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
function logErrorOnUndefined(variable, logMsg) {
if (typeof variable === "undefined") {
logError(logMsg);
return false;
} else {
return true;
}
}
function translateAnswerElseLog(answersConfig, answerValue, propertyName){
var answerId = answersConfig[answerValue];
if(typeof answerId === "undefined"){
if (answerValue == '') { return null; }
else { logError(['Answer can not be translated:', propertyName, answerValue]); return undefined; }
}
else { return answerId; }
}
function getInfoFromIndexes(dataConfig, indexArray, propertyName){
var dataArray = [];
for(var i = 0; i < indexArray.length; ++i ){
var currIndex = indexArray[i];
var dataForIndex = getInfoFromIndex(dataConfig, currIndex, propertyName);
if(dataForIndex == null) {continue;}
else {dataArray.push(dataForIndex);}
}
return dataArray;
}
function getInfoFromIndex(dataConfig, index, propertyName){
var dataForIndex = dataConfig[index];
if(!logErrorOnUndefined(dataForIndex, ['Can not find data for:', propertyName, index ])){return null;}
return dataForIndex;
}
function getFormData(formSubmitObj, translationConfig) {
var formData = {};
var formAnswers = formSubmitObj.namedValues;
for (var propertyName in translationConfig) {
formData[propertyName] = { originalValue: null, value: null };
var propertyData = formData[propertyName];
var translationInfo = translationConfig[propertyName];
if(translationInfo.hasOwnProperty('staticValue')){
var staticValue = translationInfo['staticValue'];
propertyData.value = staticValue;
propertyData.originalValue = staticValue;
continue;
}
var formFieldName = translationInfo.title;
if(!logErrorOnUndefined(formFieldName, ['Missing title in translation config:', propertyName])){continue;}
var answerArrInForm = formAnswers[formFieldName];
if(!logErrorOnUndefined(answerArrInForm, ['Missing answer in form:', propertyName, formFieldName])){continue;}
answerArrInForm = answerArrInForm.filter(function(item) {return (item !== '');});
//Answers are stored in first element of an array (always)
var answerInForm = (answerArrInForm.length > 0) ? answerArrInForm[0] : '';
propertyData.originalValue = answerInForm;
if(translationInfo.hasOwnProperty('answers') &&
(translationInfo.hasOwnProperty('multiple') && translationInfo.multiple == true)) {
var splitedAnswers = answerInForm.split(', ');
var translatedAnswers = [];
//Translates batch answers to batch ids
var answersTranslation = translationInfo.answers;
for (var i = 0; i < splitedAnswers.length; ++i) {
var individualAnswer = splitedAnswers[i];
var answerId = translateAnswerElseLog(translationInfo.answers, individualAnswer, propertyName);
if(answerId == undefined) {continue;}
translatedAnswers.push(answerId);
}
propertyData.value = translatedAnswers;
}
else if(translationInfo.hasOwnProperty('answers')){
var answerId = translateAnswerElseLog(translationInfo.answers, answerInForm, propertyName);
if(answerId == undefined) {continue;}
propertyData.value = answerId;
}
else {
propertyData.value = answerInForm;
}
}
return formData;
}
///
// Old functions that are not used anymore but might be usesful at some point in the future
///
function getAnswerFromSubmitObj(formSubmitObj, questionTranslationConfig){
var titleTranslation = questionTranslationConfig['Title'];
var answerFromFormSubmit = formSubmitObj.namedValues[titleTranslation][0];
return answerFromFormSubmit;
}
function getAnswerIdFromSubmitObj(formSubmitObj, questionTranslationConfig){
var answerFromSubmit = getAnswerFromSubmitObj(formSubmitObj, questionTranslationConfig);
var answerId = questionTranslationConfig['Answers'][answerFromSubmit];
return answerId;
}