3e2b45a15db49ba2c2a17d2e0476b3d5cebbc8f3
[ccsdk/features.git] /
1 (function( app ) {
2
3         var data = app.ns("data");
4
5         data.QueryDataSourceInterface = data.DataSourceInterface.extend({
6                 defaults: {
7                         metadata: null, // (required) instanceof app.data.MetaData, the cluster metadata
8                         query: null     // (required) instanceof app.data.Query the data source
9                 },
10                 init: function() {
11                         this._super();
12                         this.config.query.on("results", this._results_handler.bind(this) );
13                         this.config.query.on("resultsWithParents", this._load_parents.bind(this) );
14                 },
15                 _results_handler: function(query, res) {
16                         this._getSummary(res);
17                         this._getMeta(res);
18                         var sort = query.search.sort[0] || { "_score": { reverse: false }};
19                         var sortField = Object.keys(sort)[0];
20                         this.sort = { column: sortField, dir: (sort[sortField].reverse ? "asc" : "desc") };
21                         this._getData(res, this.config.metadata);
22                         this.fire("data", this);
23                 },
24                 _load_parents: function(query, res) {
25                         query.loadParents(res, this.config.metadata);
26                 },
27                 _getData: function(res, metadata) {
28                         var metaColumns = ["_index", "_type", "_id", "_score"];
29                         var columns = this.columns = [].concat(metaColumns);
30
31                         this.data = res.hits.hits.map(function(hit) {
32                                 var row = (function(path, spec, row) {
33                                         for(var prop in spec) {
34                                                 if(acx.isObject(spec[prop])) {
35                                                         arguments.callee(path.concat(prop), spec[prop], row);
36                                                 } else if(acx.isArray(spec[prop])) {
37                                                         if(spec[prop].length) {
38                                                                 arguments.callee(path.concat(prop), spec[prop][0], row)
39                                                         }
40                                                 } else {
41                                                         var dpath = path.concat(prop).join(".");
42                                                         if(metadata.paths[dpath]) {
43                                                                 var field_name = metadata.paths[dpath].field_name;
44                                                                 if(! columns.contains(field_name)) {
45                                                                         columns.push(field_name);
46                                                                 }
47                                                                 row[field_name] = (spec[prop] === null ? "null" : spec[prop] ).toString();
48                                                         } else {
49                                                                 // TODO: field not in metadata index
50                                                         }
51                                                 }
52                                         }
53                                         return row;
54                                 })([ hit._index, hit._type ], hit._source, {});
55                                 metaColumns.forEach(function(n) { row[n] = hit[n]; });
56                                 row._source = hit;
57                                 if (typeof hit._parent!= "undefined") {
58                                         (function(prefix, path, spec, row) {
59                                         for(var prop in spec) {
60                                                 if(acx.isObject(spec[prop])) {
61                                                         arguments.callee(prefix, path.concat(prop), spec[prop], row);
62                                                 } else if(acx.isArray(spec[prop])) {
63                                                         if(spec[prop].length) {
64                                                                 arguments.callee(prefix, path.concat(prop), spec[prop][0], row)
65                                                         }
66                                                 } else {
67                                                         var dpath = path.concat(prop).join(".");
68                                                         if(metadata.paths[dpath]) {
69                                                                 var field_name = metadata.paths[dpath].field_name;
70                                                                 var column_name = prefix+"."+field_name;
71                                                                 if(! columns.contains(column_name)) {
72                                                                         columns.push(column_name);
73                                                                 }
74                                                                 row[column_name] = (spec[prop] === null ? "null" : spec[prop] ).toString();
75                                                         } else {
76                                                                 // TODO: field not in metadata index
77                                                         }
78                                                 }
79                                         }
80                                         })(hit._parent._type,[hit._parent._index, hit._parent._type], hit._parent._source, row);
81                                 }
82                                 return row;
83                         }, this);
84                 }
85         });
86
87 })( this.app );