import { shortcut, store } from '@core/core'; import { MaintainFormModelXtype, MaintainFormModel } from './form.model'; import { FormJdbc, FormJdbcXtype } from './components/form.jdbc'; import { FormJndiXtype } from './components/form.jndi'; import { FormPluginXtype } from './components/form.plugin'; import { connectionType } from '@constants/env'; import { ConnectionJDBC, Connection } from 'src/modules/crud/crud.typings'; import { TestStatusXtype, EVENT_RELOAD, EVENT_CLOSE } from 'src/modules/components/test_status/test_status'; import { DEFAULT_JNDI_DATA, DEFAULT_JDBC_POOL, DATA_BASE_DRIVER_LINK, DATEBASE_FILTER_TYPE } from '@constants/constant'; import { getJdbcDatabaseType } from 'src/modules/app.service'; export const MaintainFormXtype = 'dec.dcm.maintain.form'; @shortcut(MaintainFormXtype) @store(MaintainFormModelXtype) export class MaintainForm extends BI.Widget { props = { connectionType: '', } isEdit = false; connectionName = ''; model: MaintainFormModel['model']; store: MaintainFormModel['store']; form: FormJdbc; testStatus: any; watch = { saveEvent: () => { if (!this.testValue()) { return; } if (this.connectionName && !this.model.isCopy) { this.store.updateConnection(this.connectionName, this.form.getSubmitValue()); } else { const form = this.form.getSubmitValue(); form.connectionId = this.connectionName; this.store.addConnection(form); } }, testEvent: () => { const value = this.form.getSubmitValue(); if (!value.connectionName) { BI.Msg.toast(BI.i18nText('Dec-Dcm_Connection_ConnectionName_Cannt_Null'), { level: 'error', }); return false; } const id = BI.UUID(); const testConnection = () => { const formValue = this.form.getSubmitValue(); if (this.isEdit || this.model.isCopy) { formValue.connectionId = this.connectionName; } this.store.testConnection(formValue).then(re => { if (re && re.errorCode) { // 判断是否是缺少驱动,如果缺少驱动则显示下载驱动的连接 if (this.store.isDriverError(re.errorCode)) { if (formValue.connectionType === connectionType.JDBC) { const driver = (formValue.connectionData as ConnectionJDBC).driver; const databaseType = (formValue.connectionData as ConnectionJDBC).database; const databaseLink = BI.get(DATA_BASE_DRIVER_LINK.find(item => item.databaseType === databaseType), 'link'); this.testStatus.setFail(re.errorMsg, driver, Dec.system[DecCst.Hyperlink.DECISION_HYPERLINK_CONFIG][databaseLink]); } else { this.testStatus.setFail(re.errorMsg); } } else { this.testStatus.setFail(re.errorMsg); } } else if (re.data) { this.testStatus.setSuccess(); if (re.data.length > 0) { this.form.setSchemas && this.form.setSchemas(re.data); } setTimeout(() => { BI.Maskers.remove(id); }, 1000 * 2); } else { BI.Msg.toast(BI.i18nText('Dec-Dcm_Connection_Error'), { level: 'error', }); BI.Maskers.remove(id); } }); }; BI.Maskers.create(id, null, { render: { type: TestStatusXtype, loadingText: BI.i18nText('Dec-Dcm_Connection_Testing'), loadingCls: 'upload-loading-icon', successText: BI.i18nText('Dec-Dcm_Connection_Test_Success'), successCls: 'upload-success-icon', failText: BI.i18nText('Dec-Dcm_Connection_Test_Fail', name), failCls: 'upload-fail-icon', retryText: BI.i18nText('Dec-Dcm_Connection_ReConnect'), ref: (_ref: any) => { this.testStatus = _ref; }, listeners: [ { eventName: EVENT_RELOAD, action: () => { this.testStatus.setLoading(); testConnection(); }, }, { eventName: EVENT_CLOSE, action: () => { BI.Maskers.remove(id); }, }, ], }, }); BI.Maskers.show(id); testConnection(); }, } render() { const formData = BI.clone(this.getFormData()); if (this.model.isCopy) { formData.connectionName = `${formData.connectionName}-${BI.i18nText('Dec-Dcm_Copy')}`; this.isEdit = false; } return { type: this.getFormType(), formData, ref: (_ref: any) => { this.form = _ref; }, }; } private getFormType() { switch (this.options.connectionType) { case connectionType.JDBC: return FormJdbcXtype; case connectionType.JNDI: return FormJndiXtype; case DATEBASE_FILTER_TYPE.OTHER: return FormJdbcXtype; default: return FormPluginXtype; } } private getFormData():Connection { switch (this.options.connectionType) { case connectionType.JDBC: return this.getJdbcConnection(); case connectionType.JNDI: return this.getJndiConnection(); case DATEBASE_FILTER_TYPE.OTHER: return this.getJdbcConnection(); default: return this.getPluginConnection(); } } private getJdbcConnection():Connection { const connectionName = this.getConnectionName(); let editConnection: Connection; let connectionData: ConnectionJDBC; if (this.model.datebaseTypeSelected) { connectionData = { driver: this.model.datebaseTypeSelectedOne.driver, url: this.model.datebaseTypeSelectedOne.url, database: this.model.datebaseTypeSelectedOne.databaseType, connectionName, connectionPoolAttr: DEFAULT_JDBC_POOL, port:'', host: 'localhost', }; editConnection = { connectionId: '', connectionData, connectionType: connectionType.JDBC, connectionName, }; return editConnection; } this.isEdit = true; this.connectionName = this.model.connectionSelectedOne.connectionName; const connection = BI.clone(this.model.connectionSelectedOne); const { database, driver } = connection.connectionData as ConnectionJDBC; (connection.connectionData as ConnectionJDBC).database = getJdbcDatabaseType(database, driver).databaseType; return connection; } private getJndiConnection():Connection { if (this.model.datebaseTypeSelected) { return { connectionId: '', connectionType: connectionType.JNDI, connectionName: this.getConnectionName(), connectionData: DEFAULT_JNDI_DATA, }; } this.connectionName = this.model.connectionSelectedOne.connectionName; this.isEdit = true; return this.model.connectionSelectedOne; } private getPluginConnection():Connection { if (!this.model.datebaseTypeSelected) { this.connectionName = this.model.connectionSelectedOne.connectionName; this.isEdit = true; return this.model.connectionSelectedOne; } return { connectionId: '', connectionType: this.model.datebaseTypeSelectedOne.databaseType, connectionName: this.getConnectionName(), connectionData: '', }; } private testValue():boolean { const value = this.form.getSubmitValue(); if (!value.connectionName) { BI.Msg.toast(BI.i18nText('Dec-Dcm_Connection_ConnectionName_Cannt_Null'), { level: 'error', }); return false; } if (this.connectionName !== value.connectionName) { const hasNamed = this.model.connections.some(item => item.connectionName === value.connectionName); if (hasNamed) { BI.Msg.toast(BI.i18nText('Dec-Dcm_Connection_Is_Existence'), { level: 'error', }); return false; } } return true; } private getConnectionName() { const name = BI.i18nText('Dec-Dcm_Data_Connections'); const connections = this.model.connections.filter(item => item.connectionName.startsWith(name)); if (connections.length === 0) { return name; } let index = 0; connections.forEach(item => { const num = parseFloat(item.connectionName.replace(name, '')); if (num > index) { index = num; } }); return `${name}${index + 1}`; } }