searchController.js 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
18 19
'use strict';

20 21
angular.module('dgc.search').controller('searchController', ['$scope', '$location', '$http', '$state', '$stateParams', 'lodash', 'searchResource', 'detailsResource', 'notificationService', 'atlasConfig',
    function($scope, $location, $http, $state, $stateParams, _, searchResource, detailsResource, notificationService, atlasConfig) {
22

23
        $scope.results = [];
Vishal Kadam committed
24
        $scope.resultCount = 0;
25
        $scope.isCollapsed = true;
26
        $scope.currentPage = 1;
27
        $scope.itemsPerPage = 10;
28 29
        $scope.filteredResults = [];
        $scope.resultRows = [];
30
        $scope.resultType = '';
31 32 33 34
        $scope.isObject = angular.isObject;
        $scope.isString = angular.isString;
        $scope.isArray = angular.isArray;
        $scope.isNumber = angular.isNumber;
35
        $scope.searchTypes = atlasConfig.SEARCH_TYPE;
36 37
        $scope.mapAttr = ['guid', 'typeName', 'owner', 'description', 'createTime', '$traits$', '$id$', 'comment', 'dataType'];

Vishal Kadam committed
38
        $scope.setPage = function(pageNo) {
39 40
            $scope.currentPage = pageNo;
        };
41 42 43 44 45 46 47 48 49 50 51 52 53 54

        $scope.initalPage = function() {
            var begin = (($scope.currentPage - 1) * $scope.itemsPerPage),
                end = begin + $scope.itemsPerPage,
                currentRowData = [],
                res = [],
                count = 0,
                firstRowId = 0;

            if ($scope.transformedResults && $scope.transformedResults.length > 0) {
                currentRowData = $scope.transformedResults.slice(begin, end);
            }

            var getName = function(gid, obj) {
55
                detailsResource.get({
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
                    id: gid
                }, function(data) {
                    if (data.values && data.values.name) {
                        obj.name = data.values.name;
                    } else {
                        obj.name = gid;
                    }
                    res.push(obj);
                    count++;

                    $scope.filteredResults = res;
                    if (!$scope.transformedProperties && firstRowId === gid) {
                        $scope.transformedProperties = $scope.filterProperties();
                    }
                });
            };

            angular.forEach(currentRowData, function(value) {
                var objVal = value;

                if (objVal.guid && !objVal.name) {
                    if (!firstRowId) {
                        firstRowId = objVal.guid;
                    }
                    getName(objVal.guid, objVal);
                } else {
                    res.push(objVal);
                    count++;

                    $scope.filteredResults = res;
                    if (!$scope.transformedProperties) {
                        $scope.transformedProperties = $scope.filterProperties();
                    }
                }
            });
        };

93
        $scope.search = function(query, type) {
94
            $scope.results = [];
95
            notificationService.reset();
96
            $scope.limit = 4;
97
            $scope.searchMessage = 'load-gif';
98
            $scope.$parent.query = query;
99
            searchResource.search({
100 101
                query: query,
                searchType: type
Vishal Kadam committed
102 103
            }, function searchSuccess(response) {
                $scope.resultCount = response.count;
104
                $scope.results = response.results;
105 106
                $scope.resultRows = ($scope.results && $scope.results.rows) ? $scope.results.rows : $scope.results;

107
                $scope.totalItems = $scope.resultCount;
108
                $scope.transformedResults = {};
109
                $scope.dataTransitioned = false;
110

111 112 113 114 115
                $scope.showText = "Text";
                if ($scope.searchTypeModel === $scope.searchTypes.dsl.value) {
                    $scope.showText = "DSL";
                }

116 117 118
                if ($scope.results) {
                    if (response.dataType) {
                        $scope.resultType = response.dataType.typeName;
119 120 121 122
                    } else if (response.results.dataType) {
                        $scope.resultType = response.results.dataType.typeName;
                    } else if (typeof response.dataType === 'undefined') {
                        $scope.resultType = "full text";
123
                    }
124
                    $scope.searchMessage = $scope.resultCount + ' results matching your ' + $scope.showText + '  search query ' + $scope.query + ' were found';
125
                } else {
126
                    $scope.searchMessage = '0 results matching your ' + $scope.showText + '  search query ' + $scope.query + ' were found';
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
                if (response.dataType && response.dataType.typeName && response.dataType.typeName.toLowerCase().indexOf('table') === -1) {
                    $scope.dataTransitioned = true;
                    var attrDef = response.dataType.attributeDefinitions;
                    if (attrDef.length === 1) {
                        $scope.searchKey = attrDef[0].name;
                    } else {
                        angular.forEach(attrDef, function(value) {
                            if (value.dataTypeName === '__IdType') {
                                $scope.searchKey = value.name;
                            }
                        });
                        if ($scope.searchKey === undefined || $scope.searchKey === '') {
                            $scope.searchKey = '';
                        }
                    }
                    $scope.transformedResults = $scope.filterResults();
                } else if (typeof response.dataType === 'undefined') {
                    $scope.dataTransitioned = true;
                    $scope.searchKey = '';
                    $scope.transformedResults = $scope.filterResults();
                } else if (response.dataType.typeName && response.dataType.typeName.toLowerCase().indexOf('table') !== -1) {
                    $scope.searchKey = "Table";
                    $scope.transformedResults = $scope.resultRows;
                } else if (response.results.dataType && response.results.dataType.typeName && response.results.dataType.typeName.toLowerCase().indexOf('table') !== -1) {
                    $scope.searchKey = "Table";
                    $scope.transformedResults = $scope.resultRows;
                }

157
                $scope.$watch('currentPage + itemsPerPage', function() {
158
                    $scope.initalPage();
159 160 161 162
                    $scope.pageCount = function() {
                        return Math.ceil($scope.resultCount / $scope.itemsPerPage);
                    };
                    if ($scope.results.length < 1) {
163
                        notificationService.error('No Result found', false);
164
                    }
165
                });
166
            }, function searchError(err) {
167 168 169 170 171 172 173 174 175 176
                if (($scope.searchTypeModel === $scope.searchTypes.dsl.value) && err.status === 400) {
                    $scope.searchMessageDsl = true;
                    $scope.searchMessage = false;
                } else {
                    $scope.searchMessageDsl = false;
                    $scope.searchMessage = true;
                    $scope.searchMessage = '0 results matching your ' + $scope.showText + '  search query ' + $scope.query + ' were found';
                    notificationService.error('Error occurred during executing search query, error status code = ' + err.status + ', status text = ' + err.statusText, false);
                }

177
            });
178 179 180 181 182
            $state.go('search', {
                query: query
            }, {
                location: 'replace'
            });
183
        };
184

185 186
        $scope.filterResults = function() {
            var res = [];
187

188 189
            if ($scope.searchKey !== '') {
                angular.forEach($scope.resultRows, function(value) {
190 191
                    var objVal = value[$scope.searchKey];
                    res.push(objVal);
192 193 194 195 196
                });
            } else {
                angular.forEach($scope.resultRows, function(value) {
                    var objVal = {},
                        curVal = value,
197 198
                        onlyId = false,
                        traits = false;
199 200 201 202 203 204 205 206 207 208

                    if (curVal.name) {
                        objVal.name = curVal.name;
                        delete curVal.name;
                    }
                    angular.forEach(curVal, function(vl, ky) {
                        if ($scope.mapAttr.indexOf(ky) !== -1 || ky.indexOf('_col_') !== -1 || ky.toLowerCase().indexOf('name') !== -1) {
                            if (ky === '$id$') {
                                objVal.id = curVal[ky].id;
                                onlyId = true;
209
                                traits = true;
210 211 212 213 214 215 216
                            } else if (ky.indexOf('$') === -1) {
                                objVal[ky] = vl;
                                onlyId = false;
                            }
                        }
                    });

217 218 219 220 221
                    if (traits) {
                        objVal.$traits$ = curVal.$traits$ || {};
                        objVal.Tools = objVal.id;
                    }

222 223 224 225 226 227
                    if (onlyId) {
                        objVal.guid = objVal.id;
                    }
                    res.push(objVal);
                });
            }
228
            return res;
229
        };
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

        $scope.filterProperties = function() {
            var results = $scope.transformedResults,
                pro = [];
            if (results && results.length > 0) {
                var result = results[0];
                if (typeof result === 'object') {
                    angular.forEach(result, function(value, key) {
                        if (key.indexOf('$typeName$') === -1) {
                            pro.push(key);
                        }
                    });
                } else {
                    pro.push($scope.searchKey);
                }
            }
            return pro;
        };

Vishal Kadam committed
249
        $scope.doToggle = function($event, el) {
250 251
            this.isCollapsed = !el;
        };
252

253 254
        $scope.openAddTagHome = function(traitId) {
            $state.go('addTagHome', {
255
                tId: traitId
256 257
            });
        };
258

259
        $scope.isTag = function(typename) {
260 261

            if (typename.indexOf("__tempQueryResultStruct") > -1 || $scope.searchKey === '') {
262 263 264
                return true;
            } else {
                return false;
265
            }
266
        };
267

268
        $scope.getResourceDataHome = function(event, id) {
269
            detailsResource.get({
270
                id: id
271 272 273 274 275 276 277 278
            }, function(data) {
                if ($scope.filteredResults !== null && Object.keys($scope.filteredResults).length > 0) {
                    angular.forEach($scope.filteredResults, function(obj, trait) {
                        if ((obj.$id$ && obj.$id$.id.indexOf(id) > -1) || (obj.id && obj.id.indexOf(id) > -1)) {
                            $scope.filteredResults[trait].$traits$ = data.traits;
                        }
                    });
                }
279 280
            });
        };
281

282
        $scope.$on('refreshResourceData', $scope.getResourceDataHome);
283

284 285
        $scope.filterSearchResults = function(items) {
            var res = {};
286
            var count = 0;
287
            items = _.omit(items, ['name', 'description', 'guid']);
288
            angular.forEach(items, function(value, key) {
289
                if (typeof value !== 'object' && (key.indexOf('$$') < 0)) {
290
                    res[key] = value;
291 292
                    count++;
                }
293
            });
294
            $scope.keyLength = count;
295 296
            return res;
        };
297

298
        $scope.searchQuery = $location.search();
299
        $scope.searchTypeModel = $stateParams.searchType;
300
        $scope.query = ($location.search()).query;
301 302 303
        $stateParams.searchType = ($location.search()).searchType;
        if ($scope.query && $stateParams.searchType) {
            $scope.search($scope.query, $stateParams.searchType);
304
        }
305 306 307 308 309 310 311 312 313 314 315

        $scope.searchTypeChanged = function() {
            if ($scope.query) {
                $state.go('search', {
                    query: $scope.query,
                    searchType: $scope.searchTypeModel
                }, {
                    location: 'replace'
                });
            }
        };
316
    }
317
]);