WsEcl.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/lang",
  19. "dojo/_base/array",
  20. "dojo/_base/Deferred",
  21. "dojo/request",
  22. "dojo/request/script",
  23. "hpcc/WsTopology"
  24. ], function (declare, lang, arrayUtil, Deferred, request, script,
  25. WsTopology) {
  26. return {
  27. _flattenResults: function (results) {
  28. if (Object.prototype.toString.call(results) === '[object Array]') {
  29. for (var i = 0; i < results.length; ++i) {
  30. results[i] = this._flattenResults(results[i]);
  31. }
  32. } else if (Object.prototype.toString.call(results) === '[object Object]') {
  33. var d = Object.prototype.toString.call(results);
  34. for (var key in results) {
  35. results[key] = this._flattenResults(results[key]);
  36. if (key === "Row") {
  37. return results.Row;
  38. }
  39. }
  40. }
  41. return results;
  42. },
  43. //http://192.168.1.201:8002/WsEcl/submit/query/roxie/countydeeds.1/json?year=2013&jsonp=XYZ
  44. Submit: function (target, method, query) {
  45. var deferred = new Deferred();
  46. var context = this;
  47. WsTopology.GetWsEclURL("submit").then(function (response) {
  48. var url = response + target + "/" + method + "/json";
  49. script.get(url, {
  50. query: query,
  51. jsonp: "jsonp"
  52. }).then(function (response) {
  53. var results = response[method + "Response"] && response[method + "Response"].Results ? response[method + "Response"].Results : {};
  54. results = context._flattenResults(results);
  55. deferred.resolve(results);
  56. });
  57. });
  58. return deferred.promise;
  59. },
  60. SubmitXML: function (target, domXml) {
  61. domXml = domXml.firstChild;
  62. var method = domXml.tagName;
  63. method = method.slice(0, -7); //"Request"
  64. var query = {};
  65. arrayUtil.forEach(domXml.childNodes, function (item, idx) {
  66. query[item.tagName] = item.textContent;
  67. });
  68. return this.Submit(target, method, query);
  69. },
  70. //http://192.168.1.201:8002/WsEcl/example/request/query/roxie/countydeeds.1
  71. ExampleRequest: function(target, method) {
  72. var deferred = new Deferred();
  73. var context = this;
  74. WsTopology.GetWsEclURL("example/request").then(function (response) {
  75. var url = response + target + "/" + method;
  76. // HPCC-10488 ---
  77. // script.get(url, {
  78. // query: query,
  79. // jsonp: "jsonp"
  80. request.get(url, {
  81. handleAs: "xml"
  82. }).then(function (response) {
  83. var fields = [];
  84. arrayUtil.forEach(response.getElementsByTagName(method + "Request"), function (item, idx) {
  85. arrayUtil.forEach(item.childNodes, function (child_item, idx) {
  86. fields.push(child_item.tagName);
  87. });
  88. });
  89. deferred.resolve(fields);
  90. });
  91. });
  92. return deferred.promise;
  93. }
  94. };
  95. });