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 * "UtilityService" contains various generic methods.
26 * (*** DEPRECATED - Use PropertyService instead ***) setProperties() and
29 * SYNTAX: hasContents(object)
31 * Returns "true" if "object" contains contents (i.e. is NOT undefined, null or
32 * ""), "false" otherwise.
34 * SYNTAX: checkUndefined(name, value)
36 * Throws an exception if "value" is undefined. The exception includes "name" as
37 * the cause of the exception. Returns "value" if it is defined.
39 * SYNTAX: getCurrentTime()
41 * Returns the current local date and time in the format "MM/DD/YY HH:MM:SS"
43 * SYNTAX: setHttpErrorHandler(function)
45 * Sets the HTTP error handler to "function".
47 * SYNTAX: runHttpErrorHandler(response, status)
49 * Logs warning messages and the runs the HTTP error handler previously set by
50 * "setHttpErrorHandler". The intended usage is for "$http" calls. Example:
51 * $http.get(...).then(...)["catch"](UtilityService.runHttpErrorHandler);
53 * SYNTAX: getHttpStatusText(statusCode)
55 * Expects "statusCode" to be an HTTP response code (e.g. 404). The function
56 * returns a string that includes both the code and an equivalent text summary.
57 * Example: "Not found (404)"
59 * SYNTAX: getHttpErrorMessage(response)
61 * Expects "response" to be the response object generated by a "$http" error
62 * condition. "getHttpErrorMessage" examines the object and returns a summary
63 * string for some known conditions.
66 var UtilityService = function($log) {
70 function hasContents(object) {
71 if (object === undefined || object === null || object === "") {
77 function padZero(number) {
85 var httpErrorHandler = function(response, status) {
86 $log.warn("UtilityService:httpErrorHandler: response:");
88 $log.warn("UtilityService:httpErrorHandler: status:");
90 if (angular.isFunction(_this.httpErrorHandler)) {
91 _this.httpErrorHandler(response, status);
95 var startNextAsyncOperation = function() {
96 if (_this.asyncOperations.count < _this.asyncOperations.operationList.length) {
97 _this.asyncOperations.operationList[_this.asyncOperations.count++]
100 if (angular.isFunction(_this.asyncOperations.callbackFunction)) {
101 _this.asyncOperations.callbackFunction();
107 setProperties : function(properties) {
108 _this.properties = properties;
110 getProperties : function() {
111 return _this.properties;
113 hasContents : hasContents,
114 checkUndefined : function(name, value) {
115 if (value === undefined) {
117 type : "undefinedObject",
118 message : "undefined object: \"" + name + "\""
123 getCurrentTime : function() {
124 var time = new Date();
125 return padZero(time.getMonth() + 1) + "/"
126 + padZero(time.getDate() + 1) + "/"
127 + (time.getFullYear() - 2000) + " "
128 + padZero(time.getHours()) + ":"
129 + padZero(time.getMinutes()) + ":"
130 + padZero(time.getSeconds())
132 getHttpStatusText : function(statusCode) {
137 "400" : "Bad Request",
138 "401" : "Unauthorized",
140 "405" : "Method Not Allowed",
142 "500" : "Internal Server Error",
143 "503" : "Service Unavailable",
144 "504" : "Gateway Timeout"
147 if (status === undefined) {
151 var statusText = statusMap[statusCode];
152 if (statusText === undefined) {
153 statusText = "Unknown";
156 return statusText + " (" + statusCode + ")";
158 getHttpErrorMessage : function(response) {
159 var data = response.data;
160 if (response.status === 500 && hasContents(data.exception)) {
161 var summary = "exception: " + data.exception;
162 if (hasContents(data.message)) {
163 summary += " message: " + data.message;
167 if (response.status === 0 && response.statusText === "") {
169 * This logic is somewhat "fuzzy". Potential (brainstorming)
170 * enhancements if users find the message unreliable include:
172 * A) SERVER TIMEOUT: perhaps a newer version of Angular can
173 * reliably determine timeouts.
175 * B) SERVER TIMEOUT: recording start / end times and using that
176 * to determine if timeout occured
178 * C) SESSION TIMEOUT "Potentially" examine cookies, although
179 * that may not be feasible if cookies are set to "httponly".
182 return "possible server timeout";
185 return "Possible reasons include a session timeout or a server issue. "
186 + "A session timeout might be resolved by refreshing the screen and re-logging in";
190 if (response.status !== undefined && response.status > 0) {
191 summary = "status: " + response.status;
193 if (hasContents(response.statusText)) {
194 if (summary !== "") {
197 summary += "message: " + response.statusText;
201 setHttpErrorHandler : function(httpErrorHandler) {
202 _this.httpErrorHandler = httpErrorHandler;
204 runHttpErrorHandler : function(response, status) {
205 httpErrorHandler(response, status);
207 startAsyncOperations : function(operationList, callbackFunction) {
208 for (var i = 0; i < operationList.length; i++) {
209 if (!angular.isFunction(operationList[i])) {
210 throw "UtilityService:startAsyncOperations: invalid function: index: "
214 _this.asyncOperations = {
215 operationList : operationList,
216 callbackFunction : callbackFunction,
219 startNextAsyncOperation();
221 startNextAsyncOperation : startNextAsyncOperation,
222 stopAsyncOperations : function() {
223 _this.asyncOperations.count = _this.asyncOperations.operationList.length;
228 app.factory("UtilityService", UtilityService);