7b6d4af6c1a68d2388ea8174ac573b9b175fa5bf
[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             if(DataService.getIsInstantiationTemplateExists()){
97                 $scope.url = COMPONENT.INSTANTIATION_TEMPLATES_IFRAME_URL + request.modelNameVersionId;
98                 window.addEventListener("message", receiveMessage, false);
99             }else {
100                 $scope.url = COMPONENT.SERVICE_POPUP_IFRAME_URL + request.modelNameVersionId + "&isCreate=true&r=" + Math.random();
101                 window.addEventListener("message", receiveMessage, false);
102             }
103         }
104         else {
105             callbackFunction = request.callbackFunction;
106             componentId = request.componentId;
107             CreationService.initializeComponent(request.componentId);
108
109             CreationService.setHttpErrorHandler(function (response) {
110                 $scope.isServiceError = true;
111                 showError(FIELD.ERROR.SYSTEM_FAILURE, UtilityService
112                     .getHttpErrorMessage(response));
113             });
114
115             $scope.componentName = CreationService.getComponentDisplayName();
116
117             CreationService.getParameters(handleGetParametersResponse);
118         }
119
120     });
121
122     var handleGetParametersResponse = function (parameters) {
123         $scope.summaryControl.setList(parameters.summaryList);
124         $scope.userProvidedControl.setList(parameters.userProvidedList);
125
126         $scope.isSpinnerVisible = false;
127         $scope.isDataVisible = true;
128         $scope.isConfirmEnabled = true;
129     };
130
131     var validateInstanceName = function (iname) {
132         var patt1 = /^([a-z])+([0-9a-z\-_\.]*)$/i;
133
134         if (iname == null) {
135             return false;
136         }
137         if (!iname.match(patt1)) {
138             return false;
139         }
140         return true;
141     };
142     var validateMap = function (mname) {
143         var patt1 = /^{(\s*\w+\s*:\s*\w+\s*)(\s*,\s*\w+\s*:\s*\w+\s*)*}$/im;
144         if (mname == null) {
145             return true;
146         }
147         if (!mname.match(patt1)) {
148             return false;
149         }
150         return true;
151     };
152
153     var validateList = function (lname) {
154         var patt1 = /^\[(\s*\w+\s*)(\s*,\s*\w+\s*)*\]$/i;
155         if (lname == null) {
156             return true;
157         }
158         if (!lname.match(patt1)) {
159             return false;
160         }
161         return true;
162     };
163
164     $scope.userParameterChanged = function (id) {
165         CreationService.updateUserParameterList(id, $scope.userProvidedControl);
166     };
167
168     $scope.confirm = function () {
169
170         var requiredFields = $scope.userProvidedControl.getRequiredFields();
171         if (requiredFields !== "") {
172             showError(FIELD.ERROR.MISSING_DATA, requiredFields);
173             return;
174         }
175
176         var isUploadAvailable = false;
177         var uploadIndex = 0;
178         var paramList = $scope.userProvidedControl.getList();
179         var isAnyError = false;
180         for (var i = 0; i < paramList.length; i++) {
181             if (paramList[i].id === FIELD.ID.SUPPLEMENTORY_DATA_FILE) {
182                 isUploadAvailable = true;
183                 uploadIndex = i;
184             }
185             if (paramList[i].id === FIELD.ID.UPLOAD_SUPPLEMENTORY_DATA_FILE && paramList[i].value && document.getElementById(FIELD.ID.SUPPLEMENTORY_DATA_FILE).value == '') {
186                 isAnyError = true;
187             }
188         }
189
190         if (isUploadAvailable && isAnyError) {
191             showError(FIELD.ERROR.MISSING_DATA, FIELD.ERROR.MISSING_FILE);
192
193         } else if (isUploadAvailable && document.getElementById(FIELD.ID.SUPPLEMENTORY_DATA_FILE).value != '') {
194             var errorMsg = "";
195             var fileInput = document.getElementById(FIELD.ID.SUPPLEMENTORY_DATA_FILE);
196             var file = fileInput.files[0];
197             var reader = new FileReader();
198             reader.onload = function (e) {
199                 try {
200                     paramList[uploadIndex].value = JSON.parse(reader.result);
201                     FIELD.PARAMETER.SUPPLEMENTORY_DATA_FILE['value'] = paramList[uploadIndex].value;
202
203                     var instanceName = "";
204
205                     if (DataService.getALaCarte()) {
206                         if (paramList != null) {
207                             for (var i = 0; i < paramList.length; i++) {
208                                 if (paramList[i].id === FIELD.ID.INSTANCE_NAME) {
209                                     instanceName = paramList[i].value;
210                                     break;
211                                 }
212                             }
213                         }
214                         var isValid = validateInstanceName(instanceName);
215                         if (isValid) {
216                             $scope.isErrorVisible = false;
217                         } else {
218                             showError(FIELD.ERROR.INVALID_INSTANCE_NAME + instanceName,
219                                 FIELD.ERROR.INSTANCE_NAME_VALIDATE);
220                             return;
221                         }
222                     }
223                     var arbitraryParametersList = DataService.getArbitraryParameters();
224                     var p = null;
225                     if (UtilityService.hasContents(arbitraryParametersList)) {
226                         for (var i = 0; i < arbitraryParametersList.length; i++) {
227                             p = arbitraryParametersList[i];
228                             if (p.type === PARAMETER.MAP) {
229                                 //validate a map: { <entry_key_1>: <entry_value_1>, ... , <entry_key_n>: <entry_value_n> }
230                                 // need to find the value in paramList
231                                 for (var j = 0; j < paramList.length; j++) {
232                                     if (paramList[j].id === p.id) {
233                                         p.value = paramList[j].value;
234                                         var isValid = validateMap(p.value);
235                                         if (isValid) {
236                                             $scope.isErrorVisible = false;
237                                             break;
238                                         }
239                                         else {
240                                             showError(FIELD.ERROR.INVALID_MAP + p.id,
241                                                 FIELD.ERROR.MAP_VALIDATE);
242                                             return;
243                                         }
244                                     }
245                                 }
246                             } else if (p.type === PARAMETER.LIST) {
247                                 //validate a list: { value or a list of comma separated values }
248                                 // need to find the value in paramList
249                                 for (var j = 0; j < paramList.length; j++) {
250                                     if (paramList[j].id === p.id) {
251                                         p.value = paramList[j].value;
252                                         var isValid = validateList(p.value);
253                                         if (isValid) {
254                                             $scope.isErrorVisible = false;
255                                             break;
256                                         }
257                                         else {
258                                             showError(FIELD.ERROR.INVALID_LIST + p.id,
259                                                 FIELD.ERROR.LIST_VALIDATE);
260                                             return;
261                                         }
262                                     }
263                                 }
264                             }
265                         }
266                     }
267                     var requestDetails = CreationService
268                         .getMsoRequestDetails($scope.userProvidedControl.getList());
269
270                     $scope.isDialogVisible = false;
271
272                     $scope.$broadcast(COMPONENT.MSO_CREATE_REQ, {
273                         url: CreationService.getMsoUrl(),
274                         requestDetails: requestDetails,
275                         componentId: componentId,
276                         callbackFunction: function (response) {
277                             if (response.isSuccessful) {
278                                 $scope.popup.isVisible = false;
279                                 runCallback(response);
280                             } else {
281                                 $scope.isDialogVisible = false;
282                                 $scope.popup.isVisible = false;
283                             }
284                         }
285                     });
286
287                 } catch (e) {
288                     errorMsg = errorMsg + FIELD.ERROR.INVALID_DATA_FORMAT;
289                 }
290                 if (errorMsg !== "") {
291                     showError(FIELD.ERROR.SYSTEM_FAILURE, errorMsg);
292
293                 }
294             };
295             reader.readAsText(file);
296         } else {
297
298             var paramList = $scope.userProvidedControl.getList();
299             var instanceName = "";
300
301             if (DataService.getALaCarte()) {
302                 if (paramList != null) {
303                     for (var i = 0; i < paramList.length; i++) {
304                         if (paramList[i].id === FIELD.ID.INSTANCE_NAME) {
305                             instanceName = paramList[i].value;
306                             break;
307                         }
308                     }
309                 }
310                 var isValid = validateInstanceName(instanceName);
311                 if (isValid) {
312                     $scope.isErrorVisible = false;
313                 } else {
314                     showError(FIELD.ERROR.INVALID_INSTANCE_NAME + instanceName,
315                         FIELD.ERROR.INSTANCE_NAME_VALIDATE);
316                     return;
317                 }
318             }
319             var arbitraryParametersList = DataService.getArbitraryParameters();
320             var p = null;
321             if (UtilityService.hasContents(arbitraryParametersList)) {
322                 for (var i = 0; i < arbitraryParametersList.length; i++) {
323                     p = arbitraryParametersList[i];
324                     if (p.type === PARAMETER.MAP) {
325                         //validate a map: { <entry_key_1>: <entry_value_1>, ... , <entry_key_n>: <entry_value_n> }
326                         // need to find the value in paramList
327                         for (var j = 0; j < paramList.length; j++) {
328                             if (paramList[j].id === p.id) {
329                                 p.value = paramList[j].value;
330                                 var isValid = validateMap(p.value);
331                                 if (isValid) {
332                                     $scope.isErrorVisible = false;
333                                     break;
334                                 }
335                                 else {
336                                     showError(FIELD.ERROR.INVALID_MAP + p.id,
337                                         FIELD.ERROR.MAP_VALIDATE);
338                                     return;
339                                 }
340                             }
341                         }
342                     } else if (p.type === PARAMETER.LIST) {
343                         //validate a list: { value or a list of comma separated values }
344                         // need to find the value in paramList
345                         for (var j = 0; j < paramList.length; j++) {
346                             if (paramList[j].id === p.id) {
347                                 p.value = paramList[j].value;
348                                 var isValid = validateList(p.value);
349                                 if (isValid) {
350                                     $scope.isErrorVisible = false;
351                                     break;
352                                 }
353                                 else {
354                                     showError(FIELD.ERROR.INVALID_LIST + p.id,
355                                         FIELD.ERROR.LIST_VALIDATE);
356                                     return;
357                                 }
358                             }
359                         }
360                     }
361                 }
362             }
363             var requestDetails = CreationService
364                 .getMsoRequestDetails($scope.userProvidedControl.getList());
365
366             $scope.isDialogVisible = false;
367
368             $scope.$broadcast(COMPONENT.MSO_CREATE_REQ, {
369                 url: CreationService.getMsoUrl(),
370                 requestDetails: requestDetails,
371                 componentId: componentId,
372                 callbackFunction: function (response) {
373                     if (response.isSuccessful) {
374                         $scope.popup.isVisible = false;
375                         runCallback(response);
376                     } else {
377                         $scope.isDialogVisible = false;
378                         $scope.popup.isVisible = false;
379                         runCallback(response);
380                     }
381                 }
382             });
383         }
384     };
385
386     $scope.cancel = function () {
387         $scope.isDialogVisible = false;
388         $scope.popup.isVisible = false;
389         runCallback(false);
390     };
391
392
393     var runCallback = function (response) {
394         if (angular.isFunction(callbackFunction)) {
395             callbackFunction({
396                 isSuccessful: response.isSuccessful,
397                 control: $scope.userProvidedControl.getList(),
398                 instanceId: response.instanceId
399             });
400         }
401     };
402
403     var showError = function (summary, details) {
404         var message = summary;
405         if (UtilityService.hasContents(details)) {
406             message += " (" + details + ")";
407         }
408         $scope.isSpinnerVisible = false;
409         $scope.isErrorVisible = true;
410         $scope.error = message;
411     }
412
413 };
414
415 appDS2.controller("creationDialogController", ["COMPONENT", "FIELD", "PARAMETER", "$scope", "$http",
416     "$timeout", "$log", "CreationService", "UtilityService", "DataService", "VIDCONFIGURATION", "$location",
417     "$uibModal", "featureFlags",
418     creationDialogController]);