Security/ Package Name changes
[portal.git] / ecomp-portal-FE-common / client / app / views / role / rolepopupmodelController.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 app.controller('rolepopupController',  function ($scope, role, roleId, confirmBoxService, $http, $state, availableRoles, conf, availableRoleFunctions,ngDialog){
39         
40                         $scope.role = role;
41                         console.log($scope.role);
42                         if($scope.role.childRoles==null){
43                                 $scope.role.childRoles=[];
44                         }
45                         
46                         $scope.ociavailableRoles=availableRoles;
47                         console.log($scope.ociavailableRoles);
48                         
49                         $scope.availableRoles=[];
50                         if($scope.ociavailableRoles)
51                                 $.each($scope.ociavailableRoles, function(i, a){ 
52                                         var availableRole = a;
53                                         availableRole.selected = false;
54                                         if($scope.role.childRoles){
55                                     $.each($scope.role.childRoles, function(j, b){ 
56                                         if(a.id === b.id) {
57                                                 availableRole.selected = true;
58                                         }
59                                     });
60                                         };
61                                     $scope.availableRoles.push(availableRole);      
62                                 });     
63                                 
64                         $scope.ociavailableRoleFunctions = availableRoleFunctions; 
65                         console.log($scope.ociavailableRoleFunctions);
66                         $scope.availableRoleFunctions = []; 
67                         if($scope.ociavailableRoleFunctions)
68                                 $.each($scope.ociavailableRoleFunctions, function(i, a){ 
69                                         var availableRoleFunction = a;
70                                         availableRoleFunction.selected = false;
71                                     $.each($scope.role.roleFunctions, function(j, b){ 
72                                         if(a.code === b.code) {
73                                                 availableRoleFunction.selected = true;
74                                         }
75                                     });
76                                     $scope.availableRoleFunctions.push(availableRoleFunction);      
77                         });
78                         //$scope.resetMenu();
79                 
80                 $scope.toggleRoleFunction = function(selected,availableRoleFunction) {
81                         //alert('toggleRole: '+selected);
82                          
83                         if(!selected) {
84                                 //remove role function
85                                 if(role.id==null){
86                                         var index = $scope.role.roleFunctions.indexOf(availableRoleFunction);
87                                         if(index>=0)
88                                                 $scope.role.roleFunctions.splice(index, 1);
89                                         return;
90                                 }
91                                 var uuu = conf.api.toggleRoleRoleFunction + "?role_id=" + roleId;
92                                 confirmBoxService.confirm("You are about to remove the role function "+availableRoleFunction.name+" from the role for "+$scope.role.name+". Do you want to continue?").then(
93                                         function(confirmed){
94                                                         if(confirmed) { 
95                                                                         availableRoleFunction.id = roleId;
96                                                                         var index = $scope.role.roleFunctions.indexOf(availableRoleFunction);
97                                                                         if(index>=0)
98                                                                                 $scope.role.roleFunctions.splice(index, 1);
99                                                                         return;                                                                         
100                                                                         
101                                                                 }
102                                         }).catch(function(err) {
103                                                           availableRoleFunction.selected=!availableRoleFunction.selected;
104                                                         $log.error('roleListController::confirmBoxService.confirm error:', err);
105
106                                         });
107         
108                         } else {
109                                 //add role function
110                                 if(role.id==null){
111                                         $scope.role.roleFunctions.push(availableRoleFunction);
112                                         return;
113                                 }
114                                 var uuu = conf.api.addRoleRoleFunction + "?role_id=" + roleId;
115                                 
116                                 confirmBoxService.confirm("You are about to add the role function "+availableRoleFunction.name+" to the role for "+$scope.role.name+". Do you want to continue?").then(
117                                         function(confirmed){
118                                                 if(confirmed) {
119                                                         availableRoleFunction.id = roleId;
120                                                         $scope.role.roleFunctions.push(availableRoleFunction);
121                                                         return;
122                                                         }
123                         }).catch(function(err) {
124                                   availableRoleFunction.selected=!availableRoleFunction.selected;
125                                 $log.error('roleListController::confirmBoxService.confirm error:', err);
126                 });
127                         
128                           
129         }
130                 };
131         
132         $scope.toggleChildRole = function(selected,availableRole) {
133                 //alert('toggleRole: '+selected);
134
135                 if(!selected) {
136                         //remove role
137                         if(role.id==null){
138                                 var index = $scope.role.childRoles.indexOf(availableRole);
139                                 if(index>=0)
140                                         $scope.role.childRoles.splice(index, 1);
141                                 return;
142                         }
143                         var uuu = conf.api.toggleRoleChildRole +"?role_id=" + roleId;
144                         
145                         confirmBoxService.confirm("You are about to remove the child role "+availableRole.name+" from the role for "+$scope.role.name+". Do you want to continue?").then(
146                                 function(confirmed){
147                                         if(confirmed) {
148                                 
149                                                 var postData={childRole:availableRole};
150                                                  $http.post(uuu,postData).then(function(response) {
151                                                           $scope.role=response.data.role;
152                                                           },
153                                                           function(data) {
154                                                                   confirmBoxService.showInformation("Error while saving.");
155                                                           });
156                                                   }
157                                 }).catch(function(err) {
158                                         availableRole.selected=!availableRole.selected;
159                                         $log.error('roleListController::confirmBoxService.confirm error:', err);
160                         });
161                                 
162                 
163                 } else {
164                         //add role
165                         if(role.id==null){
166                                 $scope.role.childRoles.push(availableRole);
167                                 return;
168                         }
169                         var uuu = conf.api.addRoleChildRole +"?role_id=" + roleId;
170                         
171                         confirmBoxService.confirm("You are about to add the child role "+availableRole.name+" to the role for "+$scope.role.name+". Do you want to continue?").then(
172                                 function(confirmed){
173                                         if(confirmed) {
174                                                   var postData={childRole:availableRole};
175                                                   $http.post(uuu,postData).then(function(response) {
176                                                           $scope.role=response.data.role;
177                                                           },
178                                                           function(data) {
179                                                                   confirmBoxService.showInformation("Error while saving.");
180                                                           });
181                                                   }
182                                 
183                 }).catch(function(err) {
184                                 availableRole.selected=!availableRole.selected;
185                         $log.error('roleListController::confirmBoxService.confirm error:', err);
186         });
187                 
188                   
189         }
190         };
191         
192         $scope.close = function() {
193                 console.log('role', $scope.role);
194                 //$modalInstance.close({role:$scope.role});
195                 this.closeThisDialog(true);
196         };
197         
198 });