ca_updateSheet1InUploadedFile.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: BI Dashboard
  6. *| (C) Copyright IBM Corp. 2016, 2018
  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', '../../../lib/@waca/core-client/js/core-client/errors/BaseError', 'underscore'], function (Class, UpgradeBase, BaseError, _) {
  13. var Upgrade = Class.extend([UpgradeBase], {
  14. init: function init() {
  15. this.VERSION = 1002;
  16. },
  17. _getSourcesInfo: function _getSourcesInfo(boardSpec) {
  18. // Get datasets involved in widgets.
  19. // I assume that dataset shaping can not reference a dataset that is not used in widget.
  20. var datasetInfos = [];
  21. _.each(boardSpec.widgets, function (widgetSpec) {
  22. if (widgetSpec.dataSet) {
  23. var datasetType = widgetSpec.dataSet.type;
  24. var datasetId = widgetSpec.dataSet.id;
  25. if (('uploadedFile' === datasetType || 'dataSet2' === datasetType) && !_.findWhere(datasetInfos, {
  26. id: datasetId
  27. })) {
  28. datasetInfos.push({
  29. id: datasetId,
  30. type: datasetType
  31. });
  32. }
  33. }
  34. });
  35. if (datasetInfos.length === 0) {
  36. return datasetInfos;
  37. }
  38. return datasetInfos;
  39. },
  40. /**
  41. * Perform upgrade
  42. *
  43. * @param {object} spec - spec to perform upgrade on
  44. *
  45. * @return {Promise} Promise to be resolved when upgrade performed
  46. */
  47. up: function up(spec) {
  48. if (!spec) {
  49. return Promise.resolve(spec);
  50. }
  51. return this._updateColumnIdForUploadedFileAndDataset2(spec).catch(function (error) {
  52. if (this.data.logger) {
  53. this.data.logger.error(error);
  54. } // catch and report the error, but don't fail, return the old spec.
  55. return spec;
  56. }.bind(this));
  57. },
  58. down: function down(spec) {
  59. // no downgrade at this time; return as is
  60. return Promise.resolve(spec);
  61. },
  62. // Replace "Sheet1." in columnId by the query subject identifier + '.' in uploadedFile and dataset2.
  63. _updateColumnIdForUploadedFileAndDataset2: function _updateColumnIdForUploadedFileAndDataset2(boardSpec) {
  64. var promise = null;
  65. var datasetInfos = this._getSourcesInfo(boardSpec);
  66. // Get metadata of datasets to retrieve the query subject identifier in these datasets
  67. if (datasetInfos.length > 0) {
  68. boardSpec.queriedForUpgrade = true;
  69. var metadataPromises = [];
  70. _.each(datasetInfos, function (datasetInfo) {
  71. metadataPromises.push(this._getMetadata(datasetInfo));
  72. }.bind(this));
  73. // Upgrade identifiers in widgets and dataset shaping
  74. promise = Promise.all(metadataPromises).then(function () {
  75. this._updateColumnIds(datasetInfos, boardSpec);
  76. return boardSpec;
  77. }.bind(this), function (error) {
  78. throw error;
  79. });
  80. } else {
  81. promise = Promise.resolve();
  82. }
  83. return promise;
  84. },
  85. _updateColumnIds: function _updateColumnIds(datasetInfos, boardSpec) {
  86. // update in widgets
  87. _.each(boardSpec.widgets, function (widgetSpec) {
  88. var datasetInfo = widgetSpec.dataSet ? _.findWhere(datasetInfos, {
  89. id: widgetSpec.dataSet.id
  90. }) : null;
  91. if (datasetInfo) {
  92. this._updateColumnIdInWidget(datasetInfo.querySubjectId, widgetSpec);
  93. }
  94. }.bind(this));
  95. // update in datasetShaping
  96. _.each(boardSpec.datasetShaping, function (shaping) {
  97. var datasetInfo = _.findWhere(datasetInfos, {
  98. id: shaping.id
  99. });
  100. if (datasetInfo) {
  101. this._updateColumnIdInShaping(datasetInfo.querySubjectId, shaping);
  102. }
  103. }.bind(this));
  104. },
  105. _updateColumnIdInWidget: function _updateColumnIdInWidget(qsId, widgetModel) {
  106. // update widget mapping
  107. _.each(widgetModel.mapping, function (mapping) {
  108. if (mapping.columnId) {
  109. mapping.columnId = this._replaceQuerySubjectId(mapping.columnId, qsId);
  110. }
  111. }.bind(this));
  112. // Update model filters attributes
  113. _.each(widgetModel.filters, function (filter) {
  114. this._updateEdgeOrDataPointFilter(filter, qsId);
  115. }.bind(this));
  116. // Update model local filters
  117. _.each(widgetModel.localFilters, function (filter) {
  118. this._updateEdgeOrDataPointFilter(filter, qsId);
  119. }.bind(this));
  120. },
  121. _updateEdgeOrDataPointFilter: function _updateEdgeOrDataPointFilter(filter, qsId) {
  122. if (filter.columnId) {
  123. filter.columnId = this._replaceQuerySubjectId(filter.columnId, qsId);
  124. }
  125. if (filter.values) {
  126. this._updateColumnIdInFilterValues(filter.values, qsId);
  127. }
  128. },
  129. _updateColumnIdInShaping: function _updateColumnIdInShaping(qsId, shaping) {
  130. // Update columnId in shaping calculations
  131. _.each(shaping.calculations, function (calculation) {
  132. if (calculation.expr && calculation.expr.params && calculation.expr.params.length > 0) {
  133. _.each(calculation.expr.params, function (param) {
  134. if (param.col) {
  135. param.col = this._replaceQuerySubjectId(param.col, qsId);
  136. }
  137. }.bind(this));
  138. }
  139. }.bind(this));
  140. // Update columnId in shaping filters
  141. _.each(shaping.filters, function (filter) {
  142. if (filter.columnId) {
  143. filter.columnId = this._replaceQuerySubjectId(filter.columnId, qsId);
  144. }
  145. }.bind(this));
  146. },
  147. _replaceQuerySubjectId: function _replaceQuerySubjectId(oldColumnId, qsId) {
  148. return oldColumnId.replace('Sheet1.', qsId);
  149. },
  150. _updateColumnIdInFilterValues: function _updateColumnIdInFilterValues(values, qsId) {
  151. _.each(values, function (value) {
  152. if (value.columnId) {
  153. value.columnId = this._replaceQuerySubjectId(value.columnId, qsId);
  154. }
  155. if (value.values) {
  156. this._updateColumnIdInFilterValues(value.values, qsId);
  157. }
  158. }.bind(this));
  159. },
  160. // Call REST-API to get metadata.
  161. // As this upgrade happens before the upgrade of shaping and widget spec, I use Moser REST-API
  162. // to get metadata and the dataset Id is the storeID of the dataset in the old spec.
  163. _getMetadata: function _getMetadata(datasetInfo) {
  164. return new Promise(function (resolve, reject) {
  165. try {
  166. var datasetId = datasetInfo.id;
  167. this.data.ajaxSvc.ajax({
  168. url: 'v1/metadata/base_modules/' + datasetId + '/metadata?shortIdForExpression=true&item=on&type=' + datasetInfo.type,
  169. type: 'GET',
  170. headers: {
  171. 'Accept': 'application/json'
  172. }
  173. }).then(function (response) {
  174. var _response$data = response.data,
  175. data = _response$data === undefined ? {} : _response$data;
  176. // Get the query subject identifier from the metadata response
  177. if (data.querySubject && data.querySubject.length > 0) {
  178. datasetInfo.querySubjectId = data.querySubject[0].identifier + '.';
  179. }
  180. resolve(data);
  181. }.bind(this), function (jqXHR) {
  182. if (this.data.logger) {
  183. this.data.logger.error('Error calling Modeling service API to get metadata');
  184. }
  185. var errorMsg = '';
  186. if (jqXHR.responseText) {
  187. errorMsg = jqXHR.responseText;
  188. if (this.data.logger) {
  189. this.data.logger.error(errorMsg);
  190. }
  191. }
  192. reject(new BaseError(errorMsg));
  193. }.bind(this));
  194. } catch (error) {
  195. reject(error);
  196. }
  197. }.bind(this));
  198. }
  199. });
  200. return new Upgrade();
  201. });
  202. //# sourceMappingURL=ca_updateSheet1InUploadedFile.js.map