ESPResult.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/data/ObjectStore",
  19. "dojo/dom-construct",
  20. "dojox/xml/parser",
  21. "dojox/xml/DomParser",
  22. "hpcc/WsWorkunits",
  23. "hpcc/ESPBase"
  24. ], function (declare, ObjectStore, domConstruct,
  25. parser, DomParser,
  26. WsWorkunits, ESPBase) {
  27. return declare(ESPBase, {
  28. store: null,
  29. hasChildDataset: false,
  30. Total: "-1",
  31. constructor: function (args) {
  32. declare.safeMixin(this, args);
  33. this.store = new WsWorkunits.WUResult({
  34. wuid: this.wuid,
  35. sequence: this.Sequence,
  36. isComplete: this.isComplete()
  37. });
  38. },
  39. getName: function () {
  40. return this.Name;
  41. },
  42. isComplete: function () {
  43. return this.Total != "-1";
  44. },
  45. getFirstSchemaNode: function (node, name) {
  46. if (node && node.attributes) {
  47. if ((node.localName && node.localName == name) || (node.hasAttributes() && node.getAttribute("name") == name)) {
  48. return node;
  49. }
  50. }
  51. for (var i = 0; i < node.childNodes.length; ++i) {
  52. var retVal = this.getFirstSchemaNode(node.childNodes[i], name);
  53. if (retVal) {
  54. return retVal;
  55. }
  56. }
  57. return null;
  58. },
  59. getFirstSequenceNode: function (schemaNode) {
  60. var row = this.getFirstSchemaNode(schemaNode, "Row");
  61. if (!row)
  62. return null;
  63. var complexType = this.getFirstSchemaNode(row, "complexType");
  64. if (!complexType)
  65. return null;
  66. return this.getFirstSchemaNode(complexType, "sequence");
  67. },
  68. rowToTable: function (cell) {
  69. var table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" });
  70. if (cell && cell.Row) {
  71. if (!cell.Row.length) {
  72. cell.Row = [cell.Row];
  73. }
  74. for (i = 0; i < cell.Row.length; ++i) {
  75. if (i == 0) {
  76. var tr = domConstruct.create("tr", null, table);
  77. for (key in cell.Row[i]) {
  78. var th = domConstruct.create("th", { innerHTML: key }, tr);
  79. }
  80. }
  81. var tr = domConstruct.create("tr", null, table);
  82. for (key in cell.Row[i]) {
  83. if (cell.Row[i][key].Row) {
  84. var td = domConstruct.create("td", null, tr);
  85. td.appendChild(this.rowToTable(cell.Row[i][key]));
  86. } else {
  87. var td = domConstruct.create("td", { innerHTML: cell.Row[i][key] }, tr);
  88. }
  89. }
  90. }
  91. }
  92. return table;
  93. },
  94. getRowStructure: function (parentNode) {
  95. var retVal = [];
  96. var sequence = this.getFirstSequenceNode(parentNode, "sequence");
  97. if (!sequence)
  98. return retVal;
  99. for (var i = 0; i < sequence.childNodes.length; ++i) {
  100. var node = sequence.childNodes[i];
  101. if (node.hasAttributes()) {
  102. var name = node.getAttribute("name");
  103. var type = node.getAttribute("type");
  104. if (name && type) {
  105. retVal.push({
  106. name: name,
  107. field: name,
  108. width: this.extractWidth(type, name),
  109. classes: "resultGridCell"
  110. });
  111. }
  112. if (node.hasChildNodes()) {
  113. this.hasChildDataset = true;
  114. var context = this;
  115. retVal.push({
  116. name: name,
  117. field: name,
  118. formatter: function (cell, row, grid) {
  119. var div = document.createElement("div");
  120. div.appendChild(context.rowToTable(cell));
  121. return div.innerHTML;
  122. },
  123. width: this.getRowWidth(node),
  124. classes: "resultGridCell"
  125. });
  126. }
  127. }
  128. }
  129. return retVal;
  130. },
  131. getStructure: function () {
  132. var structure = [
  133. {
  134. cells: [
  135. [
  136. {
  137. name: "##", field: this.store.idProperty, width: "40px", classes: "resultGridCell"
  138. }
  139. ]
  140. ]
  141. }
  142. ];
  143. var dom = parser.parse(this.XmlSchema);
  144. var dataset = this.getFirstSchemaNode(dom, "Dataset");
  145. var innerStruct = this.getRowStructure(dataset);
  146. for (var i = 0; i < innerStruct.length; ++i) {
  147. structure[0].cells[structure[0].cells.length - 1].push(innerStruct[i]);
  148. }
  149. return structure;
  150. },
  151. getRowWidth: function (parentNode) {
  152. var retVal = 0;
  153. var sequence = this.getFirstSequenceNode(parentNode, "sequence");
  154. if (!sequence)
  155. return retVal;
  156. for (var i = 0; i < sequence.childNodes.length; ++i) {
  157. var node = sequence.childNodes[i];
  158. if (node.hasAttributes()) {
  159. var name = node.getAttribute("name");
  160. var type = node.getAttribute("type");
  161. if (name && type) {
  162. retVal += this.extractWidth(type, name);
  163. } else if (node.hasChildNodes()) {
  164. retVal += this.getRowWidth(node);
  165. }
  166. }
  167. }
  168. return retVal;
  169. },
  170. extractWidth: function (type, name) {
  171. var retVal = -1;
  172. switch (type) {
  173. case "xs:boolean":
  174. retVal = 5;
  175. break;
  176. case "xs:integer":
  177. retVal = 8;
  178. break;
  179. case "xs:nonNegativeInteger":
  180. retVal = 8;
  181. break;
  182. case "xs:double":
  183. retVal = 8;
  184. break;
  185. default:
  186. var numStr = "0123456789";
  187. var underbarPos = type.lastIndexOf("_");
  188. var i = underbarPos > 0 ? underbarPos : type.length;
  189. while (i >= 0) {
  190. if (numStr.indexOf(type.charAt(--i)) == -1)
  191. break;
  192. }
  193. if (i > 0 && i + 1 < type.length) {
  194. retVal = parseInt(type.substring(i + 1, type.length));
  195. }
  196. if (type.indexOf("data") == 0) {
  197. retVal *= 2;
  198. }
  199. break;
  200. }
  201. if (retVal < name.length)
  202. retVal = name.length;
  203. return retVal;
  204. },
  205. getObjectStore: function () {
  206. return ObjectStore({
  207. objectStore: this.store
  208. });
  209. },
  210. getECLRecord: function () {
  211. var retVal = "RECORD\n";
  212. for (var i = 0; i < this.ECLSchemas.length; ++i) {
  213. retVal += "\t" + this.ECLSchemas[i].ColumnType + "\t" + this.ECLSchemas[i].ColumnName + ";\n";
  214. }
  215. retVal += "END;\n";
  216. return retVal;
  217. }
  218. });
  219. });