Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / policy-models / Editor / src / js / controllers / main.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
21 (function(window, angular, $) {
22     'use strict';
23     angular.module('abs').controller('FileManagerCtrl', [
24         '$scope', '$q', '$window', '$translate', '$cookies', 'fileManagerConfig', 'item', 'fileNavigator', 'fileUploader', 'AdminTabService', 'RolesService', 'Notification', 'PolicyDictionaryService', 'PapUrlService',
25         function($scope, $q, $Window, $translate, $cookies, fileManagerConfig, Item, FileNavigator, FileUploader, AdminTabService, RolesService, Notification, PolicyDictionaryService, PapUrlService ) {
26                 
27         $scope.isDisabled = true;
28         $scope.superAdminId = false;
29         $scope.exportPolicyId = false;
30         $scope.importPolicyId = false;
31         $scope.createScopeId = false;
32         $scope.deleteScopeId = false;
33         $scope.renameId = false;
34         $scope.createPolicyId = false;
35         $scope.cloneId = false;
36         $scope.editPolicyId = false;
37         $scope.switchVersionId = false;
38         $scope.describePolicyId = false;
39         $scope.viewPolicyId = false;
40         $scope.deletePolicyId = false;
41         AdminTabService.getData().then(function(data){
42                 var j = data;
43                 $scope.data = JSON.parse(j.data);
44                 $scope.lockdowndata = JSON.parse($scope.data.lockdowndata);
45                 if($scope.lockdowndata[0].lockdown == true){
46                         $scope.isDisabled = true;
47                 }else{
48                         $scope.isDisabled = false;
49                 }
50                 console.log($scope.data);
51         },function(error){
52                 console.log("failed");
53         });
54         var papUrl;
55         PapUrlService.getPapUrl().then(function(data) {
56                 var config = data;
57                 papUrl = config.PAP_URL;
58                 console.log(papUrl); 
59                 PolicyDictionaryService.getDescriptiveDictionaryData(papUrl).then(function(data){
60                         var j = data;
61                         $scope.data = JSON.parse(j.data);
62                         console.log($scope.data);
63                         $scope.descriptiveScopeDictionaryDatas = JSON.parse($scope.data.descriptiveScopeDictionaryDatas);       
64                 }, function (error) {
65                         console.log("failed");
66                 });
67                 
68                 PolicyDictionaryService.getEcompDictionaryData(papUrl).then(function(data){
69                         var j = data;
70                         $scope.data = JSON.parse(j.data);
71                         console.log($scope.data);
72                         $scope.ecompNameDictionaryDatas = JSON.parse($scope.data.ecompNameDictionaryDatas);     
73                 }, function (error) {
74                         console.log("failed");
75                 });
76                 
77                 PolicyDictionaryService.getVSCLActionDictionaryData(papUrl).then(function(data){
78                         var j = data;
79                         $scope.data = JSON.parse(j.data);
80                         console.log($scope.data);
81                         $scope.vsclActionDictionaryDatas = JSON.parse($scope.data.vsclActionDictionaryDatas);   
82                 }, function (error) {
83                         console.log("failed");
84                 });
85                 
86                 PolicyDictionaryService.getVnfTypeDictionaryData(papUrl).then(function(data){
87                         var j = data;
88                         $scope.data = JSON.parse(j.data);
89                         console.log($scope.data);
90                         $scope.vnfTypeDictionaryDatas = JSON.parse($scope.data.vnfTypeDictionaryDatas); 
91                 }, function (error) {
92                         console.log("failed");
93                 });
94         });
95         
96         RolesService.getRolesData().then(function (data) {
97            var j = data;
98            $scope.data = JSON.parse(j.data);
99            console.log($scope.data);
100            $scope.userRolesDatas = JSON.parse($scope.data.userRolesDatas);
101            console.log($scope.userRolesDatas);
102            if($scope.userRolesDatas[0] == 'super-admin'){
103                   $scope.superAdminId = true;
104                   $scope.exportPolicyId = true;
105               $scope.importPolicyId = true;
106                   $scope.createScopeId = true;
107               $scope.deleteScopeId = true;
108               $scope.renameId = true;
109               $scope.createPolicyId = true;
110               $scope.cloneId = true;
111               $scope.editPolicyId = true;
112               $scope.switchVersionId = true;
113               $scope.describePolicyId = true;
114               $scope.viewPolicyId = true;
115               $scope.deletePolicyId = true; 
116            }else if($scope.userRolesDatas[0] == 'super-editor' || $scope.userRolesDatas[0] == 'editor'){
117                   $scope.exportPolicyId = true;
118               $scope.importPolicyId = true; 
119                   $scope.cloneId = true;
120                   $scope.editPolicyId = true;
121                   $scope.createPolicyId = true;
122                   $scope.cloneId = true;
123                   $scope.editPolicyId = true;
124                   $scope.switchVersionId = true;
125                   $scope.describePolicyId = true;
126                   $scope.viewPolicyId = true;
127                   $scope.deletePolicyId = true; 
128            }else if($scope.userRolesDatas[0] == 'super-guest' || $scope.userRolesDatas[0] == 'guest'){
129                   $scope.describePolicyId = true;
130                   $scope.viewPolicyId = true;
131            }else if($scope.userRolesDatas[0] == 'admin'){
132                   $scope.exportPolicyId = true;
133               $scope.importPolicyId = true;
134                   $scope.createScopeId = true;
135                   $scope.renameId = true;
136                   $scope.createPolicyId = true;
137                   $scope.cloneId = true;
138                   $scope.editPolicyId = true;
139                   $scope.switchVersionId = true;
140                   $scope.describePolicyId = true;
141                   $scope.viewPolicyId = true;
142                   $scope.deletePolicyId = true;  
143            }
144            }, function (error) {
145               console.log("failed");
146         });
147         
148         $scope.config = fileManagerConfig;
149         $scope.reverse = false;
150         $scope.predicate = ['model.type', 'model.name'];
151         $scope.order = function(predicate) {
152             $scope.reverse = ($scope.predicate[1] === predicate) ? !$scope.reverse : false;
153             $scope.predicate[1] = predicate;
154         };
155
156         $scope.query = '';
157         $scope.temp = new Item();
158         $scope.fileNavigator = new FileNavigator();
159         $scope.fileUploader = FileUploader;
160         $scope.uploadFileList = [];
161         $scope.viewTemplate = $cookies.viewTemplate || 'main-table.html';
162
163         $scope.setTemplate = function(name) {
164             $scope.viewTemplate = $cookies.viewTemplate = name;
165         };
166
167         $scope.changeLanguage = function (locale) {
168             if (locale) {
169                 return $translate.use($cookies.language = locale);
170             }
171             $translate.use($cookies.language || fileManagerConfig.defaultLang);
172         };
173
174         $scope.touch = function(item) {
175             item = item instanceof Item ? item : new Item();
176             item.revert();
177             $scope.temp = item;
178         };
179
180         $scope.smartClick = function(item) {
181             if (item.isFolder()) {
182                 return $scope.fileNavigator.folderClick(item);
183             }
184             if (item.isImage()) {
185                 return $scope.openImagePreview(item);
186             }
187             if (item.isEditable()) {
188                 return $scope.openEditItem(item);
189             }
190         };
191
192         $scope.openImagePreview = function(item) {
193             item.inprocess = true;
194             $scope.modal('imagepreview')
195                 .find('#imagepreview-target')
196                 .attr('src', item.getUrl(true))
197                 .unbind('load error')
198                 .on('load error', function() {
199                     item.inprocess = false;
200                     $scope.$apply();
201                 });
202             return $scope.touch(item);
203         };
204
205         $scope.openEditItem = function(item) {
206             item.getContent();
207             $scope.modal('createNewPolicy');
208             return $scope.touch(item);
209         };
210
211         $scope.modal = function(id, hide) {
212             return $('#' + id).modal(hide ? 'hide' : 'show');
213         };
214
215         $scope.isInThisPath = function(path) {
216             var currentPath = $scope.fileNavigator.currentPath.join('/');
217             return currentPath.indexOf(path) !== -1;
218         };
219
220         $scope.edit = function(item) {
221             item.edit().then(function() {
222                 $scope.modal('createNewPolicy', true);
223             });
224         };
225
226         $scope.createNewPolicy = function(item) {
227              item.createNewPolicy().then(function() {
228                   $scope.modal('createNewPolicy', true);
229               });
230          };
231          
232        $scope.watch = function(item){
233            var uuu = "watchPolicy";
234            var data = {name : item.model.name,
235                            path : item.model.path};
236            var postData={watchData: data};
237            $.ajax({
238                type : 'POST',
239                url : uuu,
240                dataType: 'json',
241                contentType: 'application/json',
242                data: JSON.stringify(postData),
243                success : function(data){
244                    $scope.$apply(function(){
245                        $scope.watchData=data.watchData;});
246                    Notification.success($scope.watchData);
247                    console.log($scope.watchData);
248                },
249                error : function(data){
250                    alert("Error while saving.");
251                }
252            });
253        };
254        
255             $scope.Search = function(search){
256                     var deferred = $q.defer();
257            var uuu = "searchPolicy";
258            var postData = {searchdata : search};
259            $.ajax({
260                type : 'POST',
261                url : uuu,
262                dataType: 'json',
263                contentType: 'application/json',
264                data: JSON.stringify(postData),
265                success : function(data){
266                    $scope.$apply(function(){
267                        $scope.searchdata=data.result;});
268                    if($scope.searchdata[0].error != undefined){
269                            Notification.info($scope.searchdata[0].error);
270                    }else{
271                         var j = data;  
272                         $scope.data = JSON.stringify(data.result);
273                            $scope.searchDatas = JSON.parse($scope.data);           
274                                                 var searchString = "Policies List" + "<br>";
275                                                 var i;
276                                                 for(i = 0 ; i < $scope.searchDatas.length; i++){
277                                                         searchString += $scope.searchDatas[i].name + ".xml" + "<br>";
278                                                 }
279                                                  var myWindow = window.open("", "MsgWindow", "width=500,height=500");
280                                                  myWindow.document.write("<p>Search List</p>");
281                                                  myWindow.document.write("<p>"+searchString+"</p>");
282                    }      
283                },
284                error : function(data){
285                    alert("Error while Searching.");
286                }
287            });
288        };
289            
290       
291          $scope.describePolicy = function(item){
292                  item.describePolicy().then(function(){
293                          $scope.modal('describePolicy', true);
294                  });
295          };
296          
297          $scope.exportPolicy = function(item){
298                  item.exportPolicy().then(function(){
299                          $scope.modal('exportPolicy', true);
300                  });
301          };
302          
303          $scope.switchVersion = function(item){
304                  if ($scope.fileNavigator.fileNameExists(item.tempModel.content.activeVersion)) {
305                  item.error = $translate.instant('error_invalid_filename');
306                  return false;
307              }
308                  item.getSwitchVersionContent().then(function(){
309                          $scope.fileNavigator.refresh();
310                          $scope.modal('switchVersion', true);
311                  });
312          };
313
314
315          $scope.viewPolicy = function(item){
316                  item.viewPolicy().then(function(){
317                          $scope.modal('createNewPolicy', true);
318                  });
319          };
320
321         $scope.copy = function(item) {
322             var samePath = item.tempModel.path.join() === item.model.path.join();
323             if (samePath && $scope.fileNavigator.fileNameExists(item.tempModel.name)) {
324                 item.error = $translate.instant('error_invalid_filename');
325                 return false;
326             }
327             item.copy().then(function() {
328                 $scope.fileNavigator.refresh();
329                 $scope.modal('copy', true);
330             });
331         };
332
333         $scope.remove = function(item) {
334             item.remove().then(function() {
335                 $scope.fileNavigator.refresh();
336                 $scope.modal('delete', true);
337             });
338         };
339
340         $scope.removePolicy = function(item) {
341             item.removePolicy().then(function() {
342                 $scope.fileNavigator.refresh();
343                 $scope.modal('deletePolicy', true);
344             });
345         };
346         
347         $scope.rename = function(item) {
348             var samePath = item.tempModel.path.join() === item.model.path.join();
349             if (samePath && $scope.fileNavigator.fileNameExists(item.tempModel.name)) {
350                 item.error = $translate.instant('error_invalid_filename');
351                 return false;
352             }
353             item.rename().then(function() {
354                 $scope.fileNavigator.refresh();
355                 $scope.modal('rename', true);
356             });
357         };
358         
359         $scope.move = function(item) {
360             var samePath = item.tempModel.path.join() === item.model.path.join();
361             if (samePath && $scope.fileNavigator.fileNameExists(item.tempModel.name)) {
362                 item.error = $translate.instant('error_invalid_filename');
363                 return false;
364             }
365             item.move().then(function() {
366                 $scope.fileNavigator.refresh();
367                 $scope.modal('move', true);
368             });
369         };
370
371         $scope.createFolder = function(item) {
372             var name = item.tempModel.name && item.tempModel.name.trim();
373             item.tempModel.type = 'dir';
374             item.tempModel.path = $scope.fileNavigator.currentPath;
375             if (name && !$scope.fileNavigator.fileNameExists(name)) {
376                 item.createFolder().then(function() {
377                     $scope.fileNavigator.refresh();
378                     $scope.modal('newfolder', true);
379                 });
380             } else {
381                 item.error = $translate.instant('error_invalid_filename');
382                 return false;
383             }
384         };
385
386         $scope.subScopeFolder = function(item) {
387                 var name = item.tempModel.name +"\\" + item.tempModel.subScopename && item.tempModel.name.trim() + "\\"+item.tempModel.subScopename.trim() ;
388                 item.tempModel.type = 'dir';
389                 item.tempModel.path = $scope.fileNavigator.currentPath;
390                 if (name && !$scope.fileNavigator.fileNameExists(name)) {
391                         item.getScopeContent().then(function() {
392                                 $scope.fileNavigator.refresh();
393                                 $scope.modal('addSubScope', true);
394                         });
395                 } else {
396                         item.error = $translate.instant('error_invalid_filename');
397                         return false;
398                 }
399         };
400
401         $scope.uploadFiles = function() {
402             $scope.fileUploader.upload($scope.uploadFileList, $scope.fileNavigator.currentPath).then(function() {
403                 $scope.fileNavigator.refresh();
404                 $scope.modal('uploadfile', true);
405             }, function(data) {
406                 var errorMsg = data.result && data.result.error || $translate.instant('error_uploading_files');
407                 $scope.temp.error = errorMsg;
408             });
409         };
410
411         $scope.getQueryParam = function(param) {
412             var found;
413             window.location.search.substr(1).split('&').forEach(function(item) {
414                 if (param ===  item.split('=')[0]) {
415                     found = item.split('=')[1];
416                     return false;
417                 }
418             });
419             return found;
420         };
421
422         $scope.changeLanguage($scope.getQueryParam('lang'));
423         $scope.isWindows = $scope.getQueryParam('server') === 'Windows';
424         $scope.fileNavigator.refresh();
425     }]);
426 })(window, angular, jQuery);