WsWorkunits.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*##############################################################################
  2. # Copyright (C) 2011 HPCC Systems.
  3. #
  4. # All rights reserved. This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ############################################################################## */
  17. define([
  18. "dojo/_base/declare",
  19. "dojo/_base/lang",
  20. "dojo/_base/xhr",
  21. "dojo/_base/Deferred",
  22. "dojo/store/util/QueryResults",
  23. "hpcc/ESPBase"
  24. ], function (declare, lang, xhr, Deferred, QueryResults, ESPBase) {
  25. var WUQuery = declare(ESPBase, {
  26. idProperty: "Wuid",
  27. constructor: function (options) {
  28. declare.safeMixin(this, options);
  29. },
  30. getIdentity: function (object) {
  31. return object[this.idProperty];
  32. },
  33. query: function (query, options) {
  34. var request = {};
  35. lang.mixin(request, options.query);
  36. if (options.start)
  37. request['PageStartFrom'] = options.start;
  38. if (options.count)
  39. request['Count'] = options.count;
  40. if (options.sort) {
  41. request['Sortby'] = options.sort[0].attribute;
  42. request['Descending'] = options.sort[0].descending;
  43. }
  44. request['rawxml_'] = "1";
  45. var results = xhr.get({
  46. url: this.getBaseURL("WsWorkunits") + "/WUQuery",
  47. handleAs: "xml",
  48. content: request
  49. });
  50. var context = this;
  51. var parsedResults = results.then(function (domXml) {
  52. data = context.getValues(domXml, "ECLWorkunit");
  53. data.total = context.getValue(domXml, "NumWUs");
  54. return data;
  55. });
  56. lang.mixin(parsedResults, {
  57. total: Deferred.when(parsedResults, function (data) {
  58. return data.total;
  59. })
  60. });
  61. return QueryResults(parsedResults);
  62. }
  63. });
  64. var WUResult = declare(ESPBase, {
  65. idProperty: "myInjectedRowNum",
  66. wuid: "",
  67. sequence: 0,
  68. isComplete: false,
  69. constructor: function (args) {
  70. declare.safeMixin(this, args);
  71. },
  72. getIdentity: function (object) {
  73. return object[this.idProperty];
  74. },
  75. queryWhenComplete: function (query, options, deferredResults) {
  76. var context = this;
  77. if (this.isComplete == true) {
  78. var request = {};
  79. request['Wuid'] = this.wuid;
  80. request['Sequence'] = this.sequence;
  81. request['Start'] = options.start;
  82. request['Count'] = options.count;
  83. request['rawxml_'] = "1";
  84. var results = xhr.post({
  85. url: this.getBaseURL("WsWorkunits") + "/WUResult",
  86. handleAs: "xml",
  87. content: request,
  88. load: function (domXml) {
  89. var rows = context.getValues(domXml, "Row");
  90. for (var i = 0; i < rows.length; ++i) {
  91. rows[i].myInjectedRowNum = options.start + i + 1;
  92. }
  93. rows.total = context.getValue(domXml, "Total");
  94. // TODO - Need to check why this happens only sometimes (Suspect non XML from the server) ---
  95. if (rows.total == null) {
  96. var debug = context.flattenXml(domXml);
  97. setTimeout(function () {
  98. context.queryWhenComplete(query, options, deferredResults);
  99. }, 100);
  100. }
  101. else {
  102. deferredResults.resolve(rows);
  103. }
  104. }
  105. });
  106. } else {
  107. setTimeout(function () {
  108. context.queryWhenComplete(query, options, deferredResults);
  109. }, 100);
  110. }
  111. },
  112. query: function (query, options) {
  113. var deferredResults = new Deferred();
  114. this.queryWhenComplete(query, options, deferredResults);
  115. var retVal = lang.mixin({
  116. total: Deferred.when(deferredResults, function (rows) {
  117. return rows.total;
  118. })
  119. }, deferredResults);
  120. return QueryResults(retVal);
  121. }
  122. });
  123. return {
  124. WUQuery: WUQuery,
  125. WUResult: WUResult
  126. };
  127. });