DataItemUpgrade.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. *+------------------------------------------------------------------------+
  7. *| Licensed Materials - Property of IBM
  8. *| IBM Cognos Products: BI Dashboard
  9. *| (C) Copyright IBM Corp. 2018, 2019
  10. *|
  11. *| US Government Users Restricted Rights - Use, duplication or disclosure
  12. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  13. *+------------------------------------------------------------------------+
  14. */
  15. define(['underscore', '../../../lib/@waca/upgrades/UpgradeBase'], function (_, UpgradeBase) {
  16. /**
  17. * Upgrade data item ids in dashboard spec for better performance to avoid querying same data items with different ids.
  18. **/
  19. var DataItemUpgrade = function (_UpgradeBase) {
  20. _inherits(DataItemUpgrade, _UpgradeBase);
  21. function DataItemUpgrade() {
  22. _classCallCheck(this, DataItemUpgrade);
  23. var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
  24. _this.VERSION = 1015;
  25. return _this;
  26. }
  27. /**
  28. * Perform upgrade
  29. *
  30. * @param {object} spec - spec to perform upgrade on
  31. *
  32. * @return {Promise} Promise to be resolved when upgrade performed
  33. */
  34. DataItemUpgrade.prototype.up = function up(spec) {
  35. var _this2 = this;
  36. if (!spec || !spec.widgets || _.isEmpty(spec.widgets)) {
  37. return Promise.resolve(spec);
  38. }
  39. for (var key in spec.widgets) {
  40. var widget = spec.widgets[key];
  41. widget = this._mergeIncorrectDataItems(widget);
  42. var dataViews = widget.data && widget.data.dataViews;
  43. if (dataViews) {
  44. dataViews.forEach(function (dataView) {
  45. dataView.dataItems.forEach(function (dataItem, curIndex) {
  46. dataView.dataItems[curIndex] = _this2._upgradeDataItemFormat(dataItem);
  47. });
  48. });
  49. spec.widgets[key].data.dataViews = dataViews;
  50. }
  51. }
  52. return Promise.resolve(spec);
  53. };
  54. // Downgrades are not available in CA
  55. DataItemUpgrade.prototype.down = function down(spec) {
  56. return Promise.resolve(spec);
  57. };
  58. DataItemUpgrade.prototype._mergeIncorrectDataItems = function _mergeIncorrectDataItems(widget) {
  59. // RTC Defect #262782
  60. // we encounter this issue when upgrading r13-endor in a widget with a multi-measure group created by moving a dataitem from a different slot
  61. // we have 2 data items inside the dataview where the modelref of dataitem 2 matches the id of dataitem 1
  62. // the correct version features just 1 data item with a modelref = pagecontext.sources.id
  63. if (!widget || !widget.data || !widget.data.dataViews || !widget.slotmapping) return widget;
  64. var dataViews = widget.data.dataViews;
  65. // for map widgets we have multiple valid data views and layers, we ignore this case
  66. if (dataViews.length < 2 || widget.slotmapping.layers) return dataViews;
  67. var base = void 0;
  68. var slotMappingDataItems = [];
  69. widget.slotmapping.slots.forEach(function (slot) {
  70. return slotMappingDataItems = slotMappingDataItems.concat(slot.dataItems);
  71. });
  72. // merge 2 dataviews data items
  73. var _merge = function _merge(base, dataView) {
  74. dataView.dataItems.forEach(function (dataItem) {
  75. if (slotMappingDataItems.indexOf(dataItem.id) !== -1) {
  76. base.dataItems.push(dataItem);
  77. }
  78. });
  79. return base;
  80. };
  81. dataViews.forEach(function (dataView, curIndex) {
  82. if (!base) {
  83. base = dataView;
  84. }
  85. if (base && base.id === dataView.modelRef) {
  86. base = _merge(base, dataView);
  87. dataViews.splice(curIndex); // delete old data view
  88. }
  89. });
  90. dataViews[0] = base;
  91. widget.data.dataViews = dataViews;
  92. return widget;
  93. };
  94. DataItemUpgrade.prototype._upgradeDataItemFormat = function _upgradeDataItemFormat(dataItem) {
  95. if (dataItem && dataItem.format && dataItem.format.formatSpec && dataItem.format.formatSpec.type) {
  96. var formatSpec = dataItem.format.formatSpec,
  97. type = formatSpec.type;
  98. if (type === 'datetime' || type === 'date') {
  99. if (type === 'datetime') {
  100. formatSpec = this._upgradeDateTimeFormat(formatSpec);
  101. }
  102. formatSpec = this._syncDateStyle(formatSpec);
  103. } else if (type === 'number') {
  104. formatSpec = this._upgradeNumberFormat(formatSpec);
  105. }
  106. dataItem.format.formatSpec = formatSpec;
  107. }
  108. return dataItem;
  109. };
  110. DataItemUpgrade.prototype._syncDateStyle = function _syncDateStyle(formatSpec) {
  111. if (formatSpec.formatLength && formatSpec.selectedFormat) {
  112. formatSpec.dateStyle = formatSpec.selectedFormat;
  113. }
  114. return formatSpec;
  115. };
  116. DataItemUpgrade.prototype._upgradeDateTimeFormat = function _upgradeDateTimeFormat(formatSpec) {
  117. if (formatSpec.formatLength && formatSpec.selectedFormat) {
  118. if (formatSpec.formatLength === 'medium' && formatSpec.selectedFormat === 'medium') {
  119. formatSpec.formatLength = 'long';
  120. formatSpec.selectedFormat = 'long';
  121. }
  122. formatSpec.dateStyle = formatSpec.selectedFormat;
  123. }
  124. return formatSpec;
  125. };
  126. DataItemUpgrade.prototype._upgradeNumberFormat = function _upgradeNumberFormat(formatSpec) {
  127. // sometimes maximumFractionDigits is the number 0 which will return false so we explicitly check against null and undefined
  128. if (formatSpec.maximumFractionDigits !== null && formatSpec.maximumFractionDigits !== undefined && _.isNumber(formatSpec.maximumFractionDigits)) {
  129. formatSpec.maximumFractionDigits = formatSpec.maximumFractionDigits.toString();
  130. }
  131. if (formatSpec.minimumFractionDigits !== null && formatSpec.minimumFractionDigits !== undefined && _.isNumber(formatSpec.minimumFractionDigits)) {
  132. formatSpec.minimumFractionDigits = formatSpec.minimumFractionDigits.toString();
  133. } else {
  134. formatSpec.minimumFractionDigits = formatSpec.maximumFractionDigits;
  135. }
  136. return formatSpec;
  137. };
  138. return DataItemUpgrade;
  139. }(UpgradeBase);
  140. return new DataItemUpgrade();
  141. });
  142. //# sourceMappingURL=DataItemUpgrade.js.map