import { model, Model } from '@core/core'; import { ConnectionJDBC } from '../../../../crud/crud.typings'; import { getJdbcDatabaseType } from '../../../../app.service'; import { ApiFactory } from '../../../../crud/apiFactory'; const api = new ApiFactory().create(); @model() export class DriverSelectorModel extends Model { static xtype = 'dec.dcm.model.maintain.form.jdbc.driver_selector'; state = () => { const defaultDrivers = this.getDrivers(); const [driverSource, selectedDriver] = this.resolveSelectedDriverType(); return { defaultDrivers, driverSource, selectedDriverType: driverSource === '' ? 'default' : 'custom', customDrivers: [], defaultDriver: { driver: driverSource === '' ? selectedDriver : '', }, customDriver: { driver: driverSource !== '' ? selectedDriver : '', text: driverSource !== '' ? `${this.options.driver} (${driverSource})` : '', }, }; }; computed = { driverClassItems: () => this.model.customDrivers.map(driver => { return { text: `${driver.driverClass} (${driver.name})`, value: driver.driverClass, }; }), driverTypeComboValue: () => this.model.driverSource === '' ? 'default' : 'custom', driverManageEntryVisible: () => this.model.selectedDriverType === 'custom' && BI.Services.getService('dec.service.global').isAdmin(), }; actions = { initDriverClassList: cb => { api.getSimpleDriverList().then(res => { this.model.customDrivers = res.data; cb(); }); }, changeDefaultDriver: driver => { this.model.defaultDriver.driver = driver; this.model.driverSource = ''; }, changeCustomDriver: driver => { this.model.customDriver.driver = driver; this.model.customDrivers.some(customDriver => { if (customDriver.driverClass === driver) { this.model.driverSource = customDriver.name; this.model.customDriver.text = `${driver} (${customDriver.name})`; return true; } return false; }); }, changeSelectedDriverType: driverTypeComboValue => { this.model.selectedDriverType = driverTypeComboValue; this.model.driverSource = driverTypeComboValue === 'default' ? '' : this.model.driverSource; }, changeDriverSource: driverTypeComboValue => { this.model.driverSource = driverTypeComboValue === 'default' ? '' : this.model.driverSource; }, }; private resolveSelectedDriverType = () => { if (BI.isNotEmptyString(this.options.driverSource)) { return [this.options.driverSource, this.options.driver]; } return [this.options.driverSource, this.options.driver]; }; private getDrivers = () => { const connectionData = this.options.connectionData as ConnectionJDBC; const connectionType = getJdbcDatabaseType(connectionData.database, connectionData.driver); const drivers = connectionType.drivers ? connectionType.drivers.map(item => { return { text: item, value: item, }; }) : [{ text: connectionType.driver, value: connectionType.driver, }]; if (!drivers.some(item => item.text === connectionData.driver)) { return [ { text: connectionData.driver, value: connectionData.driver, }, ...drivers, ]; } return drivers; }; }