Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / fusion / scripts / controllers / self-profile-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('selfProfileController', function ($scope,$http,ProfileService,$routeParams,modalService){
21         $scope.tableData=[];
22         $scope.profile=[];
23         $scope.ociavailableRoles=[];
24         $scope.ociTimeZones;
25         $scope.ociCountries;
26         var stateList=[];
27         $scope.availableRoles = []; 
28         $scope.timeZones = []; 
29         $scope.selectedTimeZone = null;
30         $scope.countries = []; 
31         $scope.selectedCountry = null;
32         $scope.isUserSystemAdmin = false;
33
34         ProfileService.getSelfProfileDetail().then(function(data){
35                 var j = data;
36                 $scope.data = JSON.parse(j.data);
37                 $scope.profile =JSON.parse($scope.data.profile);
38                 $scope.profileId = $scope.profile.id;
39                 $scope.ociavailableRoles =JSON.parse($scope.data.availableRoles);
40                 $scope.ociTimeZones=JSON.parse($scope.data.timeZones);
41                 $scope.ociCountries=JSON.parse($scope.data.countries);
42                 stateList=JSON.parse($scope.data.stateList);
43                 $scope.orgUserId=$scope.profile.orgUserId;
44                 $scope.orgManagerUserId=$scope.profile.orgManagerUserId;
45                 
46                 if($scope.ociavailableRoles)
47                         $.each($scope.ociavailableRoles, function(i, a){ 
48                                 var availableRole = a;
49                                 availableRole.selected = false;
50                             $.each($scope.profile.roles, function(j, b){ 
51                                 if(a.id === b.id) {
52                                         availableRole.selected = true;
53                                         if(a.id === 1){
54                                                 $scope.isUserSystemAdmin = true;
55                                         }
56                                 } 
57                             });
58                             $scope.availableRoles.push(availableRole);      
59                         });     
60                         ;
61                 
62                 /*$scope.ociTimeZones = ${model.timeZones};*/
63                 
64                 if($scope.ociTimeZones){
65                         $.each($scope.ociTimeZones, function(i, a){ 
66                                 var timeZone = {"index":i, "value":a.value, "title":a.label};
67                             $scope.timeZones.push(timeZone);
68                             if($scope.profile.timeZoneId !== null && a.value === $scope.profile.timeZoneId.toString()){
69                                 $scope.selectedTimeZone = timeZone;
70                                 }           
71                         });     
72                 };
73                 
74                 /*$scope.ociCountries = ${model.countries};*/
75                 
76                 //alert($scope.ociCountries[0].label);
77                 if($scope.ociCountries)
78                 $.each($scope.ociCountries, function(i, a){ 
79                         var country = {"index":i, "value":a.value, "title":a.label};
80                     $scope.countries.push(country);
81                     if(a.value === $scope.profile.country){
82                         $scope.selectedCountry = country;
83                         }           
84                 });     
85                 ;
86                 
87                 /*var stateList=${model.stateList};*/
88                 //alert(stateList[0].label);
89                 stateList = stateList== null? []: stateList;
90                 var selectedState= $scope.profile.state ? $scope.profile.state:"";
91                 $scope.stateList = initDropdownWithLookUp(stateList,selectedState );
92                 
93                 //$scope.resetMenu();
94             },function(error){
95                 console.log("failed");
96                 reloadPageOnce();
97         });
98
99         /*$scope.profile=${model.profile};*/
100         $scope.orgUserId=$scope.profile.orgUserId;
101         $scope.orgManagerUserId=$scope.profile.orgManagerUserId;
102         
103         $scope.viewPerPage = 2;
104         $scope.currentPage = 1;
105         $scope.totalPage;
106         $scope.searchCategory = "";
107         $scope.searchString = "";
108         
109         $( "#dialog" ).hide();
110         
111         /*$scope.ociavailableRoles=${model.availableRoles};*/
112         //modalService.showFailure('Error','') ; 
113         
114         
115         $scope.saveProfile = function() {
116                   var uuu = "profile/saveProfile?profile_id=" + $scope.profileId;;
117                   var postData={profile: $scope.profile,
118                                         selectedCountry:$scope.selectedCountry!=null?$scope.selectedCountry.value:"",
119                                         selectedState:$scope.stateList.selected!=null?$scope.stateList.selected.value:"",
120                                         selectedTimeZone:$scope.selectedTimeZone!=null?$scope.selectedTimeZone.value:""
121                                };
122                   $.ajax({
123                          type : 'POST',
124                          url : uuu,
125                          dataType: 'json',
126                          contentType: 'application/json',
127                          data: JSON.stringify(postData),
128                          success : function(data){
129                                 modalService.showSuccess("Success","Update Successful.");
130                          },
131                          error : function(data){
132                                  modalService.showFailure("Fail","Error while saving.");
133                          }
134                   });
135         };
136                 
137                 $scope.addNewRolePopup = function(role) {
138                         $( "#dialog" ).dialog({
139                               modal: true,
140                               width: 500,
141                               height:600
142                             });
143                         
144                         $(".ui-dialog").css("z-index",10002);  
145                         $(".ui-dialog-titlebar").hide();     
146                 };
147                 
148                 $scope.toggleRole = function(selected,availableRole) {
149                                 //alert('toggleRole: '+selected);
150                                 if(!selected) {
151                                         //remove role
152                                         var uuu = "profile/removeRole?profile_id=" + $scope.profileId;;
153                                         modalService.popupConfirmWinWithCancel("Confirm","You are about to remove the role "+availableRole.name+" from the profile for "+$scope.profile.firstName+" "+$scope.profile.lastName+". Do you want to continue?",
154                                                 function(){
155                                                                         var postData={role:availableRole};
156                                                                           $.ajax({
157                                                                                  type : 'POST',
158                                                                                  url : uuu,
159                                                                                  dataType: 'json',
160                                                                                  contentType: 'application/json',
161                                                                                  data: JSON.stringify(postData),
162                                                                                  success : function(data){
163                                                                                         $scope.$apply(function(){$scope.profile=data;}); 
164                                                                                  },
165                                                                                  error : function(data){
166                                                                                          modalService.showFailure("Fail","Error while saving.");
167                                                                                  }
168                                                                           });
169                                         },
170                                     function(){
171                                           availableRole.selected=!availableRole.selected;
172                                         });
173                                                                                 
174                                         
175                                 } else {
176                                         //add role
177                                         var uuu = "profile/addNewRole?profile_id=" + $scope.profileId;;
178                                         modalService.popupConfirmWinWithCancel("Confirm","You are about to add the role "+availableRole.name+" from the profile for "+$scope.profile.firstName+" "+$scope.profile.lastName+". Do you want to continue?",
179                                                 function(){
180                                                                         var postData={role:availableRole};
181                                                                           $.ajax({
182                                                                                  type : 'POST',
183                                                                                  url : uuu,
184                                                                                  dataType: 'json',
185                                                                                  contentType: 'application/json',
186                                                                                  data: JSON.stringify(postData),
187                                                                                  success : function(data){
188                                                                                         $scope.$apply(function(){$scope.profile=data;}); 
189                                                                                  },
190                                                                                  error : function(data){
191                                                                                          modalService.showFailure("Fail","Error while saving.");
192                                                                                  }
193                                                                           });
194                                                 
195                                 },function(){
196                                           availableRole.selected=!availableRole.selected;
197                                 })
198                                 
199                                 }
200                                 
201                                   
202                 };
203
204                 $scope.removeRole = function(role) {
205                         
206                         
207                         modalService.popupConfirmWin("Confirm","You are about to remove the role "+role.name+" from the profile for "+$scope.profile.firstName+" "+$scope.profile.lastName+". Do you want to continue?",
208                                 function(){
209                                                         var uuu = "profile/removeRole?profile_id=" + $scope.profileId;;
210                                                           var postData={role:role};
211                                                           $.ajax({
212                                                                  type : 'POST',
213                                                                  url : uuu,
214                                                                  dataType: 'json',
215                                                                  contentType: 'application/json',
216                                                                  data: JSON.stringify(postData),
217                                                                  success : function(data){
218                                                                         $scope.$apply(function(){
219                                                                                 $scope.profile=data;
220                                                                                 $.each($scope.availableRoles, function(k, c){ 
221                                                                                 if(c.id === role.id) {
222                                                                                         c.selected = false;
223                                                                                 }
224                                                                             });
225                                                                         });
226                                                                         
227                                                                  },
228                                                                  error : function(data){
229                                                                          modalService.showFailure("Fail","Error while saving.");
230                                                                  }
231                                                           });
232                                 
233                 })
234                 
235                         
236                 };
237                 
238                 function initDropdownWithLookUp(arr,selectedValue){
239                         var dropdownArray=[];
240                         var selected = null;
241                         if(arr){
242                                 for(var i = 0,l = arr.length; i < l; i++) {
243                                         var option = { 
244                                                 "index" : i ,
245                                                 "value" : arr[i].value,
246                                                 "title" : arr[i].label
247                                             };
248                                         dropdownArray.push(option);
249                                         if(arr[i].value === selectedValue){
250                                                 selected = option;
251                                         }
252                                 }
253                         }
254                         var dropDown={};
255                         dropDown.options = dropdownArray;
256                         dropDown.selected = selected;
257                         return dropDown;
258                 };
259                 
260                 $scope.doRolePopup = function() {       
261                          var modalInstance = $modal.open({
262                             templateUrl: 'roles_popup.html',
263                             controller: 'rolepopupController',
264                             resolve: {
265                                  message: function () {
266                                          var message ={
267                                                          availableRoles: $scope.availableRoles
268                                           };
269                                   return message;
270                                 }
271                               }
272                           });   
273                                 modalInstance.result.then(function (opts) {
274                                         if(opts!=null){
275                                                 $scope.profile=opts.profile;
276                                         }               
277                    });
278                 } 
279                 
280                 $scope.close = function(){
281                         $('#dialog').dialog('close');
282                 }
283
284 });