'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 Cognos Products: Dashboard *| (C) Copyright IBM Corp. 2017, 2020 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['../../lib/@waca/upgrades/lib/UpgradeRunner', '../../lib/@waca/core-client/js/core-client/utils/ClassFactory', '../../app/nls/StringResources'], function (UpgradeRunner, ClassFactory, StringResources) { var UpgradeService = function () { /** * @param {object} options * @param {object} options.upgrades - a list of paths to upgrades */ function UpgradeService() { _classCallCheck(this, UpgradeService); //default version meaning no upgrades will be performed this._latestDashboardSpecVersion = 0; } UpgradeService.prototype.getLatestDashboardSpecVersion = function getLatestDashboardSpecVersion() { return parseInt(this._latestDashboardSpecVersion); }; UpgradeService.prototype.initialize = function initialize(glassContext) { var _this = this; return glassContext.getCoreSvc('.Perspective').getModel('dashboard').then(function (model) { var upgradesPath = model.content.options.config.upgrades; if (!upgradesPath) { return Promise.resolve(); } return ClassFactory.loadModule(upgradesPath).then(function (upgrades) { var upgradeVersions = Object.keys(upgrades); if (_this._isValidUpgrade(upgradeVersions)) { _this._latestDashboardSpecVersion = upgradeVersions[upgradeVersions.length - 1]; _this.upgradeRunner = new UpgradeRunner({ migrations: upgrades }); } }).catch(function (error) { _this._logger = glassContext.getCoreSvc('.Logger'); _this._logger.error(error); }); }); }; /** * Verifies if an upgrade version is valid by performing the following checks * Checks if the version is an empty string or a string that does not translate to a number then returns false * Converts the version from string to a number and checks if its an integer, if its not an integer returns false * @param {object} upgradeVersions - an object containing mappings of upgrade version number and upgrade paths * * @return {Boolean} true if the version is valid else false */ UpgradeService.prototype._isValidUpgrade = function _isValidUpgrade(upgradeVersions) { if (!upgradeVersions.length) { return false; } var latestUpgradeVersion = upgradeVersions[upgradeVersions.length - 1]; if (!latestUpgradeVersion || isNaN(latestUpgradeVersion)) { return false; } return Number.isInteger(Number(latestUpgradeVersion)); }; /** * Do we need to perform an upgrade? * * @param {object} spec - spec to upgrade * @param {number} requestedVersion - requested version * * @return {Boolean} */ UpgradeService.prototype.needsUpgrade = function needsUpgrade(spec, requestedVersion) { if (this.upgradeRunner) { return this.upgradeRunner.needsUpgrade(spec, requestedVersion); } return false; }; /** * Upgrade board spec * * @param {object} spec - spec to upgrade * @param {number} requestedVersion - requested version * @param {object} [data] - additional data to pass into upgrades * * @return {Promise} Promise to be resolved when upgrade complete */ UpgradeService.prototype.upgrade = function upgrade(spec, requestedVersion, data) { if (this.upgradeRunner) { return this.upgradeRunner.upgrade(spec, requestedVersion, data).then(function (result) { if (result) { // IF we've done a query to perform the upgrade and the user has write access // then we should let them know that they should save the dashboard for performance reasons if (result.upgradePerformed && data.hasWrite && result.obj.queriedForUpgrade) { delete result.obj.queriedForUpgrade; if (!data.noToast) { data.dashboardApi.showToast(StringResources.get('toastUpgradeDone'), { type: 'info' }); } } return result.obj; } else { // TODO: log the error // no result? don't blow up, return the spec as is return spec; } }); } else { if (requestedVersion && spec.version !== requestedVersion) { return Promise.reject(new Error('The requested spec upgrade version is not available')); } else { return Promise.resolve(spec); } } }; UpgradeService.prototype.destroy = function destroy() { this.upgradeRunner = null; }; return UpgradeService; }(); return UpgradeService; }); //# sourceMappingURL=UpgradeService.js.map