WsWorkunits.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/xhr",
  20. "dojo/_base/Deferred",
  21. "dojo/store/util/QueryResults",
  22. "hpcc/ESPBase"
  23. ], function (declare, lang, xhr, Deferred, QueryResults, ESPBase) {
  24. var WUQuery = declare(ESPBase, {
  25. idProperty: "Wuid",
  26. constructor: function (options) {
  27. declare.safeMixin(this, options);
  28. },
  29. getIdentity: function (object) {
  30. return object[this.idProperty];
  31. },
  32. query: function (query, options) {
  33. var request = {};
  34. lang.mixin(request, options.query);
  35. if (options.start)
  36. request['PageStartFrom'] = options.start;
  37. if (options.count)
  38. request['Count'] = options.count;
  39. if (options.sort) {
  40. request['Sortby'] = options.sort[0].attribute;
  41. request['Descending'] = options.sort[0].descending;
  42. }
  43. request['rawxml_'] = "1";
  44. var results = xhr.get({
  45. url: this.getBaseURL("WsWorkunits") + "/WUQuery",
  46. handleAs: "xml",
  47. content: request
  48. });
  49. var context = this;
  50. var parsedResults = results.then(function (domXml) {
  51. data = context.getValues(domXml, "ECLWorkunit");
  52. data.total = context.getValue(domXml, "NumWUs");
  53. return data;
  54. });
  55. lang.mixin(parsedResults, {
  56. total: Deferred.when(parsedResults, function (data) {
  57. return data.total;
  58. })
  59. });
  60. return QueryResults(parsedResults);
  61. }
  62. });
  63. var WUResult = declare(ESPBase, {
  64. idProperty: "myInjectedRowNum",
  65. wuid: "",
  66. sequence: 0,
  67. isComplete: false,
  68. constructor: function (args) {
  69. declare.safeMixin(this, args);
  70. },
  71. getIdentity: function (object) {
  72. return object[this.idProperty];
  73. },
  74. queryWhenComplete: function (query, options, deferredResults) {
  75. var context = this;
  76. if (this.isComplete == true) {
  77. var request = {};
  78. request['Wuid'] = this.wuid;
  79. request['Sequence'] = this.sequence;
  80. request['Start'] = options.start;
  81. request['Count'] = options.count;
  82. request['rawxml_'] = "1";
  83. var results = xhr.post({
  84. url: this.getBaseURL("WsWorkunits") + "/WUResult",
  85. handleAs: "xml",
  86. content: request,
  87. load: function(domXml) {
  88. var rows = context.getValues(domXml, "Row");
  89. for (var i = 0; i < rows.length; ++i) {
  90. rows[i].myInjectedRowNum = options.start + i + 1;
  91. }
  92. rows.total = context.getValue(domXml, "Total");
  93. // TODO - Need to check why this happens only sometimes (Suspect non XML from the server) ---
  94. if (rows.total == null) {
  95. var debug = context.flattenXml(domXml);
  96. setTimeout(function () {
  97. context.queryWhenComplete(query, options, deferredResults);
  98. }, 100);
  99. }
  100. else {
  101. deferredResults.resolve(rows);
  102. }
  103. }
  104. });
  105. } else {
  106. setTimeout(function() {
  107. context.queryWhenComplete(query, options, deferredResults);
  108. }, 100);
  109. }
  110. },
  111. query: function (query, options) {
  112. var deferredResults = new Deferred();
  113. this.queryWhenComplete(query, options, deferredResults);
  114. var retVal = lang.mixin({
  115. total: Deferred.when(deferredResults, function (rows) {
  116. return rows.total;
  117. })
  118. }, deferredResults);
  119. return QueryResults(retVal);
  120. }
  121. });
  122. return {
  123. WUQuery: WUQuery,
  124. WUResult: WUResult
  125. };
  126. });