[UI] Common class for 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.blueprintNames = [];
28                         $scope.blueprintVersions = [];
29                         $scope.blueprintLayers = [];
30                         $scope.optionals = [];
31                         $scope.selectedBlueprintName = {};
32                         $scope.selectedBlueprintVersion = {};
33                         $scope.selectedBlueprintLayer = {};
34                         $scope.selectedOptional = "";
35                         restAPISvc
36                                 .getRestAPI(
37                                         "/api/v1/blueprintinstance/",
38                                         function(data) {
39                                             $scope.blueprintInstances = data;
40                                             angular
41                                                     .forEach(
42                                                             $scope.blueprintInstances,
43                                                             function(
44                                                                     blueprintInstance) {
45                                                                 if ($scope.blueprintNames
46                                                                         .indexOf(blueprintInstance["blueprint"]["blueprintName"]
47                                                                                 .trim()) === -1) {
48                                                                     $scope.blueprintNames
49                                                                             .push(blueprintInstance["blueprint"]["blueprintName"]
50                                                                                     .trim());
51                                                                 }
52                                                             });
53                                         });
54                         restAPISvc.getRestAPI("/api/v1/timeslots/", function(
55                                 data) {
56                             $scope.timeslots = data;
57                             $scope.declerativeTimeslots = [];
58                             angular.forEach($scope.timeslots,
59                                     function(timeslot) {
60                                         var temp = "id: " + timeslot.timeslotId
61                                                 + " Start date and time: "
62                                                 + timeslot.startDateTime
63                                                 /*
64                                                  * + " duration(in sec) :" +
65                                                  * blueprintInstance["timeslot"].duration
66                                                  */
67                                                 + " lab :"
68                                                 + timeslot.labInfo.lab;
69                                         $scope.declerativeTimeslots.push(temp);
70                                     });
71                         });
72                     }
73
74                     $scope.selectedBlueprintNameChange = function() {
75                         $scope.blueprintVersions = [];
76                         $scope.blueprintLayers = [];
77                         $scope.optionals = [];
78                         $scope.selectedBlueprintVersion = {};
79                         $scope.selectedBlueprintLayer = {};
80                         $scope.selectedOptional = "";
81                         angular
82                                 .forEach(
83                                         $scope.blueprintInstances,
84                                         function(blueprintInstance) {
85                                             if ($scope.selectedBlueprintName
86                                                     .trim() === blueprintInstance["blueprint"]["blueprintName"]
87                                                     .trim()) {
88                                                 if ($scope.blueprintVersions
89                                                         .indexOf(blueprintInstance["version"]
90                                                                 .trim()) === -1) {
91                                                     $scope.blueprintVersions
92                                                             .push(blueprintInstance["version"]
93                                                                     .trim());
94                                                 }
95                                             }
96                                         });
97                     }
98                     $scope.selectedBlueprintVersionChange = function() {
99                         if (!$scope.selectedBlueprintName) {
100                             return;
101                         }
102                         $scope.blueprintLayers = [];
103                         $scope.optionals = [];
104                         $scope.selectedBlueprintLayer = {};
105                         $scope.selectedOptional = "";
106                         angular
107                                 .forEach(
108                                         $scope.blueprintInstances,
109                                         function(blueprintInstance) {
110                                             if ($scope.selectedBlueprintName
111                                                     .trim() === blueprintInstance["blueprint"]["blueprintName"]
112                                                     .trim()) {
113                                                 if ($scope.selectedBlueprintVersion
114                                                         .trim() === blueprintInstance["version"]
115                                                         .trim()) {
116                                                     angular
117                                                             .forEach(
118                                                                     blueprintInstance.blueprintLayers,
119                                                                     function(
120                                                                             layer) {
121                                                                         $scope.blueprintLayers
122                                                                                 .push(layer.layer);
123                                                                     });
124                                                 }
125                                             }
126                                         });
127                         $scope.blueprintLayers.push("all");
128                     }
129
130                     $scope.selectedBlueprintLayerChange = function() {
131                         $scope.optionals = [ 'true', 'false' ];
132                     }
133
134                     $scope.submit = function() {
135                         if (!$scope.selectedBlueprintName
136                                 || !$scope.selectedBlueprintVersion
137                                 || !$scope.selectedBlueprintLayer
138                                 || !$scope.selectedOptional
139                                 || !$scope.selectedDeclerativeTimeslot) {
140                             confirm("You must specify all data fields");
141                             return;
142                         }
143                         var finalTimeslot;
144                         var selectedDeclerativeTimeslotId = $scope.selectedDeclerativeTimeslot
145                                 .substring(
146                                         $scope.selectedDeclerativeTimeslot
147                                                 .indexOf("id:") + 4,
148                                         $scope.selectedDeclerativeTimeslot
149                                                 .indexOf("Start date and time:") - 1);
150                         angular
151                                 .forEach(
152                                         $scope.timeslots,
153                                         function(timeslot) {
154                                             if (selectedDeclerativeTimeslotId
155                                                     .toString().trim() === timeslot.timeslotId
156                                                     .toString().trim()) {
157                                                 finalTimeslot = timeslot;
158                                             }
159                                         });
160                         var allLayers = "false";
161                         if ($scope.selectedBlueprintLayer === 'all') {
162                             allLayers = "true";
163                         }
164                         var wrobotTestResults = [];
165                         if (allLayers === "false") {
166                             wrobotTestResults = [ {
167                                 "layer" : $scope.selectedBlueprintLayer
168                             } ];
169                         }
170
171                         var blueprintInstanceData = "";
172                         angular
173                                 .forEach(
174                                         $scope.blueprintInstances,
175                                         function(blueprintInstance) {
176                                             if ($scope.selectedBlueprintName
177                                                     .trim() === blueprintInstance["blueprint"]["blueprintName"]
178                                                     .trim()) {
179                                                 if ($scope.selectedBlueprintVersion
180                                                         .trim() === blueprintInstance["version"]
181                                                         .trim()) {
182                                                     blueprintInstanceData = blueprintInstance;
183                                                 }
184                                             }
185                                         });
186                         var validationDbTestResult = {
187                             "blueprintInstance" : blueprintInstanceData,
188                             "allLayers" : allLayers,
189                             "wrobotDbTestResults" : wrobotTestResults,
190                             "optional" : $scope.selectedOptional,
191                             "lab" : finalTimeslot.labInfo
192                         };
193                         var submission = {
194                             "validationDbTestResult" : validationDbTestResult,
195                             "timeslot" : finalTimeslot
196                         };
197                         restAPISvc
198                                 .postRestAPI(
199                                         "/api/v1/submission/",
200                                         submission,
201                                         function(data) {
202                                             if (data) {
203                                                 var confirmText = "The blueprint instance for validation has been submitted successfully. Submission id:"
204                                                         + data.submissionId;
205                                                 confirm(confirmText);
206                                             } else {
207                                                 confirm("Error when committing the submission");
208                                             }
209                                         });
210                         $scope.selectedBlueprintName = {};
211                         $scope.selectedBlueprintVersion = {};
212                         $scope.selectedBlueprintLayer = {};
213                         $scope.selectedOptional = "";
214                         $scope.selectedDeclerativeTimeslot = {};
215                     }
216
217                 });