Fixed Sonar issues for Console Logs
[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     }, function(error) {
48     });
49
50     PolicyAppService.getData('getDictionary/get_DescriptiveScopeByName').then(function(data) {
51     var j = data;
52     $scope.data = JSON.parse(j.data);
53     $scope.descriptiveScopeDictionaryDatas = JSON.parse($scope.data.descriptiveScopeDictionaryDatas);
54     });
55
56     PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function(data) {
57     var j = data;
58     $scope.data = JSON.parse(j.data);
59     $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
60     });
61
62     PolicyAppService.getData('getDictionary/get_VSCLActionDataByName').then(function(data) {
63     var j = data;
64     $scope.data = JSON.parse(j.data);
65     $scope.vsclActionDictionaryDatas = JSON.parse($scope.data.vsclActionDictionaryDatas);
66     });
67
68     PolicyAppService.getData('getDictionary/get_VNFTypeDataByName').then(function(data) {
69     var j = data;
70     $scope.data = JSON.parse(j.data);
71     $scope.vnfTypeDictionaryDatas = JSON.parse($scope.data.vnfTypeDictionaryDatas);     
72     });
73
74
75     PolicyAppService.getData('get_UserRolesData').then(function (data) {
76     var j = data;
77     $scope.data = JSON.parse(j.data);
78     $scope.userRolesDatas = JSON.parse($scope.data.userRolesDatas);
79     if ($scope.userRolesDatas[0] == 'super-admin') {
80          $scope.superAdminId = true;
81          $scope.exportPolicyId = true;
82          $scope.importPolicyId = true;
83         } else if ($scope.userRolesDatas[0] == 'super-editor' || $scope.userRolesDatas[0] == 'editor' || $scope.userRolesDatas[0] == 'admin') {
84         $scope.exportPolicyId = true;
85         $scope.importPolicyId = true; 
86         }
87     });
88
89     $scope.config = policyManagerConfig;
90     $scope.reverse = false;
91     $scope.predicate = ['model.type', 'model.name'];
92     $scope.order = function(predicate) {
93     $scope.reverse = ($scope.predicate[1] === predicate) ? !$scope.reverse : false;
94     $scope.predicate[1] = predicate;
95     };
96
97     $scope.query = '';
98     $scope.temp = new Item();
99     $scope.policyNavigator = new PolicyNavigator();
100     $scope.policyUploader = PolicyUploader;
101     $scope.uploadFileList = [];
102
103     $scope.setTemplate = function(name) {
104     $scope.viewTemplate = $cookies.viewTemplate = name;
105     };
106
107     $scope.touch = function(item) {
108     item = item instanceof Item ? item : new Item();
109     item.revert();
110     $scope.temp = item;
111     $scope.createScopeId = false;
112     $scope.deleteScopeId = false;
113     $scope.renameId = false;
114     $scope.createPolicyId = false;
115     $scope.cloneId = false;
116     $scope.editPolicyId = false;
117     $scope.switchVersionId = false;
118     $scope.describePolicyId = false;
119     $scope.viewPolicyId = false;
120     $scope.deletePolicyId = false;
121     if ($scope.temp.model.roleType == 'super-admin') {
122          $scope.createScopeId = true;
123          $scope.deleteScopeId = true;
124          $scope.renameId = true;
125          $scope.createPolicyId = true;
126          $scope.cloneId = true;
127          $scope.editPolicyId = true;
128          $scope.switchVersionId = true;
129          $scope.describePolicyId = true;
130          $scope.viewPolicyId = true;
131          $scope.deletePolicyId = true; 
132     } else if ($scope.temp.model.roleType == 'super-editor' || $scope.temp.model.roleType == 'editor') {
133          $scope.cloneId = true;
134          $scope.editPolicyId = true;
135          $scope.createPolicyId = true;
136          $scope.switchVersionId = true;
137          $scope.describePolicyId = true;
138          $scope.viewPolicyId = true;
139          $scope.deletePolicyId = true; 
140     } else if ($scope.temp.model.roleType == 'super-guest' || $scope.temp.model.roleType == 'guest') {
141          $scope.describePolicyId = true;
142          $scope.viewPolicyId = true;
143     } else if ($scope.temp.model.roleType == 'admin') {
144          $scope.createScopeId = true;
145          $scope.renameId = true;
146          $scope.createPolicyId = true;
147          $scope.cloneId = true;
148          $scope.editPolicyId = true;
149          $scope.switchVersionId = true;
150          $scope.describePolicyId = true;
151          $scope.viewPolicyId = true;
152          $scope.deletePolicyId = true;  
153     }
154     };
155
156     $scope.smartClick = function(item) {
157     if (item.isFolder()) {
158          return $scope.policyNavigator.folderClick(item);
159     }
160     if (item.isEditable()) {
161          return $scope.openEditItem(item);
162     }
163     };
164
165     $scope.openEditItem = function(item) {
166     item.getContent();
167     $scope.modal('createNewPolicy');
168     return $scope.touch(item);
169     };
170
171     $scope.modal = function(id, hide) {
172     return $('#' + id).modal(hide ? 'hide' : 'show');
173     };
174
175     $scope.isInThisPath = function(path) {
176     var currentPath = $scope.policyNavigator.currentPath.join('/');
177     return currentPath.indexOf(path) !== -1;
178     };
179       
180          $scope.watchPolicy = function(item) {
181         var uuu = "watchPolicy";
182         var data = {name : item.model.name,
183        path : item.model.path};
184         var postData={watchData: data};
185         $.ajax({
186         type : 'POST',
187         url : uuu,
188         dataType: 'json',
189         contentType: 'application/json',
190         data: JSON.stringify(postData),
191         success : function(data) {
192        $scope.$apply(function() {
193       $scope.watchData=data.watchData;});
194        Notification.success($scope.watchData);
195         },
196         error : function(data) {
197       Notification.error("Error while saving.");
198         }
199         });
200          };
201
202          $scope.refresh = function() {
203         $scope.policyNavigator.refresh();
204          };
205
206       $scope.switchVersion = function(item) {
207      if ($scope.policyNavigator.fileNameExists(item.tempModel.content.activeVersion)) {
208     item.error = 'Invalid filename or already exists, specify another name';
209     return false;
210      }
211      item.getSwitchVersionContent().then(function(){
212     $scope.policyNavigator.refresh();
213     $scope.modal('switchVersion', true);
214      });
215       };
216
217     $scope.copy = function(item) {
218     var samePath = item.tempModel.path.join() === item.model.path.join();
219     if (samePath && $scope.policyNavigator.fileNameExists(item.tempModel.name)) {
220          item.error = 'Invalid filename or already exists, specify another name';
221          return false;
222     }
223     item.copy().then(function() {
224          $scope.policyNavigator.refresh();
225          $scope.modal('copy', true);
226     });
227     };
228
229     $scope.remove = function(item) {
230     item.remove().then(function() {
231          $scope.policyNavigator.refresh();
232          $scope.modal('delete', true);
233     });
234     };
235
236     $scope.removePolicy = function(item) {
237     item.removePolicy().then(function() {
238          $scope.policyNavigator.refresh();
239          $scope.modal('deletePolicy', true);
240     });
241     };
242
243     $scope.rename = function(item) {
244     var samePath = item.tempModel.path.join() === item.model.path.join();
245     if (samePath && $scope.policyNavigator.fileNameExists(item.tempModel.name)) {
246          item.error = 'Invalid filename or already exists, specify another name';
247          return false;
248     }
249     item.rename().then(function() {
250          $scope.policyNavigator.refresh();
251          $scope.modal('rename', true);
252     });
253     };
254
255     $scope.move = function(item) {
256     var samePath = item.tempModel.path.join() === item.model.path.join();
257     if (samePath && $scope.policyNavigator.fileNameExists(item.tempModel.name)) {
258          item.error = 'Invalid filename or already exists, specify another name';
259          return false;
260     }
261     item.move().then(function() {
262          $scope.policyNavigator.refresh();
263          $scope.modal('move', true);
264     });
265     };
266
267     $scope.createFolder = function(item) {
268     var name = item.tempModel.name && item.tempModel.name.trim();
269     item.tempModel.type = 'dir';
270     item.tempModel.path = $scope.policyNavigator.currentPath;
271     if (name && !$scope.policyNavigator.fileNameExists(name)) {
272          item.createFolder().then(function() {
273         $scope.policyNavigator.refresh();
274         $scope.modal('newfolder', true);
275          });
276     } else {
277          item.error = 'Invalid filename or already exists, specify another name';
278          return false;
279     }
280     };
281
282     $scope.subScopeFolder = function(item) {
283     var name = item.tempModel.name +"\\" + item.tempModel.subScopename && item.tempModel.name.trim() + "\\"+item.tempModel.subScopename.trim() ;
284     item.tempModel.type = 'dir';
285     item.tempModel.path = $scope.policyNavigator.currentPath;
286     if (name && !$scope.policyNavigator.fileNameExists(name)) {
287          item.getScopeContent().then(function() {
288         $scope.policyNavigator.refresh();
289         $scope.modal('addSubScope', true);
290          });
291     } else {
292          item.error = 'Invalid filename or already exists, specify another name';
293          return false;
294     }
295     };
296
297     $scope.closefunction = function(fianlPath) {
298     $scope.policyNavigator.policyrefresh(fianlPath);
299     };
300
301     $scope.uploadFiles = function() {
302     $scope.policyUploader.upload($scope.uploadFileList, $scope.policyNavigator.currentPath).then(function() {
303          $scope.policyNavigator.refresh();
304          $scope.modal('uploadfile', true);
305     }, function(data) {
306          var errorMsg = data.result && data.result.error || 'Error Occured while Uploading....';
307          $scope.temp.error = errorMsg;
308     });
309     };
310
311     $scope.getQueryParam = function(param) {
312     var found;
313     window.location.search.substr(1).split('&').forEach(function(item) {
314          if (param ===  item.split('=')[0]) {
315         found = item.split('=')[1];
316         return false;
317          }
318     });
319     return found;
320     };
321
322     $scope.isWindows = $scope.getQueryParam('server') === 'Windows';
323     $scope.policyNavigator.refresh();
324      }]);