42e6796cddf6f1e1a6b4008a3bc041e3614fa736
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / js / controllers / policySearchManager.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 app.controller('PolicySearchController', [
21         '$scope', '$q', '$window', '$cookies', 'policyManagerConfig', 'item', 'policyNavigator', 'policyUploader', 'Notification','PolicyAppService',
22         function($scope, $q, $Window, $cookies, policyManagerConfig, Item, PolicyNavigator, PolicyUploader, Notification, PolicyAppService ) {
23                 
24         $scope.isDisabled = true;
25         $scope.superAdminId = false;
26         $scope.exportPolicyId = false;
27         $scope.importPolicyId = false;
28         $scope.createScopeId = false;
29         $scope.deleteScopeId = false;
30         $scope.renameId = false;
31         $scope.createPolicyId = false;
32         $scope.cloneId = false;
33         $scope.editPolicyId = false;
34         $scope.switchVersionId = false;
35         $scope.describePolicyId = false;
36         $scope.viewPolicyId = false;
37         $scope.deletePolicyId = false;
38         PolicyAppService.getData('get_LockDownData').then(function(data){
39                 var j = data;
40                 $scope.data = JSON.parse(j.data);
41                 $scope.lockdowndata = JSON.parse($scope.data.lockdowndata);
42                 if($scope.lockdowndata[0].lockdown == true){
43                         $scope.isDisabled = true;
44                 }else{
45                         $scope.isDisabled = false;
46                 }
47                 console.log($scope.data);
48         },function(error){
49                 console.log("failed");
50         });
51         
52         PolicyAppService.getData('getDictionary/get_DescriptiveScopeByName').then(function(data){
53                 var j = data;
54                 $scope.data = JSON.parse(j.data);
55                 console.log($scope.data);
56                 $scope.descriptiveScopeDictionaryDatas = JSON.parse($scope.data.descriptiveScopeDictionaryDatas);       
57         }, function (error) {
58                 console.log("failed");
59         });
60
61         PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function(data){
62                 var j = data;
63                 $scope.data = JSON.parse(j.data);
64                 console.log($scope.data);
65                 $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);       
66         }, function (error) {
67                 console.log("failed");
68         });
69
70         PolicyAppService.getData('getDictionary/get_VSCLActionDataByName').then(function(data){
71                 var j = data;
72                 $scope.data = JSON.parse(j.data);
73                 console.log($scope.data);
74                 $scope.vsclActionDictionaryDatas = JSON.parse($scope.data.vsclActionDictionaryDatas);   
75         }, function (error) {
76                 console.log("failed");
77         });
78
79         PolicyAppService.getData('getDictionary/get_VNFTypeDataByName').then(function(data){
80                 var j = data;
81                 $scope.data = JSON.parse(j.data);
82                 console.log($scope.data);
83                 $scope.vnfTypeDictionaryDatas = JSON.parse($scope.data.vnfTypeDictionaryDatas); 
84         }, function (error) {
85                 console.log("failed");
86         });
87
88         
89         PolicyAppService.getData('get_UserRolesData').then(function (data) {
90                 var j = data;
91                 $scope.data = JSON.parse(j.data);
92                 console.log($scope.data);
93                 $scope.userRolesDatas = JSON.parse($scope.data.userRolesDatas);
94                 console.log($scope.userRolesDatas);
95                 if($scope.userRolesDatas[0] == 'super-admin'){
96                         $scope.superAdminId = true;
97                         $scope.createPolicyId = true; 
98                         $scope.editPolicyId = true;
99                         $scope.describePolicyId = true;
100                         $scope.viewPolicyId = true;
101                 }else if($scope.userRolesDatas[0] == 'super-editor' || $scope.userRolesDatas[0] == 'editor' || $scope.userRolesDatas[0] == 'admin'){
102                         $scope.editPolicyId = true;
103                         $scope.createPolicyId = true;
104                         $scope.describePolicyId = true;
105                         $scope.viewPolicyId = true;
106                 }else if($scope.userRolesDatas[0] == 'super-guest' || $scope.userRolesDatas[0] == 'guest'){
107                         $scope.describePolicyId = true;
108                         $scope.viewPolicyId = true;
109                 }
110         }, function (error) {
111               console.log("failed");
112         });
113         
114         $scope.config = policyManagerConfig;
115         $scope.reverse = false;
116         $scope.predicate = ['model.type', 'model.name'];
117         $scope.order = function(predicate) {
118             $scope.reverse = ($scope.predicate[1] === predicate) ? !$scope.reverse : false;
119             $scope.predicate[1] = predicate;
120         };
121
122         $scope.query = '';
123         $scope.temp = new Item();
124         $scope.policyNavigator = new PolicyNavigator();
125
126         $scope.setTemplate = function(name) {
127             $scope.viewTemplate = $cookies.viewTemplate = name;
128         };
129
130         $scope.touch = function(item) {
131             item = item instanceof Item ? item : new Item();
132             item.revert();
133             $scope.temp = item;
134         };
135
136         $scope.smartClick = function(item) {
137             if (item.isFolder()) {
138                 return $scope.policyNavigator.folderClick(item);
139             }
140             if (item.isEditable()) {
141                 return $scope.openEditItem(item);
142             }
143         };
144
145         $scope.openEditItem = function(item) {
146             item.getContent();
147             $scope.modal('createNewPolicy');
148             return $scope.touch(item);
149         };
150
151         $scope.modal = function(id, hide) {
152             return $('#' + id).modal(hide ? 'hide' : 'show');
153         };
154
155         $scope.isInThisPath = function(path) {
156             var currentPath = $scope.policyNavigator.currentPath.join('/');
157             return currentPath.indexOf(path) !== -1;
158         };
159        
160         $scope.searchPolicy = function(searchContent){
161                 if(searchContent != undefined){
162                         var uuu = "searchPolicy";
163                         var postData = {searchdata : searchContent};
164                         $.ajax({
165                                 type : 'POST',
166                                 url : uuu,
167                                 dataType: 'json',
168                                 contentType: 'application/json',
169                                 data: JSON.stringify(postData),
170                                 success : function(data){
171                                         $scope.$apply(function(){
172                                                 var searchdata = data.result;
173                                                 if(searchdata.length > 0){
174                                                         if(searchdata[0] == "Exception"){
175                                                                 Notification.error(searchdata[1]);
176                                                         }else{
177                                                                 $scope.policyNavigator.searchrefresh(searchdata);  
178                                                         }
179                                                 }else{
180                                                         Notification.info("No Matches Found with your Search");
181                                                 }
182                                         });     
183                                 },
184                                 error : function(data){
185                                         Notification.error("Error while Searching.");
186                                 }
187                         });
188                 }else{
189                         Notification.error("No data has been entered or selected to search");
190                 }
191         };
192        
193        $scope.refresh = function(searchData){
194            $scope.policyNavigator.searchrefresh(null);
195        };
196          
197         $scope.getQueryParam = function(param) {
198             var found;
199             window.location.search.substr(1).split('&').forEach(function(item) {
200                 if (param ===  item.split('=')[0]) {
201                     found = item.split('=')[1];
202                     return false;
203                 }
204             });
205             return found;
206         };
207
208         $scope.isWindows = $scope.getQueryParam('server') === 'Windows';
209         $scope.policyNavigator.searchrefresh(null);
210         $scope.policyNavigator.setSearchModalActiveStatus();
211     }]);