ESPResult.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. "dojox/html/entities",
  23. "hpcc/WsWorkunits",
  24. "hpcc/ESPBase"
  25. ], function (declare, ObjectStore, domConstruct,
  26. parser, DomParser, entities,
  27. WsWorkunits, ESPBase) {
  28. return declare(ESPBase, {
  29. store: null,
  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.baseName && node.baseName == name) || (node.localName && node.localName == name) || (typeof(node.getAttribute) != "undefined" && 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: entities.encode(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]) {
  84. if (cell.Row[i][key].Row) {
  85. var td = domConstruct.create("td", null, tr);
  86. td.appendChild(this.rowToTable(cell.Row[i][key]));
  87. } else {
  88. var td = domConstruct.create("td", { innerHTML: entities.encode(cell.Row[i][key]) }, tr);
  89. }
  90. } else {
  91. var td = domConstruct.create("td", { innerHTML: "" }, tr);
  92. }
  93. }
  94. }
  95. }
  96. return table;
  97. },
  98. getRowStructure: function (parentNode) {
  99. var retVal = [];
  100. var sequence = this.getFirstSequenceNode(parentNode, "sequence");
  101. if (!sequence)
  102. return retVal;
  103. for (var i = 0; i < sequence.childNodes.length; ++i) {
  104. var node = sequence.childNodes[i];
  105. if (typeof (node.getAttribute) != "undefined") {
  106. var name = node.getAttribute("name");
  107. var type = node.getAttribute("type");
  108. if (name && type) {
  109. retVal.push({
  110. name: name,
  111. field: name,
  112. width: this.extractWidth(type, name),
  113. classes: "resultGridCell"
  114. });
  115. }
  116. if (node.hasChildNodes()) {
  117. var context = this;
  118. retVal.push({
  119. name: name,
  120. field: name,
  121. formatter: function (cell, row, grid) {
  122. var div = document.createElement("div");
  123. div.appendChild(context.rowToTable(cell));
  124. return div.innerHTML;
  125. },
  126. width: this.getRowWidth(node),
  127. classes: "resultGridCell"
  128. });
  129. }
  130. }
  131. }
  132. return retVal;
  133. },
  134. getStructure: function () {
  135. var structure = [
  136. {
  137. cells: [
  138. [
  139. {
  140. name: "##", field: this.store.idProperty, width: "40px", classes: "resultGridCell"
  141. }
  142. ]
  143. ]
  144. }
  145. ];
  146. var dom = parser.parse(this.XmlSchema);
  147. var dataset = this.getFirstSchemaNode(dom, "Dataset");
  148. var innerStruct = this.getRowStructure(dataset);
  149. for (var i = 0; i < innerStruct.length; ++i) {
  150. structure[0].cells[structure[0].cells.length - 1].push(innerStruct[i]);
  151. }
  152. return structure;
  153. },
  154. getRowWidth: function (parentNode) {
  155. var retVal = 0;
  156. var sequence = this.getFirstSequenceNode(parentNode, "sequence");
  157. if (!sequence)
  158. return retVal;
  159. for (var i = 0; i < sequence.childNodes.length; ++i) {
  160. var node = sequence.childNodes[i];
  161. if (typeof (node.getAttribute) != "undefined") {
  162. var name = node.getAttribute("name");
  163. var type = node.getAttribute("type");
  164. if (name && type) {
  165. retVal += this.extractWidth(type, name);
  166. } else if (node.hasChildNodes()) {
  167. retVal += this.getRowWidth(node);
  168. }
  169. }
  170. }
  171. return retVal;
  172. },
  173. extractWidth: function (type, name) {
  174. var retVal = -1;
  175. switch (type) {
  176. case "xs:boolean":
  177. retVal = 5;
  178. break;
  179. case "xs:integer":
  180. retVal = 8;
  181. break;
  182. case "xs:nonNegativeInteger":
  183. retVal = 8;
  184. break;
  185. case "xs:double":
  186. retVal = 8;
  187. break;
  188. case "xs:string":
  189. retVal = 32;
  190. break;
  191. default:
  192. var numStr = "0123456789";
  193. var underbarPos = type.lastIndexOf("_");
  194. var length = underbarPos > 0 ? underbarPos : type.length;
  195. var i = length - 1;
  196. for (; i >= 0; --i) {
  197. if (numStr.indexOf(type.charAt(i)) == -1)
  198. break;
  199. }
  200. if (i + 1 < length) {
  201. retVal = parseInt(type.substring(i + 1, length));
  202. }
  203. if (type.indexOf("data") == 0) {
  204. retVal *= 2;
  205. }
  206. break;
  207. }
  208. if (retVal < name.length)
  209. retVal = name.length;
  210. return retVal;
  211. },
  212. getObjectStore: function () {
  213. return ObjectStore({
  214. objectStore: this.store
  215. });
  216. },
  217. getECLRecord: function () {
  218. var retVal = "RECORD\n";
  219. for (var i = 0; i < this.ECLSchemas.length; ++i) {
  220. retVal += "\t" + this.ECLSchemas[i].ColumnType + "\t" + this.ECLSchemas[i].ColumnName + ";\n";
  221. }
  222. retVal += "END;\n";
  223. return retVal;
  224. }
  225. });
  226. });