SourceModelManager.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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, 2019
  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(['../../../DynamicFileLoader', 'underscore'], function (DynamicFileLoader, _) {
  14. 'use strict';
  15. return function () {
  16. function SourceManager(options) {
  17. _classCallCheck(this, SourceManager);
  18. this.sourceModel = options.sourceModel;
  19. this.dashboardApi = options.dashboardApi;
  20. this.sourcesCollectionManager = options.sourcesCollectionManager;
  21. // If the source was just added then we should already have the localized name
  22. if (this.sourceModel) {
  23. this.localizedName = this.sourceModel.get('localizedName') || null;
  24. this.cmObjectExists = this.sourceModel.get('cmObjectExists') || undefined;
  25. this.modificationTime = this.sourceModel.get('modificationTime') || null;
  26. this.sourceModel.setAjaxSvc(this.dashboardApi.getGlassCoreSvc('.Ajax'));
  27. this.sourceModel.setUserProfileSvc(this.dashboardApi.getGlassCoreSvc('.UserProfile'));
  28. }
  29. }
  30. /**
  31. * Returns a promise which will resolve with a module API wrapper around the moserJS module object
  32. * @param moduleID optional moduleID that already exists on the server
  33. * @param {boolean} [showErrorToast] optional flag for showing error toast
  34. * @param {boolean} [clearTemporaryModule] optional flag to clear temporary module (e.g. when relinking)
  35. * @return {Promise} Promise that will get resolved with the ModuleAPI object
  36. */
  37. SourceManager.prototype.getModule = function getModule(moduleId, showErrorToast, clearTemporaryModule) {
  38. var _this = this;
  39. if (this._moduleAPI) {
  40. if (this.isInErrorState()) {
  41. return Promise.reject({
  42. sourceInfo: {
  43. name: this.localizedName
  44. }
  45. });
  46. }
  47. if (clearTemporaryModule) {
  48. this._moduleAPI.clearTemporaryModule();
  49. }
  50. // wait for the temporary module to be loaded
  51. // which can take longer without optimizations
  52. return this._moduleAPI.whenTemporaryModuleReady().then(function () {
  53. return _this._moduleAPI;
  54. });
  55. }
  56. //if the load module is in progress, return the current promise instead of creating another module.
  57. if (this.modulePromise) {
  58. return this.modulePromise;
  59. }
  60. // Delay loading this class as long as possible since it loads the modelling code
  61. this.modulePromise = DynamicFileLoader.load(['dashboard-analytics/dataSources/modelapis/ModuleAPI']).then(this._loadModuleAPI.bind(this, moduleId, showErrorToast)).then(function (res) {
  62. _this.modulePromise = null;
  63. return res;
  64. }).catch(function (error) {
  65. _this.dashboardApi.getGlassCoreSvc('.Logger').error(error);
  66. _this.sourceModel.set({
  67. state: 'error'
  68. }, {
  69. silent: true
  70. });
  71. _this.modulePromise = null;
  72. throw error;
  73. });
  74. return this.modulePromise;
  75. };
  76. SourceManager.prototype._loadModuleAPI = function _loadModuleAPI(moduleId, showErrorToast, modules) {
  77. var _this2 = this;
  78. var ModuleAPI = modules[0];
  79. var moduleAPI = new ModuleAPI({
  80. 'sourceModel': this.sourceModel,
  81. 'dashboardApi': this.dashboardApi,
  82. 'sourceModelManager': this
  83. });
  84. // Make sure we call load before resolving the promise. The load will take care of making
  85. // sure the moserJS object is initialzed with the metadata
  86. return moduleAPI.load(moduleId, showErrorToast).then(function () {
  87. _this2._moduleAPI = moduleAPI;
  88. return _this2._moduleAPI;
  89. }).catch(function (error) {
  90. _this2.dashboardApi.getGlassCoreSvc('.Logger').error(error);
  91. _this2.sourceModel.set({
  92. state: 'error',
  93. errorCode: error.code
  94. }, {
  95. silent: true
  96. });
  97. _this2._moduleAPI = moduleAPI;
  98. throw error;
  99. });
  100. };
  101. /**
  102. * Returns the type of the intiial source
  103. * @return {String} The type of the source
  104. */
  105. SourceManager.prototype.getType = function getType() {
  106. return this.sourceModel.get('type');
  107. };
  108. SourceManager.prototype._queryCMObject = function _queryCMObject() {
  109. if (this.hasQueriedCM) {
  110. return Promise.resolve();
  111. }
  112. if (this.sourceModel.hasInlineModule()) {
  113. this._handleQueryCMObjectSuccess({
  114. data: {
  115. data: [{
  116. defaultName: this.sourceModel.name,
  117. searchPath: this.sourceModel.assetId,
  118. modificationTime: null
  119. }]
  120. }
  121. });
  122. return Promise.resolve();
  123. }
  124. if (!this._queryCMObjectPromise) {
  125. // Should query sourceModel only once.
  126. this._queryCMObjectPromise = this.dashboardApi.getGlassCoreSvc('.Ajax').ajax({
  127. url: 'v1/objects/' + this.sourceModel.get('assetId') + '?fields=userInterfaces,defaultName,searchPath',
  128. type: 'GET',
  129. headers: {
  130. 'Accept': 'application/json'
  131. }
  132. }).then(this._handleQueryCMObjectSuccess.bind(this)).catch(this._handleQueryCMObjectError.bind(this));
  133. }
  134. return this._queryCMObjectPromise;
  135. };
  136. SourceManager.prototype._handleQueryCMObjectSuccess = function _handleQueryCMObjectSuccess(response) {
  137. var data = response.data.data;
  138. this.hasQueriedCM = true;
  139. if (data && data.length > 0) {
  140. this.localizedName = data[0].defaultName;
  141. this.modificationTime = data[0].modificationTime;
  142. this.sourceModel.searchPath = data[0].searchPath;
  143. if (data[0].type === 'package' && data[0].userInterfaces && _.indexOf(data[0].userInterfaces, 'analysisStudio') >= 0) {
  144. this.sourceModel.isOlapPackage = true;
  145. } else {
  146. this.sourceModel.isOlapPackage = false;
  147. }
  148. this.cmObjectExists = true;
  149. } else {
  150. this._setErrorState();
  151. }
  152. if (this.sourceModel.get('pinSourceName')) {
  153. this.localizedName = this.localizedName + ' - ' + this.sourceModel.get('pinSourceName');
  154. }
  155. };
  156. SourceManager.prototype._handleQueryCMObjectError = function _handleQueryCMObjectError() {
  157. this._setErrorState();
  158. };
  159. SourceManager.prototype._setErrorState = function _setErrorState() {
  160. this.localizedName = this.sourceModel.get('name');
  161. this.cmObjectExists = false;
  162. this.sourceModel.set({
  163. state: 'error'
  164. }, {
  165. silent: true
  166. });
  167. };
  168. /**
  169. * Get the localized name of the source
  170. * @return {Promise} Promise that will be resolved with localized name of the data source
  171. */
  172. SourceManager.prototype.getLocalizedName = function getLocalizedName() {
  173. var _this3 = this;
  174. if (this.localizedName) {
  175. return Promise.resolve(this.localizedName);
  176. }
  177. return this._queryCMObject().then(function () {
  178. return _this3.localizedName;
  179. });
  180. };
  181. SourceManager.prototype.getSearchPath = function getSearchPath() {
  182. var _this4 = this;
  183. if (this.sourceModel.searchPath) {
  184. return Promise.resolve(this.sourceModel.searchPath);
  185. }
  186. return this._queryCMObject().then(function () {
  187. return _this4.sourceModel.searchPath;
  188. });
  189. };
  190. SourceManager.prototype.exists = function exists() {
  191. var _this5 = this;
  192. if (this.cmObjectExists !== undefined) {
  193. return Promise.resolve(this.cmObjectExists);
  194. }
  195. return this._queryCMObject().then(function () {
  196. return _this5.cmObjectExists;
  197. });
  198. };
  199. SourceManager.prototype.getModificationTime = function getModificationTime() {
  200. var _this6 = this;
  201. if (this.modificationTime) {
  202. return Promise.resolve(this.modificationTime);
  203. }
  204. return this._queryCMObject().then(function () {
  205. return _this6.modificationTime;
  206. });
  207. };
  208. SourceManager.prototype.relink = function relink(newDatasourceInfo) {
  209. var _this7 = this;
  210. delete this.localizedName;
  211. delete this.modificationTime;
  212. delete this.cmObjectExists;
  213. this.hasQueriedCM = false;
  214. this.sourceModel.set({
  215. state: '',
  216. errorCode: ''
  217. }, {
  218. silent: true
  219. });
  220. this.sourceModel.set(newDatasourceInfo, {
  221. payloadData: {
  222. skipUndoRedo: true
  223. }
  224. });
  225. return this.getModule(null, null, true).then(function (moduleAPI) {
  226. return moduleAPI.relink(_this7.sourceModel);
  227. }).then(function (payload) {
  228. return payload;
  229. }).catch(this.generalRequestErrorHandler.bind(this));
  230. };
  231. SourceManager.prototype.reloadMetadata = function reloadMetadata() {
  232. if (!this._moduleAPI) {
  233. return Promise.resolve();
  234. }
  235. return this._moduleAPI.reloadMetadata().catch(this.generalRequestErrorHandler.bind(this));
  236. };
  237. /*
  238. * A generic error handler that sets the state in the source model and re-throws the error
  239. */
  240. SourceManager.prototype.generalRequestErrorHandler = function generalRequestErrorHandler(error) {
  241. this.sourceModel.set({
  242. state: 'error'
  243. });
  244. throw error;
  245. };
  246. SourceManager.prototype.getState = function getState() {
  247. return this.sourceModel.get('state');
  248. };
  249. SourceManager.prototype.setState = function setState(state) {
  250. this.sourceModel.set({
  251. state: state
  252. });
  253. };
  254. SourceManager.prototype.getLastModified = function getLastModified() {
  255. return this._lastModified;
  256. };
  257. SourceManager.prototype.setLastModified = function setLastModified(lastModified) {
  258. this._lastModified = lastModified;
  259. };
  260. SourceManager.prototype.isInErrorState = function isInErrorState() {
  261. return this.sourceModel.get('state') === 'error';
  262. };
  263. return SourceManager;
  264. }();
  265. });
  266. //# sourceMappingURL=SourceModelManager.js.map