[VID-12] Delivery of remaining features for v1.1
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / controller / creationDialogController.js
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * VID\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\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  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 "use strict";\r
22 \r
23 var creationDialogController = function( COMPONENT, FIELD, PARAMETER, $scope, $http, $timeout, $log,\r
24                 CreationService, UtilityService, DataService,VIDCONFIGURATION) {\r
25 \r
26         $scope.isDialogVisible = false;\r
27         $scope.summaryControl = {};\r
28         $scope.userProvidedControl = {};\r
29 \r
30         var callbackFunction = undefined;\r
31         var componentId = undefined;\r
32 \r
33         $scope.$on(COMPONENT.CREATE_COMPONENT, function(event, request) {\r
34 \r
35                 $scope.isSpinnerVisible = true;\r
36                 $scope.isErrorVisible = false;\r
37                 $scope.isDataVisible = false;\r
38                 $scope.isConfirmEnabled = false;\r
39                 $scope.isDialogVisible = true;\r
40                 $scope.popup.isVisible = true;\r
41 \r
42                 callbackFunction = request.callbackFunction;\r
43                 componentId = request.componentId;\r
44                 CreationService.initializeComponent(request.componentId);\r
45 \r
46                 CreationService.setHttpErrorHandler(function(response) {\r
47                         showError(FIELD.ERROR.SYSTEM_FAILURE, UtilityService\r
48                                         .getHttpErrorMessage(response));\r
49                 });\r
50 \r
51                 $scope.componentName = CreationService.getComponentDisplayName();\r
52 \r
53                 CreationService.getParameters(handleGetParametersResponse);\r
54 \r
55         });\r
56 \r
57         var handleGetParametersResponse = function(parameters) {\r
58                 $scope.summaryControl.setList(parameters.summaryList);\r
59                 $scope.userProvidedControl.setList(parameters.userProvidedList);\r
60 \r
61                 $scope.isSpinnerVisible = false;\r
62                 $scope.isDataVisible = true;\r
63                 $scope.isConfirmEnabled = true;\r
64         };\r
65         \r
66         var validateInstanceName = function(iname) {\r
67                 var patt1 = /^([a-z])+([0-9a-z\-_\.]*)$/i;\r
68                 \r
69                 if ( iname == null ){\r
70                         return false;\r
71                 }\r
72                 if ( !iname.match(patt1) ) {\r
73                         return false;\r
74                 }\r
75                 return true;\r
76         };\r
77         var validateMap = function(mname) {\r
78                 var patt1 = /^{(\s*\w+\s*:\s*\w+\s*)(\s*,\s*\w+\s*:\s*\w+\s*)*}$/im;\r
79                 if ( mname == null ){\r
80                         return true;\r
81                 }\r
82                 if ( !mname.match(patt1) ) {\r
83                         return false;\r
84                 }\r
85                 return true;\r
86         };\r
87         \r
88         var validateList = function(lname) {\r
89                 var patt1 = /^\[(\s*\w+\s*)(\s*,\s*\w+\s*)*\]$/i;\r
90                 if ( lname == null ){\r
91                         return true;\r
92                 }\r
93                 if ( !lname.match(patt1) ) {\r
94                         return false;\r
95                 }\r
96                 return true;\r
97         };\r
98                 \r
99         $scope.userParameterChanged = function(id) {\r
100                 CreationService.updateUserParameterList(id, $scope.userProvidedControl);\r
101         }\r
102 \r
103         $scope.confirm = function() {\r
104 \r
105                 var requiredFields = $scope.userProvidedControl.getRequiredFields();\r
106                 if (requiredFields !== "") {\r
107                         showError(FIELD.ERROR.MISSING_DATA, requiredFields);\r
108                         return;\r
109                 }\r
110         \r
111                 var paramList = $scope.userProvidedControl.getList();\r
112                 var instanceName = "";\r
113 \r
114                 if ( DataService.getALaCarte() ) {\r
115                         if ( paramList != null ) {\r
116                                 for (var i = 0; i < paramList.length; i++) {\r
117                                         if (paramList[i].id === FIELD.ID.INSTANCE_NAME) {\r
118                                                 instanceName = paramList[i].value;\r
119                                                 break;\r
120                                         }\r
121                                 }\r
122                         }\r
123                         var isValid = validateInstanceName (instanceName);\r
124                         if ( isValid ) {\r
125                                 $scope.isErrorVisible = false;\r
126                         } else {\r
127                                 showError(FIELD.ERROR.INVALID_INSTANCE_NAME + instanceName, \r
128                                                 FIELD.ERROR.INSTANCE_NAME_VALIDATE);\r
129                                 return;\r
130                         }\r
131                 }\r
132                 var arbitraryParametersList = DataService.getArbitraryParameters();\r
133                 var p = null;\r
134                 if (UtilityService.hasContents (arbitraryParametersList)) {\r
135                         for (var i = 0; i < arbitraryParametersList.length; i++) {\r
136                                 p = arbitraryParametersList[i];\r
137                                 if (p.type === PARAMETER.MAP) {\r
138                                         //validate a map: { <entry_key_1>: <entry_value_1>, ... , <entry_key_n>: <entry_value_n> }\r
139                                         // need to find the value in paramList\r
140                                         for (var j = 0; j < paramList.length; j++) {\r
141                                                 if (paramList[j].id === p.id) {\r
142                                                         p.value = paramList[j].value;\r
143                                                         var isValid = validateMap (p.value);\r
144                                                         if ( isValid ) {\r
145                                                                 $scope.isErrorVisible = false;\r
146                                                                 break;\r
147                                                         } \r
148                                                         else {\r
149                                                                 showError(FIELD.ERROR.INVALID_MAP + p.id, \r
150                                                                                 FIELD.ERROR.MAP_VALIDATE);\r
151                                                                 return;\r
152                                                         }       \r
153                                                 }\r
154                                         }\r
155                                 } else if (p.type === PARAMETER.LIST) {\r
156                                         //validate a list: { value or a list of comma separated values }\r
157                                         // need to find the value in paramList\r
158                                         for (var j = 0; j < paramList.length; j++) {\r
159                                                 if (paramList[j].id === p.id) {\r
160                                                         p.value = paramList[j].value;\r
161                                                         var isValid = validateList (p.value);\r
162                                                         if ( isValid ) {\r
163                                                                 $scope.isErrorVisible = false;\r
164                                                                 break;\r
165                                                         } \r
166                                                         else {\r
167                                                                 showError(FIELD.ERROR.INVALID_LIST + p.id, \r
168                                                                                 FIELD.ERROR.LIST_VALIDATE);\r
169                                                                 return;\r
170                                                         }       \r
171                                                 }\r
172                                         }\r
173                                 }\r
174                         }\r
175                 }\r
176                 var requestDetails = CreationService\r
177                                 .getMsoRequestDetails($scope.userProvidedControl.getList());\r
178 \r
179                 $scope.isDialogVisible = false;\r
180 \r
181                 $scope.$broadcast(COMPONENT.MSO_CREATE_REQ, {\r
182                         url : CreationService.getMsoUrl(),\r
183                         requestDetails : requestDetails,\r
184                         componentId: componentId,\r
185                         callbackFunction : function(response) {\r
186                                 if (response.isSuccessful) {\r
187                                         $scope.popup.isVisible = false;\r
188                                         runCallback(response);\r
189                                 } else {\r
190                                         $scope.isDialogVisible = false;\r
191                                         $scope.popup.isVisible = false;\r
192                                 }\r
193                         }\r
194                 });\r
195         }\r
196 \r
197         $scope.cancel = function() {\r
198                 $scope.isDialogVisible = false;\r
199                 $scope.popup.isVisible = false;\r
200                 runCallback(false);\r
201         }\r
202 \r
203         var runCallback = function(response) {\r
204                 if (angular.isFunction(callbackFunction)) {\r
205                         callbackFunction({\r
206                                 isSuccessful : response.isSuccessful,\r
207                                 control : $scope.userProvidedControl.getList(),\r
208                                 instanceId : response.instanceId\r
209                         });\r
210                 }\r
211         }\r
212 \r
213         var showError = function(summary, details) {\r
214                 var message = summary;\r
215                 if (UtilityService.hasContents(details)) {\r
216                         message += " (" + details + ")";\r
217                 }\r
218                 $scope.isSpinnerVisible = false;\r
219                 $scope.isErrorVisible = true;\r
220                 $scope.error = message;\r
221         }\r
222         \r
223 }\r
224 \r
225 appDS2.controller("creationDialogController", [ "COMPONENT", "FIELD", "PARAMETER", "$scope", "$http",\r
226                                 "$timeout", "$log", "CreationService", "UtilityService", "DataService","VIDCONFIGURATION",\r
227                                 creationDialogController ]);\r