VisQuerySelectionBuilder.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. /**
  6. * Licensed Materials - Property of IBM
  7. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2017, 2020
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *
  10. */
  11. define(['./postProcess/LocalAggregateSortInfo', '../../dashboard/queryService/QuerySelectionBuilder', 'underscore'], function (LocalAggregateSortInfo, QuerySelectionBuilder, _) {
  12. var VisQuerySelectionBuilder = function (_QuerySelectionBuilde) {
  13. _inherits(VisQuerySelectionBuilder, _QuerySelectionBuilde);
  14. function VisQuerySelectionBuilder(_ref) {
  15. var internalVisualization = _ref.internalVisualization,
  16. layerToQuerySlotsMap = _ref.layerToQuerySlotsMap,
  17. applyServerSort = _ref.applyServerSort;
  18. _classCallCheck(this, VisQuerySelectionBuilder);
  19. var _this = _possibleConstructorReturn(this, _QuerySelectionBuilde.call(this));
  20. _this.internalVisualization = internalVisualization;
  21. _this._querySlotSortPriority = 0;
  22. _this._serverSort = applyServerSort;
  23. _this._localAggregateSortInfo = new LocalAggregateSortInfo(internalVisualization);
  24. _this._querySlotSortPriority = _this._getDataItemsHighestSortPriority(layerToQuerySlotsMap) + 1;
  25. return _this;
  26. }
  27. VisQuerySelectionBuilder.prototype._forEachQuerySlotDataItem = function _forEachQuerySlotDataItem(layerToQuerySlotsMap, callback) {
  28. layerToQuerySlotsMap.forEach(function (querySlots) {
  29. querySlots.forEach(function (slot) {
  30. var dataItems = slot.getDataItemList();
  31. dataItems.forEach(function (item) {
  32. callback(item, slot);
  33. });
  34. });
  35. });
  36. };
  37. /**
  38. * override the super build.
  39. */
  40. VisQuerySelectionBuilder.prototype.build = function build(dataItem, slot, outputEdgeFilterExceptions) {
  41. var finalSelections = [];
  42. if (!dataItem.getBinning()) {
  43. //By default, return selections in the order agreed to by PM.
  44. finalSelections.push.apply(finalSelections, this._buildSelectionsFromDrillStateForHierarchyOrSet(dataItem));
  45. finalSelections.push.apply(finalSelections, this._buildSelectionsFromLocalFiltersForOLAPDataItems(dataItem, outputEdgeFilterExceptions));
  46. finalSelections.push.apply(finalSelections, this._buildSelectionsFromSearchFiltersForOLAPDataItems(dataItem, outputEdgeFilterExceptions));
  47. finalSelections.push.apply(finalSelections, this._buildSelectionsForTopBottom(dataItem));
  48. finalSelections.push.apply(finalSelections, this._buildSelectionsForSort(dataItem, slot));
  49. } else {
  50. finalSelections.push.apply(finalSelections, this._buildSelectionsFromLocalFiltersForOLAPDataItems(dataItem, outputEdgeFilterExceptions));
  51. finalSelections.push.apply(finalSelections, this._buildSelectionsFromSearchFiltersForOLAPDataItems(dataItem, outputEdgeFilterExceptions));
  52. finalSelections.push.apply(finalSelections, this._buildSelectionsForSort(dataItem, slot));
  53. }
  54. return finalSelections;
  55. };
  56. //For hierarchies or OLAP set, build one of root members, children of single root or children of current drill member.
  57. VisQuerySelectionBuilder.prototype._buildSelectionsFromDrillStateForHierarchyOrSet = function _buildSelectionsFromDrillStateForHierarchyOrSet(dataItem) {
  58. var localFilters = this.internalVisualization.getLocalFilters();
  59. var filterList = localFilters && localFilters.getFilterList() || [];
  60. return _QuerySelectionBuilde.prototype._buildSelectionsFromDrillStateForHierarchyOrSet.call(this, dataItem, filterList);
  61. };
  62. VisQuerySelectionBuilder.prototype._buildSelectionsFromSearchFiltersForOLAPDataItems = function _buildSelectionsFromSearchFiltersForOLAPDataItems(dataItem, outputEdgeFilterExceptions) {
  63. var searchFilterList = this.internalVisualization.getSearchFilterList();
  64. return _QuerySelectionBuilde.prototype._buildSelectionsFromFiltersForOLAPDataItems.call(this, dataItem, searchFilterList, outputEdgeFilterExceptions);
  65. };
  66. VisQuerySelectionBuilder.prototype._buildSelectionsFromLocalFiltersForOLAPDataItems = function _buildSelectionsFromLocalFiltersForOLAPDataItems(dataItem, outputEdgeFilterExceptions) {
  67. var localFilters = this.internalVisualization.getLocalFilters();
  68. var localFilterList = localFilters.getFilterList();
  69. return _QuerySelectionBuilde.prototype._buildSelectionsFromFiltersForOLAPDataItems.call(this, dataItem, localFilterList, outputEdgeFilterExceptions);
  70. };
  71. /*
  72. * Sort application priority:
  73. * Apply user defined sort if there is one,
  74. * Apply model defined sort if there is one and user defined sort doesn't exit,
  75. * Apply default sort
  76. */
  77. VisQuerySelectionBuilder.prototype._buildSelectionsForSort = function _buildSelectionsForSort(dataItem, slot) {
  78. var bClientSortEnabled = this._serverSort ? false : this._isClientSideSortEnabledForDataItem(dataItem);
  79. var selections = [];
  80. if (!bClientSortEnabled) {
  81. var dataItemOrModelSort = dataItem.getSort();
  82. var sort = !dataItem.hasDefaultSort() && dataItemOrModelSort;
  83. sort = sort || dataItem.hasModelSort() && dataItemOrModelSort;
  84. var sortObj = sort && sort.type ? sort : null;
  85. var isIgnoreSlotSort = this._ignoreDefaultSlotSort(dataItem);
  86. // 3. Default sort: preserve the hierarchical order for Olap column.
  87. if (!sortObj && !isIgnoreSlotSort) {
  88. sortObj = this._getDefaultSort(slot, dataItem);
  89. if (sortObj) {
  90. sortObj.priority = this._querySlotSortPriority++;
  91. }
  92. }
  93. if (sortObj) {
  94. var sortSelectionSpec = {
  95. 'operation': 'order',
  96. 'sort': _.pick(sortObj, 'type', 'by', 'priority')
  97. };
  98. if (sortObj.context) {
  99. sortSelectionSpec.context = [{
  100. itemId: sortObj.context
  101. }];
  102. }
  103. selections = [sortSelectionSpec];
  104. }
  105. }
  106. return selections;
  107. };
  108. VisQuerySelectionBuilder.prototype._isClientSideSortEnabledForDataItem = function _isClientSideSortEnabledForDataItem(dataItem) {
  109. var localSortInfo = this._localAggregateSortInfo.getSortInfo();
  110. if (localSortInfo) {
  111. return localSortInfo.aggregateDataItemId === dataItem.getId();
  112. }
  113. return false;
  114. };
  115. /**
  116. *
  117. * @param {*} slot
  118. * @param {*} dataItem
  119. * todo: the original implementation at VisDataItemManager.getDefaultSort() need to be removed once useAPI.
  120. */
  121. VisQuerySelectionBuilder.prototype._getDefaultSort = function _getDefaultSort(slot, dataItem) {
  122. var sortObj = null;
  123. var sortType = void 0;
  124. var topBottomInfo = dataItem.getTopBottom();
  125. if (topBottomInfo) {
  126. var topBottomType = topBottomInfo.type;
  127. sortType = topBottomType === 'topcount' || topBottomType === 'toppercent' ? 'desc' : 'asc';
  128. sortObj = {
  129. type: sortType
  130. };
  131. if (topBottomInfo.context) {
  132. //Dataitem with top/bottom will always sort by 'value'.
  133. sortObj.by = 'value';
  134. sortObj.context = topBottomInfo.context.itemId;
  135. }
  136. } else {
  137. // 2. Sort from slot definition, but only if there is no topBottom set
  138. sortType = slot.getDefinition().getSort();
  139. // since after binning, dataItem type is attribute, if there is no sort on slot and measure add 'asc' sort
  140. if (!sortType && !!dataItem.getBinning()) {
  141. sortType = 'asc';
  142. }
  143. if (sortType) {
  144. sortObj = {
  145. type: sortType
  146. };
  147. }
  148. }
  149. return sortObj;
  150. };
  151. /**
  152. *
  153. * Default sort is ignored in the following cases:
  154. * - data source is OLAP or NamedSet and no topBottom is defined or no rank is defined for the dataItem
  155. * - data source is not OLAP and is not NamedSet and no topBottom is defined up to the passed one and one topBottom is defined after it
  156. * @param {Object} dataItem - current dataItem; see {@link DataItemAPI}
  157. * @returns {boolean} true if the default sort is to be ignored, false otherwise
  158. *
  159. * todo: the original implementation at VisDataSlotsManager.ignoreDefaultSlotSort() need to be removed once useAPI.
  160. */
  161. VisQuerySelectionBuilder.prototype._ignoreDefaultSlotSort = function _ignoreDefaultSlotSort(dataItem) {
  162. var ignore = false;
  163. var dataItemMappingInfoList = this.internalVisualization.getSlots().getMappingInfoList();
  164. var indexInMappingInfoList = -1;
  165. _.find(dataItemMappingInfoList, function (dataItemMappingObj, ind) {
  166. if (dataItemMappingObj.dataItem.getId() === dataItem.getId()) {
  167. indexInMappingInfoList = ind;
  168. return true;
  169. }
  170. return false;
  171. });
  172. var dataitemTopBottom = dataItem.getTopBottom();
  173. if (dataItem.getMetadataColumn().isOlapColumn() || dataItem.getMetadataColumn().isNamedSet()) {
  174. ignore = !dataitemTopBottom || !dataitemTopBottom.rank;
  175. } else {
  176. ignore = !this._hasPreviousTopBottom(dataItemMappingInfoList, indexInMappingInfoList) && !dataitemTopBottom && this._hasFollowedTopBottom(dataItemMappingInfoList, indexInMappingInfoList);
  177. }
  178. return ignore;
  179. };
  180. /**
  181. * check if there is any topBottom definition previous to the passed index
  182. * @param {Object} dataItemMappingInfoList - arrays of mappingInfo
  183. * @param {number} index - index of the current dataItem
  184. * @returns true if there is a topBottom definition defined before the passed index, false otherwise
  185. */
  186. VisQuerySelectionBuilder.prototype._hasPreviousTopBottom = function _hasPreviousTopBottom(dataItemMappingInfoList, index) {
  187. var hasPrevious = false;
  188. for (var i = 0; i < index; i++) {
  189. if (dataItemMappingInfoList[i].dataItem.getTopBottom()) {
  190. hasPrevious = true;
  191. break;
  192. }
  193. }
  194. return hasPrevious;
  195. };
  196. /**
  197. * check if there is any topBottom definition after to the passed index
  198. * @param {Object} dataItemMappingInfoList - arrays of mappingInfo
  199. * @param {number} index - index of the current dataItem
  200. * @returns true if there is a topBottom definition defined after the passed index, false otherwise
  201. */
  202. VisQuerySelectionBuilder.prototype._hasFollowedTopBottom = function _hasFollowedTopBottom(dataItemMappingInfoList, index) {
  203. var hasFollowed = false;
  204. for (var i = index + 1; i < dataItemMappingInfoList.length; i++) {
  205. if (dataItemMappingInfoList[i].dataItem.getTopBottom()) {
  206. hasFollowed = true;
  207. break;
  208. }
  209. }
  210. return hasFollowed;
  211. };
  212. VisQuerySelectionBuilder.prototype._getDataItemsHighestSortPriority = function _getDataItemsHighestSortPriority(layerToQuerySlotsMap) {
  213. var maxPriority = 0;
  214. this._forEachQuerySlotDataItem(layerToQuerySlotsMap, function (dataItem) {
  215. var sortObj = dataItem.getSort();
  216. if (sortObj && maxPriority < sortObj.priority) {
  217. maxPriority = sortObj.priority;
  218. }
  219. });
  220. return maxPriority;
  221. };
  222. return VisQuerySelectionBuilder;
  223. }(QuerySelectionBuilder);
  224. return VisQuerySelectionBuilder;
  225. });
  226. //# sourceMappingURL=VisQuerySelectionBuilder.js.map