[VID-3] Setting docker image tag
[vid.git] / vid / src / main / webapp / app / vid / scripts / services / utilityService.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 "use strict";
22
23 /*
24  * "UtilityService" contains various generic methods.
25  * 
26  * (*** DEPRECATED - Use PropertyService instead ***) setProperties() and
27  * getProperties()
28  * 
29  * SYNTAX: hasContents(object)
30  * 
31  * Returns "true" if "object" contains contents (i.e. is NOT undefined, null or
32  * ""), "false" otherwise.
33  * 
34  * SYNTAX: checkUndefined(name, value)
35  * 
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.
38  * 
39  * SYNTAX: getCurrentTime()
40  * 
41  * Returns the current local date and time in the format "MM/DD/YY HH:MM:SS"
42  * 
43  * SYNTAX: setHttpErrorHandler(function)
44  * 
45  * Sets the HTTP error handler to "function".
46  * 
47  * SYNTAX: runHttpErrorHandler(response, status)
48  * 
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);
52  * 
53  * SYNTAX: getHttpStatusText(statusCode)
54  * 
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)"
58  * 
59  * SYNTAX: getHttpErrorMessage(response)
60  * 
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.
64  */
65
66 var UtilityService = function($log) {
67
68     var _this = this;
69
70     function hasContents(object) {
71         if (object === undefined || object === null || object === "") {
72             return false;
73         }
74         return true;
75     }
76
77     function padZero(number) {
78         if (number < 10) {
79             return "0" + number;
80         } else {
81             return "" + number;
82         }
83     }
84
85     var httpErrorHandler = function(response, status) {
86         $log.warn("UtilityService:httpErrorHandler: response:");
87         $log.warn(response);
88         $log.warn("UtilityService:httpErrorHandler: status:");
89         $log.warn(status);
90         if (angular.isFunction(_this.httpErrorHandler)) {
91             _this.httpErrorHandler(response, status);
92         }
93     };
94
95     var startNextAsyncOperation = function() {
96         if (_this.asyncOperations.count < _this.asyncOperations.operationList.length) {
97             _this.asyncOperations.operationList[_this.asyncOperations.count++]
98                     ();
99         } else {
100             if (angular.isFunction(_this.asyncOperations.callbackFunction)) {
101                 _this.asyncOperations.callbackFunction();
102             }
103         }
104     };
105
106     return {
107         setProperties : function(properties) {
108             _this.properties = properties;
109         },
110         getProperties : function() {
111             return _this.properties;
112         },
113         hasContents : hasContents,
114         checkUndefined : function(name, value) {
115             if (value === undefined) {
116                 throw {
117                     type : "undefinedObject",
118                     message : "undefined object: \"" + name + "\""
119                 };
120             }
121             return value;
122         },
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())
131         },
132         getHttpStatusText : function(statusCode) {
133             var statusMap = {
134                 "200" : "OK",
135                 "201" : "Created",
136                 "202" : "Accepted",
137                 "400" : "Bad Request",
138                 "401" : "Unauthorized",
139                 "404" : "Not Found",
140                 "405" : "Method Not Allowed",
141                 "409" : "Locked",
142                 "500" : "Internal Server Error",
143                 "503" : "Service Unavailable",
144                 "504" : "Gateway Timeout"
145             }
146
147             if (status === undefined) {
148                 return "Undefined";
149             }
150
151             var statusText = statusMap[statusCode];
152             if (statusText === undefined) {
153                 statusText = "Unknown";
154             }
155
156             return statusText + " (" + statusCode + ")";
157         },
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;
164                 }
165                 return summary;
166             }
167             if (response.status === 0 && response.statusText === "") {
168                 /*
169                  * This logic is somewhat "fuzzy". Potential (brainstorming)
170                  * enhancements if users find the message unreliable include:
171                  * 
172                  * A) SERVER TIMEOUT: perhaps a newer version of Angular can
173                  * reliably determine timeouts.
174                  * 
175                  * B) SERVER TIMEOUT: recording start / end times and using that
176                  * to determine if timeout occured
177                  * 
178                  * C) SESSION TIMEOUT "Potentially" examine cookies, although
179                  * that may not be feasible if cookies are set to "httponly".
180                  */
181                 if (data === null) {
182                     return "possible server timeout";
183                 }
184                 if (data === "") {
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";
187                 }
188             }
189             var summary = "";
190             if (response.status !== undefined && response.status > 0) {
191                 summary = "status: " + response.status;
192             }
193             if (hasContents(response.statusText)) {
194                 if (summary !== "") {
195                     summary += " ";
196                 }
197                 summary += "message: " + response.statusText;
198             }
199             return summary;
200         },
201         setHttpErrorHandler : function(httpErrorHandler) {
202             _this.httpErrorHandler = httpErrorHandler;
203         },
204         runHttpErrorHandler : function(response, status) {
205             httpErrorHandler(response, status);
206         },
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: "
211                             + i;
212                 }
213             }
214             _this.asyncOperations = {
215                 operationList : operationList,
216                 callbackFunction : callbackFunction,
217                 count : 0
218             };
219             startNextAsyncOperation();
220         },
221         startNextAsyncOperation : startNextAsyncOperation,
222         stopAsyncOperations : function() {
223             _this.asyncOperations.count = _this.asyncOperations.operationList.length;
224         }
225     }
226 }
227
228 app.factory("UtilityService", UtilityService);