DataItemIdUpgrade.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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
  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', '../../../lib/@waca/dashboard-common/dist/core/UniqueHashIdBuilder'], function (_, UpgradeBase, UniqueHashIdBuilder) {
  16. /**
  17. * Upgrade data item ids in dashboard spec for better performance to avoid querying same data items with different ids.
  18. **/
  19. var DataItemIdUpgrade = function (_UpgradeBase) {
  20. _inherits(DataItemIdUpgrade, _UpgradeBase);
  21. function DataItemIdUpgrade() {
  22. _classCallCheck(this, DataItemIdUpgrade);
  23. var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
  24. _this.VERSION = 1014;
  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. DataItemIdUpgrade.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. var dataViews = widget.data && widget.data.dataViews;
  42. if (dataViews) {
  43. (function () {
  44. var dataItemIdsMap = {};
  45. dataViews.forEach(function (dataView) {
  46. dataView.dataItems.forEach(function (dataItem) {
  47. if (dataItem.id !== '_multiMeasuresSeries') {
  48. dataItemIdsMap[dataItem.id] = UniqueHashIdBuilder.createUniqueHashId(dataItem.itemId, dataItemIdsMap);
  49. }
  50. });
  51. });
  52. spec.widgets[key] = _this2._upgradeDataItemIds(widget, dataItemIdsMap);
  53. })();
  54. }
  55. }
  56. return Promise.resolve(spec);
  57. };
  58. // Downgrades are not available in CA
  59. DataItemIdUpgrade.prototype.down = function down(spec) {
  60. return Promise.resolve(spec);
  61. };
  62. /**
  63. * Replace all the old dataItem id with the new unique hash id in the widget
  64. * @param {object} widget
  65. * @param {map} dataItemIdsMap - key is dataItem.id and value is unique hash id
  66. */
  67. DataItemIdUpgrade.prototype._upgradeDataItemIds = function _upgradeDataItemIds(widget, dataItemIdsMap) {
  68. var widgetStr = JSON.stringify(widget);
  69. for (var key in dataItemIdsMap) {
  70. var re = new RegExp('"' + key + '"', 'g');
  71. widgetStr = widgetStr.replace(re, '"' + dataItemIdsMap[key] + '"');
  72. }
  73. return JSON.parse(widgetStr);
  74. };
  75. return DataItemIdUpgrade;
  76. }(UpgradeBase);
  77. return new DataItemIdUpgrade();
  78. });
  79. //# sourceMappingURL=DataItemIdUpgrade.js.map