'use strict'; 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, 2019 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * */ define(['../../dataSources/utils/DatasourceUtil', 'underscore', '../../widgets/livewidget/nls/StringResources', '../../util/ContentUtil', '../../lib/@waca/dashboard-common/dist/utils/ContentUtil'], function (DatasourceUtil, _, StringResources, ContentUtil, ContentUtilCommon) { var paramProperties = ['name', 'modelFilterItem', 'type', 'capabilities']; var REPORT = 'report'; var DrillThroughMappingManager = function () { /** * @param {object} dashboardApi - Dashboard API * @param {object} drillService - drill service */ function DrillThroughMappingManager(dashboardApi, drillService) { _classCallCheck(this, DrillThroughMappingManager); this.dashboard = dashboardApi; this.drillService = drillService; } /** * @param storeId - The cm store id of the target report */ DrillThroughMappingManager.prototype.getParameters = function getParameters(storeId) { var _this = this; return this.dashboard.getGlassSvc('.DrillInfoService').then(function (drillInfoService) { var attrs = { type: REPORT, storeId: storeId }; return drillInfoService.getParameters(attrs).then(function (response) { var parameters = response.parameters || []; var result = []; _.each(parameters, function (parameter) { result.push(_.pick(parameter, paramProperties)); }); return result; }).catch(function (e) { _this.dashboard.getGlassCoreSvc('.Logger').error('Could not get prompt information from the selected report.', e); _this.dashboard.showErrorMessage(StringResources.get('drillThroughDefinitionErrorQueryingReport'), StringResources.get('drillThroughDefinitionErrorDlgTitle')); }); }); }; /** * function to map drill target parameters to current dashboard metadata, also returns the metadata * columns within the current context, i.e. same data asset and scope (tab) as the definitionContent. * @param {string} storeId - The cm store id of the target report * @param {object} definitionContent - the content that the drill through definition is based on. * @param {string} mappings - The existing mappings from a drill definition entry. */ DrillThroughMappingManager.prototype.doMapping = function doMapping(storeId, definitionContent, mappings) { var _this2 = this; return this.getParameters(storeId).then(function (parameters) { if (!parameters.length) { return Promise.resolve(); } var datasourceAPI = definitionContent.getFeature('Visualization').getDataSource(); return datasourceAPI.getModule().then(function (module) { return _this2._mapToMetadata(parameters, definitionContent, mappings, module); }); }); }; /** * @param {object} parameters - parameters in the target * @param {object} definitionContent - the content that the drill through definition is based on. * @param {string} mappings - The existing mappings from a drill definition entry. * @param {object} module - module api */ DrillThroughMappingManager.prototype._mapToMetadata = function _mapToMetadata(parameters, definitionContent, mappings, module) { var metadataColumnIds = this.getMetadataIds(definitionContent, module); var mapToColumnIds = []; // columns exist in drill entry mappings, but not in the current content context parameters.forEach(function (parameter) { // If saved mappings is passed in, propagate the mapTo to the parameter and do not remap from the current // content context if (mappings) { var mappedParameter = _.find(mappings, function (mapping) { return mapping.modelFilterItem === parameter.modelFilterItem && mapping.name === parameter.name; }); if (mappedParameter && mappedParameter.mapTo) { parameter.mapTo = mappedParameter.mapTo; if (metadataColumnIds.indexOf(mappedParameter.mapTo) === -1) { mapToColumnIds.push(mappedParameter.mapTo); } } } else { // New definition: map the parameter to the current content context parameter.mapTo = _.find(metadataColumnIds, function (columnId) { return parameter.modelFilterItem === columnId; }); } }); return { parameterMappings: parameters, columnLabels: this._getColumnLabels(definitionContent, metadataColumnIds.concat(mapToColumnIds)) }; }; DrillThroughMappingManager.prototype._getColumnLabels = function _getColumnLabels(definitionContent, metadataColumnIds) { // Collect the column names based on the metadataIds var columnLabels = []; var visualization = definitionContent.getFeature('Visualization'); var dataSource = visualization.getDataSource(); metadataColumnIds.forEach(function (columnId) { var metadataColumn = !DatasourceUtil.isMultiMeasuresSeriesOrValue(columnId) ? dataSource.getMetadataColumn(columnId) : null; if (metadataColumn) { columnLabels.push({ columnId: columnId, label: metadataColumn.getLabel() }); } }); return _.sortBy(columnLabels, 'label'); }; /** * @returns data item ids of the corresponding context, i.e. same data asset and scope * from dashboard model */ DrillThroughMappingManager.prototype.getMetadataIds = function getMetadataIds(definitionContent, module) { var _this3 = this; var metadataIds = []; var visualization = definitionContent.getFeature('Visualization'); var dataSource = visualization.getDataSource(); // Collect metadata from content var eventGroups = this.dashboard.getFeature('EventGroups'); if (eventGroups) { var groupId = eventGroups.getGroupId(definitionContent.getId()); var contentList = eventGroups.getContentIdList(groupId); contentList.forEach(function (contentId) { metadataIds.push.apply(metadataIds, _this3._collectWidgetMetadata(contentId, dataSource.getId())); }); } // Collect metadata from localFilters of the visualization this._collectLocalFilterMetadata(visualization, metadataIds); // Collect metadata from pageContext of the definitionContent scope this._collectPageContextMetadata(definitionContent, metadataIds, visualization, 'visualization', module); this._collectPageContextMetadata(definitionContent, metadataIds, visualization, 'filter', module); return _.uniq(metadataIds); }; DrillThroughMappingManager.prototype._collectWidgetMetadata = function _collectWidgetMetadata(contentId, sourceId) { var content = this.dashboard.getCanvas().getContent(contentId); var metadataIds = []; //we should exclude the content where visualization does not exist var visualization = content.getFeature('Visualization'); if (visualization) { var dataSource = visualization.getDataSource(); if (!dataSource || dataSource.getId() !== sourceId) { return metadataIds; } var slots = visualization.getSlots(); slots.getDataItemList().forEach(function (dataItem) { var metadataColumn = dataItem.getMetadataColumn(); if (metadataColumn) { metadataIds.push(metadataColumn.getId()); } }); } return metadataIds; }; DrillThroughMappingManager.prototype._collectLocalFilterMetadata = function _collectLocalFilterMetadata(visualization, metadataIds) { var localFilters = visualization.getLocalFilters().getFilterList(); // Collect metadata from local filters. localFilters.forEach(function (filter) { metadataIds.push(filter.columnId); }); }; DrillThroughMappingManager.prototype._collectPageContextMetadata = function _collectPageContextMetadata(definitionContent, metadataIds, visualization, origin, module) { //@todo: Endor add support for multiple data sources var type = this.dashboard.getAppConfig('pageContainerType'); var pageContent = ContentUtilCommon.getPageContent(definitionContent, type); var pageContext = ContentUtil.getPageContext(this.dashboard); var dataSource = visualization.getDataSource(); var pageContextItems = pageContext.getNetPageContextItems({ scope: pageContent && pageContent.getId(), sourceId: dataSource.getId(), origin: origin }); if (!pageContextItems.length) { return; } if (origin === 'filter') { pageContextItems = DrillThroughMappingManager.getNetSynchronizedFilterPageContext(visualization, definitionContent, pageContextItems, module); } pageContextItems.forEach(function (contextItem) { metadataIds.push(contextItem.getItemId()); }); }; /** * Returns an array of net synchronized filter page context * * @param {object} visualization - the content visualization * @param {object} ownerContent - the livewidget instance * @param {object} filterPageContextItems - the page context filter items * @param {object} module - module api * * @return {array} merged synchronized and filter net page context array */ DrillThroughMappingManager.getNetSynchronizedFilterPageContext = function getNetSynchronizedFilterPageContext(visualization, ownerContent, filterPageContextItems, module) { var dataSource = visualization.getDataSource(); if (dataSource.getType() === 'package') { return filterPageContextItems; } var mappedSlots = visualization.getSlots().getMappedSlotList(); var tableRef = []; mappedSlots.forEach(function (mappedSlot) { var dataItems = mappedSlot.getDataItemList(); dataItems.forEach(function (dataItem) { tableRef.push(dataItem.getMetadataColumn().getTableName()); }); }); filterPageContextItems = filterPageContextItems.filter(function (pageContextItem) { var paggeContextTableRef = DatasourceUtil.getPageContextItemTableRef(dataSource, pageContextItem.getPageContextSpec()); return DatasourceUtil.haveTableJoinsInSameDataSource(module, paggeContextTableRef, tableRef); //return dataSource.hasJoinedTables(paggeContextTableRef, tableRef); }); // TODO: last widget reference in JumpTo to be removed var widget = ownerContent.getFeature('WidgetAPI.deprecated').getVisApi().ownerWidget; return DatasourceUtil.mergeSynchronizePageContextFilter(filterPageContextItems, widget.getSynchDataFilterEntries()); }; return DrillThroughMappingManager; }(); return DrillThroughMappingManager; }); //# sourceMappingURL=DrillThroughMappingManager.js.map