[UI] Support data registration
[validation.git] / ui / src / main / webapp / app / BluvalUI / RegisterTimeslot / RegisterTimeslotController.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('RegisterTimeslot');
18 app
19         .controller(
20                 'RegisterTimeslotController',
21                 function($scope, restAPISvc, $q) {
22
23                     initialize();
24
25                     function initialize() {
26                         $scope.loadingLabs = true;
27                         $scope.loadingBlueprintInstances = true;
28                         $scope.definedStart = "now";
29                         $scope.labInfos = [];
30                         $scope.labs = [];
31                         $scope.selectedLab = '';
32                         $scope.blueprintInstances = [];
33                         $scope.declerativeInsts = [];
34                         $scope.selectedDeclerativeInst = '';
35                         $scope.insts = [];
36                         restAPISvc
37                                 .getRestAPI(
38                                         "/api/v1/lab/",
39                                         function(data) {
40                                             if (data) {
41                                                 $scope.labInfos = data;
42                                                 angular.forEach(data, function(
43                                                         lab) {
44                                                     $scope.labs.push(lab.lab);
45                                                 });
46                                                 restAPISvc
47                                                         .getRestAPI(
48                                                                 "/api/v1/blueprintinstance/",
49                                                                 function(data2) {
50                                                                     if (data2) {
51                                                                         $scope.blueprintInstances = data2;
52                                                                         angular
53                                                                                 .forEach(
54                                                                                         $scope.blueprintInstances,
55                                                                                         function(
56                                                                                                 blueprintInstance) {
57                                                                                             var temp = "id: "
58                                                                                                     + blueprintInstance.blueprintInstanceId
59                                                                                                     + " name: "
60                                                                                                     + blueprintInstance["blueprint"]["blueprintName"]
61                                                                                                     + " version: "
62                                                                                                     + blueprintInstance["version"];
63                                                                                             $scope.declerativeInsts
64                                                                                                     .push(temp);
65                                                                                         });
66                                                                     } else {
67                                                                         confirm("No blueprint instances found");
68                                                                     }
69                                                                 });
70                                             } else {
71                                                 confirm("No labs found");
72                                             }
73                                             $scope.loadingLabs = false;
74                                             $scope.loadingBlueprintInstances = false;
75                                         });
76                     }
77
78                     $scope.addInst = function(selectedDeclerativeInst) {
79                         if ($scope.insts
80                                 .indexOf(selectedDeclerativeInst.trim()) === -1) {
81                             $scope.insts.push(selectedDeclerativeInst);
82                         }
83                     }
84
85                     $scope.deleteInst = function(index) {
86                         $scope.insts.splice(index, 1);
87                     }
88
89                     $scope.register = function() {
90                         if (!$scope.selectedLab || !$scope.insts
91                                 || $scope.insts.length === 0) {
92                             confirm("You must specify all the fields");
93                             return;
94                         }
95                         var lab = '';
96                         angular.forEach($scope.labInfos, function(labInfo) {
97                             if (labInfo.lab.trim() === $scope.selectedLab) {
98                                 lab = labInfo;
99                             }
100                         });
101                         if (!lab) {
102                             confirm("Error in lab data");
103                             return;
104                         }
105                         var timeslot = {
106                             "startDateTime" : $scope.definedStart,
107                             "labInfo" : lab
108                         };
109                         restAPISvc
110                                 .postRestAPI(
111                                         "/api/v1/timeslot/",
112                                         timeslot,
113                                         function(data) {
114                                             if (data) {
115                                                 var confirmText = "The timeslot has been registered successfully. Timeslot id:"
116                                                         + data.timeslotId;
117                                                 confirm(confirmText);
118                                                 updateBlusInsts(data);
119                                             } else {
120                                                 confirm("Error when registering the timeslot");
121                                             }
122                                         });
123                     }
124
125                     function updateBlusInsts(timeslot) {
126                         var blueprintInstances = [];
127                         angular
128                                 .forEach(
129                                         $scope.insts,
130                                         function(inst) {
131                                             var id = inst.substring(inst
132                                                     .indexOf("id:") + 4, inst
133                                                     .indexOf("name") - 1);
134                                             angular
135                                                     .forEach(
136                                                             $scope.blueprintInstances,
137                                                             function(
138                                                                     blueprintInstance) {
139                                                                 if (blueprintInstance.blueprintInstanceId
140                                                                         .toString()
141                                                                         .trim() === id
142                                                                         .toString()
143                                                                         .trim()) {
144                                                                     blueprintInstances
145                                                                             .push(blueprintInstance);
146                                                                 }
147                                                             });
148                                         });
149                         if (!blueprintInstances) {
150                             confirm("Error in blueprint instances data");
151                             return;
152                         }
153                         var promises = [];
154                         angular
155                                 .forEach(
156                                         blueprintInstances,
157                                         function(blueprintInstance) {
158                                             if (blueprintInstance.timeslots) {
159                                                 blueprintInstance.timeslots
160                                                         .push(timeslot);
161                                             } else {
162                                                 blueprintInstance.timeslots = [ timeslot ];
163                                             }
164                                             promises
165                                                     .push(restAPISvc
166                                                             .postRestAPI(
167                                                                     "/api/v1/blueprintinstance/",
168                                                                     blueprintInstance,
169                                                                     function(
170                                                                             data) {
171                                                                         if (data) {
172                                                                             var text = "Blueprint instance: "
173                                                                                     + blueprintInstance.blueprint.blueprintName
174                                                                                     + " version: "
175                                                                                     + blueprintInstance.version
176                                                                                     + " updated successfully";
177                                                                             confirm(text);
178                                                                         } else {
179                                                                             var text2 = "Failed to update blueprint instance: "
180                                                                                     + blueprintInstance.blueprint.blueprintName
181                                                                                     + " version: "
182                                                                                     + blueprintInstance.version;
183                                                                             confirm(text2);
184                                                                         }
185                                                                     }));
186                                         });
187                         $q
188                                 .all(promises)
189                                 .then(
190                                         function() {
191                                             confirm("All blueprint instances have been updated");
192                                             $scope.selectedLab = '';
193                                             $scope.selectedDeclerativeInst = '';
194                                             $scope.insts = [];
195                                             initialize();
196                                         });
197                     }
198                 });