You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
291 lines
10 KiB
291 lines
10 KiB
import { shortcut, store } from '@core/core'; |
|
import { MaintainFormModel } from './form.model'; |
|
import { FormJdbc } from './components/form.jdbc'; |
|
import { FormJndi } from './components/form.jndi'; |
|
import { FormPlugin } from './components/form.plugin'; |
|
import { connectionType, errorCode } from '@constants/env'; |
|
import { ConnectionJDBC, Connection, ResultType } from 'src/modules/crud/crud.typings'; |
|
import { DEFAULT_JNDI_DATA, DEFAULT_JDBC_POOL, DATEBASE_FILTER_TYPE } from '@constants/constant'; |
|
import { getJdbcDatabaseType, getChartLength } from '../../../app.service'; |
|
import { NAME_MAX_LENGTH } from '../../../app.constant'; |
|
|
|
@shortcut() |
|
@store(MaintainFormModel) |
|
export class MaintainForm extends BI.Widget { |
|
static xtype = 'dec.dcm.maintain.form'; |
|
|
|
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) { |
|
const value = this.form.getSubmitValue(); |
|
(value.connectionData as ConnectionJDBC).creator = BI.get(this.getFormData(), 'creator'); |
|
// DEC-10155 为了适配插件的数据连接,在外层也加一个creator字段 |
|
value.creator = BI.get(this.getFormData(), 'creator'); |
|
this.store.updateConnection(this.connectionName, value).then(result => { |
|
if (result.errorCode) { |
|
if (result.errorCode === DecCst.ErrorCode.NO_IP_AUTHORIZED) { |
|
BI.Msg.toast(BI.i18nText("Dec-Basic_Save_Fail"), { |
|
level: "error", |
|
}); |
|
} |
|
this.showError(result); |
|
|
|
return; |
|
} |
|
this.store.goFirstPage(); |
|
}); |
|
} else { |
|
const form = this.form.getSubmitValue(); |
|
form.connectionId = this.connectionName; |
|
// DEC-10155 为了适配插件的数据连接,在外层也加一个creator字段 |
|
form.creator = Dec ? Dec.personal.username : ''; |
|
this.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 FormJdbc.xtype; |
|
case connectionType.JNDI: |
|
return FormJndi.xtype; |
|
case DATEBASE_FILTER_TYPE.OTHER: |
|
return FormJdbc.xtype; |
|
default: |
|
return FormPlugin.xtype; |
|
} |
|
} |
|
|
|
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) { |
|
const { databaseType: database, fetchSize } = this.model.datebaseTypeSelectedOne; |
|
|
|
connectionData = { |
|
...this.model.datebaseTypeSelectedOne, |
|
database, |
|
connectionName, |
|
connectionPoolAttr: DEFAULT_JDBC_POOL, |
|
port: '', |
|
host: 'localhost', |
|
fetchSize: fetchSize ?? -1, |
|
identity: BI.UUID(), |
|
}; |
|
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); |
|
connectionData = connection.connectionData as ConnectionJDBC; |
|
const { database, driver } = connectionData; |
|
connectionData.database = getJdbcDatabaseType(database, driver).databaseType; |
|
if (this.model.isCopy) { |
|
connectionData.identity = BI.UUID(); |
|
} |
|
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) { |
|
this.setFromError(BI.i18nText('Dec-Dcm_Connection_ConnectionName_Cannt_Null')); |
|
|
|
return false; |
|
} |
|
if (this.connectionName !== value.connectionName) { |
|
const hasNamed = this.model.connections.some(item => item.connectionName === value.connectionName); |
|
if (hasNamed) { |
|
this.setFromError(BI.i18nText('Dec-Dcm_Connection_Is_Existence')); |
|
|
|
return false; |
|
} |
|
} |
|
if (getChartLength(value.connectionName) > NAME_MAX_LENGTH) { |
|
this.setFromError(BI.i18nText('Dec-Dcm_Connection_Cannot_Too_Lang', NAME_MAX_LENGTH)); |
|
|
|
return false; |
|
} |
|
|
|
if (this.form.validation && !this.form.validation()) { |
|
return false; |
|
} |
|
|
|
return true; |
|
} |
|
|
|
private showError(result: ResultType) { |
|
result.errorCode === errorCode.DUPLICATE_NAMES ? |
|
this.setFromError(BI.i18nText(result.errorMsg)) : |
|
BI.Msg.toast(BI.i18nText(result.errorMsg), { |
|
level: 'error', |
|
}); |
|
|
|
return; |
|
} |
|
|
|
private setFromError(errMsg: string) { |
|
this.form.setError ? this.form.setError(errMsg) : null; |
|
} |
|
|
|
private getConnectionName(name = BI.i18nText('Dec-Dcm_Data_Connections')) { |
|
return BI.Func.createDistinctName(this.model.connections.map(item => item.connectionName), name); |
|
} |
|
|
|
private testConnection() { |
|
const formValue = this.form.getSubmitValue(); |
|
if (!formValue.connectionName) { |
|
this.setFromError(BI.i18nText('Dec-Dcm_Connection_ConnectionName_Cannt_Null')); |
|
|
|
return false; |
|
} |
|
if (getChartLength(formValue.connectionName) > NAME_MAX_LENGTH) { |
|
this.setFromError(BI.i18nText('Dec-Dcm_Connection_Cannot_Too_Lang', NAME_MAX_LENGTH)); |
|
|
|
return false; |
|
} |
|
|
|
if (this.form.validation && !this.form.validation()) { |
|
return false; |
|
} |
|
|
|
if (this.isEdit || this.model.isCopy) { |
|
formValue.connectionId = this.connectionName; |
|
} |
|
this.store.testConnection(formValue).then(re => { |
|
this.form.setSchemas(re); |
|
}); |
|
} |
|
|
|
private addConnection(form: Connection) { |
|
this.store.addConnection(form).then(result => { |
|
if (result.errorCode) { |
|
if (result.errorCode === DecCst.ErrorCode.NO_IP_AUTHORIZED) { |
|
BI.Msg.toast(BI.i18nText("Dec-Basic_Save_Fail"), { |
|
level: "error", |
|
}); |
|
} |
|
if (result.errorCode === errorCode.DUPLICATE_NAMES) { |
|
if (form.connectionType !== connectionType.JDBC && form.connectionType !== connectionType.JNDI) { |
|
// 如果不是jdbc或jndi,即如果是插件,名称重复的时候需要修改名字重新提交给后台 |
|
const newName = `${form.connectionName}${Math.floor(Math.random() * 10)}`; |
|
form.connectionName = newName; |
|
if (!this.model.isCopy) { |
|
form.connectionId = newName; |
|
} |
|
this.addConnection(form); |
|
} else { |
|
this.setFromError(BI.i18nText(result.errorMsg)); |
|
} |
|
} else { |
|
BI.Msg.toast(BI.i18nText(result.errorMsg), { |
|
level: 'error', |
|
}); |
|
} |
|
|
|
return; |
|
} |
|
this.store.goFirstPage(); |
|
this.store.setIsCopy(false); |
|
}); |
|
} |
|
}
|
|
|