Merge "[UI] UI enhancements"
[validation.git] / ui / src / main / webapp / app / AECBlueprintValidationUI / NewSubmission / AECNewSubmissionController.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('AECNewSubmission');
18 app
19         .controller(
20                 'AECNewSubmissionController',
21                 function($scope, restAPISvc) {
22
23                     initialize();
24
25                     function initialize() {
26                         restAPISvc
27                                 .getRestAPI(
28                                         "/api/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/timeslots/",
45                                 function(data) {
46                                     $scope.timeslots = data;
47                                     $scope.declerativeTimeslots = [];
48                                     angular.forEach($scope.timeslots, function(
49                                             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                         angular
66                                 .forEach(
67                                         $scope.blueprintInstancesForValidation,
68                                         function(blueprintInstance) {
69                                             if ($scope.selectedBlueprintName === blueprintInstance["blueprint"]["blueprintName"]) {
70                                                 if ($scope.blueprintVersions
71                                                         .indexOf(blueprintInstance["version"]) === -1) {
72                                                     $scope.blueprintVersions
73                                                             .push(blueprintInstance["version"]);
74                                                 }
75                                             }
76                                         });
77                     }
78                     $scope.selectedBluePrintVersionChange = function() {
79                         $scope.blueprintLayers = [];
80                         angular
81                                 .forEach(
82                                         $scope.blueprintInstancesForValidation,
83                                         function(blueprintInstance) {
84                                             if ($scope.selectedBlueprintName === blueprintInstance["blueprint"]["blueprintName"]) {
85                                                 if ($scope.selectedBlueprintVersion === blueprintInstance["version"]) {
86                                                     if ($scope.blueprintLayers
87                                                             .indexOf(blueprintInstance["layer"]) === -1) {
88                                                         $scope.blueprintLayers
89                                                                 .push(blueprintInstance["layer"]);
90                                                     }
91                                                 }
92                                             }
93                                         });
94                     }
95
96                     $scope.submit = function() {
97                         if (!$scope.selectedBlueprintName
98                                 || !$scope.selectedBlueprintVersion
99                                 || !$scope.selectedBlueprintLayer
100                                 || !$scope.selectedDeclerativeTimeslot) {
101                             confirm("You must specify all data fields");
102                             return;
103                         }
104                         var finalBlueprint;
105                         var finalTimeslot;
106                         angular
107                                 .forEach(
108                                         $scope.blueprintInstancesForValidation,
109                                         function(blueprintInstance) {
110                                             if (blueprintInstance["blueprint"]["blueprintName"] === $scope.selectedBlueprintName) {
111                                                 if (blueprintInstance["version"] === $scope.selectedBlueprintVersion) {
112                                                     if (blueprintInstance["layer"] === $scope.selectedBlueprintLayer) {
113                                                         finalBlueprint = blueprintInstance;
114                                                     }
115                                                 }
116                                             }
117                                         });
118                         var selectedDeclerativeTimeslotId = $scope.selectedDeclerativeTimeslot
119                                 .substring(
120                                         $scope.selectedDeclerativeTimeslot
121                                                 .indexOf("id:") + 4,
122                                         $scope.selectedDeclerativeTimeslot
123                                                 .indexOf("Start date and time:") - 1);
124                         angular
125                                 .forEach(
126                                         $scope.timeslots,
127                                         function(timeslot) {
128                                             if (selectedDeclerativeTimeslotId
129                                                     .toString().trim() === timeslot.timeslotId
130                                                     .toString().trim()) {
131                                                 finalTimeslot = timeslot;
132                                             }
133                                         });
134                         var submission = {
135                             "blueprintInstanceForValidation" : finalBlueprint,
136                             "timeslot" : finalTimeslot
137                         };
138                         restAPISvc
139                                 .postRestAPI(
140                                         "/api/submission/",
141                                         submission,
142                                         function(data) {
143                                             if (data !== undefined) {
144                                                 var confirmText = "The blueprint instance for validation has been submitted successfully. Submissionn id:"
145                                                         + data.submissionId;
146                                                 confirm(confirmText);
147                                             } else {
148                                                 confirm("Error when committing the submission");
149                                             }
150                                         });
151                         $scope.selectedBlueprintName = {};
152                         $scope.selectedBlueprintVersion = {};
153                         $scope.selectedBlueprintLayer = {};
154                         $scope.selectedDeclerativeTimeslot = {};
155                     }
156
157                 });