ESPResult.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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/array",
  19. "dojo/_base/Deferred",
  20. "dojo/_base/lang",
  21. "dojo/dom-construct",
  22. "dojox/xml/parser",
  23. "dojox/html/entities",
  24. "hpcc/ESPBase",
  25. "hpcc/ESPRequest",
  26. "hpcc/WsWorkunits"
  27. ], function (declare, arrayUtil, Deferred, lang, domConstruct,
  28. parser, entities,
  29. ESPBase, ESPRequest, WsWorkunits) {
  30. var Store = declare([ESPRequest.Store, ESPBase], {
  31. service: "WsWorkunits",
  32. action: "WUResult",
  33. responseQualifier: "WUResultResponse.Result",
  34. responseTotalQualifier: "WUResultResponse.Total",
  35. idProperty: "__hpcc_id",
  36. startProperty: "Start",
  37. countProperty: "Count",
  38. useSingletons: false,
  39. preRequest: function (request) {
  40. if (this.name && this.cluster) {
  41. this.idPrefix = this.name + "_" + this.cluster;
  42. request['LogicalName'] = this.name;
  43. request['Cluster'] = this.cluster;
  44. } else if (this.name) {
  45. this.idPrefix = this.name;
  46. request['LogicalName'] = this.name;
  47. } else {
  48. this.idPrefix = this.wuid + "_" + this.sequence;
  49. request['Wuid'] = this.wuid;
  50. request['Sequence'] = this.sequence;
  51. }
  52. if (request.includeXmlSchema) {
  53. request['SuppressXmlSchema'] = false;
  54. } else {
  55. request['SuppressXmlSchema'] = true;
  56. }
  57. },
  58. preProcessResponse: function (response, request) {
  59. if (lang.exists("Result.Row", response)) {
  60. var context = this;
  61. arrayUtil.forEach(response.Result.Row, function (item, index) {
  62. item.__hpcc_rowNum = request.Start + index + 1;
  63. item.__hpcc_id = context.idPrefix + "_" + item.__hpcc_rowNum;
  64. });
  65. response.Result = response.Result.Row;
  66. }
  67. }
  68. });
  69. var Result = declare(null, {
  70. store: null,
  71. Total: "-1",
  72. constructor: function (args) {
  73. if (args) {
  74. declare.safeMixin(this, args);
  75. }
  76. if (lang.exists("Sequence", this)) {
  77. this.store = new Store({
  78. wuid: this.Wuid,
  79. sequence: this.Sequence,
  80. isComplete: this.isComplete()
  81. });
  82. } else if (lang.exists("Name", this) && lang.exists("ClusterName", this)) {
  83. this.store = new Store({
  84. wuid: this.Wuid,
  85. cluster: this.ClusterName,
  86. name: this.Name,
  87. isComplete: true
  88. });
  89. } else {
  90. this.store = new Store({
  91. wuid: this.Wuid,
  92. cluster: this.Cluster,
  93. name: this.Name,
  94. isComplete: true
  95. });
  96. }
  97. },
  98. getName: function () {
  99. return this.Name;
  100. },
  101. getID: function () {
  102. if (this.Sequence != null) {
  103. return this.Sequence;
  104. }
  105. return this.Name;
  106. },
  107. isComplete: function () {
  108. return this.Total != "-1";
  109. },
  110. canShowResults: function () {
  111. if (lang.exists("Sequence", this)) { // Regular WU result
  112. return true;
  113. } else if (lang.exists("RecordCount", this) && this.RecordCount != "") { // DFU Sprayed CSV File will fail here
  114. return true;
  115. }
  116. return false;
  117. },
  118. getFirstSchemaNode: function (node, name) {
  119. if (node && node.attributes) {
  120. if ((node.baseName && node.baseName == name) || (node.localName && node.localName == name) || (typeof (node.getAttribute) != "undefined" && node.getAttribute("name") == name)) {
  121. return node;
  122. }
  123. }
  124. for (var i = 0; i < node.childNodes.length; ++i) {
  125. var retVal = this.getFirstSchemaNode(node.childNodes[i], name);
  126. if (retVal) {
  127. return retVal;
  128. }
  129. }
  130. return null;
  131. },
  132. getFirstSequenceNode: function (schemaNode) {
  133. var row = this.getFirstSchemaNode(schemaNode, "Row");
  134. if (!row)
  135. row = schemaNode;
  136. var complexType = this.getFirstSchemaNode(row, "complexType");
  137. if (!complexType)
  138. return null;
  139. return this.getFirstSchemaNode(complexType, "sequence");
  140. },
  141. rowToTable: function (cell, __row, node) {
  142. var table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" }, node);
  143. if (lang.exists("Row", cell)) {
  144. cell = cell.Row;
  145. }
  146. if (Object.prototype.toString.call(cell) === '[object Array]') {
  147. for (var i = 0; i < cell.length; ++i) {
  148. if (i == 0) {
  149. var tr = domConstruct.create("tr", null, table);
  150. for (var key in cell[i]) {
  151. var th = domConstruct.create("th", { innerHTML: entities.encode(key) }, tr);
  152. }
  153. }
  154. var tr = domConstruct.create("tr", null, table);
  155. for (var key in cell[i]) {
  156. if (cell[i][key]) {
  157. if (Object.prototype.toString.call(cell[i][key]) === '[object Object]' || Object.prototype.toString.call(cell[i][key]) === '[object Array]') {
  158. var td = domConstruct.create("td", null, tr);
  159. this.rowToTable(cell[i][key], cell[i], td);
  160. } else if (key.indexOf("__html", key.length - "__html".length) !== -1) {
  161. var td = domConstruct.create("td", { innerHTML : cell[i][key] }, tr);
  162. } else if (key.indexOf("__javascript", key.length - "__javascript".length) !== -1) {
  163. var td = domConstruct.create("td", null, tr);
  164. this.injectJavascript(cell[i][key], cell[i], td);
  165. } else {
  166. var val = cell[i][key];
  167. var td = domConstruct.create("td", { innerHTML: Object.prototype.toString.call(val) === '[object String]' ? entities.encode(val) : val }, tr);
  168. }
  169. } else {
  170. var td = domConstruct.create("td", { innerHTML: "" }, tr);
  171. }
  172. }
  173. }
  174. }
  175. },
  176. injectJavascript : function(__cellContent, __row, __cell, __width) {
  177. // Add paragraph so cells can valign ---
  178. domConstruct.create("p", {
  179. style: {
  180. height : "1px"
  181. },
  182. innerHTML: "&nbsp;"
  183. }, __cell);
  184. try {
  185. eval(__cellContent);
  186. } catch (e) {
  187. __cell.innerHTML = "<b>Error:</b>&nbsp;&nbsp;" + entities.encode(e.message) + "<br>" + entities.encode(__cellContent);
  188. }
  189. },
  190. parseName: function (nameObj) {
  191. nameObj.width = 500;
  192. var titleParts = nameObj.name.split("__");
  193. if (titleParts.length >= 3) {
  194. var specifiedWidth = parseInt(titleParts[titleParts.length - 2]);
  195. if (!isNaN(specifiedWidth)) {
  196. nameObj.width = specifiedWidth;
  197. titleParts = titleParts.slice(0, titleParts.length - 1);
  198. }
  199. }
  200. titleParts = titleParts.slice(0, titleParts.length - 1);
  201. nameObj.displayName = titleParts.join("__");
  202. },
  203. getRowStructureFromSchema: function (parentNode, prefix) {
  204. var sequence = this.getFirstSequenceNode(parentNode, "sequence");
  205. if (!sequence)
  206. return null;
  207. var retVal = [];
  208. for (var i = 0; i < sequence.childNodes.length; ++i) {
  209. var node = sequence.childNodes[i];
  210. if (typeof (node.getAttribute) != "undefined") {
  211. var name = node.getAttribute("name");
  212. var type = node.getAttribute("type");
  213. var children = this.getRowStructureFromSchema(node, name + "_");
  214. var context = this;
  215. if (name && name.indexOf("__hidden", name.length - "__hidden".length) !== -1) {
  216. } else if (name && type) {
  217. if (name.indexOf("__html", name.length - "__html".length) !== -1) {
  218. var nameObj = {
  219. name: name
  220. };
  221. this.parseName(nameObj);
  222. retVal.push({
  223. label: nameObj.displayName,
  224. field: prefix + name,
  225. width: nameObj.width,
  226. className: "resultGridCell",
  227. formatter: function (cell, row) {
  228. return cell;
  229. },
  230. sortable: false
  231. });
  232. } else if (name.indexOf("__javascript", name.length - "__javascript".length) !== -1) {
  233. var nameObj = {
  234. name: name
  235. };
  236. this.parseName(nameObj);
  237. retVal.push({
  238. label: nameObj.displayName,
  239. field: prefix + name,
  240. width: nameObj.width,
  241. className: "resultGridCell",
  242. renderCell: function(row, cell, node, options) {
  243. context.injectJavascript(cell, row, node, this.width)
  244. },
  245. sortable: false
  246. });
  247. } else {
  248. retVal.push({
  249. label: name,
  250. field: prefix + name,
  251. width: this.extractWidth(type, name) * 9,
  252. className: "resultGridCell",
  253. sortable: false
  254. });
  255. }
  256. } else if (children) {
  257. var childWidth = 10; // Allow for html table
  258. arrayUtil.forEach(children, function(item, idx) {
  259. childWidth += item.width;
  260. });
  261. /*
  262. retVal.push({
  263. label: name,
  264. children: children,
  265. width: childWidth,
  266. className: "resultGridCell",
  267. sortable: false
  268. });
  269. */
  270. retVal.push({
  271. label: name,
  272. field: name,
  273. renderCell: function(row, cell, node, options) {
  274. context.rowToTable(cell, row, node);
  275. },
  276. width: childWidth,
  277. className: "resultGridCell",
  278. sortable: false
  279. });
  280. }
  281. }
  282. }
  283. return retVal.length ? retVal : null;
  284. },
  285. getRowStructureFromData: function (rows) {
  286. var retVal = [];
  287. for (var key in rows[0]) {
  288. if (key != "myInjectedRowNum") {
  289. var context = this;
  290. retVal.push({
  291. label: key,
  292. field: key,
  293. formatter: function (cell, row, grid) {
  294. if (Object.prototype.toString.call(cell) === '[object Object]' || Object.prototype.toString.call(cell) === '[object Array]') {
  295. var div = document.createElement("div");
  296. context.rowToTable(cell, row, div);
  297. return div.innerHTML;
  298. }
  299. return cell;
  300. },
  301. width: context.extractWidth("string12", key) * 9,
  302. className: "resultGridCell"
  303. });
  304. }
  305. }
  306. return retVal;
  307. },
  308. getStructure: function () {
  309. var structure = [
  310. {
  311. cells: [
  312. [
  313. {
  314. label: "##", field: "__hpcc_rowNum", width: 54, className: "resultGridCell", sortable: false
  315. }
  316. ]
  317. ]
  318. }
  319. ];
  320. var dom = parser.parse(this.XmlSchema);
  321. var dataset = this.getFirstSchemaNode(dom, "Dataset");
  322. var innerStruct = this.getRowStructureFromSchema(dataset, "");
  323. for (var i = 0; i < innerStruct.length; ++i) {
  324. structure[0].cells[structure[0].cells.length - 1].push(innerStruct[i]);
  325. }
  326. return structure[0].cells[0];
  327. },
  328. fetchStructure: function (callback) {
  329. if (this.XmlSchema) {
  330. callback(this.getStructure());
  331. } else {
  332. var context = this;
  333. var request = {};
  334. if (this.Wuid && lang.exists("Sequence", this)) {
  335. request['Wuid'] = this.Wuid;
  336. request['Sequence'] = this.Sequence;
  337. } else if (this.Name && this.ClusterName) {
  338. request['LogicalName'] = this.Name;
  339. request['Cluster'] = this.ClusterName;
  340. } else if (this.Name) {
  341. request['LogicalName'] = this.Name;
  342. }
  343. request['Start'] = 0;
  344. request['Count'] = 1;
  345. WsWorkunits.WUResult({
  346. request: request,
  347. load: function (response) {
  348. if (lang.exists("WUResultResponse.Result.XmlSchema.xml", response)) {
  349. context.XmlSchema = "<Result>" + response.WUResultResponse.Result.XmlSchema.xml + "</Result>";
  350. callback(context.getStructure());
  351. }
  352. /*
  353. if (rows.length) {
  354. var innerStruct = context.getRowStructureFromData(rows);
  355. for (var i = 0; i < innerStruct.length; ++i) {
  356. structure[0].cells[structure[0].cells.length - 1].push(innerStruct[i]);
  357. }
  358. }
  359. */
  360. }
  361. });
  362. }
  363. },
  364. getRowWidth: function (parentNode) {
  365. var retVal = 0;
  366. var sequence = this.getFirstSequenceNode(parentNode, "sequence");
  367. if (!sequence)
  368. return retVal;
  369. for (var i = 0; i < sequence.childNodes.length; ++i) {
  370. var node = sequence.childNodes[i];
  371. if (typeof (node.getAttribute) != "undefined") {
  372. var name = node.getAttribute("name");
  373. var type = node.getAttribute("type");
  374. if (name && type) {
  375. retVal += this.extractWidth(type, name);
  376. } else if (node.hasChildNodes()) {
  377. retVal += this.getRowWidth(node);
  378. }
  379. }
  380. }
  381. return retVal;
  382. },
  383. extractWidth: function (type, name) {
  384. var retVal = -1;
  385. switch (type) {
  386. case "xs:boolean":
  387. retVal = 5;
  388. break;
  389. case "xs:integer":
  390. retVal = 8;
  391. break;
  392. case "xs:nonNegativeInteger":
  393. retVal = 8;
  394. break;
  395. case "xs:double":
  396. retVal = 8;
  397. break;
  398. case "xs:string":
  399. retVal = 32;
  400. break;
  401. default:
  402. var numStr = "0123456789";
  403. var underbarPos = type.lastIndexOf("_");
  404. var length = underbarPos > 0 ? underbarPos : type.length;
  405. var i = length - 1;
  406. for (; i >= 0; --i) {
  407. if (numStr.indexOf(type.charAt(i)) == -1)
  408. break;
  409. }
  410. if (i + 1 < length) {
  411. retVal = parseInt(type.substring(i + 1, length));
  412. }
  413. if (type.indexOf("data") == 0) {
  414. retVal *= 2;
  415. }
  416. break;
  417. }
  418. if (retVal < name.length)
  419. retVal = name.length;
  420. return retVal;
  421. },
  422. getStore: function () {
  423. return this.store;
  424. },
  425. fetchContent: function () {
  426. var deferred = new Deferred()
  427. var context = this;
  428. this.store.query({
  429. Start: 0,
  430. Count: 1
  431. }).total.then(function(total) {
  432. context.store.query({
  433. Start: 0,
  434. Count: total
  435. }).then(function(results) {
  436. deferred.resolve(results);
  437. });
  438. });
  439. return deferred.promise;
  440. },
  441. getLoadingMessage: function () {
  442. if (lang.exists("wu.state", this)) {
  443. return "<span class=\'dojoxGridWating\'>[" + this.wu.state + "]</span>";
  444. }
  445. return "<span class=\'dojoxGridWating\'>[unknown]</span>";
  446. },
  447. getECLRecord: function () {
  448. var retVal = "RECORD\n";
  449. for (var i = 0; i < this.ECLSchemas.ECLSchemaItem.length; ++i) {
  450. retVal += "\t" + this.ECLSchemas.ECLSchemaItem[i].ColumnType + "\t" + this.ECLSchemas.ECLSchemaItem[i].ColumnName + ";\n";
  451. }
  452. retVal += "END;\n";
  453. return retVal;
  454. }
  455. });
  456. return {
  457. Get: function (params) {
  458. return new Result(params);
  459. }
  460. }
  461. });