[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / role / role-controller.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 \r
21 app.controller('roleController', function ($scope, $http, confirmBoxService, ngDialog, RoleService, conf, $stateParams){\r
22         //$scope.role=${role};\r
23                 \r
24         $( "#dialogRoleFunction" ).hide();\r
25         $( "#dialogChildRole" ).hide();\r
26         \r
27         //$scope.ociavailableRoleFunctions=${availableRoleFunctions};\r
28         $scope.fetchRoles = function() {\r
29         \r
30         RoleService.getRole($stateParams.roleId).then(function(data){\r
31                 \r
32                 var j = data;\r
33                 $scope.data = JSON.parse(j.data);\r
34                 \r
35                 $scope.role =JSON.parse($scope.data.role);\r
36                 \r
37                 $scope.ociavailableRoleFunctions =JSON.parse($scope.data.availableRoleFunctions);\r
38                 $scope.availableRoleFunctions=[];\r
39                 \r
40                 if($scope.ociavailableRoleFunctions)\r
41                         $.each($scope.ociavailableRoleFunctions, function(i, a){ \r
42                                 var availableRoleFunction = a;\r
43                                 availableRoleFunction.selected = false;\r
44                             $.each($scope.role.roleFunctions, function(j, b){ \r
45                                 if(a.code === b.code) {\r
46                                         availableRoleFunction.selected = true;\r
47                                 }\r
48                             });\r
49                             $scope.availableRoleFunctions.push(availableRoleFunction);      \r
50                 });     \r
51                 \r
52 \r
53                 $scope.ociavailableRoles=JSON.parse($scope.data.availableRoles);\r
54                 $scope.availableRoles=[];\r
55                 \r
56                 if($scope.ociavailableRoles)\r
57                         $.each($scope.ociavailableRoles, function(i, a){ \r
58                                 var availableRole = a;\r
59                                 availableRole.selected = false;\r
60                                 if($scope.role.childRoles){\r
61                             $.each($scope.role.childRoles, function(j, b){ \r
62                                 if(a.id === b.id) {\r
63                                         availableRole.selected = true;\r
64                                 }\r
65                             });\r
66                                 };\r
67                             $scope.availableRoles.push(availableRole);      \r
68                 });\r
69                         \r
70         \r
71         },function(error){\r
72                 console.log("RoleService.getRole failed", error);\r
73                 //reloadPageOnce();\r
74         });\r
75         }\r
76         \r
77         $scope.fetchRoles();\r
78 \r
79         $scope.saveRole = function() {\r
80                                 var exists = false,x;   \r
81                                 for(x in $scope.availableRoles){\r
82                                         if($scope.availableRoles[x].name==$scope.role.name){\r
83                                                 exists = true;\r
84                                                 //$modalInstance.close({availableRoleFunctions:message.availableRoleFunctions});\r
85                                         }\r
86                                 }\r
87                                 if (exists) {\r
88                                         confirmBoxService.showInformation( "Role already exists.");\r
89                                 }\r
90                                 else {\r
91                                         var uuu = conf.api.saveRole + "?role_id="+$stateParams.roleId;\r
92                                         var postData = {\r
93                                                         role: $scope.role, \r
94                                                         childRoles: $scope.role.childRoles,\r
95                                                         roleFunctions : $scope.role.roleFunctions\r
96                                         };\r
97                                         $http.post(uuu, JSON.stringify(postData)).then(function(res) {\r
98                                                 // console.log('roleController::saveRole: ' + JSON.stringify(res));\r
99                                                 if (res && res.data && res.data.role)\r
100                                                         confirmBoxService.showInformation("Update Successful.");\r
101                                                 else\r
102                                                         confirmBoxService.showInformation('Failed to create role: ' + res.data.error)\r
103                                         },\r
104                                         function(res){\r
105                                                 console.log('post failed', res.data);\r
106                                                 confirmBoxService.showInformation("Error while saving.");\r
107                                         }\r
108                                         );\r
109                                 }\r
110                         };\r
111                 \r
112         $scope.addNewRoleFunctionModalPopup = function() {\r
113                         var modalInstance = ngDialog.open({\r
114                             templateUrl: 'app/views/role/role_functions_popup.html',\r
115                             controller: 'rolepopupController',\r
116                             \r
117                             resolve: {\r
118                                 roleId: function () {\r
119                                           return $stateParams.roleId;\r
120                                         },\r
121                                 role: function () {\r
122                                   return $scope.role;\r
123                                 },\r
124                                 availableRoles: function () {\r
125                                           return $scope.ociavailableRoles;\r
126                                     },\r
127                                     availableRoleFunctions: function () {\r
128                                           return $scope.ociavailableRoleFunctions;\r
129                                     },\r
130                               }\r
131                           });\r
132                          modalInstance.closePromise.then(response => {\r
133                                         if($stateParams.roleId === '0'){\r
134                                                 return $scope.role;\r
135                                         }else{\r
136                                                 $scope.fetchRoles();\r
137                                         }\r
138                    // $scope.role=response.role;\r
139                 });\r
140         };\r
141                 \r
142          $scope.addNewChildRoleModalPopup = function() {\r
143                         var modalInstance = ngDialog.open({\r
144                             templateUrl: 'app/views/role/role_childrole_popup.html',\r
145                             controller: 'rolepopupController',\r
146                           \r
147                             resolve: {\r
148                                 roleId: function () {\r
149                                           return $stateParams.roleId;\r
150                                         },\r
151                                 role: function () {\r
152                                   return $scope.role;\r
153                                 },\r
154                                 availableRoles: function () {\r
155                                           return $scope.ociavailableRoles;\r
156                                     },\r
157                                     availableRoleFunctions: function () {\r
158                                           return $scope.ociavailableRoleFunctions;\r
159                                     },\r
160                               }\r
161                           }).closePromise.then(function(response){\r
162                                         if($stateParams.roleId === '0'){\r
163                                                  return $scope.role;\r
164                                         }else{\r
165                                     $scope.fetchRoles();\r
166                                         }\r
167                     //$scope.role=response.role;\r
168                 });\r
169                 };\r
170                 \r
171                 $scope.removeRoleFunction = function(roleFunction) {\r
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(\r
173                                 function(confirmed){\r
174                                                 var uuu = conf.api.toggleRoleRoleFunction + "?role_id=" + $stateParams.roleId;\r
175                                                   var postData={roleFunction:roleFunction};\r
176                                                         if(confirmed) { \r
177                                                                 $http.post(uuu, postData).then(\r
178                                                                                 function(response) {\r
179                                                                                         $scope.role= response.data.role;\r
180                                                                                         $.each($scope.availableRoleFunctions, function(k, c){ \r
181                                                                                         if(c.code === roleFunction.code) {\r
182                                                                                                 c.selected = false;\r
183                                                                                         }\r
184                                                                                     });\r
185                                                                                 }, \r
186                                                                                 function(response) {\r
187                                                                                         confirmBoxService.showInformation("Error while saving.");\r
188                                                                                 }\r
189                                                                 );                                                                      \r
190                                                                 }\r
191                                 \r
192                 });\r
193                 \r
194                 };\r
195                 \r
196                 $scope.removeChildRole = function(childRole) {\r
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(\r
198                                 function(confirmed){\r
199                                         var uuu = conf.api.toggleRoleChildRole + "?role_id=" + $stateParams.roleId;\r
200                                           var postData={childRole:childRole};\r
201                                           if(confirmed) {\r
202                                                           $http.post(uuu,postData).then( function(response) {\r
203                                                                   $scope.role=response.data.role;\r
204                                                                   $.each($scope.availableRoles, function(k, c){ \r
205                                                                         if(c.id === childRole.id) {\r
206                                                                                 c.selected = false;\r
207                                                                         }\r
208                                                                     });\r
209                                                                   },\r
210                                                                   \r
211                                                                   function(data) {\r
212                                                                           confirmBoxService.showInformation("Error while saving.");\r
213                                                                   });\r
214                                                 }                               \r
215                 });\r
216                         \r
217                 };\r
218                 \r
219 });\r