CommonQueryHelper.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Dashboard
  5. * (C) Copyright IBM Corp. 2017, 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. *
  8. */
  9. define(['../../../lib/@waca/dashboard-common/dist/core/Model', 'jquery', './CommonQueryBuilder', './QueryResultData'], function (Model, $, CommonQueryBuilder, QueryResultData) {
  10. 'use strict';
  11. /**
  12. * @classdesc A helper class to build and execute queries.
  13. * APIs included in this class:
  14. * 1. whenSingleItemQueryReady - build and run single item query with pageContext and search term.
  15. * 2. whenColumnsMinMaxQueryReady - build and run minmax query from a column.
  16. * 3. whenDataItemsMinMaxQueryReady - build and run minmax query from an array of dataItems.
  17. * 4. getRootMembers - build and run query to get root members of a hierarchy.
  18. * 5. getChildren - build and run query to get children of a hierarchy or level with a memberUniqueName.
  19. * 6. getSiblings - build and run query to get siblings of a hierarchy or level with a memberUniqueName.
  20. * 7. getAscendants - build and run query to get ascendants of a hierarchy with a set of memberUniqueNames.
  21. *
  22. * Common parameters used in the APIs:
  23. * @param {Object} sourceIdOrModule - mandatory: The sourceId or the module object for which we're running queries against
  24. * @param {Object} queryContext - optional: the query context that may includes pageContext, searchTerm, etc.
  25. * @param {Object} queryContext.pageContext - optional: the netPageContext.
  26. * @param {Object} queryDefinition - mandatory: the query definitions used to build the query spec.
  27. * @param {Object} queryDefinition.sort - optional: indicate if sort option for the item query.
  28. * @param {Object} queryDefinition.limit - optional: the result limit, i.e. how many rows to return.
  29. */
  30. var CommonQueryHelper = Model.extend({
  31. // Constant to decide the prompt priority
  32. 'BASE_PROMPT_WEIGHT': 0,
  33. 'SIGNON_PROMPT_WEIGHT': 100,
  34. init: function init(options, logger) {
  35. CommonQueryHelper.inherited('init', this, arguments);
  36. this.logger = logger;
  37. },
  38. getVesion: function getVesion() {
  39. return 'endor';
  40. },
  41. /**
  42. * Execute a query for a specific column or data item.
  43. * @param {Object} queryDefinition.column - optional: the column definition includes columnId. Note: column is mandatory if dataItems is not provided.
  44. * @param {Object} queryDefinition.dataItems - optional: required if queryDefinition doesn't have column.
  45. * @param {String} queryContext.searchTerm - optional: the search string used to refine the query result.
  46. */
  47. whenSingleItemQueryReady: function whenSingleItemQueryReady(sourceIdOrModule, queryDefinition, queryContext) {
  48. var oQuerySpec = CommonQueryBuilder.buildSingleItemQuery(queryDefinition, queryContext, this.logger);
  49. return this._runQuery(sourceIdOrModule, oQuerySpec);
  50. },
  51. /**
  52. * Execute minmax query of a specific column. The result of the query is pre-aggregated minmax values without any other context.
  53. * To get context dependent minmax values, please call whenDataItemsMinMaxQueryReady.
  54. * @param {Object} queryDefinition.column - mandatory: the column to get the minmax values.
  55. * @example
  56. * queryDefinition:
  57. * {
  58. * column: {
  59. * columnId: 'Sheet1.Revenue'
  60. * }
  61. * };
  62. *
  63. */
  64. whenColumnsMinMaxQueryReady: function whenColumnsMinMaxQueryReady(sourceIdOrModule, queryDefinition, queryContext) {
  65. var oQuerySpec = CommonQueryBuilder.buildMinMaxQueryFromColumns(queryDefinition, queryContext, this.logger);
  66. return this._runQuery(sourceIdOrModule, oQuerySpec);
  67. },
  68. /**
  69. * Execute minmax query with dataitems and projections definitions.
  70. * @param {Object} queryDefinition.dataItems - mandatory: each dataitem definition should provide an 'aggregate' if this dataitem is to get minmax values.
  71. * @param {Object} queryDefinition.projections - mandatory: the projections should only includes the dataitem ids requesting the minmax values.
  72. * @example
  73. * queryDefinition:
  74. * {
  75. * 'dataItems': [
  76. * {
  77. * 'id': 'Sheet1.Product_line',
  78. * 'itemId': 'Sheet1.Product_line',
  79. * 'itemLabel': 'Product line'
  80. * },
  81. * {
  82. * 'id': 'Sheet1.Quantity',
  83. * 'itemId': 'Sheet1.Quantity',
  84. * 'itemLabel': 'Quantity',
  85. * 'aggregate': 'sum'
  86. * },
  87. * {
  88. * 'id': 'Sheet1.Unit_cost',
  89. * 'itemId': 'Quantity',
  90. * 'itemLabel': 'Unit cost',
  91. * 'aggregate': 'sum'
  92. * }
  93. * ],
  94. * 'projections': [
  95. * 'Sheet1.Quantity',
  96. * 'Sheet1.Unit_cost'
  97. * ]
  98. * };
  99. * The minmax result will have 2 set of minmax values, one for Sheet1.Quantity, and one for Sheet1.Unit_cost
  100. *
  101. */
  102. whenDataItemsMinMaxQueryReady: function whenDataItemsMinMaxQueryReady(sourceIdOrModule, queryDefinition, queryContext) {
  103. var oQuerySpec = CommonQueryBuilder.buildMinMaxQueryFromDataItems(queryDefinition, queryContext, this.logger);
  104. return this._runQuery(sourceIdOrModule, oQuerySpec);
  105. },
  106. /**
  107. * Execute functional query.
  108. * @param {Object} queryDefinition.dataItems - mandatory: dataItems definitions of the query spec.
  109. */
  110. _runFunctionQuery: function _runFunctionQuery(sourceIdOrModule, queryDefinition, queryContext) {
  111. var queryDefinition_1 = {
  112. 'dataItems': queryDefinition.dataItems,
  113. 'projections': CommonQueryBuilder.buildProjections(queryDefinition, this.logger)
  114. };
  115. return this.whenSingleItemQueryReady(sourceIdOrModule, queryDefinition_1, queryContext, this.logger);
  116. },
  117. /**
  118. * @param {Object} queryDefinition.column - mandatory: the hierarchy column used to build dataItems and projections in query spec.
  119. * @example
  120. * queryDefinition:
  121. * {
  122. * column: {columnId: '[hierarchyUniqueName]'},
  123. * limit: 51
  124. * };
  125. */
  126. getRootMembers: function getRootMembers(sourceIdOrModule, queryDefinition, queryContext) {
  127. var functionDefinitions = {
  128. 'functionName': 'rootMembers',
  129. 'functionParameter': 'true'
  130. };
  131. var dataItems = CommonQueryBuilder.buildFunctionQueryDataItems(queryDefinition, functionDefinitions);
  132. queryDefinition.dataItems = dataItems;
  133. return this._runFunctionQuery(sourceIdOrModule, queryDefinition, queryContext);
  134. },
  135. /**
  136. * @param {Object} queryDefinition.column - mandatory: the column used to build dataItems and projections in query spec.
  137. * @param {Object} queryDefinition.mun - mandatory: the member unique name of the function query.
  138. * @example
  139. * queryDefinition:
  140. * {
  141. * column: {columnId: '[hierarchyUniqueName]'},
  142. * limit: 51,
  143. * mun: '[memberUniqueName]'
  144. * };
  145. */
  146. getChildren: function getChildren(sourceIdOrModule, queryDefinition, queryContext) {
  147. var functionDefinitions = {
  148. 'functionName': 'children',
  149. 'functionParameter': queryDefinition.mun
  150. };
  151. var dataItems = CommonQueryBuilder.buildFunctionQueryDataItems(queryDefinition, functionDefinitions);
  152. queryDefinition.dataItems = dataItems;
  153. return this._runFunctionQuery(sourceIdOrModule, queryDefinition, queryContext);
  154. },
  155. /**
  156. * @see getChildren.
  157. */
  158. getSiblings: function getSiblings(sourceIdOrModule, queryDefinition, queryContext) {
  159. var functionDefinitions = {
  160. 'functionName': 'siblings',
  161. 'functionParameter': queryDefinition.mun
  162. };
  163. var dataItems = CommonQueryBuilder.buildFunctionQueryDataItems(queryDefinition, functionDefinitions);
  164. queryDefinition.dataItems = dataItems;
  165. return this._runFunctionQuery(sourceIdOrModule, queryDefinition, queryContext);
  166. },
  167. /**
  168. * @param {Object} queryDefinition.muns - mandatory: an array of member unique names.
  169. * @example
  170. * queryDefinition:
  171. * {
  172. * column: {columnId: '[hierarchyUniqueName]'},
  173. * limit: 51,
  174. * mun: ['[memberUniqueName1]',
  175. * '[memberUniqueName2]']
  176. * };
  177. */
  178. getAscendants: function getAscendants(sourceIdOrModule, queryDefinition, queryContext) {
  179. var functionDefinitions = {
  180. 'functionName': 'ascendants',
  181. 'functionParameter': queryDefinition.muns
  182. };
  183. var dataItems = CommonQueryBuilder.buildFunctionQueryDataItems(queryDefinition, functionDefinitions);
  184. queryDefinition.dataItems = dataItems;
  185. return this._runFunctionQuery(sourceIdOrModule, queryDefinition, queryContext);
  186. },
  187. extactListOfQueryResultData: function extactListOfQueryResultData(oQueryResultData) {
  188. var resultRowSize = oQueryResultData.getDatapointCount();
  189. var aResults = [];
  190. for (var rowIndex = 0; rowIndex < resultRowSize; rowIndex++) {
  191. var value = oQueryResultData.getCellValue(rowIndex, 0)[0];
  192. aResults.push(value);
  193. }
  194. return aResults;
  195. },
  196. _runQuery: function _runQuery(sourceIdOrModule, oQuerySpec) {
  197. var queryOptions = {
  198. 'querySpec': oQuerySpec,
  199. 'sourceIdOrModule': sourceIdOrModule
  200. };
  201. return this.queryService.runQuery(queryOptions).then(function (queryResponses) {
  202. return new QueryResultData(queryResponses.data);
  203. });
  204. }
  205. });
  206. return CommonQueryHelper;
  207. });
  208. //# sourceMappingURL=CommonQueryHelper.js.map