SummaryQueryModifier.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../dataQueryExecution/api/QueryModifierAPI', './api/SummaryQueryModifierAPI', 'underscore'], function (APIFactory, QueryModifierAPI, SummaryQueryModifierAPI, _) {
  9. var SummaryQueryModifier = function () {
  10. function SummaryQueryModifier(options) {
  11. _classCallCheck(this, SummaryQueryModifier);
  12. var features = options && options.features;
  13. if (features) {
  14. this._mainModifier = features.QueryModifier;
  15. }
  16. var queryExecution = features && features.DataQueryExecution;
  17. if (queryExecution) {
  18. queryExecution.registerQueryModifier(this.getAPI());
  19. }
  20. this._dataItemLists = [];
  21. }
  22. SummaryQueryModifier.prototype.getAPI = function getAPI() {
  23. if (!this.api) {
  24. this.api = APIFactory.createAPI(this, [QueryModifierAPI, SummaryQueryModifierAPI]);
  25. }
  26. return this.api;
  27. };
  28. SummaryQueryModifier.prototype.getType = function getType() {
  29. return 'summary';
  30. };
  31. SummaryQueryModifier.prototype.resetDataItemIdList = function resetDataItemIdList() {
  32. this._dataItemLists = [];
  33. };
  34. SummaryQueryModifier.prototype.addDataItemIdList = function addDataItemIdList(dataItemList) {
  35. this._dataItemLists.push(dataItemList);
  36. };
  37. /**
  38. * Modify query spec based on the data item list added from SummaryTask in order.
  39. */
  40. SummaryQueryModifier.prototype.modifyQuerySpecList = function modifyQuerySpecList(specList) {
  41. var _this = this;
  42. specList = this._mainModifier && this._mainModifier.modifyQuerySpecList(specList);
  43. var extraFiltersRequired = [];
  44. var dataItemList = this._dataItemLists.shift();
  45. specList.forEach(function (specEntry) {
  46. specEntry.spec.dataItems = _.filter(specEntry.spec.dataItems, function (dataItem) {
  47. if (dataItemList.indexOf(dataItem.id) < 0) {
  48. // if the dataItem is not projected, but the dataItem affects the shape of the data result, e.g topbottom,
  49. // keep the dataItem in the list AND add a filter entry.
  50. var extraFilter = _this._getSummaryExtraFilter(dataItem);
  51. if (extraFilter) {
  52. extraFiltersRequired.push(extraFilter);
  53. return true;
  54. }
  55. // otherwise remove it from the summary query
  56. return false;
  57. }
  58. // keep it as part of the summary query
  59. return true;
  60. });
  61. specEntry.spec.projections = _.filter(specEntry.spec.projections, function (projection) {
  62. return dataItemList.indexOf(projection) > -1;
  63. });
  64. if (extraFiltersRequired.length) {
  65. var addExtraFilters = function addExtraFilters(specFilters, extraFilters) {
  66. specFilters.push.apply(specFilters, extraFilters);
  67. return specFilters;
  68. };
  69. specEntry.spec.filters = specEntry.spec.filters ? addExtraFilters(specEntry.spec.filters, extraFiltersRequired) : extraFiltersRequired;
  70. }
  71. });
  72. return specList;
  73. };
  74. /**
  75. * @private
  76. * Get the extra filter required for non-projected dataitems in a summary query
  77. * @param {object} dataItem - dataItem
  78. * @return {object} filter entry representing the non-projected data item context, null if an extra filter is not required
  79. */
  80. SummaryQueryModifier.prototype._getSummaryExtraFilter = function _getSummaryExtraFilter(dataItem) {
  81. var extraFilter = null;
  82. _.each(dataItem.selection, function (select) {
  83. if (select.operation !== 'order') {
  84. extraFilter = {
  85. type: 'pre',
  86. expression: {
  87. operator: 'in',
  88. itemId: dataItem.itemId,
  89. valueDataItem: dataItem.id
  90. }
  91. };
  92. }
  93. });
  94. return extraFilter;
  95. };
  96. return SummaryQueryModifier;
  97. }();
  98. return SummaryQueryModifier;
  99. });
  100. //# sourceMappingURL=SummaryQueryModifier.js.map