9daf4d0df9c6c498bcc8d0e7d22fdba44c79ab17
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this software except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 (function( app ) {
17
18         var data = app.ns("data");
19
20         data.QueryDataSourceInterface = data.DataSourceInterface.extend({
21                 defaults: {
22                         metadata: null, // (required) instanceof app.data.MetaData, the cluster metadata
23                         query: null     // (required) instanceof app.data.Query the data source
24                 },
25                 init: function() {
26                         this._super();
27                         this.config.query.on("results", this._results_handler.bind(this) );
28                         this.config.query.on("resultsWithParents", this._load_parents.bind(this) );
29                 },
30                 _results_handler: function(query, res) {
31                         this._getSummary(res);
32                         this._getMeta(res);
33                         var sort = query.search.sort[0] || { "_score": { reverse: false }};
34                         var sortField = Object.keys(sort)[0];
35                         this.sort = { column: sortField, dir: (sort[sortField].reverse ? "asc" : "desc") };
36                         this._getData(res, this.config.metadata);
37                         this.fire("data", this);
38                 },
39                 _load_parents: function(query, res) {
40                         query.loadParents(res, this.config.metadata);
41                 },
42                 _getData: function(res, metadata) {
43                         var metaColumns = ["_index", "_type", "_id", "_score"];
44                         var columns = this.columns = [].concat(metaColumns);
45
46                         this.data = res.hits.hits.map(function(hit) {
47                                 var row = (function(path, spec, row) {
48                                         for(var prop in spec) {
49                                                 if(acx.isObject(spec[prop])) {
50                                                         arguments.callee(path.concat(prop), spec[prop], row);
51                                                 } else if(acx.isArray(spec[prop])) {
52                                                         if(spec[prop].length) {
53                                                                 arguments.callee(path.concat(prop), spec[prop][0], row)
54                                                         }
55                                                 } else {
56                                                         var dpath = path.concat(prop).join(".");
57                                                         if(metadata.paths[dpath]) {
58                                                                 var field_name = metadata.paths[dpath].field_name;
59                                                                 if(! columns.contains(field_name)) {
60                                                                         columns.push(field_name);
61                                                                 }
62                                                                 row[field_name] = (spec[prop] === null ? "null" : spec[prop] ).toString();
63                                                         } else {
64                                                                 // TODO: field not in metadata index
65                                                         }
66                                                 }
67                                         }
68                                         return row;
69                                 })([ hit._index, hit._type ], hit._source, {});
70                                 metaColumns.forEach(function(n) { row[n] = hit[n]; });
71                                 row._source = hit;
72                                 if (typeof hit._parent!= "undefined") {
73                                         (function(prefix, path, spec, row) {
74                                         for(var prop in spec) {
75                                                 if(acx.isObject(spec[prop])) {
76                                                         arguments.callee(prefix, path.concat(prop), spec[prop], row);
77                                                 } else if(acx.isArray(spec[prop])) {
78                                                         if(spec[prop].length) {
79                                                                 arguments.callee(prefix, path.concat(prop), spec[prop][0], row)
80                                                         }
81                                                 } else {
82                                                         var dpath = path.concat(prop).join(".");
83                                                         if(metadata.paths[dpath]) {
84                                                                 var field_name = metadata.paths[dpath].field_name;
85                                                                 var column_name = prefix+"."+field_name;
86                                                                 if(! columns.contains(column_name)) {
87                                                                         columns.push(column_name);
88                                                                 }
89                                                                 row[column_name] = (spec[prop] === null ? "null" : spec[prop] ).toString();
90                                                         } else {
91                                                                 // TODO: field not in metadata index
92                                                         }
93                                                 }
94                                         }
95                                         })(hit._parent._type,[hit._parent._index, hit._parent._type], hit._parent._source, row);
96                                 }
97                                 return row;
98                         }, this);
99                 }
100         });
101
102 })( this.app );