[UI] Ignore malformed results
[validation.git] / ui / src / main / webapp / app / BluvalUI / NewSubmission / NewSubmissionController.js
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 var app = angular.module('NewSubmission');
18 app
19         .controller(
20                 'NewSubmissionController',
21                 function($scope, restAPISvc) {
22
23                     initialize();
24
25                     function initialize() {
26                         $scope.blueprintInstances = [];
27                         $scope.blueprintInstance = '';
28                         $scope.blueprintNames = [];
29                         $scope.blueprintVersions = [];
30                         $scope.declerativeTimeslots = [];
31                         $scope.blueprintLayers = [];
32                         $scope.optionals = [];
33                         $scope.selectedBlueprintName = '';
34                         $scope.selectedBlueprintVersion = '';
35                         $scope.selectedDeclerativeTimeslot = '';
36                         $scope.selectedBlueprintLayer = '';
37                         $scope.selectedOptional = '';
38                         restAPISvc
39                                 .getRestAPI(
40                                         "/api/v1/blueprintinstance/",
41                                         function(data) {
42                                             $scope.blueprintInstances = data;
43                                             angular
44                                                     .forEach(
45                                                             $scope.blueprintInstances,
46                                                             function(
47                                                                     blueprintInstance) {
48                                                                 if ($scope.blueprintNames
49                                                                         .indexOf(blueprintInstance["blueprint"]["blueprintName"]
50                                                                                 .trim()) === -1) {
51                                                                     $scope.blueprintNames
52                                                                             .push(blueprintInstance["blueprint"]["blueprintName"]
53                                                                                     .trim());
54                                                                 }
55                                                             });
56                                         });
57                     }
58                     $scope.selectedBlueprintNameChange = function() {
59                         $scope.blueprintInstance = '';
60                         $scope.blueprintVersions = [];
61                         $scope.declerativeTimeslots = [];
62                         $scope.blueprintLayers = [];
63                         $scope.optionals = [];
64                         $scope.selectedBlueprintVersion = '';
65                         $scope.selectedDeclerativeTimeslot = '';
66                         $scope.selectedBlueprintLayer = '';
67                         $scope.selectedOptional = '';
68                         angular
69                                 .forEach(
70                                         $scope.blueprintInstances,
71                                         function(blueprintInstance) {
72                                             if ($scope.selectedBlueprintName
73                                                     .trim() === blueprintInstance["blueprint"]["blueprintName"]
74                                                     .trim()) {
75                                                 if ($scope.blueprintVersions
76                                                         .indexOf(blueprintInstance["version"]
77                                                                 .trim()) === -1) {
78                                                     $scope.blueprintVersions
79                                                             .push(blueprintInstance["version"]
80                                                                     .trim());
81                                                 }
82                                             }
83                                         });
84                     }
85                     $scope.selectedBlueprintVersionChange = function() {
86                         if (!$scope.selectedBlueprintName) {
87                             return;
88                         }
89                         $scope.blueprintInstance = '';
90                         $scope.declerativeTimeslots = [];
91                         $scope.blueprintLayers = [];
92                         $scope.optionals = [];
93                         $scope.selectedDeclerativeTimeslot = '';
94                         $scope.selectedBlueprintLayer = '';
95                         $scope.selectedOptional = '';
96                         angular
97                                 .forEach(
98                                         $scope.blueprintInstances,
99                                         function(blueprintInstance) {
100                                             if ($scope.selectedBlueprintName
101                                                     .trim() === blueprintInstance["blueprint"]["blueprintName"]
102                                                     .trim()) {
103                                                 if ($scope.selectedBlueprintVersion
104                                                         .trim() === blueprintInstance["version"]
105                                                         .trim()) {
106                                                     $scope.blueprintInstance = blueprintInstance;
107                                                 }
108                                             }
109                                         });
110                         if (!$scope.blueprintInstance
111                                 || !$scope.blueprintInstance.timeslots
112                                 || $scope.blueprintInstance.timeslots.length === 0) {
113                             confirm("No available timeslots for this blueprint instance in this lab");
114                             return;
115                         }
116                         angular.forEach($scope.blueprintInstance.timeslots,
117                                 function(timeslot) {
118                                     var temp = "id: " + timeslot.timeslotId
119                                             + " Start date and time: "
120                                             + timeslot.startDateTime
121                                             /*
122                                              * + " duration(in sec) :" +
123                                              * blueprintInstance["timeslot"].duration
124                                              */
125                                             + " lab :" + timeslot.labInfo.lab;
126                                     $scope.declerativeTimeslots.push(temp);
127                                 });
128                     }
129
130                     $scope.selectedDeclerativeTimeslotChange = function() {
131                         $scope.blueprintLayers = [];
132                         $scope.optionals = [];
133                         $scope.selectedBlueprintLayer = {};
134                         $scope.selectedOptional = "";
135                         angular.forEach(
136                                 $scope.blueprintInstance.blueprintLayers,
137                                 function(layer) {
138                                     $scope.blueprintLayers.push(layer.layer);
139                                 });
140                         $scope.blueprintLayers.push("all");
141                     }
142
143                     $scope.selectedBlueprintLayerChange = function() {
144                         $scope.optionals = [];
145                         $scope.selectedOptional = "";
146                         $scope.optionals = [ 'true', 'false' ];
147                     }
148
149                     $scope.submit = function() {
150                         if (!$scope.selectedBlueprintName
151                                 || !$scope.selectedBlueprintVersion
152                                 || !$scope.blueprintInstance
153                                 || !$scope.selectedBlueprintLayer
154                                 || !$scope.selectedOptional
155                                 || !$scope.selectedDeclerativeTimeslot) {
156                             confirm("You must specify all data fields");
157                             return;
158                         }
159                         var finalTimeslot;
160                         var selectedDeclerativeTimeslotId = $scope.selectedDeclerativeTimeslot
161                                 .substring(
162                                         $scope.selectedDeclerativeTimeslot
163                                                 .indexOf("id:") + 4,
164                                         $scope.selectedDeclerativeTimeslot
165                                                 .indexOf("Start date and time:") - 1);
166                         angular
167                                 .forEach(
168                                         $scope.blueprintInstance.timeslots,
169                                         function(timeslot) {
170                                             if (selectedDeclerativeTimeslotId
171                                                     .toString().trim() === timeslot.timeslotId
172                                                     .toString().trim()) {
173                                                 finalTimeslot = timeslot;
174                                             }
175                                         });
176                         var allLayers = "false";
177                         if ($scope.selectedBlueprintLayer === 'all') {
178                             allLayers = "true";
179                         }
180                         var wrobotTestResults = [];
181                         if (allLayers === "false") {
182                             wrobotTestResults = [ {
183                                 "layer" : $scope.selectedBlueprintLayer
184                             } ];
185                         }
186
187                         var validationDbTestResult = {
188                             "blueprintInstance" : $scope.blueprintInstance,
189                             "allLayers" : allLayers,
190                             "wrobotDbTestResults" : wrobotTestResults,
191                             "optional" : $scope.selectedOptional,
192                             "lab" : finalTimeslot.labInfo
193                         };
194                         var submission = {
195                             "validationDbTestResult" : validationDbTestResult,
196                             "timeslot" : finalTimeslot
197                         };
198                         restAPISvc
199                                 .postRestAPI(
200                                         "/api/v1/submission/",
201                                         submission,
202                                         function(data) {
203                                             if (data) {
204                                                 var confirmText = "The blueprint instance for validation has been submitted successfully. Submission id:"
205                                                         + data.submissionId;
206                                                 confirm(confirmText);
207                                             } else {
208                                                 confirm("Error when committing the submission");
209                                             }
210                                         });
211                         $scope.selectedBlueprintName = '';
212                         $scope.selectedBlueprintVersion = '';
213                         $scope.selectedBlueprintLayer = '';
214                         $scope.selectedOptional = "";
215                         $scope.selectedDeclerativeTimeslot = '';
216                         $scope.blueprintInstance = '';
217                     }
218
219                 });