'use strict'; 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; } 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; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Licensed Materials - Property of IBM * IBM Watson Analytics (C) Copyright IBM Corp. 2018, 2020 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', './DashboardSpecUtil'], function (_, DashboardSpecUtil) { var WIDGET_TYPE = { LIVEWIDGET: 'live', FILTER: 'filter' }; var escapeRegExp = function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string }; var replaceItemIdInString = function replaceItemIdInString() { var string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var oldItemId = arguments[1]; var newItemId = arguments[2]; var regExpr = new RegExp(escapeRegExp(oldItemId), 'g'); return string.replace(regExpr, newItemId); }; var replaceIdsInArray = function replaceIdsInArray(jsonSpec, collection, index, oldItemId, newItemId) { var string = JSON.stringify(jsonSpec); string = replaceItemIdInString(string, oldItemId, newItemId); collection[index] = JSON.parse(string); }; var BaseRelinkItemId = function () { function BaseRelinkItemId(jsonSpec, sourceId) { _classCallCheck(this, BaseRelinkItemId); if (!jsonSpec) { throw new Error('Invalid JSON specification'); } this.sourceId = sourceId; this.jsonSpec = jsonSpec; } BaseRelinkItemId.prototype.getItemIds = function getItemIds() { return []; }; BaseRelinkItemId.prototype.replaceItemIds = function replaceItemIds() {}; BaseRelinkItemId.prototype.postReplaceItemIds = function postReplaceItemIds() {}; BaseRelinkItemId.prototype.validSourceId = function validSourceId(idToCheck) { return this.sourceId === idToCheck; }; return BaseRelinkItemId; }(); var RelinkPageContext = function (_BaseRelinkItemId) { _inherits(RelinkPageContext, _BaseRelinkItemId); function RelinkPageContext() { _classCallCheck(this, RelinkPageContext); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _possibleConstructorReturn(this, _BaseRelinkItemId.call.apply(_BaseRelinkItemId, [this].concat(args))); } RelinkPageContext.prototype.getItemIds = function getItemIds() { var resultSet = []; for (var _iterator = this.jsonSpec, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref; if (_isArray) { if (_i >= _iterator.length) break; _ref = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref = _i.value; } var entry = _ref; if (this.validSourceId(entry.sourceId) && entry.hierarchyUniqueNames) { resultSet = resultSet.concat(entry.hierarchyUniqueNames); } } return resultSet; }; RelinkPageContext.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) { var _this2 = this; this.jsonSpec.forEach(function (entry, index, collection) { if (_this2.validSourceId(entry.sourceId)) { var spec = JSON.stringify(entry); spec = replaceItemIdInString(spec, oldItemId, newItemId); collection[index] = JSON.parse(spec); } }); }; return RelinkPageContext; }(BaseRelinkItemId); /** * update mappings * @example * [{ "name": "Product_line", "modelFilterItem": "[Sales (query)].[Products].[Product line]", "mapTo": "QC_Data_xlsx.Product_line" }, { "name": "Order_Method", "modelFilterItem": "[Sales (query)].[Order method].[Order method type]", "mapTo": "QC_Data_xlsx.Order_method" }] Updated "mapTo" part only and we should never change the modelFilterItem part since it comes from report. After update the mappings: [ { "name": "Product_line", "modelFilterItem": "[Sales (query)].[Products].[Product line]", "mapTo": "dataSetDemo_2_csv.Product_line" }, { "name": "Order_Method", "modelFilterItem": "[Sales (query)].[Order method].[Order method type]", "mapTo": "dataSetDemo_2_csv.Order_method" }] */ var RelinkDrillThrough = function (_BaseRelinkItemId2) { _inherits(RelinkDrillThrough, _BaseRelinkItemId2); function RelinkDrillThrough() { _classCallCheck(this, RelinkDrillThrough); for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } return _possibleConstructorReturn(this, _BaseRelinkItemId2.call.apply(_BaseRelinkItemId2, [this].concat(args))); } RelinkDrillThrough.prototype.getItemIds = function getItemIds() { var _this4 = this; var resultSet = []; this.jsonSpec.forEach(function (entry) { var sourceId = entry.modelRefs[0]; if (_this4.validSourceId(sourceId)) { var mappings = entry.mappings; for (var _iterator2 = mappings, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { var _ref2; if (_isArray2) { if (_i2 >= _iterator2.length) break; _ref2 = _iterator2[_i2++]; } else { _i2 = _iterator2.next(); if (_i2.done) break; _ref2 = _i2.value; } var mapping = _ref2; if (mapping.mapTo && mapping.mapTo !== 'none') { resultSet.push(mapping.mapTo); } } } }); return resultSet; }; RelinkDrillThrough.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) { var _this5 = this; this.jsonSpec.forEach(function (entry) { if (_this5.validSourceId(entry.modelRefs[0])) { var mappings = entry.mappings || []; for (var _iterator3 = mappings, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { var _ref3; if (_isArray3) { if (_i3 >= _iterator3.length) break; _ref3 = _iterator3[_i3++]; } else { _i3 = _iterator3.next(); if (_i3.done) break; _ref3 = _i3.value; } var mapping = _ref3; if (mapping.mapTo === oldItemId) { mapping.mapTo = newItemId; } } } }); }; return RelinkDrillThrough; }(BaseRelinkItemId); var RelinkLayout = function (_BaseRelinkItemId3) { _inherits(RelinkLayout, _BaseRelinkItemId3); function RelinkLayout() { _classCallCheck(this, RelinkLayout); for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { args[_key3] = arguments[_key3]; } var _this6 = _possibleConstructorReturn(this, _BaseRelinkItemId3.call.apply(_BaseRelinkItemId3, [this].concat(args))); _this6._widgets = _this6._getWidgetModelsObject(_this6.jsonSpec); _this6.relinkWidget = new RelinkWidget(_this6._widgets, _this6.sourceId); return _this6; } RelinkLayout.prototype.getItemIds = function getItemIds() { return this.relinkWidget.getItemIds(); }; RelinkLayout.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) { return this.relinkWidget.replaceItemIds(oldItemId, newItemId); }; RelinkLayout.prototype._getWidgetModelsObject = function _getWidgetModelsObject(layout) { var o = {}; //Take list of widget models and convert it to an object keyed by the model ids DashboardSpecUtil.getWidgetModelList(layout).forEach(function (widgetModel) { return o[widgetModel.id] = widgetModel; }); return o; }; return RelinkLayout; }(BaseRelinkItemId); var RelinkWidget = function (_BaseRelinkItemId4) { _inherits(RelinkWidget, _BaseRelinkItemId4); function RelinkWidget() { _classCallCheck(this, RelinkWidget); for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { args[_key4] = arguments[_key4]; } return _possibleConstructorReturn(this, _BaseRelinkItemId4.call.apply(_BaseRelinkItemId4, [this].concat(args))); } RelinkWidget.prototype.getItemIds = function getItemIds() { var resultSet = []; for (var prop in this.jsonSpec) { var widget = this.jsonSpec[prop]; var itemIds = (widget.type === WIDGET_TYPE.LIVEWIDGET ? this._getLiveWidgetItemIds(widget) : this._getFilterWidgetItemIds(widget)) || []; resultSet = resultSet.concat(itemIds); } return resultSet; }; RelinkWidget.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) { for (var prop in this.jsonSpec) { var updatedWidget = void 0; var widget = this.jsonSpec[prop]; if (widget.type === WIDGET_TYPE.LIVEWIDGET) { updatedWidget = this._updateLiveWidgetItemIds(widget, oldItemId, newItemId); } else { updatedWidget = this._updateFilterWidgetItemIds(widget, oldItemId, newItemId); } this.jsonSpec[prop] = updatedWidget; } }; RelinkWidget.prototype._getLiveWidgetItemIds = function _getLiveWidgetItemIds(widget) { var resultSet = []; var dataViews = widget.data && widget.data.dataViews ? widget.data && widget.data.dataViews : []; var validSourceId = false; for (var _iterator4 = dataViews, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) { var _ref4; if (_isArray4) { if (_i4 >= _iterator4.length) break; _ref4 = _iterator4[_i4++]; } else { _i4 = _iterator4.next(); if (_i4.done) break; _ref4 = _i4.value; } var dataView = _ref4; validSourceId = this.validSourceId(dataView.modelRef); if (validSourceId) { var dataItems = dataView.dataItems || []; for (var _iterator5 = dataItems, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) { var _ref5; if (_isArray5) { if (_i5 >= _iterator5.length) break; _ref5 = _iterator5[_i5++]; } else { _i5 = _iterator5.next(); if (_i5.done) break; _ref5 = _i5.value; } var dataItem = _ref5; resultSet.push(dataItem.itemId); } } } if (validSourceId) { resultSet = resultSet.concat(this._getLocalFilter(widget.localFilters || [])); } return resultSet; }; RelinkWidget.prototype._getLocalFilter = function _getLocalFilter(localFilters) { return _.pluck(localFilters, 'columnId'); }; RelinkWidget.prototype._getFilterWidgetItemIds = function _getFilterWidgetItemIds(widget) { var resultSet = []; var properties = widget.properties || {}; if (this.validSourceId(properties.sourceId)) { resultSet.push(properties.itemId); } return resultSet; }; RelinkWidget.prototype._updateLiveWidgetItemIds = function _updateLiveWidgetItemIds(widget, oldItemId, newItemId) { var _this8 = this; var validDataSource = false; var dataViews = widget.data && widget.data.dataViews ? widget.data.dataViews : []; dataViews.forEach(function (dataView, index, collection) { validDataSource = _this8.validSourceId(dataView.modelRef); if (validDataSource) { replaceIdsInArray(dataView, collection, index, oldItemId, newItemId); } }); if (validDataSource) { var localFilters = widget.localFilters || []; localFilters.forEach(function (filter, index, collection) { replaceIdsInArray(filter, collection, index, oldItemId, newItemId); }); } return widget; }; RelinkWidget.prototype._updateFilterWidgetItemIds = function _updateFilterWidgetItemIds(widget, oldItemId, newItemId) { var properties = widget.properties || {}; if (this.validSourceId(properties.sourceId)) { if (properties.itemId === oldItemId) { properties.itemId = newItemId; } } return widget; }; return RelinkWidget; }(BaseRelinkItemId); /** * datasetShaping is obsolete in R7. * During upgrade from R6 to R7, gemini/dashboard/loader/upgrader/waca_shaping.js is responsbile for upgrading this model to an embbedded shaping module. * When upgrade from R6 fail, this datasetShaping model still exist in the dashboard spec and therefore relink must handle this case. * After relink, the dashboard spec goes through the upgrade process one more time. If succeed the datasetShaping is replaced by the embedded module in dashboard spec */ var RelinkDatasetShaping = function (_BaseRelinkItemId5) { _inherits(RelinkDatasetShaping, _BaseRelinkItemId5); function RelinkDatasetShaping() { _classCallCheck(this, RelinkDatasetShaping); for (var _len5 = arguments.length, args = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { args[_key5] = arguments[_key5]; } var _this9 = _possibleConstructorReturn(this, _BaseRelinkItemId5.call.apply(_BaseRelinkItemId5, [this].concat(args))); _this9.oldCMAssetId = args[2]; _this9.newCMAssetId = args[3]; return _this9; } RelinkDatasetShaping.prototype.getItemIds = function getItemIds() { var resultSet = []; for (var prop in this.jsonSpec) { var shaping = this.jsonSpec[prop]; if (shaping.id === this.oldCMAssetId) { resultSet = resultSet.concat(this._getFilters(shaping.filters || [])); resultSet = resultSet.concat(this._getCalculations(shaping.calculations || [])); } } return resultSet; }; RelinkDatasetShaping.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) { var _this10 = this; var jsonSpec = this.jsonSpec || []; jsonSpec.forEach(function (shaping, index, collection) { if (shaping.id === _this10.oldCMAssetId) { replaceIdsInArray(shaping, collection, index, oldItemId, newItemId); } }); }; RelinkDatasetShaping.prototype.postReplaceItemIds = function postReplaceItemIds() { var _this11 = this; var jsonSpec = this.jsonSpec || []; jsonSpec.forEach(function (shaping) { if (shaping.id === _this11.oldCMAssetId) { shaping.id = _this11.newCMAssetId; } }); }; RelinkDatasetShaping.prototype._getFilters = function _getFilters(filters) { return _.pluck(filters, 'columnId'); }; RelinkDatasetShaping.prototype._getCalculations = function _getCalculations(calculations) { var expr = _.pluck(calculations, 'expr') || []; var params = _.flatten(_.pluck(expr, 'params') || []); return _.pluck(params, 'col') || []; }; return RelinkDatasetShaping; }(BaseRelinkItemId); var RelinkConditionalFormatting = function (_BaseRelinkItemId6) { _inherits(RelinkConditionalFormatting, _BaseRelinkItemId6); function RelinkConditionalFormatting() { _classCallCheck(this, RelinkConditionalFormatting); for (var _len6 = arguments.length, args = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) { args[_key6] = arguments[_key6]; } return _possibleConstructorReturn(this, _BaseRelinkItemId6.call.apply(_BaseRelinkItemId6, [this].concat(args))); } RelinkConditionalFormatting.prototype._getConditionalFormattingItemIds = function _getConditionalFormattingItemIds(location) { var resultSet = []; var conditionalFormats = location.content && location.content.conditionalFormatting && location.content.conditionalFormatting.conditionalFormats ? location.content.conditionalFormatting.conditionalFormats : []; for (var _iterator6 = conditionalFormats, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) { var _ref6; if (_isArray6) { if (_i6 >= _iterator6.length) break; _ref6 = _iterator6[_i6++]; } else { _i6 = _iterator6.next(); if (_i6.done) break; _ref6 = _i6.value; } var conditionalFormat = _ref6; resultSet.push(conditionalFormat.dataItemId); } return resultSet; }; RelinkConditionalFormatting.prototype.findDataItem = function findDataItem(path) { var _this13 = this; var resultSet = []; var items = path.items || []; items.forEach(function (item) { if (item.type === 'widget') { var itemIds = _this13._getConditionalFormattingItemIds(item); resultSet = resultSet.concat(itemIds); } else { resultSet = resultSet.concat(_this13.findDataItem(item)); } }); return resultSet; }; RelinkConditionalFormatting.prototype.getItemIds = function getItemIds() { var resultSet = []; var itemIds = this.findDataItem(this.jsonSpec); resultSet = resultSet.concat(itemIds); return resultSet; }; RelinkConditionalFormatting.prototype.replaceDataItem = function replaceDataItem(path, oldItemId, newItemId) { var _this14 = this; var items = path.items || []; items.forEach(function (item) { if (item.type === 'widget') { var conditionalFormats = item.content && item.content.conditionalFormatting && item.content.conditionalFormatting.conditionalFormats ? item.content.conditionalFormatting.conditionalFormats : []; conditionalFormats.forEach(function (conditionalFormat, index, collection) { replaceIdsInArray(conditionalFormat, collection, index, oldItemId, newItemId); }); } else { _this14.replaceDataItem(item, oldItemId, newItemId); } }); }; RelinkConditionalFormatting.prototype.replaceItemIds = function replaceItemIds(oldItemId, newItemId) { this.replaceDataItem(this.jsonSpec, oldItemId, newItemId); }; return RelinkConditionalFormatting; }(BaseRelinkItemId); var RelinkObjectClass = { 'pageContext': [RelinkPageContext], 'drillThrough': [RelinkDrillThrough], 'layout': [RelinkLayout, RelinkConditionalFormatting], 'widgets': [RelinkWidget], 'datasetShaping': [RelinkDatasetShaping] //@todo Investigate this: is it still needed? } }; var RelinkItemIdInSpec = function () { function RelinkItemIdInSpec() { var jsonSpec = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var logger = arguments[2]; _classCallCheck(this, RelinkItemIdInSpec); if (!options.sourceId) { throw new Error('Invalid sourceId provided'); } if (!options.oldCMAssetId) { throw new Error('Invalid oldCMAssetId provided'); } if (!options.newCMAssetId) { throw new Error('Invalid newCMAssetId provided'); } this.sourceId = options.sourceId; this.oldCMAssetId = options.oldCMAssetId; this.newCMAssetId = options.newCMAssetId; this.jsonSpec = jsonSpec; this.objectsToRelink = []; this.logger = logger; this._initialize(); return { getItemIds: this.getItemIds.bind(this), replaceItemIds: this.replaceItemIds.bind(this) }; } RelinkItemIdInSpec.prototype._initialize = function _initialize() { var _this15 = this; var keys = Object.keys(this.jsonSpec); keys.forEach(function (key) { var classesToInstantiate = RelinkObjectClass[key]; if (classesToInstantiate) { classesToInstantiate.forEach(function (classToInstantiate) { return _this15.objectsToRelink.push(new classToInstantiate(_this15.jsonSpec[key], _this15.sourceId, _this15.oldCMAssetId, _this15.newCMAssetId)); }); } }); }; RelinkItemIdInSpec.prototype.getItemIds = function getItemIds() { var _this16 = this; if (this.itemIds) { return this.itemIds; } this.itemIds = []; this.objectsToRelink.forEach(function (objectToRelink) { _this16.itemIds = _this16.itemIds.concat(objectToRelink.getItemIds()); }); this.itemIds = _.uniq(this.itemIds); return this.itemIds; }; RelinkItemIdInSpec.prototype.replaceItemIds = function replaceItemIds() { var _this17 = this; var newItemIds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; if (!this.itemIds || this.itemIds.length !== newItemIds.length) { throw new Error('Invalid item Ids to replace'); } var nonUpdate_ableItemIds = []; var oldItemIds = this.itemIds; oldItemIds.forEach(function (oldItemId, index) { var newItemId = newItemIds[index]; if (newItemId) { _this17.objectsToRelink.forEach(function (objectToRelink) { objectToRelink.replaceItemIds(oldItemId, newItemId); }); } else { nonUpdate_ableItemIds.push(oldItemId); } }); this.objectsToRelink.forEach(function (objectToRelink) { objectToRelink.postReplaceItemIds(); }); if (nonUpdate_ableItemIds.length > 0) { this.logger.info('Cannot update the following columns: \'' + nonUpdate_ableItemIds.join(',') + '\''); } return this.jsonSpec; }; return RelinkItemIdInSpec; }(); return RelinkItemIdInSpec; }); //# sourceMappingURL=RelinkItemIdInSpec.js.map