From b7fc16797099e586952a0c4a41b2d90c93273b0b Mon Sep 17 00:00:00 2001 From: "Destiny.Lin" Date: Fri, 6 Sep 2024 15:38:32 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-133442=20=E3=80=90fr-fbp=E5=9B=9E?= =?UTF-8?q?=E5=BD=92=E3=80=91=E3=80=90=E6=95=B0=E6=8D=AE=E9=9B=86=E3=80=91?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=BF=9E=E6=8E=A5=E9=A9=B1=E5=8A=A8=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E8=87=AA=E5=AE=9A=E4=B9=89=EF=BC=8C=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E5=90=8E=E4=BA=8C=E6=AC=A1=E7=BC=96=E8=BE=91=EF=BC=8C=E4=BB=8D?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E9=BB=98=E8=AE=A4=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../actions/server/GlobalTableDataAction.java | 11 +- .../data/datapane/TableDataPaneListPane.java | 2 +- .../data/datapane/TreeTableDataComboBox.java | 10 +- .../connect/ConnectionComboBoxPanel.java | 10 +- .../datapane/connect/ConnectionListPane.java | 14 +- .../connect/LocalConnectionSource.java | 19 ++ .../preview/ConnectionInfoBeanHelper.java | 140 +++++++++++++ .../datapane/preview/TableDataBeanHelper.java | 198 ++++++++++++++++++ .../data/tabledata/LocalTableDataSource.java | 34 +++ .../tabledata/wrapper/TableDataFactory.java | 16 +- 10 files changed, 413 insertions(+), 41 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/design/data/datapane/preview/TableDataBeanHelper.java diff --git a/designer-base/src/main/java/com/fr/design/actions/server/GlobalTableDataAction.java b/designer-base/src/main/java/com/fr/design/actions/server/GlobalTableDataAction.java index 3778e49d7c..71487b5649 100644 --- a/designer-base/src/main/java/com/fr/design/actions/server/GlobalTableDataAction.java +++ b/designer-base/src/main/java/com/fr/design/actions/server/GlobalTableDataAction.java @@ -8,7 +8,8 @@ import com.fr.design.DesignModelAdapter; import com.fr.design.actions.UpdateAction; import com.fr.design.data.DesignTableDataManager; import com.fr.design.data.datapane.TableDataTreePane; -import com.fr.workspace.server.repository.tabledata.TableDataBeanHelper; +import com.fr.design.data.datapane.preview.ConnectionInfoBeanHelper; +import com.fr.design.data.datapane.preview.TableDataBeanHelper; import com.fr.design.data.tabledata.ResponseDataSourceChange; import com.fr.design.data.tabledata.tabledatapane.TableDataManagerPane; import com.fr.design.dialog.BasicDialog; @@ -24,10 +25,8 @@ import com.fr.esd.event.DSMapping; import com.fr.esd.event.DsNameTarget; import com.fr.esd.event.StrategyEventsNotifier; -import com.fr.file.TableDataConfig; import com.fr.report.LockItem; import com.fr.security.encryption.transmission.TransmissionEncryptionManager; -import com.fr.workspace.server.entity.connection.ConnectionBean; import com.fr.workspace.server.repository.connection.ConnectionRepository; import com.fr.workspace.server.repository.tabledata.TableDataRepository; @@ -85,11 +84,7 @@ public class GlobalTableDataAction extends UpdateAction implements ResponseDataS // 锁定成功,执行后续操作 final DesignerFrame designerFrame = DesignerContext.getDesignerFrame(); final List beans = TableDataRepository.getInstance().getAllTableData(); - final List connectionInfoBeans = ConnectionRepository.getInstance().getAll(); - final Map connectionMap = new HashMap<>(); - for (ConnectionBean bean : connectionInfoBeans) { - connectionMap.put(bean.getName(), bean.getConnection()); - } + final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); final Map tableDataMap = new HashMap<>(); try { TransmissionEncryptionManager.setTransEncryptionLevel(1); diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/TableDataPaneListPane.java b/designer-base/src/main/java/com/fr/design/data/datapane/TableDataPaneListPane.java index f6d4ce7658..f5fe0edf89 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/TableDataPaneListPane.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/TableDataPaneListPane.java @@ -10,7 +10,7 @@ import com.fr.decision.webservice.bean.dataset.ServerDataSetBean; import com.fr.design.data.BasicTableDataUtils; import com.fr.design.data.DesignTableDataManager; import com.fr.design.data.MapCompareUtils; -import com.fr.workspace.server.repository.tabledata.TableDataBeanHelper; +import com.fr.design.data.datapane.preview.TableDataBeanHelper; import com.fr.design.dialog.FineJOptionPane; import com.fr.design.gui.controlpane.JListControlPane; import com.fr.design.gui.controlpane.NameableCreator; diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/TreeTableDataComboBox.java b/designer-base/src/main/java/com/fr/design/data/datapane/TreeTableDataComboBox.java index b0f9a12551..a19de0d602 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/TreeTableDataComboBox.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/TreeTableDataComboBox.java @@ -6,7 +6,8 @@ import com.fr.data.impl.Connection; import com.fr.data.impl.RecursionTableData; import com.fr.decision.webservice.bean.dataset.ServerDataSetBean; import com.fr.design.data.DesignTableDataManager; -import com.fr.workspace.server.repository.tabledata.TableDataBeanHelper; +import com.fr.design.data.datapane.preview.ConnectionInfoBeanHelper; +import com.fr.design.data.datapane.preview.TableDataBeanHelper; import com.fr.design.data.tabledata.wrapper.ServerTableDataWrapper; import com.fr.design.data.tabledata.wrapper.TableDataWrapper; import com.fr.design.data.tabledata.wrapper.TemplateTableDataWrapper; @@ -14,7 +15,6 @@ import com.fr.design.gui.icombobox.UIComboBox; import com.fr.design.gui.icombobox.UIComboBoxRenderer; import com.fr.security.encryption.transmission.TransmissionEncryptionManager; import com.fr.stable.StringUtils; -import com.fr.workspace.server.entity.connection.ConnectionBean; import com.fr.workspace.server.repository.connection.ConnectionRepository; import com.fr.workspace.server.repository.tabledata.TableDataRepository; @@ -71,11 +71,7 @@ public class TreeTableDataComboBox extends UIComboBox { // 全局数据集 List beans = TableDataRepository.getInstance().getAllTableData(); Map map = new HashMap<>(); - final List connectionInfoBeans = ConnectionRepository.getInstance().getAll(); - final Map connectionMap = new HashMap<>(); - for (ConnectionBean bean : connectionInfoBeans) { - connectionMap.put(bean.getName(), bean.getConnection()); - } + final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); try { TransmissionEncryptionManager.setTransEncryptionLevel(1); for (ServerDataSetBean bean : beans) { diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionComboBoxPanel.java b/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionComboBoxPanel.java index 4be579742a..2376872a0f 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionComboBoxPanel.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionComboBoxPanel.java @@ -7,6 +7,7 @@ import com.fr.data.impl.AbstractDatabaseConnection; import com.fr.data.impl.Connection; import com.fr.data.impl.NameDatabaseConnection; import com.fr.design.DesignerEnvManager; +import com.fr.design.data.datapane.preview.ConnectionInfoBeanHelper; import com.fr.design.editlock.ConnectionLockChangeChecker; import com.fr.design.editlock.EditLockUtils; import com.fr.design.gui.ibutton.UIButton; @@ -28,6 +29,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.Map; /** * 选择数据连接的下拉框 @@ -89,13 +91,13 @@ public class ConnectionComboBoxPanel extends ItemEditableComboBoxPanel { if (noAuthConnections == null) { return nameList.iterator(); } - List beans = ConnectionRepository.getInstance().getAll(); - for (ConnectionBean bean : beans) { - String conName = bean.getName(); + final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + for (Map.Entry connectionEntry : connectionMap.entrySet()) { + String conName = connectionEntry.getKey(); if (noAuthConnections.contains(conName)) { continue; } - filterConnection(bean.getConnection(), conName, nameList); + filterConnection(connectionEntry.getValue(), conName, nameList); } return nameList.iterator(); } diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java b/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java index 3dd4a62318..baa1f74059 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java @@ -190,11 +190,11 @@ public class ConnectionListPane extends JListControlPane implements ConnectionSh public void populate(Map connectionMap) { List nameObjectList = new ArrayList(); populatedConnectionsSnapshot.clear(); - List beans = ConnectionRepository.getInstance().getAll(); - for (com.fr.workspace.server.entity.connection.ConnectionBean entry : beans) { - nameObjectList.add(new NameObject(entry.getName(), entry.getConnection())); + final Map map = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + for (Map.Entry entry : map.entrySet()) { + nameObjectList.add(new NameObject(entry.getKey(), entry.getValue())); try { - populatedConnectionsSnapshot.put(entry.getName(), (Connection) entry.getConnection().clone()); + populatedConnectionsSnapshot.put(entry.getKey(), (Connection) entry.getValue().clone()); } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); } @@ -365,11 +365,7 @@ public class ConnectionListPane extends JListControlPane implements ConnectionSh } public static void showDialog(Window parent) { - List beans = ConnectionRepository.getInstance().getAll(); - Map connectionMap = new HashMap<>(); - for (ConnectionBean bean : beans) { - connectionMap.put(bean.getName(), bean.getConnection()); - } + final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); final ConnectionManagerPane connectionManagerPane = new ConnectionManagerPane() { @Override public void complete() { diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/connect/LocalConnectionSource.java b/designer-base/src/main/java/com/fr/design/data/datapane/connect/LocalConnectionSource.java index 54f4c3c048..bfad0a1850 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/connect/LocalConnectionSource.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/connect/LocalConnectionSource.java @@ -1,16 +1,21 @@ package com.fr.design.data.datapane.connect; +import com.fanruan.config.impl.data.ConnectionConfigProviderFactory; import com.fr.data.driver.DriverClassNotFoundException; import com.fr.data.impl.Connection; import com.fr.decision.webservice.bean.datasource.ConnectionInfoBean; import com.fr.decision.webservice.exception.general.DriverNotFoundException; import com.fr.decision.webservice.v10.datasource.connection.processor.impl.ConnectionProcessorFactory; +import com.fr.design.data.datapane.preview.ConnectionInfoBeanHelper; import com.fr.design.i18n.Toolkit; import com.fr.log.FineLoggerFactory; import com.fr.security.encryption.transmission.TransmissionEncryptionManager; import com.fr.workspace.engine.exception.DriverUnExistException; import com.fr.workspace.server.repository.connection.BaseConnectionSource; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; /** * 本地Connection资源操作 @@ -21,6 +26,20 @@ import java.sql.SQLException; */ public class LocalConnectionSource extends BaseConnectionSource { + @Override + public ConnectionInfoBean[] getAll(){ + Map map = ConnectionConfigProviderFactory.getConfigProvider().getConnections(); + List beans = new ArrayList<>(); + for (String name : map.keySet()) { + try { + beans.add(ConnectionInfoBeanHelper.createConnectionInfoBean(name, map.get(name))); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + } + return beans.toArray(new ConnectionInfoBean[0]); + } + @Override public void testConnection(ConnectionInfoBean database) throws Exception { try { diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/preview/ConnectionInfoBeanHelper.java b/designer-base/src/main/java/com/fr/design/data/datapane/preview/ConnectionInfoBeanHelper.java index b2f8aad61b..6ad25c57c6 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/preview/ConnectionInfoBeanHelper.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/preview/ConnectionInfoBeanHelper.java @@ -3,6 +3,7 @@ package com.fr.design.data.datapane.preview; import com.fr.data.auth.AuthenticationType; import com.fr.data.auth.kerberos.KerberosAuthentication; import com.fr.data.auth.kerberos.KerberosUtils; +import com.fr.data.core.db.dialect.DialectFactory; import com.fr.data.impl.Connection; import com.fr.data.impl.JDBCDatabaseConnection; import com.fr.data.impl.NameDatabaseConnection; @@ -10,7 +11,9 @@ import com.fr.data.pool.DBCPConnectionPoolAttr; import com.fr.data.security.ssh.BaseSsh; import com.fr.data.security.ssh.SshType; import com.fr.data.security.ssh.impl.KeyVerifySsh; +import com.fr.data.security.ssh.impl.NormalSsh; import com.fr.data.security.ssl.BaseSsl; +import com.fr.data.security.ssl.Ssl; import com.fr.data.security.ssl.SslType; import com.fr.data.security.ssl.impl.NormalSsl; import com.fr.decision.privilege.TransmissionTool; @@ -19,11 +22,21 @@ import com.fr.decision.webservice.bean.datasource.JDBCConnectionBean; import com.fr.decision.webservice.utils.DecisionServiceConstants; import com.fr.decision.webservice.v10.datasource.connection.processor.impl.ConnectionProcessorFactory; import com.fr.decision.webservice.v10.datasource.connection.processor.impl.JDBCConnectionProcessor; +import com.fr.log.FineLoggerFactory; +import com.fr.module.tool.ActivatorToolBox; import com.fr.security.encryption.transmission.TransmissionEncryptionManager; +import com.fr.stable.Constants; import com.fr.stable.StringUtils; +import com.fr.third.fasterxml.jackson.core.JsonProcessingException; import com.fr.third.fasterxml.jackson.databind.ObjectMapper; +import com.fr.third.springframework.beans.BeanUtils; import com.fr.workspace.server.repository.WorkplaceConstants; import com.fr.workspace.server.repository.connection.ConnectionRepository; +import com.fr.workspace.server.repository.tabledata.DataEncryptionHelper; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; /** * 数据连接传输工具类 @@ -34,6 +47,36 @@ import com.fr.workspace.server.repository.connection.ConnectionRepository; */ public class ConnectionInfoBeanHelper { private static ObjectMapper objectMapper = new ObjectMapper(); + private static final Map FETCH_SIZE_MAP = ActivatorToolBox.sandbox(new HashMap()); + private static final int ORACLE_DEFAULT_FETCH_SIZE = 128; + private static final int DB2_DEFAULT_FETCH_SIZE = 50; + private static final int POSTGRE_DEFAULT_FETCH_SIZE = 10000; + + static { + FETCH_SIZE_MAP.put("oracle", ORACLE_DEFAULT_FETCH_SIZE); + FETCH_SIZE_MAP.put("ibm-db2", DB2_DEFAULT_FETCH_SIZE); + FETCH_SIZE_MAP.put("postgresql", POSTGRE_DEFAULT_FETCH_SIZE); + } + + + /** + * 获取连接Map + */ + public static Map createConnectionMap(ConnectionInfoBean[] beans) { + Map ans = new HashMap<>(); + for (ConnectionInfoBean infoBean : beans) { + try { + if (JDBCConnectionProcessor.KEY.acceptConnectionTypes().contains(infoBean.getConnectionType())) { + ans.put(infoBean.getConnectionName(), createJDBCConnection(infoBean)); + } else { + ans.put(infoBean.getConnectionName(), ConnectionProcessorFactory.createConnection(infoBean, true)); + } + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + } + return ans; + } /** * 创建数据连接Bean,可自定义是否携带密码 @@ -137,4 +180,101 @@ public class ConnectionInfoBeanHelper { return TransmissionEncryptionManager.getInstance().getEncryption(WorkplaceConstants.getEncryptionMode()).encrypt(password, WorkplaceConstants.getEncryptionKey()); } + + private static Connection createJDBCConnection(ConnectionInfoBean connectionInfoBean) throws Exception { + String name = connectionInfoBean.getConnectionName(); + if (StringUtils.isNotEmpty(connectionInfoBean.getConnectionId())) { + name = connectionInfoBean.getConnectionId(); + } + JDBCDatabaseConnection connection = new JDBCDatabaseConnection(); + JDBCConnectionBean bean = objectMapper.readValue(connectionInfoBean.getConnectionData(), JDBCConnectionBean.class); + if (StringUtils.isNotEmpty(name)) { + bean.setConnectionName(name); + } + DBCPConnectionPoolAttr connectionPoolAttr = new DBCPConnectionPoolAttr(); + BeanUtils.copyProperties(bean.getConnectionPoolAttr(), connectionPoolAttr); + connectionPoolAttr.setValidationQuery(DataEncryptionHelper.decrypt(connectionPoolAttr.getValidationQuery())); + connectionPoolAttr.setMinEvictableIdleTimeMillis(bean.getConnectionPoolAttr().getMinEvictableIdleTimeMillis() * Constants.MILLS_PER_SECOND); + connectionPoolAttr.setMaxEvictableIdleTimeMillis((long) bean.getConnectionPoolAttr().getMaxEvictableIdleTimeMillis() * Constants.MILLS_PER_SECOND); + connection.setDatabase(bean.getDatabase()); + connection.setSchema(bean.getSchema()); + connection.setDriver(bean.getDriver()); + connection.setURL(bean.getUrl()); + connection.setOriginalCharsetName(StringUtils.isEmpty(bean.getOriginalCharsetName()) ? null : bean.getOriginalCharsetName()); + connection.setNewCharsetName(StringUtils.isEmpty(bean.getNewCharsetName()) ? null : bean.getNewCharsetName()); + connection.setCreator(bean.getCreator()); + connection.setDriverSource(bean.getDriverSource()); + connection.setDbcpAttr(connectionPoolAttr); + if (StringUtils.isNotEmpty(bean.getAuthType())) { + connection.setAuthentication(new KerberosAuthentication(bean.getPrincipal(), bean.getKeyPath(), bean.isUseJaas())); + } + connection.setUser(bean.getUser()); + TransmissionEncryptionManager.getInstance().setCurrentEncryptionMode(WorkplaceConstants.getDecryptionMode()); + connection.setPassword(DataEncryptionHelper.decrypt(bean.getPassword())); + connection.setProperties(bean.getProperties()); + + setFetchSize(bean, connection); + + if (bean.getIdentity() != null) { + connection.setIdentity(bean.getIdentity()); + } + BaseSsh baseSsh; + String sshType = bean.getSshType(); + if (SshType.KEY.toString().equals(sshType)) { + baseSsh = new KeyVerifySsh(); + ((KeyVerifySsh) baseSsh).setPrivateKeyPath(bean.getSshPrivateKeyPath()); + } else { + baseSsh = new NormalSsh(); + } + baseSsh.setIp(bean.getSshIp()); + baseSsh.setUsingSsh(bean.isUsingSsh()); + baseSsh.setPort(bean.getSshPort()); + baseSsh.setUser(bean.getSshUser()); + baseSsh.setRedirectIp(bean.getRedirectIp()); + baseSsh.setRedirectPort(bean.getRedirectPort()); + baseSsh.setSecret(DataEncryptionHelper.decrypt(bean.getSshSecret())); + baseSsh.setKeepAlive(bean.getSshKeepAlive()); + baseSsh.setTimeOut(bean.getSshTimeOut()); + connection.setSsh(baseSsh); + connection.setSsl(createSsl(bean)); + return connection; + } + + private static Ssl createSsl(JDBCConnectionBean bean) { + //目前只有NormalSsl + BaseSsl baseSsl = new NormalSsl(); + if (SslType.NORMAL.toString().equals(bean.getSslType())) { + ((NormalSsl) baseSsl).setVerifyCa(bean.isVerifyCa()); + ((NormalSsl) baseSsl).setCipher(bean.getSslCipher()); + } + baseSsl.setUsingSsl(bean.isUsingSsl()); + baseSsl.setClientCertificate(bean.getSslClientCertificate()); + baseSsl.setClientPrivateKey(bean.getSslClientPrivateKey()); + baseSsl.setCaCertificate(bean.getCaCertificate()); + return baseSsl; + } + + /** + * 前端客户设置了一个fetchSize: + * 1. 如果是合法值(>0),就使用 + * 2. 如果是非法值(<=0): + * 2.1 如果我们对这种数据库有默认的fetchSize,就用默认的 + * 2.2 如果没有默认值,就不进行任何设置 + * @param bean bean + * @param connection connection + */ + private static void setFetchSize(JDBCConnectionBean bean, Connection connection) { + + int connectionBeanFetchSize = bean.getFetchSize(); + String connectionBeanDatabaseName = bean.getDatabase(); + if (DialectFactory.isValidFetchSize(connectionBeanFetchSize)) { + connection.setFetchSize(connectionBeanFetchSize); + } else if (FETCH_SIZE_MAP.containsKey(connectionBeanDatabaseName)) { + Optional.ofNullable(FETCH_SIZE_MAP.get(connectionBeanDatabaseName)).ifPresent(connection::setFetchSize); + } + FineLoggerFactory.getLogger().info("[Connection] fetchSize set to {} for connection {}", connection.getFetchSize(), bean.getConnectionName()); + + } + + } diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/preview/TableDataBeanHelper.java b/designer-base/src/main/java/com/fr/design/data/datapane/preview/TableDataBeanHelper.java new file mode 100644 index 0000000000..68f2584153 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/data/datapane/preview/TableDataBeanHelper.java @@ -0,0 +1,198 @@ +package com.fr.design.data.datapane.preview; + +import com.fr.base.DataSetProcessors; +import com.fr.base.Parameter; +import com.fr.base.ParameterHelper; +import com.fr.base.ParameterTypeHandler; +import com.fr.base.StoreProcedureParameter; +import com.fr.base.TableData; +import com.fr.data.impl.Connection; +import com.fr.data.impl.DBTableData; +import com.fr.data.impl.NameDatabaseConnection; +import com.fr.data.impl.storeproc.StoreProcedure; +import com.fr.decision.fun.UniversalServerTableDataProvider; +import com.fr.decision.webservice.bean.dataset.ParameterBean; +import com.fr.decision.webservice.bean.dataset.ParameterExternal; +import com.fr.decision.webservice.bean.dataset.SQLDataSetBean; +import com.fr.decision.webservice.bean.dataset.ServerDataSetBean; +import com.fr.decision.webservice.bean.dataset.StoreProcedureBean; +import com.fr.decision.webservice.v10.datasource.dataset.processor.impl.SQLDataSetProcessor; +import com.fr.decision.webservice.v10.datasource.dataset.processor.impl.StoreProcedureProcessor; +import com.fr.general.GeneralUtils; +import com.fr.general.sql.SqlUtils; +import com.fr.json.JSONObject; +import com.fr.log.FineLoggerFactory; +import com.fr.script.Calculator; +import com.fr.security.encryption.transmission.TransmissionEncryptionManager; +import com.fr.stable.ParameterProvider; +import com.fr.stable.StringUtils; +import com.fr.workspace.server.repository.connection.ConnectionRepository; +import com.fr.workspace.server.repository.tabledata.DataEncryptionHelper; +import com.fr.workspace.server.repository.tabledata.TableDataRepository; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 数据集bean工具类 + * + * @author Destiny.Lin + * @since 11.0 + * Created on 2024/8/9 + */ +public class TableDataBeanHelper { + + /** + * 获取服务器数据集 + */ + public static Map getServerTableDatas() { + final List beans = TableDataRepository.getInstance().getAllTableData(); + final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + final Map tableDataMap = new HashMap<>(); + try { + TransmissionEncryptionManager.setTransEncryptionLevel(1); + for (ServerDataSetBean bean : beans) { + tableDataMap.put(bean.getDatasetName() , TableDataBeanHelper.getTableDataSet(connectionMap, bean.getDatasetType(), bean.getDatasetData())); + } + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + TransmissionEncryptionManager.removeTransEncryptionLevel(); + } + return tableDataMap; + } + + /** + * 根据序列化数据生成tabledata + */ + public static TableData getTableDataSet(Map connectionMap, String type, String tableDataSetData) throws Exception { + if (DataSetProcessors.getProcessors().containsKey(type)) { + if (StringUtils.equals(SQLDataSetProcessor.TYPE, type)) { + return deserialize4SQL(connectionMap, null, new JSONObject(tableDataSetData)); + } else if (StringUtils.equals(StoreProcedureProcessor.TYPE, type)) { + return deserialize4Procedure(connectionMap, null, new JSONObject(tableDataSetData)); + } else { + UniversalServerTableDataProvider processor = DataSetProcessors.getProcessors().get(type); + return (TableData) processor.deserialize(null, new JSONObject(tableDataSetData)); + } + + } + return null; + } + + private static TableData deserialize4Procedure(Map connectionMap, DBTableData oldDataSet, JSONObject object) { + StoreProcedure storeProcedure = new StoreProcedure(); + StoreProcedureBean bean = object.mapTo(StoreProcedureBean.class); + storeProcedure.setShare(bean.isShare()); + storeProcedure.setMaxMemRowCount(bean.getMaxMemRowCount()); + storeProcedure.setQuery(bean.getQuery()); + if (StringUtils.isNotEmpty(bean.getDatabase())) { + Connection connection = connectionMap.get(bean.getDatabase()); + if (connection != null) { + storeProcedure.setDatabaseConnection(new NameDatabaseConnection(bean.getDatabase())); + } else { + FineLoggerFactory.getLogger().info("not find conn by {}", bean.getDatabase()); + } + } + Parameter[] parameters = new Parameter[bean.getParameters().size()]; + for (int i = 0; i < parameters.length; i++) { + ParameterBean parameterBean = bean.getParameters().get(i); + parameters[i] = (Parameter) ParameterTypeHandler.getInstance().parseParameter(parameterBean, new Parameter(parameterBean.getName())); + } + List externals = bean.getStoreProcedureParameterBeanList(); + StoreProcedureParameter[] procedureParameters = new StoreProcedureParameter[bean.getParameters().size()]; + for (int i = 0; i < procedureParameters.length; i++) { + StoreProcedureParameter storeProcedureParameter = new StoreProcedureParameter(); + storeProcedureParameter.setName(parameters[i].getName()); + storeProcedureParameter.setValue(parameters[i].getValue()); + storeProcedureParameter.setSchema(externals.get(i).getSchema()); + storeProcedureParameter.setType(externals.get(i).getTypeInt()); + procedureParameters[i] = storeProcedureParameter; + } + storeProcedure.setParameters(procedureParameters); + return storeProcedure; + } + + + /** + * 获取服务器数据集传输的bean + */ + public static ServerDataSetBean getServerDataSetBean(String name, TableData tableData) { + for (UniversalServerTableDataProvider processor : DataSetProcessors.getProcessors().values()) { + if (SQLDataSetProcessor.KEY.classForTableData() == tableData.getClass()) { + return serialize4SQL(name, tableData); + } else if (processor.classForTableData() == tableData.getClass()) { + ServerDataSetBean bean = new ServerDataSetBean(); + try { + bean.setDatasetData(processor.serialize(tableData).toString()); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + bean.setDatasetData(StringUtils.EMPTY); + } + bean.setDatasetName(name); + bean.setDatasetId(name); + bean.setDatasetType(processor.nameForTableData()); + return bean; + } + } + return new ServerDataSetBean(name); + } + + private static ServerDataSetBean serialize4SQL(String name, TableData tableData) { + ServerDataSetBean bean = new ServerDataSetBean(); + try { + bean.setDatasetData(serialize4SQL0((DBTableData) tableData).toString()); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + bean.setDatasetData(StringUtils.EMPTY); + } + bean.setDatasetName(name); + bean.setDatasetId(name); + bean.setDatasetType(SQLDataSetProcessor.KEY.nameForTableData()); + return bean; + } + + private static Object serialize4SQL0(DBTableData dataSet) { + SQLDataSetBean bean = new SQLDataSetBean(); + if (dataSet.getDatabase() instanceof NameDatabaseConnection) { + bean.setDatabase(((NameDatabaseConnection) dataSet.getDatabase()).getName()); + } + bean.setQuery(DataEncryptionHelper.encrypt(dataSet.getQuery())); + List parameterBeans = new ArrayList<>(); + ParameterProvider[] parameters = dataSet.getParameters(Calculator.createCalculator()); + for (ParameterProvider parameter : parameters) { + parameterBeans.add(new ParameterBean(parameter.getValue().getClass().getSimpleName(), parameter.getName(), GeneralUtils.objectToString(parameter.getValue()))); + } + bean.setParameters(parameterBeans); + return JSONObject.mapFrom(bean); + } + + + private static TableData deserialize4SQL(Map connectionMap, DBTableData oldDataSet, JSONObject object) { + DBTableData tableData = new DBTableData(); + SQLDataSetBean bean = object.mapTo(SQLDataSetBean.class); + tableData.setQuery(DataEncryptionHelper.decrypt(bean.getQuery())); + Connection connection = connectionMap.get(bean.getDatabase()); + if (connection != null) { + tableData.setDatabase(new NameDatabaseConnection(bean.getDatabase())); + } else { + FineLoggerFactory.getLogger().info("not find conn by {}", bean.getDatabase()); + } + String sql = SqlUtils.clearSqlComments(DataEncryptionHelper.decrypt(bean.getQuery())); + Parameter[] parameters = new Parameter[bean.getParameters().size()]; + for (int i = 0; i < parameters.length; i++) { + ParameterBean parameterBean = bean.getParameters().get(i); + parameters[i] = (Parameter) ParameterTypeHandler.getInstance().parseParameter(parameterBean, new Parameter(parameterBean.getName())); + } + tableData.setParameters(ParameterHelper.analyzeAndUnionSameParameters(new String[]{sql}, parameters)); + if (oldDataSet != null) { + tableData.setMaxMemRowCount(oldDataSet.getMaxMemRowCount()); + tableData.setPageQuerySql(oldDataSet.getPageQuerySql()); + tableData.setShare(oldDataSet.isShare()); + tableData.setDataQueryProcessor(oldDataSet.getDataQueryProcessor()); + } + return tableData; + } +} diff --git a/designer-base/src/main/java/com/fr/design/data/tabledata/LocalTableDataSource.java b/designer-base/src/main/java/com/fr/design/data/tabledata/LocalTableDataSource.java index 12a8a0c0ef..5c70a87536 100644 --- a/designer-base/src/main/java/com/fr/design/data/tabledata/LocalTableDataSource.java +++ b/designer-base/src/main/java/com/fr/design/data/tabledata/LocalTableDataSource.java @@ -1,13 +1,18 @@ package com.fr.design.data.tabledata; +import com.fanruan.config.impl.data.ConnectionConfigProviderFactory; import com.fanruan.config.impl.data.TableDataConfigProviderFactory; +import com.fr.base.TableData; import com.fr.decision.webservice.bean.dataset.FileDataSetBean; +import com.fr.decision.webservice.bean.dataset.ServerDataSetBean; import com.fr.decision.webservice.bean.entry.FileNodeBean; import com.fr.decision.webservice.v10.datasource.dataset.processor.impl.FileProcessor; +import com.fr.design.data.datapane.preview.TableDataBeanHelper; import com.fr.log.FineLoggerFactory; import com.fr.stable.StringUtils; import com.fr.workspace.server.repository.tabledata.BaseTableDataSource; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -61,4 +66,33 @@ public class LocalTableDataSource extends BaseTableDataSource { return false; } } + + + @Override + public List getAllTableData() { + + List beans = new ArrayList<>(); + TableData[] tableDatas = TableDataConfigProviderFactory.getInstance().getTableDatas().values().toArray(new TableData[0]); + for (TableData data : tableDatas) { + beans.add(TableDataBeanHelper.getServerDataSetBean(data.getName(), data)); + } + return beans; + } + + + @Override + public void add(ServerDataSetBean bean) throws Exception { + TableDataConfigProviderFactory.getInstance().addTableData(bean.getDatasetName(), TableDataBeanHelper.getTableDataSet(ConnectionConfigProviderFactory.getConfigProvider().getConnections(), bean.getDatasetType(), bean.getDatasetData())); + } + + @Override + public void delete(ServerDataSetBean bean) throws Exception { + TableDataConfigProviderFactory.getInstance().removeTableData(bean.getDatasetName()); + } + + @Override + public void update(ServerDataSetBean bean) throws Exception { + TableDataConfigProviderFactory.getInstance().updateTableData(bean.getDatasetName(), TableDataBeanHelper.getTableDataSet(ConnectionConfigProviderFactory.getConfigProvider().getConnections(), bean.getDatasetType(), bean.getDatasetData())); + } + } diff --git a/designer-base/src/main/java/com/fr/design/data/tabledata/wrapper/TableDataFactory.java b/designer-base/src/main/java/com/fr/design/data/tabledata/wrapper/TableDataFactory.java index 6d87a2c1bc..2b419f90a6 100644 --- a/designer-base/src/main/java/com/fr/design/data/tabledata/wrapper/TableDataFactory.java +++ b/designer-base/src/main/java/com/fr/design/data/tabledata/wrapper/TableDataFactory.java @@ -15,6 +15,7 @@ import com.fr.data.impl.storeproc.StoreProcedure; import com.fr.datacenters.tabledata.DCTableData; import com.fr.decision.webservice.bean.dataset.ServerDataSetBean; import com.fr.design.data.datapane.TableDataNameObjectCreator; +import com.fr.design.data.datapane.preview.ConnectionInfoBeanHelper; import com.fr.design.data.tabledata.datacenter.DCTableDataPane; import com.fr.design.data.tabledata.tabledatapane.AbstractTableDataPane; import com.fr.design.data.tabledata.tabledatapane.ClassTableDataPane; @@ -29,9 +30,8 @@ import com.fr.general.ComparatorUtils; import com.fr.log.FineLoggerFactory; import com.fr.stable.ArrayUtils; import com.fr.stable.StringUtils; -import com.fr.workspace.server.entity.connection.ConnectionBean; import com.fr.workspace.server.repository.connection.ConnectionRepository; -import com.fr.workspace.server.repository.tabledata.TableDataBeanHelper; +import com.fr.design.data.datapane.preview.TableDataBeanHelper; import com.fr.workspace.server.repository.tabledata.TableDataRepository; import javax.swing.Icon; @@ -205,11 +205,7 @@ public abstract class TableDataFactory { clearAll(); try { List beans = TableDataRepository.getInstance().getAllTableData(); - final List connectionInfoBeans = ConnectionRepository.getInstance().getAll(); - final Map connectionMap = new HashMap<>(); - for (ConnectionBean bean : connectionInfoBeans) { - connectionMap.put(bean.getName(), bean.getConnection()); - } + final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); Map tableDataMap = new HashMap<>(); for (ServerDataSetBean bean : beans) { tableDataMap.put(bean.getDatasetName(), TableDataBeanHelper.getTableDataSet(connectionMap, bean.getDatasetType(), bean.getDatasetData())); @@ -233,11 +229,7 @@ public abstract class TableDataFactory { Map tableDataMap = new HashMap<>(); try { List beans = TableDataRepository.getInstance().getAllTableData(); - final List connectionInfoBeans = ConnectionRepository.getInstance().getAll(); - final Map connectionMap = new HashMap<>(); - for (ConnectionBean bean : connectionInfoBeans) { - connectionMap.put(bean.getName(), bean.getConnection()); - } + final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); for (ServerDataSetBean bean : beans) { tableDataMap.put(bean.getDatasetName(), TableDataBeanHelper.getTableDataSet(connectionMap, bean.getDatasetType(), bean.getDatasetData())); }