2bd522a4988dcbf08f46367691935a5604879294
[portal.git] / ecomp-portal-FE-common / client / app / views / role / role-controller.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
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
21 app.controller('roleController', function ($scope, $http, confirmBoxService, ngDialog, RoleService, conf, $stateParams){
22         //$scope.role=${role};
23                 
24         $( "#dialogRoleFunction" ).hide();
25         $( "#dialogChildRole" ).hide();
26         
27         //$scope.ociavailableRoleFunctions=${availableRoleFunctions};
28         $scope.fetchRoles = function() {
29         
30         RoleService.getRole($stateParams.roleId).then(function(data){
31                 
32                 var j = data;
33                 $scope.data = JSON.parse(j.data);
34                 
35                 $scope.role =JSON.parse($scope.data.role);
36                 
37                 $scope.ociavailableRoleFunctions =JSON.parse($scope.data.availableRoleFunctions);
38                 $scope.availableRoleFunctions=[];
39                 
40                 if($scope.ociavailableRoleFunctions)
41                         $.each($scope.ociavailableRoleFunctions, function(i, a){ 
42                                 var availableRoleFunction = a;
43                                 availableRoleFunction.selected = false;
44                             $.each($scope.role.roleFunctions, function(j, b){ 
45                                 if(a.code === b.code) {
46                                         availableRoleFunction.selected = true;
47                                 }
48                             });
49                             $scope.availableRoleFunctions.push(availableRoleFunction);      
50                 });     
51                 
52
53                 $scope.ociavailableRoles=JSON.parse($scope.data.availableRoles);
54                 $scope.availableRoles=[];
55                 
56                 if($scope.ociavailableRoles)
57                         $.each($scope.ociavailableRoles, function(i, a){ 
58                                 var availableRole = a;
59                                 availableRole.selected = false;
60                                 if($scope.role.childRoles){
61                             $.each($scope.role.childRoles, function(j, b){ 
62                                 if(a.id === b.id) {
63                                         availableRole.selected = true;
64                                 }
65                             });
66                                 };
67                             $scope.availableRoles.push(availableRole);      
68                 });
69                         
70         
71         },function(error){
72                 console.log("RoleService.getRole failed", error);
73                 //reloadPageOnce();
74         });
75         }
76         
77         $scope.fetchRoles();
78
79         $scope.saveRole = function() {
80                                 var exists = false,x;   
81                                 for(x in $scope.availableRoles){
82                                         if($scope.availableRoles[x].name==$scope.role.name){
83                                                 exists = true;
84                                                 //$modalInstance.close({availableRoleFunctions:message.availableRoleFunctions});
85                                         }
86                                 }
87                                 if (exists) {
88                                         confirmBoxService.showInformation( "Role already exists.");
89                                 }
90                                 else {
91                                         var uuu = conf.api.saveRole + "?role_id="+$stateParams.roleId;
92                                         var postData = {
93                                                         role: $scope.role, 
94                                                         childRoles: $scope.role.childRoles,
95                                                         roleFunctions : $scope.role.roleFunctions
96                                         };
97                                         $http.post(uuu, JSON.stringify(postData)).then(function(res) {
98                                                 // console.log('roleController::saveRole: ' + JSON.stringify(res));
99                                                 if (res && res.data && res.data.role)
100                                                         confirmBoxService.showInformation("Update Successful.");
101                                                 else
102                                                         confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
103                                         },
104                                         function(res){
105                                                 console.log('post failed', res.data);
106                                                 confirmBoxService.showInformation("Error while saving.");
107                                         }
108                                         );
109                                 }
110                         };
111                 
112         $scope.addNewRoleFunctionModalPopup = function() {
113                         var modalInstance = ngDialog.open({
114                             templateUrl: 'app/views/role/role_functions_popup.html',
115                             controller: 'rolepopupController',
116                             
117                             resolve: {
118                                 roleId: function () {
119                                           return $stateParams.roleId;
120                                         },
121                                 role: function () {
122                                   return $scope.role;
123                                 },
124                                 availableRoles: function () {
125                                           return $scope.ociavailableRoles;
126                                     },
127                                     availableRoleFunctions: function () {
128                                           return $scope.ociavailableRoleFunctions;
129                                     },
130                               }
131                           });
132                          modalInstance.closePromise.then(response => {
133                                         if($stateParams.roleId === '0'){
134                                                 return $scope.role;
135                                         }else{
136                                                 $scope.fetchRoles();
137                                         }
138                    // $scope.role=response.role;
139                 });
140         };
141                 
142          $scope.addNewChildRoleModalPopup = function() {
143                         var modalInstance = ngDialog.open({
144                             templateUrl: 'app/views/role/role_childrole_popup.html',
145                             controller: 'rolepopupController',
146                           
147                             resolve: {
148                                 roleId: function () {
149                                           return $stateParams.roleId;
150                                         },
151                                 role: function () {
152                                   return $scope.role;
153                                 },
154                                 availableRoles: function () {
155                                           return $scope.ociavailableRoles;
156                                     },
157                                     availableRoleFunctions: function () {
158                                           return $scope.ociavailableRoleFunctions;
159                                     },
160                               }
161                           }).closePromise.then(function(response){
162                                         if($stateParams.roleId === '0'){
163                                                  return $scope.role;
164                                         }else{
165                                     $scope.fetchRoles();
166                                         }
167                     //$scope.role=response.role;
168                 });
169                 };
170                 
171                 $scope.removeRoleFunction = function(roleFunction) {
172                         confirmBoxService.confirm("You are about to remove the role function "+roleFunction.name+" from the role for "+$scope.role.name+". Do you want to continue?").then(
173                                 function(confirmed){
174                                                 var uuu = conf.api.toggleRoleRoleFunction + "?role_id=" + $stateParams.roleId;
175                                                   var postData={roleFunction:roleFunction};
176                                                         if(confirmed) { 
177                                                                 $http.post(uuu, postData).then(
178                                                                                 function(response) {
179                                                                                         $scope.role= response.data.role;
180                                                                                         $.each($scope.availableRoleFunctions, function(k, c){ 
181                                                                                         if(c.code === roleFunction.code) {
182                                                                                                 c.selected = false;
183                                                                                         }
184                                                                                     });
185                                                                                 }, 
186                                                                                 function(response) {
187                                                                                         confirmBoxService.showInformation("Error while saving.");
188                                                                                 }
189                                                                 );                                                                      
190                                                                 }
191                                 
192                 });
193                 
194                 };
195                 
196                 $scope.removeChildRole = function(childRole) {
197                         confirmBoxService.confirm("You are about to remove the child role "+childRole.name+" from the role for "+$scope.role.name+". Do you want to continue?").then(
198                                 function(confirmed){
199                                         var uuu = conf.api.toggleRoleChildRole + "?role_id=" + $stateParams.roleId;
200                                           var postData={childRole:childRole};
201                                           if(confirmed) {
202                                                           $http.post(uuu,postData).then( function(response) {
203                                                                   $scope.role=response.data.role;
204                                                                   $.each($scope.availableRoles, function(k, c){ 
205                                                                         if(c.id === childRole.id) {
206                                                                                 c.selected = false;
207                                                                         }
208                                                                     });
209                                                                   },
210                                                                   
211                                                                   function(data) {
212                                                                           confirmBoxService.showInformation("Error while saving.");
213                                                                   });
214                                                 }                               
215                 });
216                         
217                 };
218                 
219 });