ESPResult.js 10 KB

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