'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, 2019 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['../../../DynamicFileLoader', 'underscore'], function (DynamicFileLoader, _) { 'use strict'; return function () { function SourceManager(options) { _classCallCheck(this, SourceManager); this.sourceModel = options.sourceModel; this.dashboardApi = options.dashboardApi; this.sourcesCollectionManager = options.sourcesCollectionManager; // If the source was just added then we should already have the localized name if (this.sourceModel) { this.localizedName = this.sourceModel.get('localizedName') || null; this.cmObjectExists = this.sourceModel.get('cmObjectExists') || undefined; this.modificationTime = this.sourceModel.get('modificationTime') || null; this.sourceModel.setAjaxSvc(this.dashboardApi.getGlassCoreSvc('.Ajax')); this.sourceModel.setUserProfileSvc(this.dashboardApi.getGlassCoreSvc('.UserProfile')); } } /** * Returns a promise which will resolve with a module API wrapper around the moserJS module object * @param moduleID optional moduleID that already exists on the server * @param {boolean} [showErrorToast] optional flag for showing error toast * @param {boolean} [clearTemporaryModule] optional flag to clear temporary module (e.g. when relinking) * @return {Promise} Promise that will get resolved with the ModuleAPI object */ SourceManager.prototype.getModule = function getModule(moduleId, showErrorToast, clearTemporaryModule) { var _this = this; if (this._moduleAPI) { if (this.isInErrorState()) { return Promise.reject({ sourceInfo: { name: this.localizedName } }); } if (clearTemporaryModule) { this._moduleAPI.clearTemporaryModule(); } // wait for the temporary module to be loaded // which can take longer without optimizations return this._moduleAPI.whenTemporaryModuleReady().then(function () { return _this._moduleAPI; }); } //if the load module is in progress, return the current promise instead of creating another module. if (this.modulePromise) { return this.modulePromise; } // Delay loading this class as long as possible since it loads the modelling code this.modulePromise = DynamicFileLoader.load(['dashboard-analytics/dataSources/modelapis/ModuleAPI']).then(this._loadModuleAPI.bind(this, moduleId, showErrorToast)).then(function (res) { _this.modulePromise = null; return res; }).catch(function (error) { _this.dashboardApi.getGlassCoreSvc('.Logger').error(error); _this.sourceModel.set({ state: 'error' }, { silent: true }); _this.modulePromise = null; throw error; }); return this.modulePromise; }; SourceManager.prototype._loadModuleAPI = function _loadModuleAPI(moduleId, showErrorToast, modules) { var _this2 = this; var ModuleAPI = modules[0]; var moduleAPI = new ModuleAPI({ 'sourceModel': this.sourceModel, 'dashboardApi': this.dashboardApi, 'sourceModelManager': this }); // Make sure we call load before resolving the promise. The load will take care of making // sure the moserJS object is initialzed with the metadata return moduleAPI.load(moduleId, showErrorToast).then(function () { _this2._moduleAPI = moduleAPI; return _this2._moduleAPI; }).catch(function (error) { _this2.dashboardApi.getGlassCoreSvc('.Logger').error(error); _this2.sourceModel.set({ state: 'error', errorCode: error.code }, { silent: true }); _this2._moduleAPI = moduleAPI; throw error; }); }; /** * Returns the type of the intiial source * @return {String} The type of the source */ SourceManager.prototype.getType = function getType() { return this.sourceModel.get('type'); }; SourceManager.prototype._queryCMObject = function _queryCMObject() { if (this.hasQueriedCM) { return Promise.resolve(); } if (this.sourceModel.hasInlineModule()) { this._handleQueryCMObjectSuccess({ data: { data: [{ defaultName: this.sourceModel.name, searchPath: this.sourceModel.assetId, modificationTime: null }] } }); return Promise.resolve(); } if (!this._queryCMObjectPromise) { // Should query sourceModel only once. this._queryCMObjectPromise = this.dashboardApi.getGlassCoreSvc('.Ajax').ajax({ url: 'v1/objects/' + this.sourceModel.get('assetId') + '?fields=userInterfaces,defaultName,searchPath', type: 'GET', headers: { 'Accept': 'application/json' } }).then(this._handleQueryCMObjectSuccess.bind(this)).catch(this._handleQueryCMObjectError.bind(this)); } return this._queryCMObjectPromise; }; SourceManager.prototype._handleQueryCMObjectSuccess = function _handleQueryCMObjectSuccess(response) { var data = response.data.data; this.hasQueriedCM = true; if (data && data.length > 0) { this.localizedName = data[0].defaultName; this.modificationTime = data[0].modificationTime; this.sourceModel.searchPath = data[0].searchPath; if (data[0].type === 'package' && data[0].userInterfaces && _.indexOf(data[0].userInterfaces, 'analysisStudio') >= 0) { this.sourceModel.isOlapPackage = true; } else { this.sourceModel.isOlapPackage = false; } this.cmObjectExists = true; } else { this._setErrorState(); } if (this.sourceModel.get('pinSourceName')) { this.localizedName = this.localizedName + ' - ' + this.sourceModel.get('pinSourceName'); } }; SourceManager.prototype._handleQueryCMObjectError = function _handleQueryCMObjectError() { this._setErrorState(); }; SourceManager.prototype._setErrorState = function _setErrorState() { this.localizedName = this.sourceModel.get('name'); this.cmObjectExists = false; this.sourceModel.set({ state: 'error' }, { silent: true }); }; /** * Get the localized name of the source * @return {Promise} Promise that will be resolved with localized name of the data source */ SourceManager.prototype.getLocalizedName = function getLocalizedName() { var _this3 = this; if (this.localizedName) { return Promise.resolve(this.localizedName); } return this._queryCMObject().then(function () { return _this3.localizedName; }); }; SourceManager.prototype.getSearchPath = function getSearchPath() { var _this4 = this; if (this.sourceModel.searchPath) { return Promise.resolve(this.sourceModel.searchPath); } return this._queryCMObject().then(function () { return _this4.sourceModel.searchPath; }); }; SourceManager.prototype.exists = function exists() { var _this5 = this; if (this.cmObjectExists !== undefined) { return Promise.resolve(this.cmObjectExists); } return this._queryCMObject().then(function () { return _this5.cmObjectExists; }); }; SourceManager.prototype.getModificationTime = function getModificationTime() { var _this6 = this; if (this.modificationTime) { return Promise.resolve(this.modificationTime); } return this._queryCMObject().then(function () { return _this6.modificationTime; }); }; SourceManager.prototype.relink = function relink(newDatasourceInfo) { var _this7 = this; delete this.localizedName; delete this.modificationTime; delete this.cmObjectExists; this.hasQueriedCM = false; this.sourceModel.set({ state: '', errorCode: '' }, { silent: true }); this.sourceModel.set(newDatasourceInfo, { payloadData: { skipUndoRedo: true } }); return this.getModule(null, null, true).then(function (moduleAPI) { return moduleAPI.relink(_this7.sourceModel); }).then(function (payload) { return payload; }).catch(this.generalRequestErrorHandler.bind(this)); }; SourceManager.prototype.reloadMetadata = function reloadMetadata() { if (!this._moduleAPI) { return Promise.resolve(); } return this._moduleAPI.reloadMetadata().catch(this.generalRequestErrorHandler.bind(this)); }; /* * A generic error handler that sets the state in the source model and re-throws the error */ SourceManager.prototype.generalRequestErrorHandler = function generalRequestErrorHandler(error) { this.sourceModel.set({ state: 'error' }); throw error; }; SourceManager.prototype.getState = function getState() { return this.sourceModel.get('state'); }; SourceManager.prototype.setState = function setState(state) { this.sourceModel.set({ state: state }); }; SourceManager.prototype.getLastModified = function getLastModified() { return this._lastModified; }; SourceManager.prototype.setLastModified = function setLastModified(lastModified) { this._lastModified = lastModified; }; SourceManager.prototype.isInErrorState = function isInErrorState() { return this.sourceModel.get('state') === 'error'; }; return SourceManager; }(); }); //# sourceMappingURL=SourceModelManager.js.map