Parcourir la source

解析jrxml文件,检查是否引用子报表,如果是,(递归)编译子报表

sunyj il y a 9 ans
Parent
commit
4867f0347b
1 fichiers modifiés avec 79 ajouts et 32 suppressions
  1. 79 32
      src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

+ 79 - 32
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -1,12 +1,16 @@
 package com.uas.report.service.impl;
 
+import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.sql.DataSource;
@@ -85,14 +89,17 @@ public class PrintServiceImpl implements PrintService {
 			String otherParameters, Integer pageIndex) {
 		DataSource dataSource = null;
 		if (!StringUtils.isEmpty(profile)) {
+			logger.info("point 6");
 			dataSource = DataSourceUtils.getPlatformDataSource(userName, profile);
 		} else {
+			logger.info("point 7");
 			// 为空,说明不是来自B2C或B2B的请求,而是UAS系统
 			dataSource = DataSourceUtils.getUASDataSource(userName);
 		}
 		if (dataSource == null) {
 			throw new ReportException("获取数据源失败");
 		}
+		logger.info("point 8");
 		return print(userName, reportName, whereCondition, otherParameters, null, pageIndex, dataSource);
 	}
 
@@ -116,41 +123,12 @@ public class PrintServiceImpl implements PrintService {
 		// throw new ReportException(e).setDetailedMessage(e);
 		// }
 
-		// 报表路径为报表根路径REPORT_DIR + 当前账套用户名userName
-		String reportDir = fileService.getMasterPath(userName);
 		String jrxmlFilePath = fileService.getJrxmlFilePath(userName, reportName);
-		File jrxmlFile = new File(jrxmlFilePath);
-		// 报表模板不存在
-		if (!jrxmlFile.exists()) {
-			// 替换windows下路径中的双反斜杠为单斜杠
-			throw new ReportException("未发现模板文件:" + jrxmlFile.getPath().replaceAll("\\\\", "/"));
-		}
-
-		String jasperFilePath = jrxmlFile.getPath().replace(".jrxml", ".jasper");
-		// 报表模板编译后的文件
-		File jasperFile = new File(jasperFilePath);
-		try {
-			// 报表模板未编译过
-			if (!jasperFile.exists()) {
-				logger.info("正在编译报表模板...");
-				JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
-				JasperCompileManager.compileReportToFile(jasperDesign, jasperFilePath);
-			} else {
-				// 如果在编译之后,报表模板有更改过 ,重新编译
-				if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
-					logger.info("正在重新编译报表模板...");
-					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
-					JasperCompileManager.compileReportToFile(jasperDesign, jasperFilePath);
-				}
-			}
-		} catch (JRException e) {
-			logger.error(e);
-			throw new ReportException("编译报表模板失败:" + e.getMessage()).setDetailedMessage(e);
-		}
+		String jasperFilePath = maybeCompileJrxmlFile(jrxmlFilePath);
 
 		// 向报表模板传递参数:报表路径、where条件、其他参数
 		Map<String, Object> parameters = new HashMap<>();
-		parameters.put(ReportConstants.PARAMETER_REPORT_DIR, reportDir);
+		parameters.put(ReportConstants.PARAMETER_REPORT_DIR, fileService.getMasterPath(userName));
 		if (!StringUtils.isEmpty(whereCondition)) {
 			parameters.put(ReportConstants.PARAMETER_WHERE_CONDITION, whereCondition);
 		}
@@ -162,7 +140,9 @@ public class PrintServiceImpl implements PrintService {
 
 		Connection connection = null;
 		try {
+			logger.info("point 13");
 			connection = dataSource.getConnection();
+			logger.info("point 14");
 			Map<String, Object> result = new HashMap<>();
 
 			// 从数据库获取数据填充报表
@@ -173,7 +153,7 @@ public class PrintServiceImpl implements PrintService {
 			if (!StringUtils.isEmpty(exportFileType)) {
 				// 只导出数据
 				if (exportFileType.equals("xls_with_only_data")) {
-					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
+					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
 					// 移除模板中多余元素
 					removeUnusedElements(jasperDesign);
 					exportFileType = "xls";
@@ -222,7 +202,9 @@ public class PrintServiceImpl implements PrintService {
 		} finally {
 			if (connection != null) {
 				try {
+					logger.info("point 21");
 					connection.close();
+					logger.info("point 22");
 				} catch (SQLException e) {
 					throw new ReportException(e).setDetailedMessage(e);
 				}
@@ -230,6 +212,71 @@ public class PrintServiceImpl implements PrintService {
 		}
 	}
 
+	/**
+	 * 可能需要编译报表模板文件
+	 * 
+	 * @param jrxmlFile
+	 *            报表模板文件
+	 * @return 编译后的jasper文件路径
+	 */
+	private String maybeCompileJrxmlFile(String jrxmlFilePath) {
+		if (StringUtils.isEmpty(jrxmlFilePath)) {
+			logger.error("参数为空:" + jrxmlFilePath);
+			return null;
+		}
+		File jrxmlFile = new File(jrxmlFilePath);
+		// 报表模板不存在
+		if (!jrxmlFile.exists()) {
+			// 替换windows下路径中的双反斜杠为单斜杠
+			throw new ReportException("未发现模板文件:" + jrxmlFilePath.replaceAll("\\\\", "/"));
+		}
+
+		// 首先解析jrxml文件,检查是否引用子报表,如果是,先(递归)编译子报表
+		try {
+			BufferedReader bufferedReader = new BufferedReader(new FileReader(jrxmlFile));
+			String line;
+			List<String> subJrxmlFilePaths = new ArrayList<>();
+			while (!StringUtils.isEmpty(line = bufferedReader.readLine())) {
+				// 引用的子报表![CDATA[$P{REPORT_DIR} + "/jrxml/FreeClaimQT.jasper"]]
+				if (line.contains("/jrxml/")) {
+					int beginIndex = line.indexOf("/jrxml/") + 7;
+					int endIndex = line.indexOf(".jasper");
+					String subJrxmlFileName = line.substring(beginIndex, endIndex) + ".jrxml";
+					subJrxmlFilePaths.add(jrxmlFile.getParent() + "/" + subJrxmlFileName);
+				}
+			}
+			bufferedReader.close();
+			for (String subJrxmlFilePath : subJrxmlFilePaths) {
+				maybeCompileJrxmlFile(subJrxmlFilePath);
+			}
+		} catch (IOException e) {
+			throw new ReportException(e).setDetailedMessage(e);
+		}
+
+		String jasperFilePath = jrxmlFilePath.replace(".jrxml", ".jasper");
+		// 报表模板编译后的文件
+		File jasperFile = new File(jasperFilePath);
+		try {
+			// 报表模板未编译过
+			if (!jasperFile.exists()) {
+				logger.info("正在编译报表模板... " + jrxmlFilePath);
+				JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
+				JasperCompileManager.compileReportToFile(jasperDesign, jasperFilePath);
+			} else {
+				// 如果在编译之后,报表模板有更改过 ,重新编译
+				if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
+					logger.info("正在重新编译报表模板... " + jrxmlFilePath);
+					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
+					JasperCompileManager.compileReportToFile(jasperDesign, jasperFilePath);
+				}
+			}
+		} catch (JRException e) {
+			logger.error(e);
+			throw new ReportException("编译报表模板失败: " + jrxmlFilePath + " " + e.getMessage()).setDetailedMessage(e);
+		}
+		return jasperFilePath;
+	}
+
 	/**
 	 * 以不同的格式导出报表
 	 *