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