2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
24 * "msoCommitController.js" provides controller code to commit MSO requests.
28 * Parent HTML/JSP code is expected to include "msoCommit.htm" (via
29 * "ng-include") and this file (via "<script>"). "msoCommit.jsp" (displayed
30 * when the parent code includes "msoCommit.htm") renders a popup window, but
31 * initially hides the display.
33 * The parent controller code is expected to broadcast either the
34 * "createInstance" or "deleteInstance" events when it is ready to commit the
37 * This controller receives these events (via "$scope.on" declarations), calls
38 * "$scope.init()" to "unhide" and initialize the popup display and then makes a
39 * REST request to the appropriate server Java controller.
41 * The initial REST response is handled by "handleInitialResponse". This method
42 * then polls the server (via "getRequestStatus") if the request is successful.
44 * The subsequent "getRequestStatus" responses are handled by
45 * "handleGetResponse".
47 * "handleInitialResponse" and "handleGetResponse" primarily filter response
48 * data, manipulate the display and provide error handling.
50 * The mechanism has these dependencies (in addition to "msoCommit.htm"):
52 * 1) Three services: MsoService, PropertyService and UtilityService
54 * 2) The popup window directive found in "popupWindow.htm" and
55 * "popupWindowDirective.js"
57 * 2) Display styling defined in "dialogs.css"
61 * The parent HTML is assumed to be the "popup-window" directive and the
62 * corresponding parent controller is assumed to define the object
66 var msoCommitController = function($scope, $http, $timeout, $window, $log,
67 MsoService, PropertyService, UtilityService, DataService) {
69 $scope.isViewVisible = false;
70 $scope.progressBarControl = {};
71 $scope.popupWindowControl = {};
75 $scope.$on("createInstance", function(event, request) {
77 MsoService.createInstance(request, handleInitialResponse);
80 $scope.$on("deleteInstance", function(event, request) {
82 MsoService.deleteInstance(request, handleInitialResponse);
85 var init = function(request) {
86 $scope.status = "Submitting Request";
87 $scope.isSpinnerVisible = true;
88 $scope.isProgressVisible = true;
91 $scope.isCloseEnabled = false;
92 $scope.isViewVisible = true;
93 $scope.popup.isVisible = true;
95 _this.pollAttempts = 0;
96 _this.callbackFunction = request.callbackFunction;
98 if (angular.isFunction($scope.progressBarControl.reset)) {
99 $scope.progressBarControl.reset();
101 $scope.percentProgress = 2; // Show "a little" progress
103 UtilityService.setHttpErrorHandler(function(response) {
104 $scope.isCloseEnabled = true;
105 showError("System failure", UtilityService
106 .getHttpErrorMessage(response));
110 var handleInitialResponse = function(response) {
112 updateViewAfterInitialResponse(response);
113 _this.timer = $timeout(getRequestStatus, PropertyService
114 .getMsoMaxPollingIntervalMsec());
116 $scope.instanceId = response.data.entity.instanceId;
117 if ($scope.instanceId == null) {
118 $scope.instanceId = response.data.entity.requestReferences.instanceId;
121 MsoService.showResponseContentError(error, showError);
125 var getRequestStatus = function() {
126 MsoService.getOrchestrationRequest(_this.requestId, handleGetResponse);
129 var handleGetResponse = function(response) {
131 if (isUpdateViewAfterGetResponseComplete(response)) {
134 console.log ( "msoCommitController _this.pollAttempts=" + _this.pollAttempts + " max polls=" + PropertyService.getMsoMaxPolls());
135 if (++_this.pollAttempts > PropertyService.getMsoMaxPolls()) {
136 showError("Maximum number of poll attempts exceeded");
138 _this.timer = $timeout(getRequestStatus, PropertyService
139 .getMsoMaxPollingIntervalMsec());
142 MsoService.showResponseContentError(error, showError);
146 var updateViewAfterInitialResponse = function(response) {
147 $scope.isCloseEnabled = true;
151 _this.requestId = UtilityService.checkUndefined("requestId",
152 UtilityService.checkUndefined("requestReferences",
153 response.data.entity.requestReferences).requestId);
155 $scope.percentProgress = 4; // Show "a little more" progress
156 $scope.status = "In Progress";
160 * Updates the view and returns "true" if the MSO operation has returned a
163 var isUpdateViewAfterGetResponseComplete = function(response) {
164 console.log("msoCommitController isUpdateViewAfterGetResponseComplete");
167 var requestStatus = UtilityService.checkUndefined("requestStatus",
168 UtilityService.checkUndefined("request",
169 response.data.entity.request).requestStatus);
171 var requestState = requestStatus.requestState;
172 console.log("msoCommitController requestState=" + requestState);
173 // look for "progress" or "pending"
174 var patt1 = /progress/i;
175 var patt2 = /pending/i;
176 var result1 = patt1.test(requestState);
177 var result2 = patt2.test(requestState)
178 if (result1 || result2) {
179 requestState = "In Progress";
181 var statusMessage = requestStatus.statusMessage;
182 console.log("msoCommitController statusMessage=" + statusMessage);
183 if (UtilityService.hasContents(statusMessage)) {
184 $scope.status = requestState + " - " + statusMessage;
186 $scope.status = requestState;
188 if (UtilityService.hasContents(requestStatus.percentProgress)) {
189 $scope.percentProgress = requestStatus.percentProgress;
192 if (requestState.toLowerCase() === "Failed".toLowerCase()) {
198 if (requestState.toLowerCase() === "Complete".toLowerCase()) {
199 $scope.isSpinnerVisible = false;
206 var updateLog = function(response) {
207 $scope.log = MsoService.getFormattedCommonResponse(response)
209 UtilityService.checkUndefined("entity", response.data.entity);
210 UtilityService.checkUndefined("status", response.data.status);
211 MsoService.checkValidStatus(response);
214 $scope.close = function() {
215 if (_this.timer !== undefined) {
216 $timeout.cancel(_this.timer);
218 $scope.isViewVisible = false;
219 if (angular.isFunction(_this.callbackFunction)) {
220 if ($scope.error === "") {
221 _this.callbackFunction({
223 instanceId : $scope.instanceId
226 _this.callbackFunction({
231 $scope.popup.isVisible = false;
235 var showError = function(summary, details) {
236 var message = summary;
237 if (UtilityService.hasContents(details)) {
238 message += " (" + details + ")";
240 $scope.isSpinnerVisible = false;
241 $scope.isProgressVisible = false;
242 $scope.error = message;
243 $scope.status = "Error";
247 app.controller("msoCommitController", [ "$scope", "$http", "$timeout",
248 "$window", "$log", "MsoService", "PropertyService", "UtilityService",
249 msoCommitController ]);