cypress test - adding option transpileOnly: true
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / controller / creationDialogController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 "use strict";
22
23 var creationDialogController = function (COMPONENT, FIELD, PARAMETER, $scope, $http, $timeout, $log,
24                                          CreationService, UtilityService, DataService, VIDCONFIGURATION, $location, $uibModal, featureFlags) {
25
26     $scope.isDialogVisible = false;
27     $scope.isServiceError = false;
28     $scope.summaryControl = {};
29     $scope.userProvidedControl = {};
30
31
32     var callbackFunction = undefined;
33     var componentId = undefined;
34
35     $scope.showReportWindow = function() {
36
37         let errorMsg;
38
39         if($scope.error !== undefined && $scope.error != null) {
40             errorMsg = $scope.error;
41         } else {
42             errorMsg = "";
43         }
44
45         const modalWindow = $uibModal.open({
46             templateUrl: 'app/vid/scripts/modals/report-modal/report-modal.html',
47             controller: 'reportModalController',
48             controllerAs: 'vm',
49             resolve: {
50                 errorMsg: function () {
51                     return errorMsg;
52                 }
53             }
54         });
55
56         $scope.isDialogVisible = false;
57         $scope.popup.isVisible = false;
58     };
59
60     $scope.isShowErrorReport = function() {
61         return featureFlags.isOn(COMPONENT.FEATURE_FLAGS.FLAG_CREATE_ERROR_REPORTS);
62     };
63
64     $scope.shouldShowOldPopup = function () {
65         return !DataService.getShouldIncludeInAsyncInstantiationFlow();
66     };
67
68     function receiveMessage(event) {
69         if (event.data == 'closeIframe') {
70             window.removeEventListener("message", receiveMessage, false);
71
72             $scope.cancel();
73         }
74         else if (event.data.eventId == 'submitIframe') {
75             {
76                 $location.path('/servicePlanning').search({serviceModelId: event.data.data.serviceModelId});
77             }
78         } else if (event.data.eventId == 'showPreviousInstantiations') {
79             {
80                 $location.path('/instantiationStatus').search({filterText: event.data.data.serviceModelId});
81             }
82         }
83         $scope.$apply();
84     }
85
86     $scope.$on(COMPONENT.CREATE_COMPONENT, function (event, request) {
87         $scope.isSpinnerVisible = true;
88         $scope.isErrorVisible = false;
89         $scope.isDataVisible = false;
90         $scope.isConfirmEnabled = false;
91         $scope.isDialogVisible = true;
92         $scope.popup.isVisible = true;
93
94
95         if (!$scope.shouldShowOldPopup()) {
96             $scope.url = COMPONENT.SERVICE_POPUP_IFRAME_URL + request.modelNameVersionId + "&isCreate=true&r=" + Math.random();
97             window.addEventListener("message", receiveMessage, false);
98
99         }
100         else {
101             callbackFunction = request.callbackFunction;
102             componentId = request.componentId;
103             CreationService.initializeComponent(request.componentId);
104
105             CreationService.setHttpErrorHandler(function (response) {
106                 $scope.isServiceError = true;
107                 showError(FIELD.ERROR.SYSTEM_FAILURE, UtilityService
108                     .getHttpErrorMessage(response));
109             });
110
111             $scope.componentName = CreationService.getComponentDisplayName();
112
113             CreationService.getParameters(handleGetParametersResponse);
114         }
115
116     });
117
118     var handleGetParametersResponse = function (parameters) {
119         $scope.summaryControl.setList(parameters.summaryList);
120         $scope.userProvidedControl.setList(parameters.userProvidedList);
121
122         $scope.isSpinnerVisible = false;
123         $scope.isDataVisible = true;
124         $scope.isConfirmEnabled = true;
125     };
126
127     var validateInstanceName = function (iname) {
128         var patt1 = /^([a-z])+([0-9a-z\-_\.]*)$/i;
129
130         if (iname == null) {
131             return false;
132         }
133         if (!iname.match(patt1)) {
134             return false;
135         }
136         return true;
137     };
138     var validateMap = function (mname) {
139         var patt1 = /^{(\s*\w+\s*:\s*\w+\s*)(\s*,\s*\w+\s*:\s*\w+\s*)*}$/im;
140         if (mname == null) {
141             return true;
142         }
143         if (!mname.match(patt1)) {
144             return false;
145         }
146         return true;
147     };
148
149     var validateList = function (lname) {
150         var patt1 = /^\[(\s*\w+\s*)(\s*,\s*\w+\s*)*\]$/i;
151         if (lname == null) {
152             return true;
153         }
154         if (!lname.match(patt1)) {
155             return false;
156         }
157         return true;
158     };
159
160     $scope.userParameterChanged = function (id) {
161         CreationService.updateUserParameterList(id, $scope.userProvidedControl);
162     };
163
164     $scope.confirm = function () {
165
166         var requiredFields = $scope.userProvidedControl.getRequiredFields();
167         if (requiredFields !== "") {
168             showError(FIELD.ERROR.MISSING_DATA, requiredFields);
169             return;
170         }
171
172         var isUploadAvailable = false;
173         var uploadIndex = 0;
174         var paramList = $scope.userProvidedControl.getList();
175         var isAnyError = false;
176         for (var i = 0; i < paramList.length; i++) {
177             if (paramList[i].id === FIELD.ID.SUPPLEMENTORY_DATA_FILE) {
178                 isUploadAvailable = true;
179                 uploadIndex = i;
180             }
181             if (paramList[i].id === FIELD.ID.UPLOAD_SUPPLEMENTORY_DATA_FILE && paramList[i].value && document.getElementById(FIELD.ID.SUPPLEMENTORY_DATA_FILE).value == '') {
182                 isAnyError = true;
183             }
184         }
185
186         if (isUploadAvailable && isAnyError) {
187             showError(FIELD.ERROR.MISSING_DATA, FIELD.ERROR.MISSING_FILE);
188
189         } else if (isUploadAvailable && document.getElementById(FIELD.ID.SUPPLEMENTORY_DATA_FILE).value != '') {
190             var errorMsg = "";
191             var fileInput = document.getElementById(FIELD.ID.SUPPLEMENTORY_DATA_FILE);
192             var file = fileInput.files[0];
193             var reader = new FileReader();
194             reader.onload = function (e) {
195                 try {
196                     paramList[uploadIndex].value = JSON.parse(reader.result);
197                     FIELD.PARAMETER.SUPPLEMENTORY_DATA_FILE['value'] = paramList[uploadIndex].value;
198
199                     var instanceName = "";
200
201                     if (DataService.getALaCarte()) {
202                         if (paramList != null) {
203                             for (var i = 0; i < paramList.length; i++) {
204                                 if (paramList[i].id === FIELD.ID.INSTANCE_NAME) {
205                                     instanceName = paramList[i].value;
206                                     break;
207                                 }
208                             }
209                         }
210                         var isValid = validateInstanceName(instanceName);
211                         if (isValid) {
212                             $scope.isErrorVisible = false;
213                         } else {
214                             showError(FIELD.ERROR.INVALID_INSTANCE_NAME + instanceName,
215                                 FIELD.ERROR.INSTANCE_NAME_VALIDATE);
216                             return;
217                         }
218                     }
219                     var arbitraryParametersList = DataService.getArbitraryParameters();
220                     var p = null;
221                     if (UtilityService.hasContents(arbitraryParametersList)) {
222                         for (var i = 0; i < arbitraryParametersList.length; i++) {
223                             p = arbitraryParametersList[i];
224                             if (p.type === PARAMETER.MAP) {
225                                 //validate a map: { <entry_key_1>: <entry_value_1>, ... , <entry_key_n>: <entry_value_n> }
226                                 // need to find the value in paramList
227                                 for (var j = 0; j < paramList.length; j++) {
228                                     if (paramList[j].id === p.id) {
229                                         p.value = paramList[j].value;
230                                         var isValid = validateMap(p.value);
231                                         if (isValid) {
232                                             $scope.isErrorVisible = false;
233                                             break;
234                                         }
235                                         else {
236                                             showError(FIELD.ERROR.INVALID_MAP + p.id,
237                                                 FIELD.ERROR.MAP_VALIDATE);
238                                             return;
239                                         }
240                                     }
241                                 }
242                             } else if (p.type === PARAMETER.LIST) {
243                                 //validate a list: { value or a list of comma separated values }
244                                 // need to find the value in paramList
245                                 for (var j = 0; j < paramList.length; j++) {
246                                     if (paramList[j].id === p.id) {
247                                         p.value = paramList[j].value;
248                                         var isValid = validateList(p.value);
249                                         if (isValid) {
250                                             $scope.isErrorVisible = false;
251                                             break;
252                                         }
253                                         else {
254                                             showError(FIELD.ERROR.INVALID_LIST + p.id,
255                                                 FIELD.ERROR.LIST_VALIDATE);
256                                             return;
257                                         }
258                                     }
259                                 }
260                             }
261                         }
262                     }
263                     var requestDetails = CreationService
264                         .getMsoRequestDetails($scope.userProvidedControl.getList());
265
266                     $scope.isDialogVisible = false;
267
268                     $scope.$broadcast(COMPONENT.MSO_CREATE_REQ, {
269                         url: CreationService.getMsoUrl(),
270                         requestDetails: requestDetails,
271                         componentId: componentId,
272                         callbackFunction: function (response) {
273                             if (response.isSuccessful) {
274                                 $scope.popup.isVisible = false;
275                                 runCallback(response);
276                             } else {
277                                 $scope.isDialogVisible = false;
278                                 $scope.popup.isVisible = false;
279                             }
280                         }
281                     });
282
283                 } catch (e) {
284                     errorMsg = errorMsg + FIELD.ERROR.INVALID_DATA_FORMAT;
285                 }
286                 if (errorMsg !== "") {
287                     showError(FIELD.ERROR.SYSTEM_FAILURE, errorMsg);
288
289                 }
290             };
291             reader.readAsText(file);
292         } else {
293
294             var paramList = $scope.userProvidedControl.getList();
295             var instanceName = "";
296
297             if (DataService.getALaCarte()) {
298                 if (paramList != null) {
299                     for (var i = 0; i < paramList.length; i++) {
300                         if (paramList[i].id === FIELD.ID.INSTANCE_NAME) {
301                             instanceName = paramList[i].value;
302                             break;
303                         }
304                     }
305                 }
306                 var isValid = validateInstanceName(instanceName);
307                 if (isValid) {
308                     $scope.isErrorVisible = false;
309                 } else {
310                     showError(FIELD.ERROR.INVALID_INSTANCE_NAME + instanceName,
311                         FIELD.ERROR.INSTANCE_NAME_VALIDATE);
312                     return;
313                 }
314             }
315             var arbitraryParametersList = DataService.getArbitraryParameters();
316             var p = null;
317             if (UtilityService.hasContents(arbitraryParametersList)) {
318                 for (var i = 0; i < arbitraryParametersList.length; i++) {
319                     p = arbitraryParametersList[i];
320                     if (p.type === PARAMETER.MAP) {
321                         //validate a map: { <entry_key_1>: <entry_value_1>, ... , <entry_key_n>: <entry_value_n> }
322                         // need to find the value in paramList
323                         for (var j = 0; j < paramList.length; j++) {
324                             if (paramList[j].id === p.id) {
325                                 p.value = paramList[j].value;
326                                 var isValid = validateMap(p.value);
327                                 if (isValid) {
328                                     $scope.isErrorVisible = false;
329                                     break;
330                                 }
331                                 else {
332                                     showError(FIELD.ERROR.INVALID_MAP + p.id,
333                                         FIELD.ERROR.MAP_VALIDATE);
334                                     return;
335                                 }
336                             }
337                         }
338                     } else if (p.type === PARAMETER.LIST) {
339                         //validate a list: { value or a list of comma separated values }
340                         // need to find the value in paramList
341                         for (var j = 0; j < paramList.length; j++) {
342                             if (paramList[j].id === p.id) {
343                                 p.value = paramList[j].value;
344                                 var isValid = validateList(p.value);
345                                 if (isValid) {
346                                     $scope.isErrorVisible = false;
347                                     break;
348                                 }
349                                 else {
350                                     showError(FIELD.ERROR.INVALID_LIST + p.id,
351                                         FIELD.ERROR.LIST_VALIDATE);
352                                     return;
353                                 }
354                             }
355                         }
356                     }
357                 }
358             }
359             var requestDetails = CreationService
360                 .getMsoRequestDetails($scope.userProvidedControl.getList());
361
362             $scope.isDialogVisible = false;
363
364             $scope.$broadcast(COMPONENT.MSO_CREATE_REQ, {
365                 url: CreationService.getMsoUrl(),
366                 requestDetails: requestDetails,
367                 componentId: componentId,
368                 callbackFunction: function (response) {
369                     if (response.isSuccessful) {
370                         $scope.popup.isVisible = false;
371                         runCallback(response);
372                     } else {
373                         $scope.isDialogVisible = false;
374                         $scope.popup.isVisible = false;
375                     }
376                 }
377             });
378         }
379     };
380
381     $scope.cancel = function () {
382         $scope.isDialogVisible = false;
383         $scope.popup.isVisible = false;
384         runCallback(false);
385     };
386
387
388     var runCallback = function (response) {
389         if (angular.isFunction(callbackFunction)) {
390             callbackFunction({
391                 isSuccessful: response.isSuccessful,
392                 control: $scope.userProvidedControl.getList(),
393                 instanceId: response.instanceId
394             });
395         }
396     };
397
398     var showError = function (summary, details) {
399         var message = summary;
400         if (UtilityService.hasContents(details)) {
401             message += " (" + details + ")";
402         }
403         $scope.isSpinnerVisible = false;
404         $scope.isErrorVisible = true;
405         $scope.error = message;
406     }
407
408 };
409
410 appDS2.controller("creationDialogController", ["COMPONENT", "FIELD", "PARAMETER", "$scope", "$http",
411     "$timeout", "$log", "CreationService", "UtilityService", "DataService", "VIDCONFIGURATION", "$location",
412     "$uibModal", "featureFlags",
413     creationDialogController]);