"use strict"; /**  * Licensed Materials - Property of IBM  * IBM Cognos Products: admin  * Copyright IBM Corp. 2015, 2017  * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.  */ define(['underscore', 'bi/commons/ui/core/Class', 'bi/admin/datasource/services/ApiSvc', 'bi/admin/nls/StringResource'], function (_, Class, Api, StringResource) { 'use strict'; //NOSONAR: meant to be strict var _singletonInstance = null; var DataSourceApp = Class.extend({ systemTypes: ['JDBC', 'CUBE', 'JSON', 'ODATA'], exclusiveTypes: ['CUBE', 'JSON', 'ODATA'], _contexts: [], PASSWORD: '###PASSWORD###', init: function init() { var filterTypes = _.difference(this.systemTypes, this.exclusiveTypes); Api.filterTypes = filterTypes; }, SlideOutDataSourceList: function SlideOutDataSourceList(glassContext, parentSlideout, dataSourceListTitle) { this._baseSlideOut = parentSlideout ? parentSlideout : this._baseSlideOut; this._glassContext = glassContext ? glassContext : this._glassContext; Api.glassContext = this._glassContext; if (dataSourceListTitle === undefined) { dataSourceListTitle = StringResource.get('dataServerPaneTitle'); } glassContext.appController.showSlideOut({ parent: this._baseSlideOut, overlay: true, width: '400px', label: dataSourceListTitle, content: { module: 'bi/admin/datasource/slideout/DataSourceListPane', id: 'DataSourcePane', title: dataSourceListTitle, showGobackButton: true } }); // get current sign on name space return this.getCurrentNameSpace().then(function (nsObj) { this._glassContext._currentNameSpace = nsObj; }.bind(this)); }, getCurrentNameSpace: function getCurrentNameSpace() { // get current sign on name space var url = 'v1/identity'; return this.glassContext.services.fetch.get(url).then(function (response) { var identity = response.data; var inNS = _.find(identity.data, function (item) { return item.type === 'namespace' && item.searchPath.split('"')[1] === ":"; }); var exNS = _.find(identity.data, function (item) { return item.type === 'namespace' && item.searchPath.split('"')[1] !== ":"; }); var currentNS = exNS ? exNS : inNS; return { id: currentNS.id, searchPath: currentNS.searchPath, type: currentNS.type }; }.bind(this)); }, //get all tables within a schema getTables: function getTables(schemaId, signonInfo) { return new Promise(function (resolve, reject) { var url = 'v1/metadata/schemas/tables?id=' + encodeURIComponent(schemaId); var connObj = { connections: signonInfo }; var connectionSpec = "connectionSpec=" + encodeURIComponent(JSON.stringify(connObj)); var ajaxOptions = { 'dataType': 'json', 'type': 'GET', 'url': url, 'data': connectionSpec }; return this.glassContext.getCoreSvc('.Ajax').ajax(ajaxOptions).then(function (response) { resolve(response.data); }, function (error) { if (error.jqXHR) { var errorMessage = error.jqXHR.responseJSON ? error.jqXHR.responseJSON.msg : error.jqXHR.responseText; this.glassContext.appController.showErrorMessage(errorMessage, StringResource.get('error')); } reject(error); }.bind(this)); }.bind(this)); }, //get schema load options spec. getSchema: function getSchema(schemaId, signonInfo) { return new Promise(function (resolve, reject) { var url = 'v1/metadata/schemas/?id=' + encodeURIComponent(schemaId); var connObj = { connections: signonInfo }; var connectionSpec = "connectionSpec=" + encodeURIComponent(JSON.stringify(connObj)); var ajaxOptions = { 'dataType': 'json', 'type': 'GET', 'url': url, 'data': connectionSpec }; return this.glassContext.getCoreSvc('.Ajax').ajax(ajaxOptions).then(function (response) { resolve(response.data.data[0]); }, function (error) { if (error.jqXHR) { var errorMessage = error.jqXHR.responseJSON ? error.jqXHR.responseJSON.msg : error.jqXHR.responseText; this.glassContext.appController.showErrorMessage(errorMessage, StringResource.get('error')); } reject(error); }.bind(this)); }.bind(this)); }, //update schema with load option spec. updateSchema: function updateSchema(schemaId, data) { return new Promise(function (resolve, reject) { Api.schemas.update(schemaId, data).then(resolve, function (err, jqXHR) { var errorMessage = jqXHR.responseJSON ? jqXHR.responseJSON.msg : jqXHR.responseText; this.glassContext.appController.showErrorMessage(errorMessage, StringResource.get('error')); reject(err, jqXHR); }.bind(this)); }.bind(this)); }, //create new schema in sources createSchemaSource: function createSchemaSource(connId, data) { return new Promise(function (resolve, reject) { Api.schemaSources.create(connId, data).then(function () { //return the Moser response containing storeId of new created schema. var obj = { getResponseHeader: arguments[2].getResponseHeader }; resolve(obj); }, function (err, jqXHR) { var errorMessage = jqXHR.responseJSON ? jqXHR.responseJSON.msg : jqXHR.responseText; this.glassContext.appController.showErrorMessage(errorMessage, StringResource.get('error')); reject(err, jqXHR); }.bind(this)); }.bind(this)); }, CancelTask: function CancelTask(taskId, xCaAffinity) { return new Promise(function (resolve, reject) { Api.tasks.cancel(taskId, xCaAffinity).then(resolve, function (err, jqXHR) { this.glassContext.appController.showErrorMessage(jqXHR.responseJSON.messages[0], StringResource.get('error')); reject(err, jqXHR); }.bind(this)); }.bind(this)); }, /** * This method fetches the history of the schema with schemaID. Makes a call to ApiSvc._getHistoryApiSet. * @param {String} The schemaID of the selected schema * @return {Promise} A promise with the schema history data */ getHistory: function getHistory(schemaId) { return new Promise(function (resolve, reject) { Api.history.list(schemaId).then(function (response) { resolve(response.data); }, function (err, jqXHR) { this.glassContext.appController.showErrorMessage(jqXHR.responseJSON.msg, StringResource.get('error')); reject(err, jqXHR); }.bind(this)); }.bind(this)); }, CreateSchema: function CreateSchema(item, view) { var dataItem = { schema: item.schema, catalog: item.catalog, type: item.type, schemaType: item.schemaType || 'user', defaultName: item.defaultName, specification: item.specification, status: 'pending' }; return this.createSchemaSource(view.conn.id, dataItem); }, ClearSchema: function ClearSchema(item) { return new Promise(function (resolve, reject) { Api.schemas.clear(item.id).then(resolve, function (err, jqXHR) { this.glassContext.appController.showErrorMessage(jqXHR.responseJSON.messages[0], StringResource.get('error')); reject(err, jqXHR); }.bind(this)); }.bind(this)); }, /** * get data source connection info and render GroupListView. * @private * @param dataSources {Array} each object includes the specified data source info. * dataSources[i]: { * _type: 'dataSource', activeConnection: Object, connectionString: ';LOCAL;OL;DBInfo_Type=MS;...', context: Object, defaultDescription: 'Description goes here', * defaultName: 'new', disabled: 'false', hasChildren: 'true', hidden: 'false', id: 'iF0651295A97E464A83F22D202AA9E797', objectClass: 'dataSource', * permissions: Array, searchPath: 'CAMID(":")/dataSource[@name='new']', shown: 'true', tenantID: '', usage: 'object' * } */ FillConnectionList: function FillConnectionList(dataSource, formView) { var items = _.map(dataSource.connections, function (conn) { var writable = conn.permissions && conn.permissions.indexOf('write') > -1 ? true : false; return { id: dataSource.id, connId: conn.id, title: conn.defaultName, defaultName: conn.defaultName, defaultDescription: conn.defaultDescription, connectionString: conn.connectionString, credentialNamespaces: conn.credentialNamespaces, disabled: conn.disabled, hidden: conn.hidden, writable: writable }; }); items = _.filter(items, function (item) { return item != null; }); var viewObj = {}; viewObj.items = items; viewObj.writable = dataSource.writable; viewObj.datasourceName = dataSource.defaultName; // defaultDescription will be undefined when user save a "", so need to change it to "" here viewObj.datasourceDescription = dataSource.defaultDescription || ''; viewObj.datasourceId = dataSource.id; formView.items = items; formView.render(viewObj); this._dcListView = formView; } }); var _static = { getInstance: function getInstance() { if (!_singletonInstance) { _singletonInstance = new DataSourceApp(); } return _singletonInstance; } }; return _static.getInstance(); });