DataSourcesService.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /*
  4. *+------------------------------------------------------------------------+
  5. *| Licensed Materials - Property of IBM
  6. *| IBM Cognos Products: Dashboard
  7. *| (C) Copyright IBM Corp. 2017, 2020
  8. *|
  9. *| US Government Users Restricted Rights - Use, duplication or disclosure
  10. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  11. *+------------------------------------------------------------------------+
  12. */
  13. define(['../modelapis/SourcesCollectionAPI', '../modelapis/modelmanagers/EmbeddedModuleManager', '../models/DataSourcesModel', '../utils/RelinkUtils'], function (SourcesCollectionAPI, EmbeddedModuleManager, DataSourcesModel, RelinkUtils) {
  14. 'use strict';
  15. var DataSourcesService = function () {
  16. function DataSourcesService(options) {
  17. _classCallCheck(this, DataSourcesService);
  18. this.dashboardApi = options.features['API'];
  19. this.logger = options.features['Logger'];
  20. if (options.features['internal']) {
  21. this.boardModel = options.features['internal'].getBoardModel();
  22. }
  23. }
  24. DataSourcesService.prototype.destroy = function destroy() {
  25. if (this._sourcesCollectionAPI) {
  26. this._sourcesCollectionAPI.destroy();
  27. }
  28. };
  29. DataSourcesService.prototype.getAPI = function getAPI() {
  30. return this;
  31. };
  32. //turn off Empty Block warning
  33. DataSourcesService.prototype.getLifeCycleHandlers = function getLifeCycleHandlers() {
  34. var _this = this;
  35. return [{
  36. name: 'pre:widget.maximize',
  37. action: function action(payload) {
  38. if (!payload || !payload.id) {
  39. // no payload and/or no modelId, do nothing...
  40. } else {
  41. try {
  42. _this._handleExpandWidget();
  43. } catch (e) {
  44. _this.logger.error(e);
  45. }
  46. }
  47. }
  48. }];
  49. };
  50. DataSourcesService.prototype._handleExpandWidget = function _handleExpandWidget() {
  51. var plugin = this.dashboardApi.findGlassPlugin('com.ibm.bi.dashboard.dataSources.sourcesBtn');
  52. if (plugin === undefined) {
  53. this.logger.warn('datasource panel is not available');
  54. } else {
  55. if (plugin.isVisible() && !plugin.isPressed()) {
  56. plugin.triggerOnPress();
  57. }
  58. }
  59. };
  60. /**
  61. * @description Get the wrapper API object for an embedded moser module object. And register the usage of `sourceId`
  62. * when the `widget` is not a preview widget
  63. * @param {String} sourceId The id internal to the spec
  64. * @param {String} referenceId The id of the widget that uses the module. It's optional
  65. * @return {Promise} promise that will resolve with the moser API object or null if the source can't be found
  66. */
  67. DataSourcesService.prototype.getModule = function getModule(sourceId) {
  68. var sourcesCollection = this.getSourcesCollection();
  69. var sourceAPI = sourcesCollection.getSource(sourceId);
  70. if (!sourceAPI) {
  71. return Promise.resolve();
  72. }
  73. return sourceAPI.getModule();
  74. };
  75. DataSourcesService.prototype._initializeSourcesCollection = function _initializeSourcesCollection(dataSources) {
  76. if (dataSources) {
  77. this.sourcesCollection = dataSources.get('sources');
  78. this._sourcesCollectionAPI = new SourcesCollectionAPI({
  79. sourcesCollection: this.sourcesCollection,
  80. dashboardApi: this.dashboardApi
  81. });
  82. } else {
  83. this.logger.error('No sources in the board model.', this, dataSources);
  84. }
  85. };
  86. /**
  87. * Initializes the collection of sources from a spec (typically used during upgrade).
  88. * If not present on the spec, it will create the sources model and save it on the spec for reuse.
  89. * @param {object} spec
  90. * @return {SourcesCollectionAPI} collection API object of sources
  91. */
  92. DataSourcesService.prototype.initializeSourcesCollection = function initializeSourcesCollection(spec) {
  93. if (!this._sourcesCollectionAPI) {
  94. if (spec && spec.dataSources instanceof DataSourcesModel === false) {
  95. spec.dataSources = new DataSourcesModel(spec.dataSources);
  96. }
  97. this._initializeSourcesCollection(spec && spec.dataSources);
  98. }
  99. return this._sourcesCollectionAPI || null;
  100. };
  101. /**
  102. * Returns the collection of sources
  103. * @param {Model} [dataSources] - datasoures model, otherwise we will get it from the boardModel
  104. * @return {SourcesCollectionAPI} collection API object of sources
  105. */
  106. DataSourcesService.prototype.getSourcesCollection = function getSourcesCollection(dataSources) {
  107. if (!this._sourcesCollectionAPI) {
  108. var sourcesModel = dataSources || this.boardModel.get('dataSources');
  109. this._initializeSourcesCollection(sourcesModel);
  110. }
  111. return this._sourcesCollectionAPI || null;
  112. };
  113. /**
  114. * Relinks an existing data source to a new source
  115. * @param {SourceModel} source The source you want to relink
  116. * @param {Object} newSourceInfo Information (assetId, name, type, searchPath, ..) of the new source
  117. * @return {Promise}
  118. */
  119. DataSourcesService.prototype.relink = function relink(source, newSourceInfo) {
  120. return RelinkUtils.relink(source, newSourceInfo, this.dashboardApi);
  121. };
  122. DataSourcesService.prototype.getDeploymentReferences = function getDeploymentReferences(isSaveAs) {
  123. var deploymentReferences = [];
  124. var sources = this.getSourcesCollection().getSources();
  125. // Keep a map of the assetIds we've gone through since we can have multiple sources that point to the same assetId
  126. var processAssetIds = {};
  127. sources.forEach(function (source) {
  128. source.getDeploymentReference(deploymentReferences, processAssetIds, isSaveAs);
  129. });
  130. return deploymentReferences;
  131. };
  132. /**
  133. * The dashboard was just saved, we need to update/create any needed modules under the dashboard object
  134. * @param {boolean} options.saveAs Boolean to let us know if it was a saveAs or save operations
  135. * @param {object} options.resaveDashboardSpecCallback Callback to re-save the dashboard spec. Used if the model gets updated
  136. * @return {Promise}
  137. */
  138. DataSourcesService.prototype.onDashboardSave = function onDashboardSave(options) {
  139. var promises = [];
  140. if (this.sourcesCollection) {
  141. options.dashboardAssetId = this.dashboardApi.getDashboardInfo().boardId;
  142. options.ajaxSvc = this.dashboardApi.getGlassCoreSvc('.Ajax');
  143. options.logger = this.logger;
  144. var sources = this.sourcesCollection.getModels();
  145. sources.forEach(function (source) {
  146. if (source.shaping) {
  147. promises.push(EmbeddedModuleManager.onDashboardSave(source.shaping, options));
  148. }
  149. });
  150. }
  151. return Promise.all(promises).then(function (results) {
  152. // If one of the onDashboardSave calls returned 'true', then the shaping model has changed. We need to re-save the dashboard spec
  153. if (results && results.indexOf(true) !== -1) {
  154. options.resaveDashboardSpecCallback();
  155. }
  156. });
  157. };
  158. /**
  159. * Clear shaping on all sources through the shaping model manager
  160. */
  161. DataSourcesService.prototype.clearShapingCache = function clearShapingCache() {
  162. if (this.sourcesCollection) {
  163. var sources = this.sourcesCollection.getModels();
  164. sources.forEach(function (source) {
  165. if (source.shaping && source.shaping.shapingModelManager) {
  166. source.shaping.shapingModelManager.clearCache();
  167. }
  168. });
  169. }
  170. };
  171. return DataSourcesService;
  172. }();
  173. return DataSourcesService;
  174. });
  175. //# sourceMappingURL=DataSourcesService.js.map