Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / fusion / scripts / controllers / role-controller.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 app.controller('roleController', function ($scope, modalService, $modal, AdminService,$routeParams){
21         //$scope.role=${role};
22         //console.log($scope.role.roleFunctions);
23                 
24         $( "#dialogRoleFunction" ).hide();
25         $( "#dialogChildRole" ).hide();
26         
27         //$scope.ociavailableRoleFunctions=${availableRoleFunctions};
28         
29         AdminService.getRole($routeParams.roleId).then(function(data){
30                 
31                 var j = data;
32                 $scope.data = JSON.parse(j.data);
33                 
34                 $scope.role =JSON.parse($scope.data.role);
35                 console.log($scope.role);
36                 
37                 $scope.ociavailableRoleFunctions =JSON.parse($scope.data.availableRoleFunctions);
38                 console.log($scope.ociavailableRoleFunctions);
39                 $scope.availableRoleFunctions=[];
40                 
41                 if($scope.ociavailableRoleFunctions)
42                         $.each($scope.ociavailableRoleFunctions, function(i, a){ 
43                                 var availableRoleFunction = a;
44                                 availableRoleFunction.selected = false;
45                             $.each($scope.role.roleFunctions, function(j, b){ 
46                                 if(a.code === b.code) {
47                                         availableRoleFunction.selected = true;
48                                 }
49                             });
50                             $scope.availableRoleFunctions.push(availableRoleFunction);      
51                 });     
52                 
53
54                 $scope.ociavailableRoles=JSON.parse($scope.data.availableRoles);
55                 console.log($scope.ociavailableRoles);
56                 console.log("testing roles if exist");
57                 $scope.availableRoles=[];
58                 
59                 if($scope.ociavailableRoles)
60                         $.each($scope.ociavailableRoles, function(i, a){ 
61                                 var availableRole = a;
62                                 availableRole.selected = false;
63                                 if($scope.role.childRoles){
64                             $.each($scope.role.childRoles, function(j, b){ 
65                                 if(a.id === b.id) {
66                                         availableRole.selected = true;
67                                 }
68                             });
69                                 };
70                             $scope.availableRoles.push(availableRole);      
71                 });
72                         
73         
74         },function(error){
75                 console.log("failed");
76                 reloadPageOnce();
77         });
78
79         $scope.saveRole = function() {
80                                 var exists = false;     
81                                 for(x in $scope.availableRoles){
82                                   console.log($scope.availableRoles[x].name);
83                                         if($scope.availableRoles[x].name==$scope.role.name){
84                                                 modalService.showFailure("Warning",  "Role already exists.");
85                                                 exists = true;
86                                                 //$modalInstance.close({availableRoleFunctions:message.availableRoleFunctions});
87                                         }
88                                 }
89                                 if(!exists){
90                                         var uuu = "role/saveRole.htm?role_id="+$routeParams.roleId;
91                                           var postData={role: $scope.role, childRoles: $scope.role.childRoles, roleFunctions : $scope.role.roleFunctions};
92                                           $.ajax({
93                                                  type : 'POST',
94                                                  url : uuu,
95                                                  dataType: 'json',
96                                                  contentType: 'application/json',
97                                                  data: JSON.stringify(postData),
98                                                  success : function(data){
99                                                         modalService.showSuccess("Success","Update Successful.");
100                                                  },
101                                                  error : function(data){
102                                                          console.log(data);
103                                                          modalService.showFailure("Fail","Error while saving.");
104                                                  }
105                                           });
106                                 }
107                           
108                         };
109                 
110         $scope.addNewRoleFunctionModalPopup = function() {
111                         var modalInstance = $modal.open({
112                             templateUrl: 'role_functions_popup.html',
113                             controller: 'rolepopupController',
114                             backdrop: 'static',
115                             resolve: {
116                                 roleId: function () {
117                                           return $routeParams.roleId;
118                                         },
119                                 role: function () {
120                                   return $scope.role;
121                                 },
122                                 availableRoles: function () {
123                                           return $scope.ociavailableRoles;
124                                     },
125                                     availableRoleFunctions: function () {
126                                           return $scope.ociavailableRoleFunctions;
127                                     }
128                               }
129                           }); 
130                         modalInstance.result.then(function(response){
131                     console.log('response', response);
132                     $scope.role=response.role;
133                 });
134         };
135                 
136          $scope.addNewChildRoleModalPopup = function() {
137                         var modalInstance = $modal.open({
138                             templateUrl: 'child_roles_popup.html',
139                             controller: 'rolepopupController',
140                             backdrop: 'static',
141                             resolve: {
142                                 roleId: function () {
143                                           return $routeParams.roleId;
144                                         },
145                                 role: function () {
146                                   return $scope.role;
147                                 },
148                                 availableRoles: function () {
149                                           return $scope.ociavailableRoles;
150                                     },
151                                     availableRoleFunctions: function () {
152                                           return $scope.ociavailableRoleFunctions;
153                                     }           
154                               }
155                           }); 
156                         modalInstance.result.then(function(response){
157                     console.log('response', response);
158                     $scope.role=response.role;
159                 });
160                 };
161                 
162                 
163
164                 $scope.removeRoleFunction = function(roleFunction) {
165                         modalService.popupConfirmWin("Confirm","You are about to remove the role function "+roleFunction.name+" from the role for "+$scope.role.name+". Do you want to continue?",
166                                 function(){
167                                                 var uuu = "role/removeRoleFunction.htm?role_id=" + $routeParams.roleId;
168                                                   var postData={roleFunction:roleFunction};
169                                                   $.ajax({
170                                                          type : 'POST',
171                                                          url : uuu,
172                                                          dataType: 'json',
173                                                          contentType: 'application/json',
174                                                          data: JSON.stringify(postData),
175                                                          success : function(data){
176                                                                 $scope.$apply(function(){
177                                                                         $scope.role=data.role;
178                                                                         $.each($scope.availableRoleFunctions, function(k, c){ 
179                                                                         if(c.code === roleFunction.code) {
180                                                                                 c.selected = false;
181                                                                         }
182                                                                     });
183                                                                 });
184                                                                 
185                                                          },
186                                                          error : function(data){
187                                                                  modalService.showFailure("Fail","Error while saving.");
188                                                          }
189                                                   });
190                                 
191                 })
192                 
193                 };
194                 
195                 $scope.removeChildRole = function(childRole) {
196                         modalService.popupConfirmWin("Confirm","You are about to remove the child role "+childRole.name+" from the role for "+$scope.role.name+". Do you want to continue?",
197                                 function(){
198                                         var uuu = "role/removeChildRole.htm?role_id=" + $routeParams.roleId;
199                                           var postData={childRole:childRole};
200                                           $.ajax({
201                                                  type : 'POST',
202                                                  url : uuu,
203                                                  dataType: 'json',
204                                                  contentType: 'application/json',
205                                                  data: JSON.stringify(postData),
206                                                  success : function(data){
207                                                         $scope.$apply(function(){
208                                                                 $scope.role=data.role;
209                                                                 $.each($scope.availableRoles, function(k, c){ 
210                                                                 if(c.id === childRole.id) {
211                                                                         c.selected = false;
212                                                                 }
213                                                             });
214                                                         });
215                                                         
216                                                  },
217                                                  error : function(data){
218                                                          modalService.showFailure("Fail","Error while saving.");
219                                                  }
220                                           });
221                                 
222                 })
223                         
224                 };
225                 
226 });