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 { DEFAULT_JNDI_DATA, DEFAULT_JDBC_POOL, 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: () => { this.testConnection(); }, } render() { const formData = BI.clone(this.getFormData()); if (this.model.isCopy) { formData.connectionName = this.getConnectionName(formData.connectionName); this.isEdit = false; } return { type: this.getFormType(), formData, ref: (_ref: any) => { this.form = _ref; }, listeners: [{ eventName: 'EVENT_TEST_CONNECTION', action: () => { this.testConnection(); }, }], }; } beforeDestroy() { if (this.isEdit) { this.store.shutdownConnectionStatus(this.connectionName); } } 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(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}`; } private testConnection() { const formValue = this.form.getSubmitValue(); if (this.isEdit || this.model.isCopy) { formValue.connectionId = this.connectionName; } this.store.testConnection(formValue).then(re => { this.form.setSchemas(re); }); } }