[UI] Support UI partial control
[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                         restAPISvc
27                                 .getRestAPI(
28                                         "/api/v1/blueprintinstanceforvalidation/",
29                                         function(data) {
30                                             $scope.blueprintInstancesForValidation = data;
31                                             $scope.blueprintNames = [];
32                                             angular
33                                                     .forEach(
34                                                             $scope.blueprintInstancesForValidation,
35                                                             function(
36                                                                     blueprintInstance) {
37                                                                 if ($scope.blueprintNames
38                                                                         .indexOf(blueprintInstance["blueprint"]["blueprintName"]) === -1) {
39                                                                     $scope.blueprintNames
40                                                                             .push(blueprintInstance["blueprint"]["blueprintName"]);
41                                                                 }
42                                                             });
43                                         });
44                         restAPISvc.getRestAPI("/api/v1/timeslots/", function(
45                                 data) {
46                             $scope.timeslots = data;
47                             $scope.declerativeTimeslots = [];
48                             angular.forEach($scope.timeslots,
49                                     function(timeslot) {
50                                         var temp = "id: " + timeslot.timeslotId
51                                                 + " Start date and time: "
52                                                 + timeslot.startDateTime
53                                                 /*
54                                                  * + " duration(in sec) :" +
55                                                  * blueprintInstance["timeslot"].duration
56                                                  */
57                                                 + " lab :" + timeslot.lab.lab;
58                                         $scope.declerativeTimeslots.push(temp);
59                                     });
60                         });
61                     }
62                     $scope.selectedBluePrintNameChange = function() {
63                         $scope.blueprintVersions = [];
64                         $scope.blueprintLayers = [];
65                         $scope.optionals = [];
66                         $scope.selectedBlueprintVersion = {};
67                         $scope.selectedBlueprintLayer = {};
68                         $scope.selectedOptional = "";
69                         angular
70                                 .forEach(
71                                         $scope.blueprintInstancesForValidation,
72                                         function(blueprintInstance) {
73                                             if ($scope.selectedBlueprintName === blueprintInstance["blueprint"]["blueprintName"]) {
74                                                 if ($scope.blueprintVersions
75                                                         .indexOf(blueprintInstance["version"]) === -1) {
76                                                     $scope.blueprintVersions
77                                                             .push(blueprintInstance["version"]);
78                                                 }
79                                             }
80                                         });
81                     }
82                     $scope.selectedBluePrintVersionChange = function() {
83                         if (!$scope.selectedBlueprintName) {
84                             return;
85                         }
86                         $scope.blueprintLayers = [];
87                         $scope.optionals = [];
88                         $scope.selectedBlueprintLayer = {};
89                         $scope.selectedOptional = "";
90                         angular
91                                 .forEach(
92                                         $scope.blueprintInstancesForValidation,
93                                         function(blueprintInstance) {
94                                             if ($scope.selectedBlueprintName === blueprintInstance["blueprint"]["blueprintName"]) {
95                                                 if ($scope.selectedBlueprintVersion === blueprintInstance["version"]) {
96                                                     if ($scope.blueprintLayers
97                                                             .indexOf(blueprintInstance["layer"]) === -1) {
98                                                         $scope.blueprintLayers
99                                                                 .push(blueprintInstance["layer"]);
100                                                     }
101                                                 }
102                                             }
103                                         });
104                         $scope.blueprintLayers.push("all");
105                     }
106
107                     $scope.selectedBluePrintLayerChange = function() {
108                         $scope.optionals = [ 'true', 'false' ];
109                     }
110
111                     $scope.submit = function() {
112                         if (!$scope.selectedBlueprintName
113                                 || !$scope.selectedBlueprintVersion
114                                 || !$scope.selectedBlueprintLayer
115                                 || !$scope.selectedOptional
116                                 || !$scope.selectedDeclerativeTimeslot) {
117                             confirm("You must specify all data fields");
118                             return;
119                         }
120                         var finalTimeslot;
121                         var selectedDeclerativeTimeslotId = $scope.selectedDeclerativeTimeslot
122                                 .substring(
123                                         $scope.selectedDeclerativeTimeslot
124                                                 .indexOf("id:") + 4,
125                                         $scope.selectedDeclerativeTimeslot
126                                                 .indexOf("Start date and time:") - 1);
127                         angular
128                                 .forEach(
129                                         $scope.timeslots,
130                                         function(timeslot) {
131                                             if (selectedDeclerativeTimeslotId
132                                                     .toString().trim() === timeslot.timeslotId
133                                                     .toString().trim()) {
134                                                 finalTimeslot = timeslot;
135                                             }
136                                         });
137                         var allLayers = "false";
138                         if ($scope.selectedBlueprintLayer === 'all') {
139                             allLayers = "true";
140                         }
141                         var wRobotTestResults = [];
142                         if (allLayers === "false") {
143                             wRobotTestResults = [ {
144                                 "blueprintLayer" : $scope.selectedBlueprintLayer
145                             } ];
146                         }
147
148                         var validationNexusTestResult = {
149                             "blueprintName" : $scope.selectedBlueprintName,
150                             "version" : $scope.selectedBlueprintVersion,
151                             "allLayers" : allLayers,
152                             "wRobotNexusTestResults" : wRobotTestResults,
153                             "optional" : $scope.selectedOptional
154                         };
155                         var submissionData = {
156                             "validationNexusTestResult" : validationNexusTestResult,
157                             "timeslot" : finalTimeslot
158                         };
159                         restAPISvc
160                                 .postRestAPI(
161                                         "/api/v1/submission/",
162                                         submissionData,
163                                         function(data) {
164                                             if (data !== undefined) {
165                                                 var confirmText = "The blueprint instance for validation has been submitted successfully. Submission id:"
166                                                         + data.submissionId;
167                                                 confirm(confirmText);
168                                             } else {
169                                                 confirm("Error when committing the submission");
170                                             }
171                                         });
172                         $scope.selectedBlueprintName = {};
173                         $scope.selectedBlueprintVersion = {};
174                         $scope.selectedBlueprintLayer = {};
175                         $scope.selectedOptional = "";
176                         $scope.selectedDeclerativeTimeslot = {};
177                     }
178
179                 });