[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / microservice-onboarding / microservice-add-details / microservice-add-details.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 MicroserviceAddDetailsCtrl {\r
23         constructor($scope, $log, $interval, widgetsCatalogService, applicationsService, adminsService, microserviceService, errorMessageByCode, ECOMP_URL_REGEX, $window,userProfileService, confirmBoxService, $cookies) {\r
24             \r
25           \r
26             let getAvailableApps = () => {       \r
27                 applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {\r
28                         this.availableApps=[];\r
29                         apps.unshift({\r
30                         id: 1,\r
31                         name: "ECOMP Portal"\r
32                     });\r
33                         for(var i = 0; i < apps.length; i++) {\r
34                         this.availableApps.push({\r
35                             id: apps[i].id,\r
36                             name: apps[i].name\r
37                         });\r
38                         if(this.isEditMode == true && this.service.appId == apps[i].id){\r
39                                 this.service.application = this.availableApps[i];\r
40                         }\r
41                     }\r
42                 }).catch(err => {\r
43                     $log.error(err);\r
44                 });\r
45             };\r
46             \r
47             let getAvailableWidgets = () => {       \r
48                 microserviceService.getWidgetListByService(this.service.id).then(widgets =>{\r
49                         this.availableWidgets = [];\r
50                         for(var i = 0; i < widgets.length; i++){\r
51                                 this.availableWidgets.push({\r
52                                         name: widgets[i]\r
53                                 })\r
54                         }\r
55                 }).catch(err => {\r
56                     $log.error(err);\r
57                 });\r
58             };\r
59             \r
60             \r
61             let getAvailableSecurityTypes = () => {   \r
62                 this.availableSecurityTypes = [];\r
63                 this.availableSecurityTypes.push({\r
64                         id: 0,\r
65                         name: 'No Authentication'\r
66                 });\r
67                 this.availableSecurityTypes.push({\r
68                         id: 1,\r
69                         name: 'Basic Authentication'\r
70                 });\r
71                 this.availableSecurityTypes.push({\r
72                         id: 2,\r
73                         name: 'Cookie based Authentication'\r
74                 });\r
75             }\r
76 \r
77             let init = () => {\r
78                 $log.info('MicroserviceAddDetailsCtrl::init');\r
79                 this.service = [];\r
80                 this.service.parameterList = [];\r
81                 this.service.active = true;\r
82                 this.emptyServiceName = false;\r
83                 this.emptyServiceDesc = false;\r
84                 this.emptyServiceURL = false;\r
85                 this.emptyServiceApp = false;\r
86                 this.dupliateName = false;\r
87                 this.serviceList = $scope.ngDialogData.list;\r
88                 \r
89                 if ($scope.ngDialogData && $scope.ngDialogData.service) {\r
90                         \r
91                     this.isEditMode = true;\r
92                     this.service = _.clone($scope.ngDialogData.service);\r
93                     \r
94                     console.log(this.service);\r
95                     if(this.service.active == 'Y')\r
96                         this.service.active = true;\r
97                     else\r
98                         this.service.active = false;\r
99                 } else {\r
100                     this.isEditMode = false;\r
101                 } \r
102                 getAvailableApps();\r
103                 getAvailableSecurityTypes();\r
104                 //getAvailableWidgets();\r
105                 \r
106                 /**\r
107                                  * 0: Basic Authentication\r
108                                  * \r
109                                  * TODO: change the structure\r
110                                  */\r
111                 if(this.service.securityType == "No Authentication"){\r
112                         this.service.security = this.availableSecurityTypes[0];\r
113                 }else if(this.service.securityType == "Basic Authentication"){\r
114                         this.service.security = this.availableSecurityTypes[1];\r
115                 }else if(this.service.securityType == "Cookie based Authentication"){\r
116                         this.service.security = this.availableSecurityTypes[2];\r
117                 }\r
118             };\r
119              \r
120             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;\r
121             this.conflictMessages = {};\r
122             this.scrollApi = {};\r
123 \r
124             let resetConflict = fieldName => {\r
125                 delete this.conflictMessages[fieldName];\r
126                 if($scope.widgetForm[fieldName]){\r
127                     $scope.widgetForm[fieldName].$setValidity('conflict', true);\r
128                 }\r
129             };\r
130 \r
131             this.addParameter = () => {\r
132                 document.getElementById('microservice-scroll-end').scrollIntoView();\r
133                 this.service.parameterList.push({}); \r
134             }\r
135             \r
136             this.closeThisDialog = () => {\r
137                 $scope.closeThisDialog(true);\r
138             }\r
139             \r
140             this.removeParamItem = (parameter) => {\r
141                 microserviceService.getUserParameterById(parameter.id).then((res) => {\r
142                         if(res.length > 0){\r
143                                 var message = res.length + " users have their own widget parameters. Are you sure you want to delete?";\r
144                                 confirmBoxService.editItem(message).then(isConfirmed => {\r
145                                         if(isConfirmed){\r
146                                                 microserviceService.deleteUserParameterById(parameter.id).then((res) => {\r
147                                                         for(var i = 0; i < this.service.parameterList.length; i++){\r
148                                                 if(this.service.parameterList[i].para_key == parameter.para_key\r
149                                                 && this.service.parameterList[i].para_value == parameter.para_value){\r
150                                                         this.service.parameterList.splice(i, 1);\r
151                                                         return;\r
152                                                 }\r
153                                         }\r
154                                                 });\r
155                                         }\r
156                                 });\r
157                         }\r
158                         else{\r
159                                 for(var i = 0; i < this.service.parameterList.length; i++){\r
160                                 if(this.service.parameterList[i].para_key == parameter.para_key\r
161                                 && this.service.parameterList[i].para_value == parameter.para_value){\r
162                                         this.service.parameterList.splice(i, 1);\r
163                                         return;\r
164                                 }\r
165                         }\r
166                         }\r
167                 });\r
168                 \r
169                 \r
170             }\r
171             \r
172             this.updateServiceName = () => {\r
173                 this.dupliateName = false;\r
174                 for(var i = 0; i < this.serviceList.length; i++){\r
175                         if(this.serviceList[i].name == this.service.name){\r
176                                 this.dupliateName = true;\r
177                                 return;\r
178                         }\r
179                 }\r
180             }\r
181             \r
182             this.updateDesc = () => {\r
183                 this.emptyServiceDesc = false;\r
184             }\r
185             \r
186             this.updateURL = () => {\r
187                 this.emptyServiceURL  = false;\r
188             }\r
189             \r
190             this.updateApp = () => {\r
191                 this.emptyServiceApp = false;\r
192             }\r
193             \r
194           //This is a fix for dropdown selection, due to b2b dropdown only update value field\r
195                 $scope.$watch('microserviceAddDetails.service.application.name', (newVal, oldVal) => {\r
196                         for(var i=0;i<$scope.microserviceAddDetails.availableApps.length;i++){                  \r
197                                 if($scope.microserviceAddDetails.availableApps[i].name==newVal){\r
198                                         $scope.microserviceAddDetails.service.application=angular.copy($scope.microserviceAddDetails.availableApps[i]);\r
199                                 }\r
200                         }\r
201                 });\r
202                 $scope.$watch('microserviceAddDetails.service.security.name', (newVal, oldVal) => {\r
203                         for(var i=0;i<$scope.microserviceAddDetails.availableSecurityTypes.length;i++){                         \r
204                                 if($scope.microserviceAddDetails.availableSecurityTypes[i].name==newVal){\r
205                                         $scope.microserviceAddDetails.service.security=angular.copy($scope.microserviceAddDetails.availableSecurityTypes[i]);\r
206                                 }\r
207                         }\r
208                 });\r
209             \r
210             let emptyCookies = () => {\r
211                 userProfileService.getUserProfile()\r
212                 .then(profile=> {\r
213                     $log.info('AppDetailsModalCtrl::emptyCookies profile: ', profile);\r
214                     $scope.attuid = profile.attuid;\r
215                     $log.info('user has the following attuid: ' + profile.attuid);\r
216                     if ($cookies.getObject($scope.attuid + '_widget') != undefined && $cookies.getObject($scope.attuid + '_widget') != null) {\r
217                         $cookies.remove($scope.attuid + '_widget');\r
218                     }\r
219                 });\r
220             };\r
221             \r
222             this.testServiceURL = () =>{\r
223                 //console.log(this.service.id);\r
224                 widgetsCatalogService.getServiceJSON(this.service.id).then(res => {\r
225                         document.getElementById("microservice-details-input-json").innerHTML = (JSON.stringify(res));\r
226                                 });\r
227             }\r
228             \r
229             this.saveChanges = () => {     \r
230                 /* TODO: add form validation */\r
231                 \r
232                 var isValid = true;\r
233                 this.updateServiceName();\r
234                 \r
235                 if(this.service.name == ''\r
236                 || this.service.name == undefined){\r
237                         this.emptyServiceName = true;\r
238                         isValid = false;\r
239                 }\r
240                 \r
241                 if(this.dupliateName == true\r
242                 && this.isEditMode == false){\r
243                         isValid = false;\r
244                 }\r
245                         \r
246                 \r
247                 if(this.service.desc == ''\r
248                 || this.service.desc == undefined){\r
249                         this.emptyServiceDesc = true;\r
250                         isValid = false;\r
251                 }\r
252                 \r
253                 if(this.service.url == ''\r
254                 || this.service.url == undefined){\r
255                         this.emptyServiceURL = true;\r
256                         isValid = false;\r
257                 }\r
258                         \r
259                 if(this.service.application == undefined\r
260                 || this.service.application == null){\r
261                         this.emptyServiceApp = true;\r
262                         isValid = false;\r
263                 }\r
264                 \r
265                 if(!isValid)\r
266                         return;\r
267                 \r
268                 /*\r
269                                  * Check the parameter list, delete those parameters that don't\r
270                                  * have key\r
271                                  */\r
272                 for(var i = 0; i < this.service.parameterList.length; i++){\r
273                         if(this.service.parameterList[i].para_key == undefined\r
274                         || this.service.parameterList[i].para_key == null\r
275                         || this.service.parameterList[i].para_key == ""){\r
276                                 this.service.parameterList.splice(i, 1);\r
277                                 i--;\r
278                         }\r
279                 }\r
280       \r
281                 var securityType;\r
282                 var username;\r
283                 var password;\r
284                 if(this.service.security == undefined ||\r
285                 this.service.security == null)\r
286                         securityType = "No Authentication";\r
287                 else{\r
288                         securityType = this.service.security.name;\r
289                         username = this.service.username;\r
290                         password = this.service.password;\r
291                 }\r
292                 \r
293                 var active = 'N';\r
294                 if(this.service.active == true)\r
295                         active = 'Y';\r
296                 \r
297                 var newService = {\r
298                                 name: this.service.name,\r
299                                 desc: this.service.desc,\r
300                                 appId: this.service.application.id,\r
301                                 url: this.service.url,\r
302                                 securityType: securityType,\r
303                                 username: username,\r
304                                 password: password,\r
305                                 active: active,\r
306                                 parameterList: this.service.parameterList\r
307                 };\r
308                 \r
309                 if(this.isEditMode){\r
310                         // console.log(this.service.parameterList);\r
311                         var message = "Are you sure you want to change '" + this.service.name + "'?"\r
312                         confirmBoxService.editItem(message).then(isConfirmed => {\r
313                                 if(isConfirmed){\r
314                                         microserviceService.updateService(this.service.id, newService).then(() => {\r
315                                                 // TODO: result validation check\r
316                                                 this.closeThisDialog(); \r
317                                         });\r
318                                 }\r
319                         });\r
320                 }else{\r
321                         microserviceService.createService(newService).then(() => {\r
322                                 // TODO: result validation check\r
323                                 $scope.closeThisDialog(true);\r
324                         });\r
325                 }\r
326                 \r
327             };\r
328             init();\r
329             $scope.$on('$stateChangeStart', e => {\r
330                 e.preventDefault();\r
331             });\r
332         }\r
333     }\r
334     MicroserviceAddDetailsCtrl.$inject = ['$scope', '$log', '$interval', 'widgetsCatalogService', 'applicationsService', 'adminsService', 'microserviceService', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window','userProfileService', 'confirmBoxService', '$cookies'];\r
335     angular.module('ecompApp').controller('MicroserviceAddDetailsCtrl', MicroserviceAddDetailsCtrl);\r
336 })(); \r