052d623dbd549dd7d230037a5ba06ba167cff546
[portal.git] / ecomp-portal-FE-common / client / app / views / widget-onboarding / widget-details-dialog / widget-details.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 'use strict';
21 (function () {
22     class WidgetOnboardingDetailsModalCtrl {
23         constructor($scope, $log, $interval, applicationsService, adminsService, microserviceService, widgetsCatalogService, errorMessageByCode, ECOMP_URL_REGEX, $window,userProfileService, confirmBoxService, $cookies,items) {
24             
25             this.appUpdate = function(){
26                 this.hasSelectedApp = false;
27                 this.appCounter = 0;
28                 for(var i = 0; i < this.availableApps.length; i++){
29                         if(this.availableApps[i].isSelected){
30                                 this.appCounter++;
31                                 if(!this.hasSelectedApp)
32                                         this.hasSelectedApp = true;
33                         }
34                         if(this.availableApps[i].isSelected
35                         && this.availableApps[i].roles.length == 0){
36                                 var index = i;
37                                 this.availableRoles = [];    
38                                 adminsService.getRolesByApp(this.availableApps[i].id).then(roles => {
39                                            for(var i = 0; i < roles.length; i++){
40                                                    this.availableRoles.push({
41                                                            id: roles[i].id,
42                                                    name: roles[i].name,
43                                                    isSelected: false,
44                                                    }); 
45                                            }
46                                            this.availableApps[index].roles = this.availableRoles;
47                                 });
48                         }
49                             }
50                 this.allRoleSelected = true;
51                 this.checkRoleSelected();
52             }
53             
54             this.roleUpdate = function(app){
55                         this.allRoleSelected = true;
56                 for(var i = 0; i < app.roles.length; i++){
57                         if(app.roles[i].isSelected){
58                                 app.roleSelected = true;
59                                 this.checkRoleSelected();
60                                 return;
61                         }
62                 }
63                         app.roleSelected = false;
64                         this.checkRoleSelected();
65             }
66             
67             this.checkRoleSelected = function(){
68                 for(var i = 0; i < this.availableApps.length; i++){
69                         if(this.availableApps[i].isSelected
70                         && !this.availableApps[i].roleSelected){
71                                 this.allRoleSelected = false;
72                                 return;
73                         }
74                 }
75             }
76                    
77             this.getAppName = function(appId){
78                  for(var i = 0; i < this.availableApps.length; i++){
79                          if(this.availableApps[i].id == appId){
80                                  return this.availableApps[i].name;
81                          }
82                  }
83             }
84             
85             let newWidgetModel = {
86                 name: null,
87                 roleId: null,
88                 roleName: null,
89                 appId: null,
90                 appName: null,
91                 url: null,
92                 showAppOptions: false,
93                 showRoleOptions: false,
94                 hasSelectedApp: false
95             };
96           
97             let getAvailableApps = () => {              
98                 
99                 if(this.isEditMode == false){   
100                         applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
101                                 this.availableApps=[];
102                                 for(var i=0;i<apps.length;i++) {
103                                 if (!apps[i].restrictedApp) {
104                             this.availableApps.push({
105                                 id: apps[i].id,
106                                 name: apps[i].name,
107                                 roles: [],
108                                 roleSelected: false,
109                                 isSelected: false,
110                             });
111                                 }
112                             }
113                         }).catch(err => {
114                             $log.error(err);
115                         });
116                 }
117                 else if(this.isEditMode == true){
118                         if(this.widget.allowAllUser == "Y")
119                                 this.widget.allUser = true;
120                         applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
121                                 this.availableApps=[];
122                                 let selectedApps = {};
123                                 var availableApps = this.availableApps;  
124                                 this.allRoleSelected = true;
125                                 for(var i=0; i < this.widget.widgetRoles.length; i++){
126                                         if(selectedApps[this.widget.widgetRoles[i].app.appId] != undefined)
127                                                 selectedApps[this.widget.widgetRoles[i].app.appId] += this.widget.widgetRoles[i].roleId + ";" + this.widget.widgetRoles[i].roleName + ";"; 
128                                         else{
129                                                 selectedApps[this.widget.widgetRoles[i].app.appId] = this.widget.widgetRoles[i].roleId + ";" + this.widget.widgetRoles[i].roleName + ";";                       
130                                                 this.appCounter++;
131                                         }
132                                 }                       
133                                 apps.forEach(function(app, index){
134                                                 availableApps.push({
135                                        id: app.id,
136                                        name: app.name,
137                                        roles: [],
138                                        roleSelected: false,
139                                        isSelected: false,
140                                                 });
141                                                 if(selectedApps[app.id] != undefined){
142                                                         adminsService.getRolesByApp(app.id).then(roles => {
143                                                         var role = selectedApps[app.id].split(';');
144                                                 var selectedRoles = [];
145                                                 var n = 0;
146                                                 while((n+1) < role.length){
147                                                         selectedRoles.push({
148                                                                            id: role[n++],
149                                                                    name: role[n++],
150                                                                    isSelected: true,
151                                                                 });
152                                                 }                                       
153                                                         for(var m = 0; m < roles.length; m++){
154                                                                 var hasSelected = true;
155                                                                 for(var n = 0; n < selectedRoles.length; n++){
156                                                                         if(selectedRoles[n].id == roles[m].id){
157                                                                                 hasSelected = false;
158                                                                                 break;
159                                                                         }
160                                                                 }
161                                                                 if(hasSelected){
162                                                                    selectedRoles.push({
163                                                                            id: roles[m].id,
164                                                                    name: roles[m].name,
165                                                                    isSelected: false,
166                                                                    }); 
167                                                                 }          
168                                                         }  
169                                                         availableApps[index].roleSelected = true;
170                                                         availableApps[index].isSelected = true;
171                                                         availableApps[index].roles = selectedRoles;
172                                                         });
173                                                 }
174                                 })
175                         })
176                 }
177             };
178             
179             let getAvailableServices = () =>{
180                 microserviceService.getServiceList().then(services => {
181                         this.availableServices = [];
182                         for(var i = 0; i < services.length; i++){
183                                 this.availableServices.push({
184                                         id: services[i].id,
185                                         name: services[i].name,
186                                         option: services[i].name + ": " + services[i].url
187                                 });
188                                 
189                                 if(this.widget.serviceId != null && this.widget.serviceId == services[i].id){
190                                         this.widget.serviceURL = this.availableServices[i];
191                                 }
192                         }
193                 }).catch(err => {
194                    $log.error(err);
195                 });
196             }
197             
198
199             let init = () => {
200                 $log.info('WidgetOnboardingDetailsModalCtrl::init');
201                 this.widgetsList = [];
202                 this.duplicatedName = true;
203                 this.allRoleSelected = false;
204                 this.appCounter = 0;
205                 this.isSaving = false;
206                 this.allUser = false;
207                 this.emptyWidgetName = false;
208                 
209                 if (items && items.widget) {
210                     this.isEditMode = true;
211                         this.allRoleSelected = true;
212                     this.widget = _.clone(items.widget);
213                 } else {
214                     this.isEditMode = false;
215                     this.widget = _.clone(newWidgetModel);
216                 }
217                 
218                 widgetsCatalogService.getManagedWidgets().then(res => {
219                         for(var i = 0; i < res.length; i++){
220                                 this.widgetsList.push(res[i].name);
221                         }
222                  }).catch(err => {
223                      $log.error('WidgetOnboardingDetailsModalCtrl::init error: ' + err);
224                  }).finally(()=> {
225                      this.isLoadingTable = false;
226                  });
227                  getAvailableApps();
228                  getAvailableServices();
229             };
230             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
231             this.conflictMessages = {};
232             this.scrollApi = {};
233
234             let resetConflict = fieldName => {
235                 delete this.conflictMessages[fieldName];
236                 if($scope.widgetForm[fieldName]){
237                     $scope.widgetForm[fieldName].$setValidity('conflict', true);
238                 }
239             };
240             
241             this.updateSelectedRole = () => {
242                 if (!this.selectedRole) {
243                     return;
244                 }
245                 this.widget.RoleId = this.selectedRole.id;
246                 this.widget.RoleName = this.selectedRole.name;
247             };
248             
249             let emptyCookies = () => {
250                 userProfileService.getUserProfile()
251                 .then(profile=> {
252                     $log.info('AppDetailsModalCtrl::emptyCookies profile: ', profile);
253                     $scope.orgUserId = profile.orgUserId;
254                     $log.info('user has the following orgUserId: ' + profile.orgUserId);
255                     if ($cookies.getObject($scope.orgUserId + '_widget') != undefined && $cookies.getObject($scope.orgUserId + '_widget') != null) {
256                         $cookies.remove($scope.orgUserId + '_widget');
257                     }
258                 });
259             };
260               
261             this.updateWidgetName = () => {
262                 for(var i = 0; i < this.widgetsList.length; i++){
263                         if(this.widget.name.toUpperCase() == this.widgetsList[i].toUpperCase()){
264                                 this.duplicatedName = false;
265                                 return;
266                         }
267                 }
268                 this.duplicatedName = true;
269             };
270             
271             this.saveChanges = () => {     
272                 
273                 if(!this.isEditMode)
274                         this.updateWidgetName();
275                 
276                 if(this.duplicatedName == false 
277                    || this.widget.name == ''
278                    || this.widget.name == undefined){
279                         this.emptyWidgetName = true;
280                         return;
281                 }
282                 
283                 if((this.widget.file == undefined && !this.isEditMode) ||
284                 (!this.widget.allUser && this.appCounter == 0) ||
285                 this.widget.name == null ||
286                 (!this.widget.allUser && !this.allRoleSelected) ||
287                 this.widget.saving == true)
288                         return; 
289                 
290                 
291                         this.widget.saving = true;
292                 var selectedRoles = [];
293                 if(!this.widget.allUser){
294                         for(var i = 0; i < this.availableApps.length; i++){
295                                 if(this.availableApps[i].isSelected){
296                                                 for(var n = 0; n < this.availableApps[i].roles.length; n++) {
297                                                         if(this.availableApps[i].roles[n].isSelected){
298                                                         var role = {
299                                                                 app: {
300                                                                         appId: this.availableApps[i].id
301                                                                 },
302                                                                 roleId: this.availableApps[i].roles[n].id,
303                                                                 roleName: this.availableApps[i].roles[n].name,
304                                                                 };
305                                                         selectedRoles.push(role);
306                                                         }
307                                         }
308                                 }
309                         }
310                 }
311                 
312                 var allowAllUser = 0;
313                 if(this.widget.allUser)
314                         allowAllUser = 1;       
315                 
316                 var serviceId = null;
317                 if(this.widget.serviceURL != null &&
318                 this.widget.serviceURL != undefined){
319                         serviceId = parseInt(this.widget.serviceURL);
320                 }
321                 
322                 
323                 var file_loc = this.widget.name + ".zip";
324                 var newWidget = {
325                                 name: this.widget.name,
326                                 desc: this.widget.desc,
327                                 widgetRoles: selectedRoles,
328                                 fileLocation: file_loc,
329                                 allowAllUser: allowAllUser,
330                                 serviceId: serviceId
331                 };
332                 
333                 if(this.isEditMode){
334                         
335                         if(this.widget.file != undefined){
336                                 widgetsCatalogService.updateWidgetWithFile(this.widget.file, this.widget.id, newWidget).then(res => {
337                                         if(!res.valid){
338                                                 if(!res.error){
339                                                         confirmBoxService.showInformation("Could not save. Please retry.");
340                                                         this.widget.saving = false;
341                                                         return;
342                                                 }
343                                                 confirmBoxService.showInformation(res.error);
344                                                 this.widget.saving = false;
345                                                 return;
346                                         }
347                                         $scope.$dismiss('cancel');
348                                 this.widget.saving = false;
349                                 });     
350                         }
351                         else{
352                                 widgetsCatalogService.updateWidget(this.widget.id, newWidget)
353                                 .then(() => {
354                                         $scope.$dismiss('cancel');
355
356                             });
357                         }
358                 }
359                 else{
360                         widgetsCatalogService.createWidget(newWidget, this.widget.file).then(res => {
361                                 if(!res.valid){
362                                                 if(!res.error)
363                                                         confirmBoxService.showInformation("Could not save. Please retry.");
364                                                 else
365                                                         confirmBoxService.showInformation(res.error);
366                                                 this.widget.saving = false;
367                                                 return;
368                                         }
369                                         $scope.$dismiss('cancel');
370
371                                 this.widget.saving = false;
372                         });
373                 }
374             };
375             init();
376             $scope.$on('$stateChangeStart', e => {
377                 e.preventDefault();
378             });
379         }
380     }
381     WidgetOnboardingDetailsModalCtrl.$inject = ['$scope', '$log', '$interval', 'applicationsService', 'adminsService', 'microserviceService', 'widgetsCatalogService', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window','userProfileService', 'confirmBoxService', '$cookies','items'];
382     angular.module('ecompApp').controller('WidgetOnboardingDetailsModalCtrl', WidgetOnboardingDetailsModalCtrl);
383 })();