-
Notifications
You must be signed in to change notification settings - Fork 0
/
birstUtils.js
232 lines (195 loc) · 6.9 KB
/
birstUtils.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
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
/* BIRST UTILITY FUNCTIONS */
var birstUtils = {
// FUNCTION: escapeParam
// Replace single quotes in a string
escapeParam : function (str){
str = str.replace(/'/g, "\\'");
return str;
},
// FUNCTION: indexOf
// Lookup a particular object key in an array of objects
indexOf : function (key, array){
for (var i = 0; i < array.length; i++) {
if (key === array[i].key) {
return i; // returns index of matching object
}
}
return -1; // no match found
},
// FUNCTION: textBetween
// Returns the text from a string between the specified start and end characters
textBetween : function (textString, startChar, endChar){
var startPos = textString.indexOf(startChar) + 1;
var endPos = textString.indexOf(endChar,startPos);
return textString.substring(startPos,endPos);
},
// FUNCTION: getName
// Returns the plain text name of either a measure, dimension, or saved expression
getName : function (nameString){
if (nameString.indexOf("Saved") >= 0){
// it's a saved expression
// get text between ' and '
return this.textBetween(nameString,"'","'");
}else{
// it's a dimension or measures
if (nameString.indexOf(":") >= 0){
// it's a measure
// check for second :
var firstPos = nameString.indexOf(":");
var secondPos = nameString.indexOf(":",firstPos + 1);
if (secondPos >= 0){
// second :
return this.textBetween(nameString.substr(firstPos + 1),":","]");
}
// get text between : and ]
return this.textBetween(nameString,":","]");
}else{
// it's a dimension
// get text between . and ]
return this.textBetween(nameString,".","]");
}
}
},
// FUNCTION: prepareFilters
// Validates and prepares the filter state
prepareFilters : function(state, apply) {
if (apply == null) {
console.debug("apply is null. All filters valid.");
return state;
}
// apply is empty
if (apply.length == 0) {
console.debug("apply is empty. All filters valid.");
return state;
}
var idx = this.indexOf("filters", apply)
if (idx >= 0) {
var value = apply[idx].value;
console.debug("value:" + value);
}
switch (value) {
case "ALL":
console.log("apply is ALL. All filters valid.");
return state;
break;
case "NONE":
console.debug("apply is NONE. No filters valid.");
state.length = 0;
return state;
break;
case "VALID":
for (var f = 0; f < state.length; f++) {
console.debug("STATE[" + f + "]");
console.debug(state[f]);
console.debug(state[f].key);
var test = this.indexOf(state[f].key, apply);
console.debug("test: " + test);
if (test < 0) {
// remove filter from state
state.splice(f, 1);
console.debug("Filter not listed as VALID. Removed.");
}
}
return state;
break;
case "INVALID":
for (var f = 0; f < state.length; f++) {
console.debug(state[f]);
console.debug(state[f].key);
var test = this.indexOf(state[f].key, apply);
if (test >= 0) {
// remove filter from state
state.splice(f, 1);
console.debug("Filter listed as INVALID. Removed.");
}
}
return state;
break;
default:
// apply is misconfigured, then ALL assumed
console.debug("apply was misconfigured, leaving filter state as is");
return state;
}
},
// FUNCTION: addDefaultFilters
// Adds default filters to the filter state
addDefaultFilters : function(state, defaults) {
for (var d = 0; d < defaults.length; d++) {
var filt = defaults[d];
// If filter does NOT exist in the current filter state
if (indexOf(filt.key, state) < 0) {
// Add filter to state
console.debug("F" + d + ": Default filter not present. Add to filter state.")
state.push(filt);
} else {
console.debug("F" + d + ": Default filter already exists. Ignore default filter.")
}
}
return state;
},
// FUNCTION: createWhere
// Creates WHERE clause based on filter state
createWhere : function (state) {
var clause = "";
// Check to see if there are current filters
if (state.length != 0) {
clause += " WHERE ("; // Start WHERE
// Get the filters from the filter state array and append them to the base query
for (var i = 0; i < state.length; i++) {
var filter = state[i];
// Split multiple filter values into an array of filter values
var values = String(filter.value).split(",");
// IF NOT the first or only param, add an AND
if (i != 0) {
clause += " AND ";
}
// Start Filter
clause += "(";
// Loop through filter value array and append filter params to the query
for (var q = 0; q < values.length; q++) {
// For each filter value, set the key and the value
if (q === values.length - 1) {
// Single or final filter value
clause += "[" + filter.key + "]" + filter.operator + "'" + this.escapeParam(values[q]) + "'";
} else {
// OR for multiple filter values
clause += "[" + filter.key + "]" + filter.operator + "'" + this.escapeParam(values[q]) + "' OR ";
}
}
// Close Filter
clause += ")";
}
// Close WHERE
clause += ")";
console.log("Clause: " + clause);
} else {
console.debug("Filter state empty. No clause created.")
}
// Return the finalized WHERE clause
return clause;
},
// FUNCTION: runQuery
// Sets filters, creates and runs the query
runQuery : function() {
console.log("runQuery");
// Test for applyFilters definition
if (applyFilters == null) {
console.log("Valid filters not set!");
}
// Test for defaultFilters definition
if (defaultFilters == null) {
console.log("Default filters not set!");
}
// Prepare filter state for WHERE clause construction
var state = this.prepareFilters(filterState, applyFilters);
state = this.addDefaultFilters(state, defaultFilters);
// Create WHERE clause
var clause = this.createWhere(state);
// Append final query params
var query = bql + clause + bqlSuffix;
// Verify the final query
console.log(query);
// Execute the query
BirstConfig.getData(query);
}
};