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