ESPRequest.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  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. define([
  17. "dojo/_base/declare",
  18. "dojo/_base/array",
  19. "dojo/_base/lang",
  20. "dojo/_base/config",
  21. "dojo/_base/Deferred",
  22. "dojo/request",
  23. "dojo/request/script",
  24. "dojo/store/util/QueryResults",
  25. "dojo/store/Observable"
  26. ], function (declare, arrayUtil, lang, config, Deferred, request, script, QueryResults, Observable) {
  27. var RequestHelper = declare(null, {
  28. serverIP: null,
  29. constructor: function (args) {
  30. if (args) {
  31. declare.safeMixin(this, args);
  32. }
  33. this.serverIP = this.getParamFromURL("ServerIP");
  34. },
  35. getParamFromURL: function (key) {
  36. var value = "";
  37. if (dojo.doc.location.search) {
  38. var searchStr = dojo.doc.location.search.substr((dojo.doc.location.search.substr(0, 1) == "?" ? 1 : 0));
  39. value = searchStr ? dojo.queryToObject(searchStr)[key] : "";
  40. }
  41. if (value)
  42. return value;
  43. return config[key];
  44. },
  45. getBaseURL: function (service) {
  46. if (service === undefined) {
  47. service = "WsWorkunits";
  48. }
  49. if (this.serverIP)
  50. return "http://" + this.serverIP + ":8010/" + service;
  51. return "/" + service;
  52. },
  53. isCrossSite: function () {
  54. return this.serverIP ? true : false;
  55. },
  56. _send: function(service, action, _params) {
  57. var params = lang.mixin({
  58. load: function (response) {
  59. },
  60. error: function (error) {
  61. },
  62. event: function (evt) {
  63. }
  64. }, _params);
  65. lang.mixin(params.request, {
  66. rawxml_: true
  67. });
  68. var handleAs = params.handleAs ? params.handleAs : "json";
  69. var postfix = "";
  70. if (handleAs === "json") {
  71. postfix = ".json";
  72. }
  73. var method = params.method ? params.method : "get";
  74. var retVal = null;
  75. if (this.isCrossSite()) {
  76. retVal = script.get(this.getBaseURL(service) + "/" + action + postfix, {
  77. query: params.request,
  78. jsonp: "jsonp"
  79. });
  80. } else {
  81. retVal = request.post(this.getBaseURL(service) + "/" + action + postfix, {
  82. data: params.request,
  83. handleAs: handleAs
  84. });
  85. }
  86. return retVal.then(function (response) {
  87. params.load(response);
  88. return response;
  89. },
  90. function (error) {
  91. params.error(error);
  92. return error;
  93. },
  94. function (event) {
  95. params.event(event);
  96. return event;
  97. });
  98. },
  99. send: function (service, action, params) {
  100. if (!params)
  101. params = {};
  102. dojo.publish("hpcc/standbyBackgroundShow");
  103. var handleAs = params.handleAs ? params.handleAs : "json";
  104. return this._send(service, action, params).then(function (response) {
  105. if (!params.suppressExceptionToaster && handleAs == "json") {
  106. var deletedWorkunit = false; // Deleted WU
  107. if (lang.exists("Exceptions.Source", response)) {
  108. var message = "<h3>" + response.Exceptions.Source + "</h3>";
  109. if (lang.exists("Exceptions.Exception", response)) {
  110. exceptions = response.Exceptions.Exception;
  111. for (var i = 0; i < response.Exceptions.Exception.length; ++i) {
  112. if ((service === "WsWorkunits" && action === "WUInfo" && response.Exceptions.Exception[i].Code === 20080) ||
  113. (service === "WsWorkunits" && action === "WUQuery" && response.Exceptions.Exception[i].Code === 20081)) {
  114. deletedWorkunit = true;
  115. }
  116. message += "<p>" + response.Exceptions.Exception[i].Message + "</p>";
  117. }
  118. }
  119. if (!deletedWorkunit) {
  120. dojo.publish("hpcc/brToaster", {
  121. message: message,
  122. type: "error",
  123. duration: -1
  124. });
  125. }
  126. }
  127. }
  128. dojo.publish("hpcc/standbyBackgroundHide");
  129. return response;
  130. },
  131. function (error) {
  132. var message = "Unknown Error";
  133. if (lang.exists("response.text", error)) {
  134. message = error.response.text;
  135. } else if (error.message && error.stack) {
  136. message = "<h3>" + error.message + "</h3>";
  137. message += "<p>" + error.stack + "</p>";
  138. }
  139. dojo.publish("hpcc/brToaster", {
  140. message: message,
  141. type: "error",
  142. duration: -1
  143. });
  144. dojo.publish("hpcc/standbyBackgroundHide");
  145. return error;
  146. });
  147. },
  148. // XML to JSON helpers ---
  149. getValue: function (domXml, tagName, knownObjectArrays) {
  150. var retVal = this.getValues(domXml, tagName, knownObjectArrays);
  151. if (retVal.length == 0) {
  152. return null;
  153. } else if (retVal.length != 1) {
  154. alert("Invalid length: " + retVal.length);
  155. }
  156. return retVal[0];
  157. },
  158. getValues: function (domXml, tagName, knownObjectArrays) {
  159. var retVal = [];
  160. var items = domXml.getElementsByTagName(tagName);
  161. var parentNode = items.length ? items[0].parentNode : null; // Prevent <Dataset><row><field><row> scenario
  162. for (var i = 0; i < items.length; ++i) {
  163. if (items[i].parentNode == parentNode)
  164. retVal.push(this.flattenXml(items[i], knownObjectArrays));
  165. }
  166. return retVal;
  167. },
  168. flattenXml: function (domXml, knownObjectArrays) {
  169. var retValArr = [];
  170. var retValStr = "";
  171. var retVal = {};
  172. for (var i = 0; i < domXml.childNodes.length; ++i) {
  173. var childNode = domXml.childNodes[i];
  174. if (childNode.childNodes) {
  175. if (childNode.nodeName && knownObjectArrays != null && dojo.indexOf(knownObjectArrays, childNode.nodeName) >= 0) {
  176. retValArr.push(this.flattenXml(childNode, knownObjectArrays));
  177. } else if (childNode.nodeName == "#text") {
  178. retValStr += childNode.nodeValue;
  179. } else if (childNode.childNodes.length == 0) {
  180. retVal[childNode.nodeName] = null;
  181. } else {
  182. var value = this.flattenXml(childNode, knownObjectArrays);
  183. if (retVal[childNode.nodeName] == null) {
  184. retVal[childNode.nodeName] = value;
  185. } else if (dojo.isArray(retVal[childNode.nodeName])) {
  186. retVal[childNode.nodeName].push(value);
  187. } else if (dojo.isObject(retVal[childNode.nodeName])) {
  188. var tmp = retVal[childNode.nodeName];
  189. retVal[childNode.nodeName] = [];
  190. retVal[childNode.nodeName].push(tmp);
  191. retVal[childNode.nodeName].push(value);
  192. }
  193. }
  194. }
  195. }
  196. if (retValArr.length)
  197. return retValArr;
  198. else if (retValStr.length)
  199. return retValStr;
  200. return retVal;
  201. }
  202. });
  203. _StoreSingletons = [];
  204. return {
  205. flattenArray: function (target, arrayName, arrayID) {
  206. if (lang.exists(arrayName + ".length", target)) {
  207. var tmp = {};
  208. for (var i = 0; i < target[arrayName].length; ++i) {
  209. tmp[arrayName + "_i" + i] = target[arrayName][i][arrayID];
  210. }
  211. delete target[arrayName];
  212. return lang.mixin(target, tmp);
  213. }
  214. return target;
  215. },
  216. flattenMap: function (target, arrayName) {
  217. if (lang.exists(arrayName, target)) {
  218. var appData = target[arrayName];
  219. delete target[arrayName];
  220. var singularName = arrayName.substr(0, arrayName.length - 1);
  221. var i = 0;
  222. for (var key in appData) {
  223. target[arrayName + "." + singularName + "." + i + '.Application'] = "ESPRequest.js";
  224. target[arrayName + "." + singularName + "." + i + '.Name'] = key;
  225. target[arrayName + "." + singularName + "." + i + '.Value'] = appData[key];
  226. ++i;
  227. }
  228. target[arrayName + "." + singularName + ".itemcount"] = i;
  229. }
  230. return target;
  231. },
  232. getBaseURL: function (service) {
  233. var helper = new RequestHelper();
  234. return helper.getBaseURL(service);
  235. },
  236. send: function (service, action, params) {
  237. var helper = new RequestHelper();
  238. return helper.send(service, action, params);
  239. },
  240. Store: declare(null, {
  241. SortbyProperty: 'Sortby',
  242. DescendingProperty: 'Descending',
  243. constructor: function (options) {
  244. if (!this.service) {
  245. throw new Error("service: Undefined - Missing service name (eg 'WsWorkunts').");
  246. }
  247. if (!this.action) {
  248. throw new Error("action: Undefined - Missing action name (eg 'WUQuery').");
  249. }
  250. if (!this.responseQualifier) {
  251. throw new Error("responseQualifier: Undefined - Missing action name (eg 'Workunits.ECLWorkunit').");
  252. }
  253. if (!this.idProperty) {
  254. throw new Error("idProperty: Undefined - Missing ID field (eg 'Wuid').");
  255. }
  256. if (options) {
  257. declare.safeMixin(this, options);
  258. }
  259. },
  260. getIdentity: function (item) {
  261. return item[this.idProperty];
  262. },
  263. exists: function (id) {
  264. var item = lang.getObject(this.service + "." + this.action, false, _StoreSingletons);
  265. if (item) {
  266. return item[id] !== undefined;
  267. }
  268. return false;
  269. },
  270. get: function (id, item) {
  271. if (!this.exists(id)) {
  272. var newItem = lang.getObject(this.service + "." + this.action, true, _StoreSingletons);
  273. newItem[id] = this.create(id, item);
  274. return newItem[id];
  275. }
  276. var item = lang.getObject(this.service + "." + this.action, false, _StoreSingletons);
  277. return item[id];
  278. },
  279. create: function (id, item) {
  280. var retVal = {
  281. };
  282. retVal[this.idProperty] = id;
  283. return retVal;
  284. },
  285. update: function (id, item) {
  286. lang.mixin(this.get(id), item);
  287. },
  288. _hasResponseContent: function(response) {
  289. return lang.exists(this.responseQualifier, response);
  290. },
  291. _getResponseContent: function(response) {
  292. return lang.getObject(this.responseQualifier, false, response);
  293. },
  294. query: function (query, options) {
  295. var request = query;
  296. if (options !== undefined && options.start !== undefined && options.count !== undefined) {
  297. if (this.startProperty) {
  298. request[this.startProperty] = options.start;
  299. }
  300. if (this.countProperty) {
  301. request[this.countProperty] = options.count;
  302. }
  303. }
  304. if (options !== undefined && options.sort !== undefined && options.sort[0].attribute !== undefined) {
  305. request[this.SortbyProperty] = options.sort[0].attribute;
  306. request[this.DescendingProperty] = options.sort[0].descending ? true : false;
  307. }
  308. if (this.preRequest) {
  309. this.preRequest(request);
  310. }
  311. var helper = new RequestHelper();
  312. var results = helper.send(this.service, this.action, {
  313. request: request
  314. });
  315. var deferredResults = new Deferred();
  316. var context = this;
  317. deferredResults.total = results.then(function (response) {
  318. if (context.responseTotalQualifier) {
  319. return lang.getObject(context.responseTotalQualifier, false, response);
  320. } else if (context._hasResponseContent(response)) {
  321. return context._getResponseContent(response).length;
  322. }
  323. return 0;
  324. });
  325. Deferred.when(results, function (response) {
  326. var items = [];
  327. if (context._hasResponseContent(response)) {
  328. if (context.preProcessResponse) {
  329. var responseQualiferArray = context.responseQualifier.split(".");
  330. context.preProcessResponse(lang.getObject(responseQualiferArray[0], false, response), request);
  331. }
  332. arrayUtil.forEach(context._getResponseContent(response), function (item, index) {
  333. if (context.preProcessRow) {
  334. context.preProcessRow(item);
  335. }
  336. var storeItem = context.get(context.getIdentity(item), item);
  337. context.update(context.getIdentity(item), item);
  338. items.push(storeItem);
  339. });
  340. }
  341. if (context.postProcessResults) {
  342. context.postProcessResults(items);
  343. }
  344. deferredResults.resolve(items);
  345. return items;
  346. });
  347. return QueryResults(deferredResults);
  348. }
  349. })
  350. };
  351. });