WsWorkunits.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. if (this.sequence != null) {
  80. request['Sequence'] = this.sequence;
  81. } else {
  82. request['LogicalName'] = this.name;
  83. }
  84. request['Start'] = options.start;
  85. request['Count'] = options.count;
  86. request['rawxml_'] = "1";
  87. var results = xhr.post({
  88. url: this.getBaseURL("WsWorkunits") + "/WUResult",
  89. handleAs: "xml",
  90. content: request,
  91. sync: options.sync != null ? options.sync : false,
  92. load: function (domXml) {
  93. var rows = context.getValues(domXml, "Row");
  94. for (var i = 0; i < rows.length; ++i) {
  95. rows[i].myInjectedRowNum = options.start + i + 1;
  96. }
  97. rows.total = context.getValue(domXml, "Total");
  98. // TODO - Need to check why this happens only sometimes (Suspect non XML from the server) ---
  99. if (rows.total == null) {
  100. var debug = context.flattenXml(domXml);
  101. setTimeout(function () {
  102. context.queryWhenComplete(query, options, deferredResults);
  103. }, 100);
  104. }
  105. else {
  106. deferredResults.resolve(rows);
  107. }
  108. }
  109. });
  110. } else {
  111. setTimeout(function () {
  112. context.queryWhenComplete(query, options, deferredResults);
  113. }, 100);
  114. }
  115. },
  116. query: function (query, options) {
  117. var deferredResults = new Deferred();
  118. this.queryWhenComplete(query, options, deferredResults);
  119. var retVal = lang.mixin({
  120. total: Deferred.when(deferredResults, function (rows) {
  121. return rows.total;
  122. })
  123. }, deferredResults);
  124. return QueryResults(retVal);
  125. }
  126. });
  127. return {
  128. WUQuery: WUQuery,
  129. WUResult: WUResult
  130. };
  131. });