Blame view

datalets/base-ajax-json-alasql-datalet/static/js/AjaxJsonAlasqlBehavior.js 8.96 KB
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() {

98d9d8a5   Renato De Donato   filters+groupby
86
          var jsonData = [this.properties.json_results.value];

0af843be   Renato De Donato   filters + alasql
87
88
89
  

          this._component.fields = JSON.parse(this._component.fields);

  

0314f487   Renato De Donato   filters opendatasoft
90
          var provider = this._getProvider(this._component.fields[0]);

0af843be   Renato De Donato   filters + alasql
91
  

98d9d8a5   Renato De Donato   filters+groupby
92
93
94
95
          var filters = JSON.parse(this._component.getAttribute("filters"));

          var aggregators = JSON.parse(this._component.getAttribute("aggregators"));

          var orders = JSON.parse(this._component.getAttribute("orders"));

  

0314f487   Renato De Donato   filters opendatasoft
96
97
98
99
100
101
102
103
          var path = this._path(this._component.fields[0], provider);

  

          var fields = [];

          for (var i=0; i < this._component.fields.length; i++)

              fields.push(this._fieldName(this._component.fields[i], provider));

  

  

  

98d9d8a5   Renato De Donato   filters+groupby
104
105
106
          //WHERE

          var where = "";

          if(filters && filters.length) {

0314f487   Renato De Donato   filters opendatasoft
107
108
109
110
  

              for (var i=0; i < filters.length; i++)

                  filters[i]["field"] = this._fieldName(filters[i]["field"], provider);

  

98d9d8a5   Renato De Donato   filters+groupby
111
112
113
              where = "WHERE ";

              for (var i=0; i < filters.length; i++) {

                  if(filters[i]["operation"] == "contains")

0314f487   Renato De Donato   filters opendatasoft
114
                      where += filters[i]["field"] + " like '%" + filters[i]["value"] + "%' AND ";

98d9d8a5   Renato De Donato   filters+groupby
115
                  else if(filters[i]["operation"] == "start")

0314f487   Renato De Donato   filters opendatasoft
116
                      where += filters[i]["field"] + " like '" + filters[i]["value"] + "%' AND ";

98d9d8a5   Renato De Donato   filters+groupby
117
                  else if(filters[i]["operation"] == "ends")

0314f487   Renato De Donato   filters opendatasoft
118
                      where += filters[i]["field"] + " like '%" + filters[i]["value"] + "' AND ";

98d9d8a5   Renato De Donato   filters+groupby
119
                  else

93ba7167   Renato De Donato   filters bug fix + ln
120
                      where += filters[i]["field"] + " " + filters[i]["operation"] + " '" + filters[i]["value"] + "' AND ";

98d9d8a5   Renato De Donato   filters+groupby
121
122
123
124
              }

              where = where.slice(0, -5);

          }

  

0314f487   Renato De Donato   filters opendatasoft
125
126
          provider="";

  

98d9d8a5   Renato De Donato   filters+groupby
127
128
129
          //ORDER BY

          var orderBy = "";

          if(orders && orders.length) {

0314f487   Renato De Donato   filters opendatasoft
130
131
132
133
  

              for (var i=0; i < orders.length; i++)

                  orders[i]["field"] = this._fieldName(orders[i]["field"], provider);

  

98d9d8a5   Renato De Donato   filters+groupby
134
135
              orderBy = "ORDER BY ";

              for (var i = 0; i < orders.length; i++)

0314f487   Renato De Donato   filters opendatasoft
136
                  orderBy += orders[i]["field"] + " " + orders[i]["operation"] + ", ";

98d9d8a5   Renato De Donato   filters+groupby
137
138
139
              orderBy = orderBy.slice(0, -2);

          }

  

cfc31d1e   Renato De Donato   alasql cast
140
          //SELECT

98d9d8a5   Renato De Donato   filters+groupby
141
142
          var select = "SELECT ";

          for (var i = 0; i < fields.length; i++)

0314f487   Renato De Donato   filters opendatasoft
143
              select += fields[i] + " as " + this._fieldName(this._component.fields[i], "") + ", ";

98d9d8a5   Renato De Donato   filters+groupby
144
145
          select = select.slice(0, -2);

  

0314f487   Renato De Donato   filters opendatasoft
146
147
          var pureSelect = select;

  

cfc31d1e   Renato De Donato   alasql cast
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
          /**/

          var res = alasql("SELECT "+ path +" FROM ?", [jsonData]);

          var records = res[0][path];

          var obj = alasql(pureSelect + " FROM ?", [records]);

          //console.log(obj);

  

          var select = "SELECT ";

          for (var i = 0; i < fields.length; i++) {

              var key = Object.keys(obj[0])[i];

              var v = obj[0][key];

              if (!isNaN(v))

                  select += fields[i] + "::NUMBER as " + this._fieldName(this._component.fields[i], "") + ", ";

              else

                  select += fields[i] + " as " + this._fieldName(this._component.fields[i], "") + ", ";

          }

          select = select.slice(0, -2);

  

          var pureSelect = select;

          /**/

  

98d9d8a5   Renato De Donato   filters+groupby
168
169
170
          //GROUP BY

          var groupBy = "";

          if(aggregators && aggregators.length) {

0314f487   Renato De Donato   filters opendatasoft
171
172
173
174
175
176
  

              for (var i=0; i < aggregators.length; i++)

                  aggregators[i]["field"] = this._fieldName(aggregators[i]["field"], provider);

  

              groupBy = "GROUP BY " + aggregators[0]["field"];

              select = "SELECT "  + aggregators[0]["field"];

98d9d8a5   Renato De Donato   filters+groupby
177
              for (var i = 1; i < aggregators.length; i++)

0314f487   Renato De Donato   filters opendatasoft
178
                  select += ", " + aggregators[i]["operation"] + "(" + aggregators[i]["field"] + ") as " + aggregators[i]["field"];

98d9d8a5   Renato De Donato   filters+groupby
179
180
181
182
183
184
185
186
          }

  

          //QUERY

          console.log('SELECT '+ path +' FROM ?');

          var res = alasql("SELECT "+ path +" FROM ?", [jsonData]);

  

          var records = res[0][path];

  

0314f487   Renato De Donato   filters opendatasoft
187
188
          //console.log(select + ' FROM ? ' + where + ' ' + groupBy + ' ' + orderBy + '');

          //var obj = alasql(select + " FROM ? " + where + " " + groupBy + " " + orderBy + "", [records]);

98d9d8a5   Renato De Donato   filters+groupby
189
  

0314f487   Renato De Donato   filters opendatasoft
190
191
          console.log(pureSelect + ' FROM ? ' + where);

          var obj = alasql(pureSelect + " FROM ? " + where, [records]);

98d9d8a5   Renato De Donato   filters+groupby
192
  

0314f487   Renato De Donato   filters opendatasoft
193
194
195
196
197
198
199
200
201
202
          if (groupBy != "") {

              console.log(select + ' FROM ? ' + groupBy + ' ' + orderBy + '');

              var obj = alasql(select + " FROM ? " + groupBy + " " + orderBy + "", [obj]);

          }

  

          //PUSH DATA

          if(!obj || obj.length == 0)

              this.data = []

          else

              this._pushData(obj);

98d9d8a5   Renato De Donato   filters+groupby
203
204
  

          this._deleteWaitImage();

0af843be   Renato De Donato   filters + alasql
205
206
      },

  

98d9d8a5   Renato De Donato   filters+groupby
207
208
      _pushData : function(obj) {

          this.data = [];

0af843be   Renato De Donato   filters + alasql
209
210
211
212
213
  

          for (var key in Object.keys(obj[0])){

  

              var name = Object.keys(obj[0])[key];

              var data = [];

98d9d8a5   Renato De Donato   filters+groupby
214
              var value;

0af843be   Renato De Donato   filters + alasql
215
216
217
  

              for (var i in obj) {

                  data.push(obj[i][name]);

98d9d8a5   Renato De Donato   filters+groupby
218
219
220
221
                  //value = obj[i][name];

                  //if(!isNaN(value) && value != "")

                  //    value = parseFloat(obj[i][name]);

                  //data.push(value);

0af843be   Renato De Donato   filters + alasql
222
223
              }

  

0af843be   Renato De Donato   filters + alasql
224
225
226
227
228
229
230
              this.data.push({

                  name: name,

                  data: data

              });

          }

      },

  

0314f487   Renato De Donato   filters opendatasoft
231
232
233
234
235
236
237
      _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
238
239
      },

  

0314f487   Renato De Donato   filters opendatasoft
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
      _fieldName : function(field, provider) {

          if(provider.indexOf("ckan") > -1) {

              return "[" + field.substring(field.lastIndexOf(",") + 1, field.length) + "]";

          }

          else if(provider.indexOf("openDataSoft") > -1) {

              return "fields->["+field.substring(field.lastIndexOf(",")+1, field.length)+"]";

          }

          else {

              return "["+field.substring(field.lastIndexOf(",")+1, field.length)+"]";

          }

      },

  

      _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
262
263
      },

  

0af843be   Renato De Donato   filters + alasql
264
265
266
      /**

       * Delete a image after loading a datalet

       */

98d9d8a5   Renato De Donato   filters+groupby
267
      _deleteWaitImage : function() {

0af843be   Renato De Donato   filters + alasql
268
269
          $("img[src$='spin.svg']").remove();

      }

98d9d8a5   Renato De Donato   filters+groupby
270
  

0af843be   Renato De Donato   filters + alasql
271
  };