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