'use strict'; /** * Licensed Materials - Property of IBM * IBM Cognos Products: Dashboard * (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(['../../lib/@waca/core-client/js/core-client/ui/core/Class', '../glassControllers/SelectDrillTargetActionHandler', '../controls/DrillThroughDefinitionDialog', './dialogs/DrillThroughManageDialog', '../../widgets/livewidget/nls/StringResources', '../../cmHelper/CMHelper', './DrillThroughManageUndoRedo', '../../util/AjaxHelper'], function (Class, SelectDrillTargetActionHandler, DrillThroughDefinitionDialog, DrillThroughManageDialog, StringResources, CMHelper, DrillThroughManageUndoRedo, AjaxHelper) { /** * Class to create and manage all drill through UI */ var DrillThroughController = Class.extend({ /** * @param {object} options.dashboarAPI - dashboard API object * @param {object} options.drillService - drillService wraps the drill through related managers */ init: function init(dashboardApi, drillService) { DrillThroughController.inherited('init', this, arguments); this.dashboardApi = dashboardApi; this.drillService = drillService; this.iconsFeature = this.dashboardApi.getFeature('Icons'); }, /** * Api to create a new drill through definition. * @param {object} definitionContent - the content that the drill through definition is based on. * @param {object} [options = undefined] - options to create drill definition * @param {object} [options.undoRedoOptions] - optional undoRedoOptions * @param {object} [options.callback] - optional callback if createDrillDefinition is successful * */ createDrillDefinition: function createDrillDefinition(definitionContent, options) { if (!this._selectDrillTargetActionHandler) { this._selectDrillTargetActionHandler = new SelectDrillTargetActionHandler(this.dashboardApi); } return this._selectDrillTargetActionHandler.selectTarget({ handlers: { onSelect: function (target) { return this._onSelectDrillTarget(definitionContent, target, options); }.bind(this) } }); }, _onSelectDrillTarget: function _onSelectDrillTarget(definitionContent, target, options) { var drillDefinitionDialog; var visualizationAPI = definitionContent.getFeature('Visualization'); return this.drillService.getDrillThroughModelApi().then(function (drillThroughModelApi) { return visualizationAPI.getDataSource().getLocalizedName().then(function (sourceName) { var handlers = { back: this.createDrillDefinition.bind(this, definitionContent, options), onApply: this.addUpdateDrillDefinitionEntry.bind(this, options), canApply: this._drillDefinitionIsUniqueName.bind(this, drillThroughModelApi) }; var dialogOptions = { controller: this, applicationName: this.dashboardApi.getApplicationName(), boardName: this.dashboardApi.getFeature('internal').getBoardModel().name, sourceName: sourceName, targetName: target.name, type: target.type, assetId: target.assetId, assetSearchPath: target.assetSearchPath, ownerId: definitionContent.getId(), modelRefs: [visualizationAPI.getDataSource().getId()], handlers: handlers, perspective: target.perspective, scope: 'connection', isLoading: target.type !== 'exploration', iconsFeature: this.iconsFeature }; drillDefinitionDialog = new DrillThroughDefinitionDialog(dialogOptions); drillDefinitionDialog.open(); if (target.type !== 'exploration') { var drillMappingManager = this.drillService.getDrillThroughMappingManager(); return drillMappingManager.doMapping(target.assetId, definitionContent).then(function (mappingResult) { var update = { isLoading: false, mappings: mappingResult && mappingResult.parameterMappings, columnLabels: mappingResult && mappingResult.columnLabels }; drillDefinitionDialog.update(update); }); } }.bind(this)); }.bind(this)).catch(function (error) { if (drillDefinitionDialog) { drillDefinitionDialog.hide(); } this._showError(error); }.bind(this)); }, /** * Api to update a drill through definition. * @param {object} definitionContent - the content that the drill through definition is based on. * @param {string} id - the unique id of the drill through definition entry. * @param {object} [options = undefined] - options to edit drill definition * @returns updated drill definition entry */ editDrillDefinition: function editDrillDefinition(definitionContent, id, options) { var drillDefinitionDialog; var visualizationAPI = definitionContent.getFeature('Visualization'); return this.drillService.getDrillThroughModelApi().then(function (drillThroughModelApi) { return visualizationAPI.getDataSource().getLocalizedName().then(function (sourceName) { var drillEntry = drillThroughModelApi.getDrillDefinitionEntry(id); var handlers = { onApply: this.addUpdateDrillDefinitionEntry.bind(this, options), canApply: this._drillDefinitionIsUniqueName.bind(this, drillThroughModelApi) }; var dialogOptions = { id: id, //Model drill entry id assetId: drillEntry.getAssetId(), //CM report drill target id assetSearchPath: drillEntry.getAssetSearchPath(), //CM report drill target searchPath applicationName: this.dashboardApi.getApplicationName(), boardName: this.dashboardApi.getFeature('internal').getBoardModel().name, sourceName: sourceName, name: drillEntry.getName(), scope: drillEntry.getScope(), type: drillEntry.getType(), ownerId: definitionContent.getId(), modelRefs: [visualizationAPI.getDataSource().getId()], handlers: handlers, perspective: drillEntry.getPerspective(), isLoading: drillEntry.getType() !== 'exploration', iconsFeature: this.iconsFeature }; drillDefinitionDialog = new DrillThroughDefinitionDialog(dialogOptions); drillDefinitionDialog.open(); if (drillEntry.getType() !== 'exploration') { return this._getTargetNameFromCM(drillEntry).then(function () { var drillMappingManager = this.drillService.getDrillThroughMappingManager(); return drillMappingManager.doMapping(drillEntry.getAssetId(), definitionContent, drillEntry.getMappings()).then(function (reMappingResult) { var update = { isLoading: false, targetName: drillEntry.getTargetName(), mappings: reMappingResult && reMappingResult.parameterMappings, columnLabels: reMappingResult && reMappingResult.columnLabels }; drillDefinitionDialog.update(update); }); }.bind(this)); } }.bind(this)); }.bind(this)).catch(function (error) { if (drillDefinitionDialog) { drillDefinitionDialog.hide(); } this._showError(error); }.bind(this)); }, manageDrillDefinition: function manageDrillDefinition(options) { options = options || {}; var undoRedoTransaction = new DrillThroughManageUndoRedo(options); var transactionInfo = undoRedoTransaction.startTransaction('manageDrillThrough', null); var onCreateCallback = function (content, payload) { this.createDrillDefinition(content, payload); }.bind(this); options.handlers = { onEdit: this.editDrillDefinition.bind(this), onCreate: onCreateCallback, onDelete: this.removeDrillDefinition.bind(this), okCallback: undoRedoTransaction.endTransaction.bind(undoRedoTransaction, transactionInfo), cancelCallback: undoRedoTransaction.rollback.bind(undoRedoTransaction, transactionInfo) }; return new DrillThroughManageDialog(options); }, /** * Api to remove a drill through definition */ removeDrillDefinition: function removeDrillDefinition(id, options) { return this.drillService.getDrillThroughModelApi().then(function (drillThroughModelApi) { options = options || {}; var success = drillThroughModelApi.deleteDrillDefinitionEntry(id, options.undoRedoOptions); if (success && options.callback) { options.callback(options); } return success; }.bind(this)); }, addUpdateDrillDefinitionEntry: function addUpdateDrillDefinitionEntry(options, spec) { return this.drillService.getDrillThroughModelApi().then(function (drillThroughModelApi) { options = options || {}; var entry = drillThroughModelApi.addUpdateDrillDefinitionEntry(spec, options.undoRedoOptions); if (options.callback) { options.callback(options); } return entry; }.bind(this)); }, _selectDrillTarget: function _selectDrillTarget() { if (!this._selectDrillTargetActionHandler) { this._selectDrillTargetActionHandler = new SelectDrillTargetActionHandler(this.dashboardApi); } return this._selectDrillTargetActionHandler.selectTarget(); }, /** * Get the drill target name from content store and cache it within the same session. */ _getTargetNameFromCM: function _getTargetNameFromCM(drillEntry) { return new Promise(function (resolve, reject) { var _this = this; if (!drillEntry.getTargetName()) { this.dashboardApi.getGlassSvc('.Content').then(function (contentService) { CMHelper.getReportName(contentService, drillEntry.getAssetId()).then(function (result) { if (result.errorCode) { reject(result); } else { drillEntry.setTargetName(result); resolve(); } }.bind(_this)); }); } else { resolve(); } }.bind(this)); }, _drillDefinitionIsUniqueName: function _drillDefinitionIsUniqueName(drillThroughModelApi, spec) { //drillThroughDefinition model is in dashboard scope, within one dashboard, there should be no drillThroughDefinition with the same name var entries = drillThroughModelApi.getDrillDefinitionEntries(); for (var count = 0; count < entries.length; count++) { var entry = entries[count]; //Make sure not to verify against the same entry //The name must be unique across all reports if (spec.modelRefs[0] === entry.getModelRefs()[0] && entry.getId() !== spec.id && entry.getName() === spec.name) { return false; } } return true; }, _showError: function _showError(error) { var jqXHR = error.jqXHR || error || {}; if (!jqXHR.responseText) { jqXHR.responseText = error.messages ? error.messages : error.errorKey ? StringResources.get(error.errorKey) : StringResources.get('errorDrillDefinition'); } AjaxHelper.showAjaxServiceErrorMessage(this.dashboardApi, jqXHR); //Eat the rejection and return as success return Promise.resolve(); } }); return DrillThroughController; }); //# sourceMappingURL=DrillThroughController.js.map