0af843be
Renato De Donato
filters + alasql
|
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
|
/*
@license
The MIT License (MIT)
Copyright (c) 2015 Dipartimento di Informatica - Universit� di Salerno - Italy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* Developed by :
* ROUTE-TO-PA Project - grant No 645860. - www.routetopa.eu
*
*/
var AjaxJsonAlasqlBehavior = {
properties: {
/**
* It contains the json data from async xhr call returned from core-ajax core component
*
* @attribute json_results
* @type object
* @default 'null'.
*/
json_results: {
type: Object,
value: {}
}
|
98d9d8a5
Renato De Donato
filters+groupby
|
48
|
|
0af843be
Renato De Donato
filters + alasql
|
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
|
},
/**
* Make an AJAX call to the dataset URL
*
* @method requestData
*/
requestData: function(){
var comp = this;
$.ajax({
url: this._component.dataUrl,
dataType: "json",
success: function(e){
comp.handleResponse(e);
}
});
},
/**
* Called when core-ajax component receive the json data from called url.
*
* @method handleResponse
*/
handleResponse: function(e) {
this.properties.json_results.value = e;
this.runWorkcycle();
},
|
0af843be
Renato De Donato
filters + alasql
|
78
79
80
81
82
83
84
85
|
/**
* selectData built a JSONPATH query based on the user selected fields then extract data from the JSON response.
* This method built an objects <name, data> for every user selected field and push it into the data array.
*
* @method selectData
*/
selectData : function() {
|
0af843be
Renato De Donato
filters + alasql
|
86
|
this._component.fields = JSON.parse(this._component.fields);
|
68ee970d
Renato De Donato
jsdatachecker
|
87
|
var provider = this._getProvider(this._component.fields[0]);
|
0af843be
Renato De Donato
filters + alasql
|
88
|
|
98d9d8a5
Renato De Donato
filters+groupby
|
89
90
91
92
|
var filters = JSON.parse(this._component.getAttribute("filters"));
var aggregators = JSON.parse(this._component.getAttribute("aggregators"));
var orders = JSON.parse(this._component.getAttribute("orders"));
|
9fb4369c
Renato De Donato
my space on add p...
|
93
|
//preview my space
|
0dafdf4f
Renato De Donato
my space bug fix
|
94
|
if(filters && filters[0] && filters[0].constructor == Array){
|
9fb4369c
Renato De Donato
my space on add p...
|
95
96
97
98
99
|
filters = filters[0];
aggregators = aggregators[0];
orders = orders[0];
}
|
0314f487
Renato De Donato
filters opendatasoft
|
100
101
102
103
|
var fields = [];
for (var i=0; i < this._component.fields.length; i++)
fields.push(this._fieldName(this._component.fields[i], provider));
|
68ee970d
Renato De Donato
jsdatachecker
|
104
105
106
107
108
109
110
|
//jsdatachecker
var _converter = new DataTypeConverter();
var path2 = this._arrayPath(provider);
var processingResult = _converter.inferJsonDataType(this.properties.json_results.value, path2);
var processingResults = _converter.convert(processingResult);
var jsonData = [processingResults.dataset];
|
98d9d8a5
Renato De Donato
filters+groupby
|
111
112
113
|
//WHERE
var where = "";
if(filters && filters.length) {
|
0314f487
Renato De Donato
filters opendatasoft
|
114
115
116
117
|
for (var i=0; i < filters.length; i++)
filters[i]["field"] = this._fieldName(filters[i]["field"], provider);
|
98d9d8a5
Renato De Donato
filters+groupby
|
118
119
120
|
where = "WHERE ";
for (var i=0; i < filters.length; i++) {
if(filters[i]["operation"] == "contains")
|
0314f487
Renato De Donato
filters opendatasoft
|
121
|
where += filters[i]["field"] + " like '%" + filters[i]["value"] + "%' AND ";
|
7464051f
Renato De Donato
not contains
|
122
123
|
else if(filters[i]["operation"] == "not contains")
where += filters[i]["field"] + " not like '%" + filters[i]["value"] + "%' AND ";
|
98d9d8a5
Renato De Donato
filters+groupby
|
124
|
else if(filters[i]["operation"] == "start")
|
0314f487
Renato De Donato
filters opendatasoft
|
125
|
where += filters[i]["field"] + " like '" + filters[i]["value"] + "%' AND ";
|
98d9d8a5
Renato De Donato
filters+groupby
|
126
|
else if(filters[i]["operation"] == "ends")
|
0314f487
Renato De Donato
filters opendatasoft
|
127
|
where += filters[i]["field"] + " like '%" + filters[i]["value"] + "' AND ";
|
98d9d8a5
Renato De Donato
filters+groupby
|
128
|
else
|
b4195555
Renato De Donato
filter bug fix
|
129
|
where += filters[i]["field"] + " " + filters[i]["operation"] + " " + filters[i]["value"] + " AND ";
|
98d9d8a5
Renato De Donato
filters+groupby
|
130
131
132
133
|
}
where = where.slice(0, -5);
}
|
98d9d8a5
Renato De Donato
filters+groupby
|
134
135
136
|
//ORDER BY
var orderBy = "";
if(orders && orders.length) {
|
0314f487
Renato De Donato
filters opendatasoft
|
137
138
|
for (var i=0; i < orders.length; i++)
|
68ee970d
Renato De Donato
jsdatachecker
|
139
|
orders[i]["field"] = this._fieldName(orders[i]["field"], "");
|
0314f487
Renato De Donato
filters opendatasoft
|
140
|
|
98d9d8a5
Renato De Donato
filters+groupby
|
141
142
|
orderBy = "ORDER BY ";
for (var i = 0; i < orders.length; i++)
|
0314f487
Renato De Donato
filters opendatasoft
|
143
|
orderBy += orders[i]["field"] + " " + orders[i]["operation"] + ", ";
|
98d9d8a5
Renato De Donato
filters+groupby
|
144
145
146
|
orderBy = orderBy.slice(0, -2);
}
|
cfc31d1e
Renato De Donato
alasql cast
|
147
|
//SELECT
|
68ee970d
Renato De Donato
jsdatachecker
|
148
|
var pureSelect = "SELECT ";
|
98d9d8a5
Renato De Donato
filters+groupby
|
149
|
for (var i = 0; i < fields.length; i++)
|
68ee970d
Renato De Donato
jsdatachecker
|
150
151
|
pureSelect += fields[i] + " as " + this._fieldName(this._component.fields[i], "") + ", ";
pureSelect = pureSelect.slice(0, -2);
|
cfc31d1e
Renato De Donato
alasql cast
|
152
|
|
68ee970d
Renato De Donato
jsdatachecker
|
153
|
//GROUP BY
|
98d9d8a5
Renato De Donato
filters+groupby
|
154
|
var groupBy = "";
|
fb05b400
Renato De Donato
controllet 2.0, n...
|
155
|
var select = "";
|
98d9d8a5
Renato De Donato
filters+groupby
|
156
|
if(aggregators && aggregators.length) {
|
0314f487
Renato De Donato
filters opendatasoft
|
157
158
|
for (var i=0; i < aggregators.length; i++)
|
68ee970d
Renato De Donato
jsdatachecker
|
159
|
aggregators[i]["field"] = this._fieldName(aggregators[i]["field"], "");
|
0314f487
Renato De Donato
filters opendatasoft
|
160
161
|
groupBy = "GROUP BY " + aggregators[0]["field"];
|
fb05b400
Renato De Donato
controllet 2.0, n...
|
162
|
select = "SELECT " + aggregators[0]["field"];
|
98d9d8a5
Renato De Donato
filters+groupby
|
163
|
for (var i = 1; i < aggregators.length; i++)
|
0314f487
Renato De Donato
filters opendatasoft
|
164
|
select += ", " + aggregators[i]["operation"] + "(" + aggregators[i]["field"] + ") as " + aggregators[i]["field"];
|
fb05b400
Renato De Donato
controllet 2.0, n...
|
165
166
167
168
169
170
171
|
if(aggregators[1] && aggregators[1]["operation"] == "GROUP BY") {
groupBy = "GROUP BY " + aggregators[0]["field"] + ", " + aggregators[1]["field"];
select = "SELECT " + aggregators[0]["field"] + ", " + aggregators[1]["field"];
for (var i = 2; i < aggregators.length; i++)
select += ", " + aggregators[i]["operation"] + "(" + aggregators[i]["field"] + ") as " + aggregators[i]["field"];
}
|
98d9d8a5
Renato De Donato
filters+groupby
|
172
173
174
|
}
//QUERY
|
68ee970d
Renato De Donato
jsdatachecker
|
175
|
var path = this._path(this._component.fields[0], provider);
|
fb05b400
Renato De Donato
controllet 2.0, n...
|
176
|
var query;
|
68ee970d
Renato De Donato
jsdatachecker
|
177
|
|
fb05b400
Renato De Donato
controllet 2.0, n...
|
178
179
180
|
query = "SELECT "+ path +" FROM ?";
//console.log(query);
var res = alasql(query, [jsonData]);
|
98d9d8a5
Renato De Donato
filters+groupby
|
181
182
183
|
var records = res[0][path];
|
fb05b400
Renato De Donato
controllet 2.0, n...
|
184
185
186
|
query = pureSelect + " FROM ? " + where + " " + orderBy;
//console.log(query);
var obj = alasql(query, [records]);
|
98d9d8a5
Renato De Donato
filters+groupby
|
187
|
|
0314f487
Renato De Donato
filters opendatasoft
|
188
|
if (groupBy != "") {
|
fb05b400
Renato De Donato
controllet 2.0, n...
|
189
190
191
|
query = select + " FROM ? " + groupBy + " " + orderBy;
//console.log(query);
obj = alasql(query, [obj]);
|
0314f487
Renato De Donato
filters opendatasoft
|
192
193
|
}
|
0314f487
Renato De Donato
filters opendatasoft
|
194
195
196
197
|
//PUSH DATA
if(!obj || obj.length == 0)
this.data = []
else
|
68ee970d
Renato De Donato
jsdatachecker
|
198
|
this._pushData(obj, fields);
|
98d9d8a5
Renato De Donato
filters+groupby
|
199
200
|
this._deleteWaitImage();
|
0af843be
Renato De Donato
filters + alasql
|
201
202
|
},
|
68ee970d
Renato De Donato
jsdatachecker
|
203
|
_pushData : function(obj, keys) {
|
98d9d8a5
Renato De Donato
filters+groupby
|
204
|
this.data = [];
|
0af843be
Renato De Donato
filters + alasql
|
205
|
|
68ee970d
Renato De Donato
jsdatachecker
|
206
207
208
209
|
if (typeof keys == 'undefined')
keys = Object.keys(obj[0]);
for (var key in keys){
|
0af843be
Renato De Donato
filters + alasql
|
210
|
|
68ee970d
Renato De Donato
jsdatachecker
|
211
|
var name = keys[key].replace(/(\[|\]|fields->)/g, "");
|
0af843be
Renato De Donato
filters + alasql
|
212
|
var data = [];
|
0af843be
Renato De Donato
filters + alasql
|
213
214
|
for (var i in obj) {
|
04097cb4
Renato De Donato
truncate 2 decimal
|
215
216
217
218
219
220
|
var v = obj[i][name];
if(!isNaN(v) && v % 1 != 0)
v = Math.floor(v * 100) / 100;
data.push(v);
//data.push(obj[i][name]);
|
0af843be
Renato De Donato
filters + alasql
|
221
222
|
}
|
0af843be
Renato De Donato
filters + alasql
|
223
224
225
226
227
228
229
|
this.data.push({
name: name,
data: data
});
}
},
|
0314f487
Renato De Donato
filters opendatasoft
|
230
231
232
233
234
235
236
|
_getProvider : function(field) {
if(field.indexOf("result,records") > -1)
return "ckan";
else if(field.indexOf("records,fields") > -1)
return "openDataSoft";
else
return "provider";
|
98d9d8a5
Renato De Donato
filters+groupby
|
237
238
|
},
|
0314f487
Renato De Donato
filters opendatasoft
|
239
240
241
242
243
|
_fieldName : function(field, provider) {
if(provider.indexOf("ckan") > -1) {
return "[" + field.substring(field.lastIndexOf(",") + 1, field.length) + "]";
}
else if(provider.indexOf("openDataSoft") > -1) {
|
68ee970d
Renato De Donato
jsdatachecker
|
244
|
return "fields->[" + field.substring(field.lastIndexOf(",") + 1, field.length)+ "]";
|
0314f487
Renato De Donato
filters opendatasoft
|
245
246
|
}
else {
|
68ee970d
Renato De Donato
jsdatachecker
|
247
|
return "[" + field.substring(field.lastIndexOf(",") + 1, field.length) + "]";
|
0314f487
Renato De Donato
filters opendatasoft
|
248
249
250
251
252
253
254
255
256
257
258
259
260
|
}
},
_path : function(field, provider) {
if(provider.indexOf("ckan") > -1) {
return "result->records"
}
else if(provider.indexOf("openDataSoft") > -1) {
return "records";
}
else {
return field.substring(0, field.lastIndexOf(",")).replace(",", "->");
}
|
98d9d8a5
Renato De Donato
filters+groupby
|
261
262
|
},
|
68ee970d
Renato De Donato
jsdatachecker
|
263
264
265
266
267
268
269
270
271
272
273
274
|
_arrayPath : function(provider) {
if(provider.indexOf("ckan") > -1) {
return ["result", "records", "*"];
}
else if(provider.indexOf("openDataSoft") > -1) {
return ["records", "fields", "*"];
}
else {
return ["*"];
}
},
|
0af843be
Renato De Donato
filters + alasql
|
275
276
277
|
/**
* Delete a image after loading a datalet
*/
|
98d9d8a5
Renato De Donato
filters+groupby
|
278
|
_deleteWaitImage : function() {
|
0af843be
Renato De Donato
filters + alasql
|
279
280
|
$("img[src$='spin.svg']").remove();
}
|
98d9d8a5
Renato De Donato
filters+groupby
|
281
|
|
0af843be
Renato De Donato
filters + alasql
|
282
|
};
|