"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/admin/common/services/ApiBase'], function (_, ApiBase) { 'use strict'; //NOSONAR: meant to be strict var _singletonInstance = null; var ApiService = ApiBase.extend({ filterTypes: [], /** * constructor * @options {object} * @example: {baseUrl: "http://localhost"} */ init: function init(options) { ApiService.inherited('init', this, arguments); this.dataSources = this._getDsApiSet(); this.dataConnections = this._getDcApiSet(); this.signOns = this._getSignonApiSet(); this.tables = this._getTableApiSet(); this.schemas = this._getSchemaApiSet(); this.tasks = this._getTaskApiSet(); this.schemaSources = this._getSchemaSourceApiSet(); this.history = this._getHistoryApiSet(); }, _getDsApiSet: function _getDsApiSet() { var self = this; var objType = 'datasources'; var patchPath = function patchPath(path) { if (path.indexOf('?') > -1) { path = path + '&intType=light'; } else { path = path + '?intType=light'; } return path; }; return { read: function read(id) { return self._ajax(patchPath(objType + "/" + id), self._getRestOptions()); }, update: function update(id, data) { return self._ajax(patchPath(objType + "/" + id), self._getRestOptions('PUT', data)); }, create: function create(data) { return self._ajax(patchPath(objType + "/"), self._getRestOptions('POST', data)); }, listDefinitions: function listDefinitions(cat) { return self._ajax(patchPath('definitions?cat=' + cat), self._getRestOptions()); } }; }, _createResource: function _createResource(resTemp) { var self = this; var getResPath = function getResPath(resTemp, params) { var resPath = resTemp; params.push(''); params.push(''); params.push(''); _.each(params, function (p, index) { var t = '[id' + index + ']'; resPath = resPath.replace(t, p); }); return resPath; }; return { template: resTemp, request: function request(params, options, xCaAffinity) { var resPath = getResPath(this.template, params); if (xCaAffinity) { options.headers = { Accept: 'application/json', "x-ca-affinity": xCaAffinity }; } return self._ajax(resPath, options); } }; }, _getDcApiSet: function _getDcApiSet() { var self = this; var res = this._createResource("datasources/[id0]/connections/[id1]"); return { list: function list(dsId) { return res.request([dsId], self._getRestOptions()); }, read: function read(dsId, dcId) { return res.request([dsId, dcId], self._getRestOptions()); }, update: function update(dsId, dcId, data) { return res.request([dsId, dcId], self._getRestOptions('PUT', data)); }, create: function create(dsId, data) { return res.request([dsId], self._getRestOptions('POST', data)); }, remove: function remove(dsId, dcId) { return res.request([dsId, dcId], self._getRestOptions('DELETE')); }, testConnection: function testConnection(data) { return self._ajax('datasourceconnection/test', self._getRestOptions('POST', data)); }, testConnectionWithCredential: function testConnectionWithCredential(data) { return self._ajax('datasourceconnection/test_with_credential', self._getRestOptions('POST', data)); } }; }, _getSchemaApiSet: function _getSchemaApiSet() { var self = this; var res = this._createResource("metadata/schemas/[id0]"); return { 'read': function read(schemaId) { return res.request([schemaId], self._getRestOptions()); }, 'update': function update(schemaId, data) { return res.request([schemaId], self._getRestOptions('PUT', data)); }, 'clear': function clear(schemaId) { return res.request([schemaId], self._getRestOptions('DELETE')); } }; }, _getSchemaSourceApiSet: function _getSchemaSourceApiSet() { var self = this; var res = this._createResource("metadata/sources/[id0]/schemas"); return { 'create': function create(dsId, data) { return res.request([dsId], $.extend(self._getRestOptions('POST', data), { 'dataType': 'text' })); } }; }, _getTableApiSet: function _getTableApiSet(schemaId) { var self = this; var res = this._createResource("metadata/schemas/[id0]/tables"); return { 'list': function list(schemaId) { return res.request([schemaId], self._getRestOptions()); } }; }, _getTaskApiSet: function _getTaskApiSet() { var self = this; var res = this._createResource('metadata/tasks/[id0]'); return { 'cancel': function cancel(taskId, xCaAffinity) { return res.request([taskId], self._getRestOptions('DELETE'), xCaAffinity); } }; }, /* * This method fetches the history of the schema with schemaID. * @param {String} The schemaID of the selected schema * @return {Object} An object containing the requested api set */ _getHistoryApiSet: function _getHistoryApiSet(schemaId) { var self = this, res = this._createResource("metadata/schemas/[id0]/history"); return { list: function list(schemaId) { return res.request([schemaId], self._getRestOptions()); } }; }, _getSignonApiSet: function _getSignonApiSet() { var self = this; var res = this._createResource("datasources/[id0]/connections/[id1]/signons/[id2]"); return { list: function list(dsId, dcId) { return res.request([dsId, dcId], self._getRestOptions()); }, read: function read(dsId, dcId, signOnId) { return res.request([dsId, dcId, signOnId], self._getRestOptions()); }, update: function update(dsId, dcId, signOnId, data) { return res.request([dsId, dcId, signOnId], self._getRestOptions('PUT', data)); }, create: function create(dsId, dcId, data) { return res.request([dsId, dcId], self._getRestOptions('POST', data)); }, remove: function remove(dsId, dcId, signOnId) { return res.request([dsId, dcId, signOnId], self._getRestOptions('DELETE')); } }; } }); var _static = { getInstance: function getInstance() { if (!_singletonInstance) { _singletonInstance = new ApiService(); } return _singletonInstance; } }; return _static.getInstance(); });