diff --git a/cf-module-report/cf-module-report-api/src/main/java/com/cf/imes/module/report/enums/ErrorCodeConstants.java b/cf-module-report/cf-module-report-api/src/main/java/com/cf/imes/module/report/enums/ErrorCodeConstants.java index deef36ac7..8e9d6de5f 100644 --- a/cf-module-report/cf-module-report-api/src/main/java/com/cf/imes/module/report/enums/ErrorCodeConstants.java +++ b/cf-module-report/cf-module-report-api/src/main/java/com/cf/imes/module/report/enums/ErrorCodeConstants.java @@ -17,6 +17,8 @@ public final class ErrorCodeConstants { // ========== UREPORT datasource模块 1-003-002-000 ========== public static final ErrorCode DATASOURCE_NOT_EXISTS = new ErrorCode(1_003_002_001, "报表数据源不存在"); public static final ErrorCode DATASOURCE_CONNECT_FAIL = new ErrorCode(1_003_002_002, "报表数据源连接失败"); + public static final ErrorCode DATASOURCE_SPRINGBEAN_GET_FAIL = new ErrorCode(1_003_002_003, "无法获取springbean【{}】"); + public static final ErrorCode DATASOURCE_SPRINGBEAN_METHODS_GET_FAIL = new ErrorCode(1_003_002_004, "获取springbean方法列表失败,请检查】"); // ========== UREPORT dataset模块 1-003-003-000 ========== diff --git a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/dataset/ReportDatasetController.java b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/dataset/ReportDatasetController.java index d0e31458e..6b882d43a 100644 --- a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/dataset/ReportDatasetController.java +++ b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/dataset/ReportDatasetController.java @@ -52,7 +52,7 @@ public class ReportDatasetController { @DeleteMapping("/dataset/{id}") @Operation(summary = "删除报表数据集") - @Parameter(name = "id", description = "编号", required = true) + @Parameter(name = "id", description = "数据集id", required = true) // @PreAuthorize("@ss.hasPermission('report:dataset:delete')") public CommonResult deleteDataset(@PathVariable("id") Long id) { datasetService.deleteDataset(id); @@ -61,7 +61,7 @@ public class ReportDatasetController { @GetMapping("/dataset/{id}") @Operation(summary = "获得报表数据集") - @Parameter(name = "id", description = "编号", required = true, example = "1") + @Parameter(name = "id", description = "数据集id", required = true, example = "1") // @PreAuthorize("@ss.hasPermission('report:dataset:query')") public CommonResult getDataset(@PathVariable("id") Long id) { ReportDatasetDO dataset = datasetService.getDataset(id); diff --git a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/datasource/ReportDatasourceController.java b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/datasource/ReportDatasourceController.java index c9f73d2da..a2124531d 100644 --- a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/datasource/ReportDatasourceController.java +++ b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/datasource/ReportDatasourceController.java @@ -23,6 +23,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; +import javax.validation.constraints.NotEmpty; import java.util.List; import java.util.Map; @@ -69,7 +70,7 @@ public class ReportDatasourceController { @DeleteMapping("/datasource/{id}") @Operation(summary = "删除报表数据源") - @Parameter(name = "id", description = "编号", required = true, example = "1") + @Parameter(name = "id", description = "数据源id", required = true, example = "1") // @PreAuthorize("@ss.hasPermission('report:datasource:delete')") public CommonResult deleteDatasource(@PathVariable("id") Long id) { datasourceService.deleteDatasource(id); @@ -78,7 +79,7 @@ public class ReportDatasourceController { @GetMapping("/datasource/{id}") @Operation(summary = "获取报表数据源") - @Parameter(name = "id", description = "编号", required = true, example = "1") + @Parameter(name = "id", description = "数据源id", required = true, example = "1") // @PreAuthorize("@ss.hasPermission('report:datasource:query')") public CommonResult getDatasource(@PathVariable("id") Long id) { ReportDatasourceDO datasource = datasourceService.getDatasource(id); @@ -88,6 +89,7 @@ public class ReportDatasourceController { @GetMapping("/{templateId}/datasources") @Operation(summary = "获取报表模板下的报表数据源") // @PreAuthorize("@ss.hasPermission('report:datasource:query')") + @Parameter(name = "templateId", description = "模版id", required = true) public CommonResult> getDatasourcePage(@PathVariable(value = "templateId") Long templateId) { return success(BeanUtils.toBean(datasourceService.getTemplateDatasourceList(ReportDatasourceReqVO.builder().templateId(templateId).build()), ReportDatasourceRespVO.class)); } @@ -99,10 +101,18 @@ public class ReportDatasourceController { return success(datasourceService.getBeanDatasourceList()); } + @GetMapping("/springbean/result/clazz") + @Operation(summary = "获取springbean数据源返回对象") + @Parameter(name = "clazz", description = "返回对象类路径全名不能为空", required = true) + public List springbeanFieldList(@RequestParam(value = "clazz") @NotEmpty(message = "类路径全名不能为空") String clazz) { + return datasourceService.getSpringBeanResultFieldList(clazz); + } + @GetMapping("/datasource/bean/methods") @Operation(summary = "获取springbean数据源方法类表") // @PreAuthorize("@ss.hasPermission('report:datasource:query')") - public CommonResult> getBeanDatasourceList(@RequestParam(value = "beanId") String beanId) { + @Parameter(name = "beanId", description = "springbeanId", required = true) + public CommonResult> getBeanDatasourceList(@RequestParam(value = "beanId") @NotEmpty(message = "beanId不能为空") String beanId) { return success(datasourceService.loadBeanMethods(beanId)); } diff --git a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/template/ReportTemplateController.java b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/template/ReportTemplateController.java index 0d9bee876..2311a4ef7 100644 --- a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/template/ReportTemplateController.java +++ b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/controller/admin/template/ReportTemplateController.java @@ -39,6 +39,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; +import javax.validation.constraints.NotEmpty; import java.io.OutputStream; import java.util.HashMap; @@ -79,7 +80,7 @@ public class ReportTemplateController { @DeleteMapping("/template/{id}") @Operation(summary = "删除报表模板信息") - @Parameter(name = "id", description = "编号", required = true) + @Parameter(name = "id", description = "模板id", required = true) // @PreAuthorize("@ss.hasPermission('report:template:delete')") public CommonResult deleteReportTemplate(@PathVariable("id") Long id) { templateService.deleteReportTemplate(id); @@ -88,7 +89,7 @@ public class ReportTemplateController { @GetMapping("/template/{id}") @Operation(summary = "获取报表模板信息") - @Parameter(name = "id", description = "编号", required = true, example = "1") + @Parameter(name = "id", description = "模板id", required = true, example = "1") // @PreAuthorize("@ss.hasPermission('report:template:query')") public CommonResult getReportTemplate(@PathVariable("id") Long id) { return success(templateService.getReportTemplateDefinition(id)); @@ -141,7 +142,7 @@ public class ReportTemplateController { @GetMapping("/scriptValidation") @Operation(summary = "校验表达式") @Parameter(name = "content", description = "表达式", required = true) - public List scriptValidation(@Param("content") String content) { + public List scriptValidation(@Param("content") @NotEmpty(message = "表达式不能为空") String content) { content = StringUtils.decode(content); ANTLRInputStream antlrInputStream = new ANTLRInputStream(content); ReportParserLexer lexer = new ReportParserLexer(antlrInputStream); @@ -163,7 +164,7 @@ public class ReportTemplateController { @GetMapping("/parseDataSet") @Operation(summary = "解析表达式中的数据集") @Parameter(name = "expr", description = "表达式", required = true) - public Map parseDatasetName(@Param("expr") String expr) { + public Map parseDatasetName(@Param("expr") @NotEmpty(message = "表达式不能为空") String expr) { ANTLRInputStream antlrInputStream = new ANTLRInputStream(expr); ReportParserLexer lexer = new ReportParserLexer(antlrInputStream); CommonTokenStream tokenStream = new CommonTokenStream(lexer); diff --git a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/framework/ureport/bean/MockSpringBeanDatasourceResult.java b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/framework/ureport/bean/MockSpringBeanDatasourceResult.java new file mode 100644 index 000000000..ab7334c59 --- /dev/null +++ b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/framework/ureport/bean/MockSpringBeanDatasourceResult.java @@ -0,0 +1,16 @@ +package com.cf.imes.module.report.framework.ureport.bean; + +import lombok.Data; +import org.springframework.stereotype.Component; + +/** + * @author Gqr + * @since 2024/8/30 11:21 + */ +@Component +@Data +public class MockSpringBeanDatasourceResult { + private String key; + + private String value; +} diff --git a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/service/datasource/ReportDatasourceService.java b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/service/datasource/ReportDatasourceService.java index a87036e9b..e459a74a3 100644 --- a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/service/datasource/ReportDatasourceService.java +++ b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/service/datasource/ReportDatasourceService.java @@ -78,5 +78,11 @@ public interface ReportDatasourceService { */ List getTableFields(PreviewParams previewParams); - + /** + * 获取数据源 + * + * @param clazz + * @return + */ + List getSpringBeanResultFieldList(String clazz); } diff --git a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/service/datasource/ReportDatasourceServiceImpl.java b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/service/datasource/ReportDatasourceServiceImpl.java index 41c87841c..4f3dbeb49 100644 --- a/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/service/datasource/ReportDatasourceServiceImpl.java +++ b/cf-module-report/cf-module-report-biz/src/main/java/com/cf/imes/module/report/service/datasource/ReportDatasourceServiceImpl.java @@ -1,9 +1,11 @@ package com.cf.imes.module.report.service.datasource; import cn.hutool.core.annotation.AnnotationUtil; +import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.toolkit.sql.SqlInjectionUtils; +import com.bstek.common.exception.ReportDesignException; import com.bstek.common.utils.MultipleJdbcTemplate; import com.bstek.datasource.bean.DataSourceInfo; import com.bstek.datasource.bean.PreviewParams; @@ -12,6 +14,7 @@ import com.bstek.ureport.definition.dataset.Parameter; import com.bstek.ureport.definition.dataset.SqlDatasetDefinition; import com.bstek.ureport.utils.ProcedureUtils; import com.cf.imes.framework.common.exception.ServiceException; +import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil; import com.cf.imes.framework.common.util.object.BeanUtils; import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX; import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetReqVO; @@ -27,6 +30,7 @@ import com.cf.imes.module.report.service.dataset.ReportDatasetService; import com.cf.imes.module.report.util.SqlInjectionUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; @@ -37,6 +41,7 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import javax.sql.DataSource; +import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.ResultSetMetaData; @@ -51,6 +56,8 @@ import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_SQL_ILL import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_SQL_INJECTION_RISK; import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_SQL_REQUIRED; import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASOURCE_NOT_EXISTS; +import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASOURCE_SPRINGBEAN_GET_FAIL; +import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASOURCE_SPRINGBEAN_METHODS_GET_FAIL; /** * 报表数据源 Service 实现类 @@ -156,29 +163,39 @@ public class ReportDatasourceServiceImpl implements ReportDatasourceService { @Override public List loadBeanMethods(String beanId) { - Object obj = applicationContext.getBean(beanId); - Class clazz = obj.getClass(); - Method[] methods = clazz.getMethods(); + Object obj; List result = new ArrayList(); - // ureport的springbean方法必须满足(String dsName, String datasetName, Map parameters) - for (Method method : methods) { - Class[] types = method.getParameterTypes(); - if (types.length != 3) { - continue; + try { + obj = applicationContext.getBean(beanId); + Class clazz = obj.getClass(); + Method[] methods = clazz.getMethods(); + + // ureport的springbean方法必须满足(String dsName, String datasetName, Map parameters) + for (Method method : methods) { + Class[] types = method.getParameterTypes(); + if (types.length != 3) { + continue; + } + Class typeClass1 = types[0]; + Class typeClass2 = types[1]; + Class typeClass3 = types[2]; + if (!String.class.isAssignableFrom(typeClass1)) { + continue; + } + if (!String.class.isAssignableFrom(typeClass2)) { + continue; + } + if (!Map.class.isAssignableFrom(typeClass3)) { + continue; + } + result.add(method.getName()); } - Class typeClass1 = types[0]; - Class typeClass2 = types[1]; - Class typeClass3 = types[2]; - if (!String.class.isAssignableFrom(typeClass1)) { - continue; - } - if (!String.class.isAssignableFrom(typeClass2)) { - continue; - } - if (!Map.class.isAssignableFrom(typeClass3)) { - continue; - } - result.add(method.getName()); + } catch (BeansException e) { + log.error("[ReportDatasourceService][loadBeanMethods]获取bean失败", e); + throw ServiceExceptionUtil.exception(DATASOURCE_SPRINGBEAN_GET_FAIL, beanId); + } catch (Exception e) { + log.error("[ReportDatasourceService][loadBeanMethods]方法列表获取异常", e); + throw ServiceExceptionUtil.exception(DATASOURCE_SPRINGBEAN_METHODS_GET_FAIL); } return result; } @@ -241,6 +258,26 @@ public class ReportDatasourceServiceImpl implements ReportDatasourceService { } } + @Override + public List getSpringBeanResultFieldList(String clazz) { + List result = new ArrayList(); + try { + Class targetClass = Class.forName(clazz); + PropertyDescriptor[] propertyDescriptors = BeanUtil.getPropertyDescriptors(targetClass); + for (PropertyDescriptor pd : propertyDescriptors) { + String name = pd.getName(); + if ("class".equals(name)) { + continue; + } + result.add(new Field(name)); + } + return result; + + } catch (Exception ex) { + throw new ReportDesignException(ex); + } + } + /** * 查询数据源下的数据集 * diff --git a/cf-module-report/cf-module-report-biz/src/main/resources/lib/ureport-core-2.3.0-SNAPSHOT.jar b/cf-module-report/cf-module-report-biz/src/main/resources/lib/ureport-core-2.3.0-SNAPSHOT.jar index ce94db844..dd9c515b3 100644 Binary files a/cf-module-report/cf-module-report-biz/src/main/resources/lib/ureport-core-2.3.0-SNAPSHOT.jar and b/cf-module-report/cf-module-report-biz/src/main/resources/lib/ureport-core-2.3.0-SNAPSHOT.jar differ