|
|
|
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, 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';
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
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 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) {
|
|
|
|
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) > 200) {
|
|
|
|
this.setFromError(BI.i18nText('Dec-Dcm_Connection_Cannot_Too_Lang'));
|
|
|
|
|
|
|
|
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')) {
|
|
|
|
const defaultName = name.replace(/\d+$/, '');
|
|
|
|
const connections = this.model.connections.filter(item => item.connectionName.startsWith(defaultName));
|
|
|
|
if (connections.length === 0) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BI.Func.createDistinctName(connections.map(item => item.connectionName), defaultName);
|
|
|
|
}
|
|
|
|
|
|
|
|
private testConnection() {
|
|
|
|
const formValue = this.form.getSubmitValue();
|
|
|
|
if (!formValue.connectionName) {
|
|
|
|
this.setFromError(BI.i18nText('Dec-Dcm_Connection_ConnectionName_Cannt_Null'));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (getChartLength(formValue.connectionName) > 200) {
|
|
|
|
this.setFromError(BI.i18nText('Dec-Dcm_Connection_Cannot_Too_Lang'));
|
|
|
|
|
|
|
|
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 === 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|