Merge "Fixed SonarIssues Alerts to Bootstrap Notification"
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / js / controllers / policyManager.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017, 2019 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('PolicyManagerController', [
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.exportPolicyId = true;
98                      $scope.importPolicyId = true;
99               } else if ($scope.userRolesDatas[0] == 'super-editor' || $scope.userRolesDatas[0] == 'editor' || $scope.userRolesDatas[0] == 'admin') {
100                     $scope.exportPolicyId = true;
101                     $scope.importPolicyId = true; 
102               }
103           }, function (error) {
104                 console.log("failed");
105           });
106
107           $scope.config = policyManagerConfig;
108           $scope.reverse = false;
109           $scope.predicate = ['model.type', 'model.name'];
110           $scope.order = function(predicate) {
111                 $scope.reverse = ($scope.predicate[1] === predicate) ? !$scope.reverse : false;
112                 $scope.predicate[1] = predicate;
113           };
114
115           $scope.query = '';
116           $scope.temp = new Item();
117           $scope.policyNavigator = new PolicyNavigator();
118           $scope.policyUploader = PolicyUploader;
119           $scope.uploadFileList = [];
120
121           $scope.setTemplate = function(name) {
122                 $scope.viewTemplate = $cookies.viewTemplate = name;
123           };
124
125           $scope.touch = function(item) {
126                 item = item instanceof Item ? item : new Item();
127                 item.revert();
128                 $scope.temp = item;
129                 $scope.createScopeId = false;
130                 $scope.deleteScopeId = false;
131                 $scope.renameId = false;
132                 $scope.createPolicyId = false;
133                 $scope.cloneId = false;
134                 $scope.editPolicyId = false;
135                 $scope.switchVersionId = false;
136                 $scope.describePolicyId = false;
137                 $scope.viewPolicyId = false;
138                 $scope.deletePolicyId = false;
139                 if ($scope.temp.model.roleType == 'super-admin') {
140                      $scope.createScopeId = true;
141                      $scope.deleteScopeId = true;
142                      $scope.renameId = true;
143                      $scope.createPolicyId = true;
144                      $scope.cloneId = true;
145                      $scope.editPolicyId = true;
146                      $scope.switchVersionId = true;
147                      $scope.describePolicyId = true;
148                      $scope.viewPolicyId = true;
149                      $scope.deletePolicyId = true; 
150                 } else if ($scope.temp.model.roleType == 'super-editor' || $scope.temp.model.roleType == 'editor') {
151                      $scope.cloneId = true;
152                      $scope.editPolicyId = true;
153                      $scope.createPolicyId = true;
154                      $scope.switchVersionId = true;
155                      $scope.describePolicyId = true;
156                      $scope.viewPolicyId = true;
157                      $scope.deletePolicyId = true; 
158                 } else if ($scope.temp.model.roleType == 'super-guest' || $scope.temp.model.roleType == 'guest') {
159                      $scope.describePolicyId = true;
160                      $scope.viewPolicyId = true;
161                 } else if ($scope.temp.model.roleType == 'admin') {
162                      $scope.createScopeId = true;
163                      $scope.renameId = true;
164                      $scope.createPolicyId = true;
165                      $scope.cloneId = true;
166                      $scope.editPolicyId = true;
167                      $scope.switchVersionId = true;
168                      $scope.describePolicyId = true;
169                      $scope.viewPolicyId = true;
170                      $scope.deletePolicyId = true;  
171                 }
172           };
173
174           $scope.smartClick = function(item) {
175                 if (item.isFolder()) {
176                      return $scope.policyNavigator.folderClick(item);
177                 }
178                 if (item.isEditable()) {
179                      return $scope.openEditItem(item);
180                 }
181           };
182
183           $scope.openEditItem = function(item) {
184                 item.getContent();
185                 $scope.modal('createNewPolicy');
186                 return $scope.touch(item);
187           };
188
189           $scope.modal = function(id, hide) {
190                 return $('#' + id).modal(hide ? 'hide' : 'show');
191           };
192
193           $scope.isInThisPath = function(path) {
194                 var currentPath = $scope.policyNavigator.currentPath.join('/');
195                 return currentPath.indexOf(path) !== -1;
196           };
197             
198          $scope.watchPolicy = function(item) {
199               var uuu = "watchPolicy";
200               var data = {name : item.model.name,
201                          path : item.model.path};
202               var postData={watchData: data};
203               $.ajax({
204                     type : 'POST',
205                     url : uuu,
206                     dataType: 'json',
207                     contentType: 'application/json',
208                     data: JSON.stringify(postData),
209                     success : function(data) {
210                          $scope.$apply(function() {
211                               $scope.watchData=data.watchData;});
212                          Notification.success($scope.watchData);
213                          console.log($scope.watchData);
214                     },
215                     error : function(data) {
216                         Notification.error("Error while saving.");
217                     }
218               });
219          };
220
221          $scope.refresh = function() {
222               $scope.policyNavigator.refresh();
223          };
224
225             $scope.switchVersion = function(item) {
226                  if ($scope.policyNavigator.fileNameExists(item.tempModel.content.activeVersion)) {
227                       item.error = 'Invalid filename or already exists, specify another name';
228                       return false;
229                  }
230                  item.getSwitchVersionContent().then(function(){
231                       $scope.policyNavigator.refresh();
232                       $scope.modal('switchVersion', true);
233                  });
234             };
235
236           $scope.copy = function(item) {
237                 var samePath = item.tempModel.path.join() === item.model.path.join();
238                 if (samePath && $scope.policyNavigator.fileNameExists(item.tempModel.name)) {
239                      item.error = 'Invalid filename or already exists, specify another name';
240                      return false;
241                 }
242                 item.copy().then(function() {
243                      $scope.policyNavigator.refresh();
244                      $scope.modal('copy', true);
245                 });
246           };
247
248           $scope.remove = function(item) {
249                 item.remove().then(function() {
250                      $scope.policyNavigator.refresh();
251                      $scope.modal('delete', true);
252                 });
253           };
254
255           $scope.removePolicy = function(item) {
256                 item.removePolicy().then(function() {
257                      $scope.policyNavigator.refresh();
258                      $scope.modal('deletePolicy', true);
259                 });
260           };
261
262           $scope.rename = function(item) {
263                 var samePath = item.tempModel.path.join() === item.model.path.join();
264                 if (samePath && $scope.policyNavigator.fileNameExists(item.tempModel.name)) {
265                      item.error = 'Invalid filename or already exists, specify another name';
266                      return false;
267                 }
268                 item.rename().then(function() {
269                      $scope.policyNavigator.refresh();
270                      $scope.modal('rename', true);
271                 });
272           };
273
274           $scope.move = function(item) {
275                 var samePath = item.tempModel.path.join() === item.model.path.join();
276                 if (samePath && $scope.policyNavigator.fileNameExists(item.tempModel.name)) {
277                      item.error = 'Invalid filename or already exists, specify another name';
278                      return false;
279                 }
280                 item.move().then(function() {
281                      $scope.policyNavigator.refresh();
282                      $scope.modal('move', true);
283                 });
284           };
285
286           $scope.createFolder = function(item) {
287                 var name = item.tempModel.name && item.tempModel.name.trim();
288                 item.tempModel.type = 'dir';
289                 item.tempModel.path = $scope.policyNavigator.currentPath;
290                 if (name && !$scope.policyNavigator.fileNameExists(name)) {
291                      item.createFolder().then(function() {
292                           $scope.policyNavigator.refresh();
293                           $scope.modal('newfolder', true);
294                      });
295                 } else {
296                      item.error = 'Invalid filename or already exists, specify another name';
297                      return false;
298                 }
299           };
300
301           $scope.subScopeFolder = function(item) {
302                 var name = item.tempModel.name +"\\" + item.tempModel.subScopename && item.tempModel.name.trim() + "\\"+item.tempModel.subScopename.trim() ;
303                 item.tempModel.type = 'dir';
304                 item.tempModel.path = $scope.policyNavigator.currentPath;
305                 if (name && !$scope.policyNavigator.fileNameExists(name)) {
306                      item.getScopeContent().then(function() {
307                           $scope.policyNavigator.refresh();
308                           $scope.modal('addSubScope', true);
309                      });
310                 } else {
311                      item.error = 'Invalid filename or already exists, specify another name';
312                      return false;
313                 }
314           };
315
316           $scope.closefunction = function(fianlPath) {
317                 $scope.policyNavigator.policyrefresh(fianlPath);
318           };
319
320           $scope.uploadFiles = function() {
321                 $scope.policyUploader.upload($scope.uploadFileList, $scope.policyNavigator.currentPath).then(function() {
322                      $scope.policyNavigator.refresh();
323                      $scope.modal('uploadfile', true);
324                 }, function(data) {
325                      var errorMsg = data.result && data.result.error || 'Error Occured while Uploading....';
326                      $scope.temp.error = errorMsg;
327                 });
328           };
329
330           $scope.getQueryParam = function(param) {
331                 var found;
332                 window.location.search.substr(1).split('&').forEach(function(item) {
333                      if (param ===  item.split('=')[0]) {
334                           found = item.split('=')[1];
335                           return false;
336                      }
337                 });
338                 return found;
339           };
340
341           $scope.isWindows = $scope.getQueryParam('server') === 'Windows';
342           $scope.policyNavigator.refresh();
343      }]);