6.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: BI Dashboard
  6. *| (C) Copyright IBM Corp. 2016, 2017
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['../../../lib/@waca/core-client/js/core-client/ui/core/Class', '../../../lib/@waca/upgrades/UpgradeBase', 'underscore'], function (Class, UpgradeBase, _) {
  13. var Upgrade = Class.extend([UpgradeBase], {
  14. init: function init() {
  15. this.VERSION = 6;
  16. },
  17. /**
  18. * Perform upgrade
  19. *
  20. * @param {object} spec - spec to perform upgrade on
  21. *
  22. * @return {Promise} Promise to be resolved when upgrade performed
  23. */
  24. up: function up(spec) {
  25. if (!spec) {
  26. return Promise.resolve(spec);
  27. }
  28. // Upgrade dataset shaping
  29. this._upgradeDatasetShaping(spec);
  30. // Upgrade widget spec;
  31. _.each(spec.widgets, this._upgradeWidgetSpec.bind(this));
  32. return Promise.resolve(spec);
  33. },
  34. _upgradeDatasetShaping: function _upgradeDatasetShaping(oBoardSpec) {
  35. var datasetShaping = oBoardSpec.datasetShaping;
  36. var widgets = oBoardSpec.widgets;
  37. //Update model mappings attributes
  38. if (!datasetShaping || !widgets) {
  39. return false; //Upgrade is not needed
  40. }
  41. var bUpgraded = false;
  42. var aDatasets = [];
  43. //Local cache all distinct data sets with type info
  44. var sID = null;
  45. _.each(widgets, function (widget) {
  46. if (widget.dataSet) {
  47. sID = widget.dataSet.id;
  48. if (sID && aDatasets.indexOf(sID) === -1) {
  49. aDatasets.push({
  50. id: sID,
  51. type: widget.dataSet.type
  52. });
  53. }
  54. }
  55. });
  56. //Upgrade data sets shaping
  57. var oDataset = null;
  58. _.each(datasetShaping, function (shaping) {
  59. if (shaping) {
  60. oDataset = _.find(aDatasets, function (dataset) {
  61. return dataset.id === shaping.id;
  62. });
  63. if (oDataset) {
  64. var options = { 'isUploadedFile': oDataset.type === 'uploadedFile' };
  65. _.each(shaping.filters, function (filter) {
  66. filter.columnId = this._getShortenedColID(filter.columnId, options);
  67. bUpgraded = true;
  68. }.bind(this));
  69. }
  70. }
  71. }.bind(this));
  72. return bUpgraded;
  73. },
  74. _upgradeWidgetSpec: function _upgradeWidgetSpec(widgetModel) {
  75. if (!widgetModel || !widgetModel.dataSet) {
  76. return false; //Upgrade is not needed
  77. }
  78. var options = {
  79. isUploadedFile: widgetModel.dataSet.type === 'uploadedFile'
  80. };
  81. _.each(widgetModel.mapping, function (mapping) {
  82. if (mapping.id) {
  83. mapping.id = this._getShortenedColID(mapping.id, options);
  84. }
  85. if (mapping.columnId) {
  86. mapping.columnId = this._getShortenedColID(mapping.columnId, options);
  87. }
  88. }.bind(this));
  89. //Update model filters attributes
  90. _.each(widgetModel.filters, function (filter) {
  91. this._upgradeFilters(filter, options);
  92. }.bind(this));
  93. //Update model local filters attributes
  94. _.each(widgetModel.localFilters, function (filter) {
  95. this._upgradeFilters(filter, options);
  96. }.bind(this));
  97. return widgetModel;
  98. },
  99. _upgradeFilters: function _upgradeFilters(filter, options) {
  100. if (!filter) {
  101. return;
  102. }
  103. if (filter.columnId) {
  104. filter.columnId = this._getShortenedColID(filter.columnId, options);
  105. } else if (filter.values) {
  106. _.each(filter.values, function (value) {
  107. this._upgradeFilters(value, options);
  108. }.bind(this));
  109. }
  110. },
  111. down: function down(spec) {
  112. // no downgrade at this time; return as is
  113. return Promise.resolve(spec);
  114. },
  115. /**
  116. * @private
  117. * Migrate the older release long format column ID to a shortened version in the new release
  118. **/
  119. _getShortenedColID: function _getShortenedColID(sOrigID, options) {
  120. if (sOrigID && sOrigID.length > 0) {
  121. var aSections = sOrigID.split('.');
  122. var nLen = aSections.length;
  123. if (options && options.isUploadedFile === true) {
  124. return 'Sheet1.' + aSections[nLen - 1];
  125. } else if (nLen >= 2) {
  126. var aResult = [];
  127. aResult.push(aSections[nLen - 2]);
  128. aResult.push(aSections[nLen - 1]);
  129. return aResult.join('.'); //Shortened version
  130. }
  131. }
  132. return sOrigID;
  133. }
  134. });
  135. return new Upgrade();
  136. });
  137. //# sourceMappingURL=6.js.map