'use strict'; /* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: BI Dashboard *| (C) Copyright IBM Corp. 2016, 2018 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ 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, _) { var Upgrade = Class.extend([UpgradeBase], { init: function init() { this.VERSION = 1002; }, _getSourcesInfo: function _getSourcesInfo(boardSpec) { // Get datasets involved in widgets. // I assume that dataset shaping can not reference a dataset that is not used in widget. var datasetInfos = []; _.each(boardSpec.widgets, function (widgetSpec) { if (widgetSpec.dataSet) { var datasetType = widgetSpec.dataSet.type; var datasetId = widgetSpec.dataSet.id; if (('uploadedFile' === datasetType || 'dataSet2' === datasetType) && !_.findWhere(datasetInfos, { id: datasetId })) { datasetInfos.push({ id: datasetId, type: datasetType }); } } }); if (datasetInfos.length === 0) { return datasetInfos; } return datasetInfos; }, /** * Perform upgrade * * @param {object} spec - spec to perform upgrade on * * @return {Promise} Promise to be resolved when upgrade performed */ up: function up(spec) { if (!spec) { return Promise.resolve(spec); } return this._updateColumnIdForUploadedFileAndDataset2(spec).catch(function (error) { if (this.data.logger) { this.data.logger.error(error); } // catch and report the error, but don't fail, return the old spec. return spec; }.bind(this)); }, down: function down(spec) { // no downgrade at this time; return as is return Promise.resolve(spec); }, // Replace "Sheet1." in columnId by the query subject identifier + '.' in uploadedFile and dataset2. _updateColumnIdForUploadedFileAndDataset2: function _updateColumnIdForUploadedFileAndDataset2(boardSpec) { var promise = null; var datasetInfos = this._getSourcesInfo(boardSpec); // Get metadata of datasets to retrieve the query subject identifier in these datasets if (datasetInfos.length > 0) { boardSpec.queriedForUpgrade = true; var metadataPromises = []; _.each(datasetInfos, function (datasetInfo) { metadataPromises.push(this._getMetadata(datasetInfo)); }.bind(this)); // Upgrade identifiers in widgets and dataset shaping promise = Promise.all(metadataPromises).then(function () { this._updateColumnIds(datasetInfos, boardSpec); return boardSpec; }.bind(this), function (error) { throw error; }); } else { promise = Promise.resolve(); } return promise; }, _updateColumnIds: function _updateColumnIds(datasetInfos, boardSpec) { // update in widgets _.each(boardSpec.widgets, function (widgetSpec) { var datasetInfo = widgetSpec.dataSet ? _.findWhere(datasetInfos, { id: widgetSpec.dataSet.id }) : null; if (datasetInfo) { this._updateColumnIdInWidget(datasetInfo.querySubjectId, widgetSpec); } }.bind(this)); // update in datasetShaping _.each(boardSpec.datasetShaping, function (shaping) { var datasetInfo = _.findWhere(datasetInfos, { id: shaping.id }); if (datasetInfo) { this._updateColumnIdInShaping(datasetInfo.querySubjectId, shaping); } }.bind(this)); }, _updateColumnIdInWidget: function _updateColumnIdInWidget(qsId, widgetModel) { // update widget mapping _.each(widgetModel.mapping, function (mapping) { if (mapping.columnId) { mapping.columnId = this._replaceQuerySubjectId(mapping.columnId, qsId); } }.bind(this)); // Update model filters attributes _.each(widgetModel.filters, function (filter) { this._updateEdgeOrDataPointFilter(filter, qsId); }.bind(this)); // Update model local filters _.each(widgetModel.localFilters, function (filter) { this._updateEdgeOrDataPointFilter(filter, qsId); }.bind(this)); }, _updateEdgeOrDataPointFilter: function _updateEdgeOrDataPointFilter(filter, qsId) { if (filter.columnId) { filter.columnId = this._replaceQuerySubjectId(filter.columnId, qsId); } if (filter.values) { this._updateColumnIdInFilterValues(filter.values, qsId); } }, _updateColumnIdInShaping: function _updateColumnIdInShaping(qsId, shaping) { // Update columnId in shaping calculations _.each(shaping.calculations, function (calculation) { if (calculation.expr && calculation.expr.params && calculation.expr.params.length > 0) { _.each(calculation.expr.params, function (param) { if (param.col) { param.col = this._replaceQuerySubjectId(param.col, qsId); } }.bind(this)); } }.bind(this)); // Update columnId in shaping filters _.each(shaping.filters, function (filter) { if (filter.columnId) { filter.columnId = this._replaceQuerySubjectId(filter.columnId, qsId); } }.bind(this)); }, _replaceQuerySubjectId: function _replaceQuerySubjectId(oldColumnId, qsId) { return oldColumnId.replace('Sheet1.', qsId); }, _updateColumnIdInFilterValues: function _updateColumnIdInFilterValues(values, qsId) { _.each(values, function (value) { if (value.columnId) { value.columnId = this._replaceQuerySubjectId(value.columnId, qsId); } if (value.values) { this._updateColumnIdInFilterValues(value.values, qsId); } }.bind(this)); }, // Call REST-API to get metadata. // As this upgrade happens before the upgrade of shaping and widget spec, I use Moser REST-API // to get metadata and the dataset Id is the storeID of the dataset in the old spec. _getMetadata: function _getMetadata(datasetInfo) { return new Promise(function (resolve, reject) { try { var datasetId = datasetInfo.id; this.data.ajaxSvc.ajax({ url: 'v1/metadata/base_modules/' + datasetId + '/metadata?shortIdForExpression=true&item=on&type=' + datasetInfo.type, type: 'GET', headers: { 'Accept': 'application/json' } }).then(function (response) { var _response$data = response.data, data = _response$data === undefined ? {} : _response$data; // Get the query subject identifier from the metadata response if (data.querySubject && data.querySubject.length > 0) { datasetInfo.querySubjectId = data.querySubject[0].identifier + '.'; } resolve(data); }.bind(this), function (jqXHR) { if (this.data.logger) { this.data.logger.error('Error calling Modeling service API to get metadata'); } var errorMsg = ''; if (jqXHR.responseText) { errorMsg = jqXHR.responseText; if (this.data.logger) { this.data.logger.error(errorMsg); } } reject(new BaseError(errorMsg)); }.bind(this)); } catch (error) { reject(error); } }.bind(this)); } }); return new Upgrade(); }); //# sourceMappingURL=ca_updateSheet1InUploadedFile.js.map