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