App.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. "use strict";
  2. /**
  3.  * Licensed Materials - Property of IBM
  4.  * IBM Cognos Products: admin
  5.  * Copyright IBM Corp. 2015, 2017
  6.  * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7.  */
  8. define(['underscore', 'bi/commons/ui/core/Class', 'bi/admin/datasource/services/ApiSvc', 'bi/admin/nls/StringResource'], function (_, Class, Api, StringResource) {
  9. 'use strict'; //NOSONAR: meant to be strict
  10. var _singletonInstance = null;
  11. var DataSourceApp = Class.extend({
  12. systemTypes: ['JDBC', 'CUBE', 'JSON', 'ODATA'],
  13. exclusiveTypes: ['CUBE', 'JSON', 'ODATA'],
  14. _contexts: [],
  15. PASSWORD: '###PASSWORD###',
  16. init: function init() {
  17. var filterTypes = _.difference(this.systemTypes, this.exclusiveTypes);
  18. Api.filterTypes = filterTypes;
  19. },
  20. SlideOutDataSourceList: function SlideOutDataSourceList(glassContext, parentSlideout, dataSourceListTitle) {
  21. this._baseSlideOut = parentSlideout ? parentSlideout : this._baseSlideOut;
  22. this._glassContext = glassContext ? glassContext : this._glassContext;
  23. Api.glassContext = this._glassContext;
  24. if (dataSourceListTitle === undefined) {
  25. dataSourceListTitle = StringResource.get('dataServerPaneTitle');
  26. }
  27. glassContext.appController.showSlideOut({
  28. parent: this._baseSlideOut,
  29. overlay: true,
  30. width: '400px',
  31. label: dataSourceListTitle,
  32. content: {
  33. module: 'bi/admin/datasource/slideout/DataSourceListPane',
  34. id: 'DataSourcePane',
  35. title: dataSourceListTitle,
  36. showGobackButton: true
  37. }
  38. }); // get current sign on name space
  39. return this.getCurrentNameSpace().then(function (nsObj) {
  40. this._glassContext._currentNameSpace = nsObj;
  41. }.bind(this));
  42. },
  43. getCurrentNameSpace: function getCurrentNameSpace() {
  44. // get current sign on name space
  45. var url = 'v1/identity';
  46. return this.glassContext.services.fetch.get(url).then(function (response) {
  47. var identity = response.data;
  48. var inNS = _.find(identity.data, function (item) {
  49. return item.type === 'namespace' && item.searchPath.split('"')[1] === ":";
  50. });
  51. var exNS = _.find(identity.data, function (item) {
  52. return item.type === 'namespace' && item.searchPath.split('"')[1] !== ":";
  53. });
  54. var currentNS = exNS ? exNS : inNS;
  55. return {
  56. id: currentNS.id,
  57. searchPath: currentNS.searchPath,
  58. type: currentNS.type
  59. };
  60. }.bind(this));
  61. },
  62. //get all tables within a schema
  63. getTables: function getTables(schemaId, signonInfo) {
  64. return new Promise(function (resolve, reject) {
  65. var url = 'v1/metadata/schemas/tables?id=' + encodeURIComponent(schemaId);
  66. var connObj = {
  67. connections: signonInfo
  68. };
  69. var connectionSpec = "connectionSpec=" + encodeURIComponent(JSON.stringify(connObj));
  70. var ajaxOptions = {
  71. 'dataType': 'json',
  72. 'type': 'GET',
  73. 'url': url,
  74. 'data': connectionSpec
  75. };
  76. return this.glassContext.getCoreSvc('.Ajax').ajax(ajaxOptions).then(function (response) {
  77. resolve(response.data);
  78. }, function (error) {
  79. if (error.jqXHR) {
  80. var errorMessage = error.jqXHR.responseJSON ? error.jqXHR.responseJSON.msg : error.jqXHR.responseText;
  81. this.glassContext.appController.showErrorMessage(errorMessage, StringResource.get('error'));
  82. }
  83. reject(error);
  84. }.bind(this));
  85. }.bind(this));
  86. },
  87. //get schema load options spec.
  88. getSchema: function getSchema(schemaId, signonInfo) {
  89. return new Promise(function (resolve, reject) {
  90. var url = 'v1/metadata/schemas/?id=' + encodeURIComponent(schemaId);
  91. var connObj = {
  92. connections: signonInfo
  93. };
  94. var connectionSpec = "connectionSpec=" + encodeURIComponent(JSON.stringify(connObj));
  95. var ajaxOptions = {
  96. 'dataType': 'json',
  97. 'type': 'GET',
  98. 'url': url,
  99. 'data': connectionSpec
  100. };
  101. return this.glassContext.getCoreSvc('.Ajax').ajax(ajaxOptions).then(function (response) {
  102. resolve(response.data.data[0]);
  103. }, function (error) {
  104. if (error.jqXHR) {
  105. var errorMessage = error.jqXHR.responseJSON ? error.jqXHR.responseJSON.msg : error.jqXHR.responseText;
  106. this.glassContext.appController.showErrorMessage(errorMessage, StringResource.get('error'));
  107. }
  108. reject(error);
  109. }.bind(this));
  110. }.bind(this));
  111. },
  112. //update schema with load option spec.
  113. updateSchema: function updateSchema(schemaId, data) {
  114. return new Promise(function (resolve, reject) {
  115. Api.schemas.update(schemaId, data).then(resolve, function (err, jqXHR) {
  116. var errorMessage = jqXHR.responseJSON ? jqXHR.responseJSON.msg : jqXHR.responseText;
  117. this.glassContext.appController.showErrorMessage(errorMessage, StringResource.get('error'));
  118. reject(err, jqXHR);
  119. }.bind(this));
  120. }.bind(this));
  121. },
  122. //create new schema in sources
  123. createSchemaSource: function createSchemaSource(connId, data) {
  124. return new Promise(function (resolve, reject) {
  125. Api.schemaSources.create(connId, data).then(function () {
  126. //return the Moser response containing storeId of new created schema.
  127. var obj = {
  128. getResponseHeader: arguments[2].getResponseHeader
  129. };
  130. resolve(obj);
  131. }, function (err, jqXHR) {
  132. var errorMessage = jqXHR.responseJSON ? jqXHR.responseJSON.msg : jqXHR.responseText;
  133. this.glassContext.appController.showErrorMessage(errorMessage, StringResource.get('error'));
  134. reject(err, jqXHR);
  135. }.bind(this));
  136. }.bind(this));
  137. },
  138. CancelTask: function CancelTask(taskId, xCaAffinity) {
  139. return new Promise(function (resolve, reject) {
  140. Api.tasks.cancel(taskId, xCaAffinity).then(resolve, function (err, jqXHR) {
  141. this.glassContext.appController.showErrorMessage(jqXHR.responseJSON.messages[0], StringResource.get('error'));
  142. reject(err, jqXHR);
  143. }.bind(this));
  144. }.bind(this));
  145. },
  146. /**
  147. * This method fetches the history of the schema with schemaID. Makes a call to ApiSvc._getHistoryApiSet.
  148. * @param {String} The schemaID of the selected schema
  149. * @return {Promise} A promise with the schema history data
  150. */
  151. getHistory: function getHistory(schemaId) {
  152. return new Promise(function (resolve, reject) {
  153. Api.history.list(schemaId).then(function (response) {
  154. resolve(response.data);
  155. }, function (err, jqXHR) {
  156. this.glassContext.appController.showErrorMessage(jqXHR.responseJSON.msg, StringResource.get('error'));
  157. reject(err, jqXHR);
  158. }.bind(this));
  159. }.bind(this));
  160. },
  161. CreateSchema: function CreateSchema(item, view) {
  162. var dataItem = {
  163. schema: item.schema,
  164. catalog: item.catalog,
  165. type: item.type,
  166. schemaType: item.schemaType || 'user',
  167. defaultName: item.defaultName,
  168. specification: item.specification,
  169. status: 'pending'
  170. };
  171. return this.createSchemaSource(view.conn.id, dataItem);
  172. },
  173. ClearSchema: function ClearSchema(item) {
  174. return new Promise(function (resolve, reject) {
  175. Api.schemas.clear(item.id).then(resolve, function (err, jqXHR) {
  176. this.glassContext.appController.showErrorMessage(jqXHR.responseJSON.messages[0], StringResource.get('error'));
  177. reject(err, jqXHR);
  178. }.bind(this));
  179. }.bind(this));
  180. },
  181. /**
  182. * get data source connection info and render GroupListView.
  183. * @private
  184. * @param dataSources {Array<Object>} each object includes the specified data source info.
  185. * dataSources[i]: {
  186. * _type: 'dataSource', activeConnection: Object, connectionString: ';LOCAL;OL;DBInfo_Type=MS;...', context: Object, defaultDescription: 'Description goes here',
  187. * defaultName: 'new', disabled: 'false', hasChildren: 'true', hidden: 'false', id: 'iF0651295A97E464A83F22D202AA9E797', objectClass: 'dataSource',
  188. * permissions: Array, searchPath: 'CAMID(":")/dataSource[@name='new']', shown: 'true', tenantID: '', usage: 'object'
  189. * }
  190. */
  191. FillConnectionList: function FillConnectionList(dataSource, formView) {
  192. var items = _.map(dataSource.connections, function (conn) {
  193. var writable = conn.permissions && conn.permissions.indexOf('write') > -1 ? true : false;
  194. return {
  195. id: dataSource.id,
  196. connId: conn.id,
  197. title: conn.defaultName,
  198. defaultName: conn.defaultName,
  199. defaultDescription: conn.defaultDescription,
  200. connectionString: conn.connectionString,
  201. credentialNamespaces: conn.credentialNamespaces,
  202. disabled: conn.disabled,
  203. hidden: conn.hidden,
  204. writable: writable
  205. };
  206. });
  207. items = _.filter(items, function (item) {
  208. return item != null;
  209. });
  210. var viewObj = {};
  211. viewObj.items = items;
  212. viewObj.writable = dataSource.writable;
  213. viewObj.datasourceName = dataSource.defaultName; // defaultDescription will be undefined when user save a "", so need to change it to "" here
  214. viewObj.datasourceDescription = dataSource.defaultDescription || '';
  215. viewObj.datasourceId = dataSource.id;
  216. formView.items = items;
  217. formView.render(viewObj);
  218. this._dcListView = formView;
  219. }
  220. });
  221. var _static = {
  222. getInstance: function getInstance() {
  223. if (!_singletonInstance) {
  224. _singletonInstance = new DataSourceApp();
  225. }
  226. return _singletonInstance;
  227. }
  228. };
  229. return _static.getInstance();
  230. });