ESPResult.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/Deferred",
  19. "dojo/_base/lang",
  20. "dojo/data/ObjectStore",
  21. "dojo/dom-construct",
  22. "dojox/xml/parser",
  23. "dojox/xml/DomParser",
  24. "dojox/html/entities",
  25. "hpcc/WsWorkunits",
  26. "hpcc/ESPBase"
  27. ], function (declare, Deferred, lang, ObjectStore, domConstruct,
  28. parser, DomParser, entities,
  29. WsWorkunits, ESPBase) {
  30. return declare(ESPBase, {
  31. store: null,
  32. Total: "-1",
  33. constructor: function (args) {
  34. declare.safeMixin(this, args);
  35. if (this.Sequence != null) {
  36. this.store = new WsWorkunits.WUResult({
  37. wuid: this.Wuid,
  38. sequence: this.Sequence,
  39. isComplete: this.isComplete()
  40. });
  41. } else {
  42. this.store = new WsWorkunits.WUResult({
  43. wuid: this.Wuid,
  44. cluster: this.Cluster,
  45. name: this.Name,
  46. isComplete: true
  47. });
  48. }
  49. },
  50. getName: function () {
  51. return this.Name;
  52. },
  53. getID: function () {
  54. if (this.Sequence != null) {
  55. return this.Sequence;
  56. }
  57. return this.Name;
  58. },
  59. isComplete: function () {
  60. return this.Total != "-1";
  61. },
  62. canShowResults: function () {
  63. if (lang.exists("Sequence", this)) { // Regular WU result
  64. return true;
  65. } else if (lang.exists("RecordCount", this) && this.RecordCount != "") { // DFU Sprayed CSV File will fail here
  66. return true;
  67. }
  68. return false;
  69. },
  70. getFirstSchemaNode: function (node, name) {
  71. if (node && node.attributes) {
  72. if ((node.baseName && node.baseName == name) || (node.localName && node.localName == name) || (typeof (node.getAttribute) != "undefined" && node.getAttribute("name") == name)) {
  73. return node;
  74. }
  75. }
  76. for (var i = 0; i < node.childNodes.length; ++i) {
  77. var retVal = this.getFirstSchemaNode(node.childNodes[i], name);
  78. if (retVal) {
  79. return retVal;
  80. }
  81. }
  82. return null;
  83. },
  84. getFirstSequenceNode: function (schemaNode) {
  85. var row = this.getFirstSchemaNode(schemaNode, "Row");
  86. if (!row)
  87. return null;
  88. var complexType = this.getFirstSchemaNode(row, "complexType");
  89. if (!complexType)
  90. return null;
  91. return this.getFirstSchemaNode(complexType, "sequence");
  92. },
  93. rowToTable: function (cell) {
  94. var table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" });
  95. if (cell && cell.Row) {
  96. if (!cell.Row.length) {
  97. cell.Row = [cell.Row];
  98. }
  99. for (i = 0; i < cell.Row.length; ++i) {
  100. if (i == 0) {
  101. var tr = domConstruct.create("tr", null, table);
  102. for (key in cell.Row[i]) {
  103. var th = domConstruct.create("th", { innerHTML: entities.encode(key) }, tr);
  104. }
  105. }
  106. var tr = domConstruct.create("tr", null, table);
  107. for (key in cell.Row[i]) {
  108. if (cell.Row[i][key]) {
  109. if (cell.Row[i][key].Row) {
  110. var td = domConstruct.create("td", null, tr);
  111. td.appendChild(this.rowToTable(cell.Row[i][key]));
  112. } else {
  113. var td = domConstruct.create("td", { innerHTML: entities.encode(cell.Row[i][key]) }, tr);
  114. }
  115. } else {
  116. var td = domConstruct.create("td", { innerHTML: "" }, tr);
  117. }
  118. }
  119. }
  120. }
  121. return table;
  122. },
  123. getRowStructureFromSchema: function (parentNode) {
  124. var retVal = [];
  125. var sequence = this.getFirstSequenceNode(parentNode, "sequence");
  126. if (!sequence)
  127. return retVal;
  128. for (var i = 0; i < sequence.childNodes.length; ++i) {
  129. var node = sequence.childNodes[i];
  130. if (typeof (node.getAttribute) != "undefined") {
  131. var name = node.getAttribute("name");
  132. var type = node.getAttribute("type");
  133. if (name && type) {
  134. retVal.push({
  135. name: name,
  136. field: name,
  137. width: this.extractWidth(type, name),
  138. classes: "resultGridCell"
  139. });
  140. }
  141. if (node.hasChildNodes()) {
  142. var context = this;
  143. retVal.push({
  144. name: name,
  145. field: name,
  146. formatter: function (cell, row, grid) {
  147. var div = document.createElement("div");
  148. div.appendChild(context.rowToTable(cell));
  149. return div.innerHTML;
  150. },
  151. width: this.getRowWidth(node),
  152. classes: "resultGridCell"
  153. });
  154. }
  155. }
  156. }
  157. return retVal;
  158. },
  159. getRowStructureFromData: function (rows) {
  160. var retVal = [];
  161. for (var key in rows[0]) {
  162. if (key != "myInjectedRowNum") {
  163. var context = this;
  164. retVal.push({
  165. name: key,
  166. field: key,
  167. formatter: function (cell, row, grid) {
  168. if (cell && cell.Row) {
  169. var div = document.createElement("div");
  170. div.appendChild(context.rowToTable(cell));
  171. return div.innerHTML;
  172. }
  173. return cell;
  174. },
  175. width: context.extractWidth("string12", key),
  176. classes: "resultGridCell"
  177. });
  178. }
  179. }
  180. return retVal;
  181. },
  182. getStructure: function () {
  183. var structure = [
  184. {
  185. cells: [
  186. [
  187. {
  188. name: "##", field: this.store.idProperty, width: "6", classes: "resultGridCell"
  189. }
  190. ]
  191. ]
  192. }
  193. ];
  194. if (this.XmlSchema) {
  195. var dom = parser.parse(this.XmlSchema);
  196. var dataset = this.getFirstSchemaNode(dom, "Dataset");
  197. var innerStruct = this.getRowStructureFromSchema(dataset);
  198. for (var i = 0; i < innerStruct.length; ++i) {
  199. structure[0].cells[structure[0].cells.length - 1].push(innerStruct[i]);
  200. }
  201. } else {
  202. var context = this;
  203. Deferred.when(this.store.query("*", {
  204. start: 0,
  205. count: 1,
  206. sync: true
  207. }), function (rows) {
  208. if (rows.length) {
  209. var innerStruct = context.getRowStructureFromData(rows);
  210. for (var i = 0; i < innerStruct.length; ++i) {
  211. structure[0].cells[structure[0].cells.length - 1].push(innerStruct[i]);
  212. }
  213. }
  214. });
  215. }
  216. return structure;
  217. },
  218. getRowWidth: function (parentNode) {
  219. var retVal = 0;
  220. var sequence = this.getFirstSequenceNode(parentNode, "sequence");
  221. if (!sequence)
  222. return retVal;
  223. for (var i = 0; i < sequence.childNodes.length; ++i) {
  224. var node = sequence.childNodes[i];
  225. if (typeof (node.getAttribute) != "undefined") {
  226. var name = node.getAttribute("name");
  227. var type = node.getAttribute("type");
  228. if (name && type) {
  229. retVal += this.extractWidth(type, name);
  230. } else if (node.hasChildNodes()) {
  231. retVal += this.getRowWidth(node);
  232. }
  233. }
  234. }
  235. return retVal;
  236. },
  237. extractWidth: function (type, name) {
  238. var retVal = -1;
  239. switch (type) {
  240. case "xs:boolean":
  241. retVal = 5;
  242. break;
  243. case "xs:integer":
  244. retVal = 8;
  245. break;
  246. case "xs:nonNegativeInteger":
  247. retVal = 8;
  248. break;
  249. case "xs:double":
  250. retVal = 8;
  251. break;
  252. case "xs:string":
  253. retVal = 32;
  254. break;
  255. default:
  256. var numStr = "0123456789";
  257. var underbarPos = type.lastIndexOf("_");
  258. var length = underbarPos > 0 ? underbarPos : type.length;
  259. var i = length - 1;
  260. for (; i >= 0; --i) {
  261. if (numStr.indexOf(type.charAt(i)) == -1)
  262. break;
  263. }
  264. if (i + 1 < length) {
  265. retVal = parseInt(type.substring(i + 1, length));
  266. }
  267. if (type.indexOf("data") == 0) {
  268. retVal *= 2;
  269. }
  270. break;
  271. }
  272. if (retVal < name.length)
  273. retVal = name.length;
  274. return retVal;
  275. },
  276. getObjectStore: function () {
  277. return ObjectStore({
  278. objectStore: this.store
  279. });
  280. },
  281. getLoadingMessage: function () {
  282. if (lang.exists("wu.state", this)) {
  283. return "<span class=\'dojoxGridWating\'>[" + this.wu.state + "]</span>";
  284. }
  285. return "<span class=\'dojoxGridWating\'>[unknown]</span>";
  286. },
  287. getECLRecord: function () {
  288. var retVal = "RECORD\n";
  289. for (var i = 0; i < this.ECLSchemas.ECLSchemaItem.length; ++i) {
  290. retVal += "\t" + this.ECLSchemas.ECLSchemaItem[i].ColumnType + "\t" + this.ECLSchemas.ECLSchemaItem[i].ColumnName + ";\n";
  291. }
  292. retVal += "END;\n";
  293. return retVal;
  294. }
  295. });
  296. });