Procházet zdrojové kódy

精简冗余代码

sunyj před 9 roky
rodič
revize
d3558396bb

+ 18 - 39
src/main/java/com/uas/report/controller/PrintController.java

@@ -2,7 +2,6 @@ package com.uas.report.controller;
 
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.HashMap;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
@@ -54,21 +53,19 @@ public class PrintController {
 	 * @return
 	 */
 	@RequestMapping("/export")
-	public String export(String userName, String reportName, String whereCondition,
-			Map<String, Object> otherParameters, String exportFileType, HttpServletRequest request,
-			HttpServletResponse response) {
+	@ResponseBody
+	public void export(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
+			String exportFileType, HttpServletRequest request, HttpServletResponse response) {
 		String message = "";
 		if (StringUtils.isEmpty(userName)) {
 			message = "未传入当前账套用户名!";
 			logger.error(message);
-			request.setAttribute("message", message);
-			return "error.jsp";
+			throw new SystemError(message);
 		}
 		if (StringUtils.isEmpty(reportName)) {
 			message = "报表名称无效!";
 			logger.error(message);
-			request.setAttribute("message", message);
-			return "error.jsp";
+			throw new SystemError(message);
 		}
 
 		logger.info("开始导出报表: " + reportName);
@@ -78,10 +75,9 @@ public class PrintController {
 
 		byte[] data = printService.export(userName, reportName, whereCondition, otherParameters, exportFileType);
 		if (ArrayUtils.isEmpty(data)) {
-			message = "报表 " + reportName + " 导出失败!";
+			message = "报表导出失败: " + reportName;
 			logger.error(message);
-			request.setAttribute("message", message);
-			return "error.jsp";
+			throw new SystemError(message);
 		}
 
 		try {
@@ -93,31 +89,7 @@ public class PrintController {
 		} catch (IOException e) {
 			logger.error("浏览器重复请求!");
 		}
-
-		message = "报表 " + reportName + " 导出完成!";
-		logger.info(message);
-		return null;
-	}
-
-	/**
-	 * 测试导出
-	 * 
-	 * @param response
-	 * @return
-	 */
-	@RequestMapping("/test")
-	public String testPrint(HttpServletRequest request, HttpServletResponse response) {
-		String reportName = "Purchase";
-		// String userName = "UAS";
-		// String whereCondition = "where pu_code = 'MP160800017' and pd_qty >
-		// 1000";
-		// 多账套测试
-		String userName = "UAS_DEMO";
-		String whereCondition = "where pu_code = 'YFMP160600001'";
-		Map<String, Object> otherParameters = new HashMap<>();
-		otherParameters.put("OTHER_PARAMETER_TEST", "天气真好!");
-		String exportFileType = "xls";
-		return export(userName, reportName, whereCondition, otherParameters, exportFileType, request, response);
+		logger.info("报表导出完成:  " + reportName);
 	}
 
 	/**
@@ -139,11 +111,16 @@ public class PrintController {
 	@ResponseBody
 	public Map<String, Object> loadPdfData(String userName, String reportName, String whereCondition,
 			Map<String, Object> otherParameters, Integer pageIndex) {
+		String message = "";
 		if (StringUtils.isEmpty(userName)) {
-			throw new SystemError("未传入当前账套用户名!");
+			message = "未传入当前账套用户名!";
+			logger.error(message);
+			throw new SystemError(message);
 		}
 		if (StringUtils.isEmpty(reportName)) {
-			throw new SystemError("报表名称无效!");
+			message = "报表名称无效!";
+			logger.error(message);
+			throw new SystemError(message);
 		}
 
 		logger.info("开始预览报表: " + reportName);
@@ -154,7 +131,9 @@ public class PrintController {
 			data = (byte[]) result.get("data");
 		}
 		if (data == null) {
-			throw new SystemError("获取预览数据失败");
+			message = "获取预览数据失败";
+			logger.error(message);
+			throw new SystemError(message);
 		}
 		// 对pdf流进行base64编码
 		result.put("data", Base64.encodeBase64String(data));

+ 19 - 10
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -53,8 +53,8 @@ public class PrintServiceImpl implements PrintService {
 	private static Logger logger = Logger.getLogger(PrintService.class);
 
 	@Override
-	public byte[] export(String userName, String reportName, String whereCondition,
-			Map<String, Object> otherParameters, String exportFileType) {
+	public byte[] export(String userName, String reportName, String whereCondition, Map<String, Object> otherParameters,
+			String exportFileType) {
 		Map<String, Object> result = print(userName, reportName, whereCondition, otherParameters, exportFileType, null);
 		if (!CollectionUtils.isEmpty(result)) {
 			return (byte[]) result.get("data");
@@ -81,8 +81,11 @@ public class PrintServiceImpl implements PrintService {
 	 */
 	private Map<String, Object> print(String userName, String reportName, String whereCondition,
 			Map<String, Object> otherParameters, String exportFileType, Integer pageIndex) {
+		String message = "";
 		if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(reportName)) {
-			throw new SystemError("参数错误");
+			message = "参数错误";
+			logger.error(message);
+			throw new SystemError(message);
 		}
 
 		// 报表路径为报表根路径REPORT_DIR + 当前账套用户名userName
@@ -94,8 +97,9 @@ public class PrintServiceImpl implements PrintService {
 		File jrxmlFile = new File(jrxmlFilePath);
 		// 报表模板不存在
 		if (!jrxmlFile.exists()) {
-			logger.error("未发现模板文件: " + jrxmlFile.getPath());
-			throw new SystemError("未发现模板文件: " + jrxmlFile.getPath());
+			message = "未发现模板文件: " + jrxmlFile.getPath();
+			logger.error(message);
+			throw new SystemError(message);
 		}
 
 		String jasperFilePath = jrxmlFile.getPath().replace(".jrxml", ".jasper");
@@ -135,7 +139,9 @@ public class PrintServiceImpl implements PrintService {
 			// 获取数据源
 			DataSource dataSource = getDataSource(userName);
 			if (dataSource == null) {
-				throw new SystemError("获取数据源失败");
+				message = "获取数据源失败";
+				logger.error(message);
+				throw new SystemError(message);
 			}
 
 			connection = dataSource.getConnection();
@@ -164,7 +170,7 @@ public class PrintServiceImpl implements PrintService {
 				JRPdfExporter exporter = new JRPdfExporter();
 				SimplePdfReportConfiguration configuration = new SimplePdfReportConfiguration();
 				configuration.setPageIndex(pageIndex);
-				exporter.setConfiguration(configuration);
+				// exporter.setConfiguration(configuration);
 				ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
 				exporter.setExporterInput(exporterInput);
 				OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
@@ -177,11 +183,14 @@ public class PrintServiceImpl implements PrintService {
 				result.put("pageSize", jasperPrint.getPages().size());
 				return result;
 			} else {
-				logger.error("exportFileType 和 pageSize 不能同时为空!");
-				throw new SystemError("exportFileType 和 pageSize 不能同时为空!");
+				message = "exportFileType 和 pageSize 不能同时为空!";
+				logger.error(message);
+				throw new SystemError(message);
 			}
 		} catch (SQLException e) {
-			logger.error("数据库连接错误!");
+			message = "数据库连接错误!";
+			logger.error(message);
+			throw new SystemError(message);
 		} catch (JRException e) {
 			e.printStackTrace();
 		} catch (IOException e) {