Destiny.Lin
3 months ago
10 changed files with 413 additions and 41 deletions
@ -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<String, TableData> getServerTableDatas() { |
||||||
|
final List<ServerDataSetBean> beans = TableDataRepository.getInstance().getAllTableData(); |
||||||
|
final Map<String, Connection> connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); |
||||||
|
final Map<String, TableData> 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<String, Connection> 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<String, Connection> 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<ParameterExternal> 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<ParameterBean> 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<String, Connection> 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; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue