QueryResultDataItem.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: BI Dashboard
  6. *| (C) Copyright IBM Corp. 2017
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['../../../lib/@waca/core-client/js/core-client/ui/core/Class', './QueryResultDataUtils', 'underscore'], function (BaseClass, Utils, _) {
  13. 'use strict';
  14. /**
  15. * QueryResultDataItem Class that stores information about one Query Result Data Item
  16. **/
  17. var DataItemClass = BaseClass.extend({
  18. /**
  19. * @private internal query data item
  20. */
  21. _dataItem: null,
  22. /**
  23. * @private internal flag to indicate if the tuple header has been normalized
  24. */
  25. _bTupleHeaderNormalized: false,
  26. /**
  27. * @constructor
  28. * @param {Object} dataItemObject Raw data item JSON object returned from dataset query service
  29. */
  30. init: function init(dataItemObject) {
  31. DataItemClass.inherited('init', this, arguments);
  32. this._dataItem = dataItemObject;
  33. this._aTuplePartNormalized = []; // internal flag to indicate if the tuple has been normalized
  34. this._bTupleHeaderNormalized = false;
  35. },
  36. getDecoration: function getDecoration() {
  37. return this._dataItem && this._dataItem.deco ? this._dataItem.deco : null;
  38. },
  39. /**
  40. * @return {Integer} Length of the data item tuples array
  41. **/
  42. getTuples: function getTuples() {
  43. return this._dataItem.items || [];
  44. },
  45. /**
  46. * @return {Integer} Length of the data item tuples array
  47. **/
  48. getTupleCount: function getTupleCount() {
  49. if (!this._dataItem || !this._dataItem.items) {
  50. return 0;
  51. }
  52. return this._dataItem.items.length;
  53. },
  54. /**
  55. * @return {Array} an array of normalized tuple parts
  56. **/
  57. getTuple: function getTuple(tupleIndex) {
  58. var aResult = this._dataItem && this._dataItem.items ? this._dataItem.items[tupleIndex].t : [];
  59. if (this._aTuplePartNormalized[tupleIndex] !== true) {
  60. //Normalize the tuple part if it has not been normalized
  61. _.each(aResult, function (oPart, index) {
  62. if (Utils.isRectifyNeeded(oPart)) {
  63. aResult[index] = Utils.getRectifiedData(oPart);
  64. Utils.normalizeData(aResult[index]);
  65. } else {
  66. Utils.normalizeData(oPart);
  67. }
  68. }.bind(this));
  69. this._aTuplePartNormalized[tupleIndex] = true; //Cache the normalized flag
  70. }
  71. return aResult;
  72. },
  73. getTupleHeader: function getTupleHeader() {
  74. return this.getTupleHeaders()[0]; /*backwards compatible legacy API*/
  75. },
  76. /**
  77. *Normally there is only one itemClassSet in one QueryResultDataItem object,
  78. *However, when there are multiple itemClassSets, each data item tuple should
  79. *have a itemClassSetIndex field that indicates its corresponding itemClassSet
  80. *@return {Number} 0 or +N, The index of the itemClassSet
  81. **/
  82. getTupleItemClassSetIndex: function getTupleItemClassSetIndex(tupleIndex) {
  83. if (this._dataItem && this._dataItem.items) {
  84. var nItemClassSetIndex = this._dataItem.items[tupleIndex].itemClassSetIndex;
  85. return nItemClassSetIndex !== undefined && nItemClassSetIndex !== null ? nItemClassSetIndex : 0;
  86. }
  87. return 0;
  88. },
  89. /**
  90. *@return {Array} of array of normalized tuple hearer
  91. **/
  92. getTupleHeaders: function getTupleHeaders() {
  93. var oResult = [];
  94. _.each(this._getTupleHeaderObjects(), function (entry) {
  95. oResult.push(entry ? entry.h || [] : []);
  96. });
  97. return oResult;
  98. },
  99. /**
  100. * @return {String} the ID of the tuple header which maps to the ProjectID in the DSS Query request
  101. */
  102. getTupleHeaderId: function getTupleHeaderId(index) {
  103. var oHeaderObject = this._getTupleHeaderObjects()[index || 0];
  104. return oHeaderObject ? oHeaderObject.id : null;
  105. },
  106. getAggregate: function getAggregate() {
  107. return this._getTupleHeaderObjects()[0].h[0].aggregate;
  108. },
  109. _getTupleHeaderObjects: function _getTupleHeaderObjects() {
  110. if (!this._dataItem || !this._dataItem.itemClass) {
  111. return null;
  112. }
  113. var itemClasses = this._getNormalizeItemClasses();
  114. if (this.isTupleHeaderNormalized !== true) {
  115. //Normalize each entry of the header if the whole header has not been normalized
  116. _.each(itemClasses, function (itemClassEntry) {
  117. this._getSingleTupleHeaderObject(itemClassEntry);
  118. }.bind(this));
  119. this.isTupleHeaderNormalized = true;
  120. }
  121. return itemClasses;
  122. },
  123. /**
  124. * Get the normalized array of itemClasses
  125. * @return {Object[]} array of itemClasses
  126. */
  127. _getNormalizeItemClasses: function _getNormalizeItemClasses() {
  128. // TODO Not sure why returns an array
  129. return _.isObject(this._dataItem.itemClass) && !_.isArray(this._dataItem.itemClass) ? [this._dataItem.itemClass] : this._dataItem.itemClass;
  130. },
  131. _getSingleTupleHeaderObject: function _getSingleTupleHeaderObject(itemClass) {
  132. _.each(itemClass.h, function (tuple) {
  133. if (Utils.isRectifyNeeded(tuple)) {
  134. tuple = Utils.getRectifiedData(tuple); //Only Create Object if tuple is not an Object
  135. }
  136. Utils.normalizeData(tuple);
  137. });
  138. },
  139. /**
  140. * Clip tuples array to the specified length
  141. * @param {Integer} the length limit of the tuples
  142. **/
  143. clipTuples: function clipTuples(limit) {
  144. if (this._dataItem && this._dataItem.items.length > 0 && this._dataItem.items.length > limit && limit >= 0) {
  145. this._dataItem.items.splice(limit, this._dataItem.items.length - limit);
  146. }
  147. },
  148. /**
  149. * Returns the number of dataitems in this result
  150. **/
  151. getNestedResultItemCount: function getNestedResultItemCount() {
  152. var count = 0;
  153. if (this._dataItem && this._dataItem.itemClass && this._dataItem.itemClass.h) {
  154. count = this._dataItem.itemClass.h.length;
  155. }
  156. return count;
  157. }
  158. });
  159. return DataItemClass;
  160. });
  161. //# sourceMappingURL=QueryResultDataItem.js.map