"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: admin * Copyright IBM Corp. 2017, 2018 * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', 'doT', 'bi/content_apps/PropertiesTab', 'bi/admin/nls/StringResource', 'bi/commons/ui/dialogs/ConfirmationDialog', 'bi/admin/datasource/services/ConnectionUtils', 'text!bi/admin/datasource/templates/TestResultTemplate.html', 'react-dom', 'react', 'mobx', 'ba-react-admin/ba-react-admin.min', 'bi/commons/utils/Utils', 'baglass/core-client/js/core-client/utils/BidiUtil', 'bi/commons/ui/content/dialog/SignOnDialog'], function (_, dot, PropertiesTab, StringResource, ConfirmationDialog, ConnectionUtils, TestResultTemplate, ReactDOM, React, mobx, AdminReact, Utils, BidiUtil, SignOnDialog) { 'use strict'; //NOSONAR: meant to be strict var PropertiesConnectionTab = PropertiesTab.extend({ init: function init(options) { PropertiesConnectionTab.inherited('init', this, arguments); _.extend(this, options); this.removedSignons = []; if (this.objectInfo.isNew) { this.hasChanged = true; } if (!this.objectInfo.connId) { this.objectInfo.connId = this.objectInfo.id; } this._setValidateInitialWhiteSpaceInput(); this.selectedSignonFromReact = null; this.myDSSignon = null; }, _setValidateInitialWhiteSpaceInput: function _setValidateInitialWhiteSpaceInput() { var elem = this.slideout.$el.find(".propertiesBannerLabel.v_defaultName"); elem.on("keyup", function () { if (elem.val() < 1) { elem.val(elem.val().trim()); } if (elem[0].value.length === 0) { elem.on("keydown", function (event) { if (event.which === 32) { event.preventDefault(); } }); } else { elem.off("keydown"); } }); this.slideout.$el.end(); }, _getContent: function _getContent() { return ConnectionUtils.getSignons(this.glassContext.getCoreSvc('.Ajax'), this.objectInfo.dataSourceId, this.objectInfo.id); }, _getConnectionParamsItems: function _getConnectionParamsItems(items) { items.push({ 'name': 'connectionInfo', 'label': StringResource.get('connectionInformation'), 'value': this.objectInfo.permissions && this.objectInfo.permissions.indexOf("write") === -1 ? StringResource.get('viewConnectionInformation') : StringResource.get('editConnectionInformation'), 'type': 'SingleLineValue', 'ellipses': true, 'editCallback': this._openConnectionInformation.bind(this) }); items.push({ 'type': 'Separator' }); }, _resetConnectionInformationSlideout: function _resetConnectionInformationSlideout() { if (this.connectionEditorSlideout) { return this.connectionEditorSlideout.hide({ force: true }); } else { return Promise.resolve(); } }, _openConnectionInformation: function _openConnectionInformation() { if (this.connectionEditorSlideout) { //the slide out is already opened return; } this.hasChanged = this.objectInfo.permissions && this.objectInfo.permissions.indexOf("write") !== -1; this.connectionEditorSlideout = this.glassContext.appController.showSlideOut({ 'pinning': { 'isPinned': true, 'display': false }, 'parent': this.slideout, 'width': "550px", 'content': { 'showGobackButton': false, 'module': this.parseResult.metadata.module, 'parentView': this, 'objectInfo': this.objectInfo, 'parseInfo': this.parseResult, 'glassContext': this.glassContext } }); this.connectionEditorSlideout.onHide = function () { delete this.connectionEditorSlideout; }.bind(this); }, _openSignonPropertiesSlideout: function _openSignonPropertiesSlideout(signon, signonList) { this.signonListFromReact = mobx.toJS(signonList); this._resetConnectionInformationSlideout().then(function () { var selectedTab = "bi/admin/common/GeneralPropertiesTab"; if (signon.isNew) { selectedTab = "bi/admin/datasource/ui/PropertiesSignonTab"; } var slideout = this.glassContext.appController.showSlideOut({ 'parent': this.slideout, 'width': "450px", 'content': { 'module': 'bi/admin/common/PropertiesPageView', 'parentView': this, 'listPane': this, 'objectInfo': signon, 'glassContext': this._glassContext, 'selectedTabModule': selectedTab, 'tabChanged': function () { var signonUICon = slideout.contentView.getPropertyUIControl(); var modProperties = signonUICon.getModifiedProperties(); _.extend(signon, modProperties); if (modProperties.defaultName) { this._modifySignon(signon); } }.bind(this) }, 'onHide': function () { var signonUICon = slideout.contentView.getPropertyUIControl(); var modProperties = signonUICon.getModifiedProperties(); delete modProperties.credentialsEx; _.extend(signon, modProperties); this._modifySignon(signon); }.bind(this) }); slideout.$el.on("credentialChanged", this.updateSignon.bind(this, slideout)); }.bind(this)); }, _confirmUnsavedChanged: function _confirmUnsavedChanged() { var oDialog = this._newConfirmationDialog('confirmApplyChanges', StringResource.get('confirmChange'), StringResource.get('datasourceConfirmApply')); oDialog.confirm(this._saveConnection.bind(this)); }, _newConfirmationDialog: function _newConfirmationDialog(sType, sTitle, sMessage) { return new ConfirmationDialog(sType, sTitle, sMessage); }, onClose: function onClose() { if (this.hasChanged) { this.hasChanged = false; this._confirmUnsavedChanged(); } ReactDOM.unmountComponentAtNode(this.$el.find('#myReactElement')[0]); return this._oPropertyUIControl.onClose(); }, updateConnectionString: function updateConnectionString() { if (this.parseResult.metadata.code === 'EXTCATALOG') { delete this.parseResult.jdbcURL; } else if (this.parseResult.metadata.categoryId === 'jdbc' && (this.parseResult.jdbcURL === '' || _.isUndefined(this.parseResult.jdbcURL))) { this.parseResult.jdbcURL = this.parseResult.metadata.urlTemplate; } var connString = ConnectionUtils.buildConnection(this.parseResult, this.objectInfo); this.objectInfo.connectionString = ConnectionUtils.getUpdatedConnectionString(this.parseResult, connString); }, _modifySignon: function _modifySignon(localSignOn) { var tempSignonArray = []; var tempSignonObject = {}; var indexToChange; this.signonListFromReact.forEach(function (item, index) { if (item.id === localSignOn.id) { tempSignonObject = { label: localSignOn.defaultName, value: index, id: localSignOn.id }; indexToChange = index; } tempSignonArray.push(this.signonListFromReact[index]); }.bind(this)); if (_.isEmpty(tempSignonObject)) { tempSignonObject = { label: localSignOn.defaultName, value: tempSignonArray.length, id: localSignOn.id }; } if (indexToChange > -1) { tempSignonArray.splice(indexToChange, 1, tempSignonObject); } else { tempSignonArray.push(tempSignonObject); } this.myDSSignon.updateSignons(tempSignonArray); this.myDSSignon.updateSelectedSignon(tempSignonObject); }, updateSignon: function updateSignon(signonSlideout, evt, localSignOn) { var signonUICon = signonSlideout.contentView.getPropertyUIControl(); var modProperties = signonUICon.getModifiedProperties(); if (modProperties.credentialUserName !== localSignOn.newUser) { localSignOn.credentialsEx.username = localSignOn.newUser; } else if (modProperties.credentialUserName) { localSignOn.credentialsEx.username = modProperties.credentialUserName; delete modProperties.credentialUserName; } if (modProperties.credentialPassword !== localSignOn.newPassword) { localSignOn.credentialsEx.password = localSignOn.newPassword; } else if (modProperties.credentialPassword) { localSignOn.credentialsEx.password = modProperties.credentialPassword; delete modProperties.credentialPassword; } delete localSignOn.newUser; delete localSignOn.newPassword; _.extend(localSignOn, modProperties); this._modifySignon(localSignOn); localSignOn.credentialsEx.updatedSignon = 'updated'; localSignOn.updated = true; }, _getTestItems: function _getTestItems(items) { items.push({ 'type': 'Separator' }); items.push({ 'name': 'testGroup', 'type': 'SingleLineLinks', 'items': [{ 'align': 'left', 'items': [{ 'type': 'icon', 'svgIcon': 'common-run', 'clickCallback': this._executeTest.bind(this), 'iconTooltip': StringResource.get('test'), 'role': 'button', 'class': 'testButton' }] }, { 'align': 'right', 'items': [] }] }); items.push({ 'type': 'Separator' }); }, _getFooter: function _getFooter(items) { items.push({ 'type': 'Footer', 'items': [{ 'type': 'Button', 'label': StringResource.get('dataServerSave'), 'primary': true, 'onSelect': this._saveConnection.bind(this) }] }); }, _saveConnection_error: function _saveConnection_error(key) { return this.glassContext.appController.showToast(StringResource.get(key), { 'type': 'error', 'onHidden': function (event) { this.slideout.contentView.getPropertyUIControl().getProperty('defaultName').value = ''; this.setFocus(); }.bind(this) }); }, _saveConnection: function _saveConnection() { var modProperties = this.slideout.contentView.getPropertyUIControl().getModifiedProperties(); _.extend(this.objectInfo, modProperties); this.hasChanged = false; this.updateConnectionString(); this.slideout.connectionSignons = this.connectionSignons; this.slideout.removedSignons = this.removedSignons; delete this.objectInfo.searchPath; delete this.objectInfo.owner; delete this.objectInfo._meta; delete this.objectInfo.ancestors; delete this.objectInfo.version; if (this.objectInfo.defaultName === '') { return this._saveConnection_error('emptyConnectionName'); } if (this.objectInfo.isNew && _.isUndefined(modProperties.defaultName)) { return this._saveConnection_error('unmodifiedConnectionName'); } return this._save().then(function () { return ConnectionUtils.getSignons(this.glassContext.getCoreSvc('.Ajax'), this.objectInfo.dataSourceId, this.objectInfo.id).then(function (signons) { this.connectionSignons = signons; this._updateDatasourceList(); }.bind(this)); }.bind(this)); }, _updateDatasourceList: function _updateDatasourceList() { $(this.slideout.$el).trigger('com.ibm.cognos.bi.admin.refreshDataServerList'); }, _save: function _save() { if (this.objectInfo.gatewayDestination && this.objectInfo.gatewayDestination[0]) { this.objectInfo.gatewayDestination = [{ type: 'gatewayDestination', searchPath: "storeID('" + this.objectInfo.gatewayDestination[0].id + "')" }]; } if (this.objectInfo.isNew) { return this._saveNewConnection(); } else { return this._saveExistingConnection(); } }, _saveNewConnection: function _saveNewConnection() { var savePromise; var isNewConnection = false; if (this.objectInfo.newConnection) { isNewConnection = true; delete this.objectInfo.newConnection; savePromise = ConnectionUtils.createNewConnection(this.glassContext.getCoreSvc('.Ajax'), this.objectInfo, this.connectionSignons); } else { savePromise = ConnectionUtils.createNewDataSource(this.glassContext.getCoreSvc('.Ajax'), this.objectInfo, this.connectionSignons); } savePromise.then(function () { var sText = StringResource.get('datasourceConnectionToastUpdateMsg', { 'name': this.objectInfo.defaultName }); this.glassContext.appController.showToast(sText, { type: 'success' }); this.slideout.contentView.selectedTabModule = "bi/admin/datasource/ui/PropertiesConnectionTab"; if (this.$el.find('#myReactElement')[0]) { ReactDOM.unmountComponentAtNode(this.$el.find('#myReactElement')[0]); } delete this.objectInfo.isNew; this.slideout.contentView.reload(); if (this.parseResult && this.parseResult.gwStringUpdated) { delete this.parseResult.gwStringUpdated; } }.bind(this)).catch(function (error) { if (isNewConnection) { this.objectInfo.newConnection = true; } this._errorResponseCheck(error); }.bind(this)); return savePromise; }, setFocus: function setFocus() { var bannerLabel = this.slideout.$el.find(".propertiesBannerLabel.v_defaultName"); if (bannerLabel.length > 0) { bannerLabel[0].focus(); } }, _saveExistingConnection: function _saveExistingConnection() { return ConnectionUtils.saveConnection(this.glassContext.getCoreSvc('.Ajax'), this.objectInfo, this.connectionSignons, this.removedSignons).then(function () { var sText = StringResource.get('datasourceConnectionToastUpdateMsg', { 'name': this.objectInfo.defaultName }); this.glassContext.appController.showToast(sText, { type: 'success' }); this.slideout.contentView.selectedTabModule = "bi/admin/datasource/ui/PropertiesConnectionTab"; if (this.$el.find('#myReactElement')[0]) { ReactDOM.unmountComponentAtNode(this.$el.find('#myReactElement')[0]); } this.slideout.contentView.reload(); this.connectionSignons.forEach(function (signon) { delete signon.isNew; }); if (this.parseResult && this.parseResult.gwStringUpdated) { delete this.parseResult.gwStringUpdated; } if (this.parseResult && this.parseResult.apiToken) { delete this.parseResult.apiToken; } this.removedSignons = []; }.bind(this)).catch(this._errorResponseCheck.bind(this)); }, _errorResponseCheck: function _errorResponseCheck(error) { var errorCode; if (error.jqXHR && error.jqXHR.responseJSON) { if (error.jqXHR.responseJSON.errors && error.jqXHR.responseJSON.errors[0].errorCode) { errorCode = error.jqXHR.responseJSON.errors[0].errorCode; } else if (error.jqXHR.responseJSON.errorCode) { errorCode = error.jqXHR.responseJSON.errorCode; } else { errorCode = StringResource.get('internalErrorMsg'); } } else { errorCode = StringResource.get('internalErrorMsg'); } var toastMsg; if (errorCode.includes("cmDuplicateName")) { toastMsg = StringResource.get('duplicateDSExists'); } else { toastMsg = StringResource.get('dataServerError', { errorCode: errorCode }); } this.glassContext.appController.showToast(toastMsg, { 'type': 'error', 'onHidden': function (event) { this.setFocus(); }.bind(this) }); }, _renderProperties: function _renderProperties() { var items = []; this._getConnectionParamsItems(items); this._getTestItems(items); if (this.objectInfo.permissions && this.objectInfo.permissions.indexOf("write") !== -1) { this._getFooter(items); } return this.renderPropertyUIControl({ 'el': this.$el, 'glassContext': this.glassContext, 'items': items }).then(function () { $(this.$el.find('.separator')[0]).after($('