Explorar o código

优化对打印、导出类型等常量的处理

sunyj %!s(int64=8) %!d(string=hai) anos
pai
achega
cbb8447dbe

+ 5 - 4
src/main/java/com/uas/report/controller/PdfController.java

@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
+import com.uas.report.model.ExportType;
 import com.uas.report.service.FileService;
 import com.uas.report.service.PrintService;
 import com.uas.report.util.ArrayUtils;
@@ -106,8 +107,8 @@ public class PdfController {
 			byte[] data = (byte[]) result.remove("data");
 			// 相对路径
 			String pdfPath = ReportConstants.GENERATED_FILES_PATH + r + "/"
-					+ fileService.generateFileName(u, pr, w, o, ReportConstants.FILE_TYPE_PDF) + "."
-					+ ReportConstants.FILE_TYPE_PDF;
+					+ fileService.generateFileName(u, pr, w, o, ExportType.PDF.getQualifier()) + "."
+					+ ExportType.PDF.getQualifier();
 			File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
 			FileUtils.write(file.getPath(), data);
 			result.put("path", pdfPath);
@@ -217,8 +218,8 @@ public class PdfController {
 
 		// 相对路径
 		String pdfPath = ReportConstants.GENERATED_FILES_PATH + r + "/"
-				+ fileService.generateFileName(u, pr, w, o, ReportConstants.FILE_TYPE_PDF) + "."
-				+ ReportConstants.FILE_TYPE_PDF;
+				+ fileService.generateFileName(u, pr, w, o, ExportType.PDF.getQualifier()) + "."
+				+ ExportType.PDF.getQualifier();
 		File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
 		String masterOfJrxml = printService.getMasterOfJrxml(u, r);
 		String jrxmlFilePath = fileService.getJrxmlFilePath(masterOfJrxml, r);

+ 28 - 45
src/main/java/com/uas/report/controller/PrintController.java

@@ -17,6 +17,8 @@ import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 
+import com.uas.report.model.ExportType;
+import com.uas.report.model.PrintType;
 import com.uas.report.service.FileService;
 import com.uas.report.service.PrintService;
 import com.uas.report.util.ArrayUtils;
@@ -81,37 +83,31 @@ public class PrintController {
 			throws JRException, IOException, DocumentException, SQLException, ServletException {
 		userName = userName == null ? null : userName.toUpperCase();
 		// printType为空,默认进入预览页
-		if (StringUtils.isEmpty(printType)) {
-			printType = ReportConstants.PRINT_TYPE_PREVIEW;
-		}
+		PrintType type = StringUtils.isEmpty(printType) ? PrintType.PREVIEW : PrintType.checkType(printType);
 
+		switch (type) {
 		// 预览或打印
-		if (printType.equals(ReportConstants.PRINT_TYPE_PREVIEW)
-				|| printType.equals(ReportConstants.PRINT_TYPE_PRINT)) {
+		case PREVIEW:
+		case PRINT:
 			request.getRequestDispatcher("preview?t=" + timestamp).forward(request, response);
-		}
-		// 下载pdf、纯数据excel
-		else if (printType.equals(ReportConstants.PRINT_TYPE_PDF)) {
-			export(userName, profile, reportName, whereCondition, otherParameters, ReportConstants.FILE_TYPE_PDF, true,
+			break;
+		case PDF:
+			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.PDF.getQualifier(), true,
 					title, request, response);
-		}
-		// 该下载接口供UAS系统使用,应其要求,PRINT_TYPE_EXCEL只能为EXCEL(该值意思本应为下载全部数据的excel),
-		// 导致与FILE_TYPE_EXCEL_DATA(下载纯数据的excel)命名不相匹配
-		else if (printType.equals(ReportConstants.PRINT_TYPE_EXCEL)) {
-			export(userName, profile, reportName, whereCondition, otherParameters, ReportConstants.FILE_TYPE_EXCEL_DATA,
+			break;
+		// 该下载接口供 UAS 系统使用,应其要求,printType 为{@link PrintType.EXCEL}时,下载纯数据的 excel
+		case EXCEL:
+			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.XLS_DATA.getQualifier(),
 					true, title, request, response);
-		}
-		// 下载word
-		else if (printType.equals(ReportConstants.PRINT_TYPE_WORD)) {
-			export(userName, profile, reportName, whereCondition, otherParameters, ReportConstants.FILE_TYPE_WORD, true,
+			break;
+		case WORD:
+			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.DOC.getQualifier(), true,
 					title, request, response);
-		}
-		// 下载text
-		else if (printType.equals(ReportConstants.PRINT_TYPE_TEXT)) {
-			export(userName, profile, reportName, whereCondition, otherParameters, ReportConstants.FILE_TYPE_TEXT, true,
+			break;
+		case TEXT:
+			export(userName, profile, reportName, whereCondition, otherParameters, ExportType.TXT.getQualifier(), true,
 					title, request, response);
-		} else {
-			throw new IllegalArgumentException("printType不合法");
+			break;
 		}
 	}
 
@@ -150,17 +146,12 @@ public class PrintController {
 		userName = userName == null ? null : userName.toUpperCase();
 		ReportUtils.checkParameters(userName, reportName);
 		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
-		if (StringUtils.isEmpty(exportFileType)) {
-			exportFileType = ReportConstants.FILE_TYPE_PDF;
-		}
+		ExportType exportType = StringUtils.isEmpty(exportFileType) ? ExportType.PDF
+				: ExportType.checkType(exportFileType);
 
 		String filePath = ReportConstants.GENERATED_FILES_PATH + reportName + "/"
-				+ fileService.generateFileName(userName, profile, whereCondition, otherParameters, exportFileType);
-		if (exportFileType.equals(ReportConstants.FILE_TYPE_EXCEL_DATA)) {
-			filePath += "." + ReportConstants.FILE_TYPE_EXCEL;
-		} else {
-			filePath += "." + exportFileType;
-		}
+				+ fileService.generateFileName(userName, profile, whereCondition, otherParameters, exportFileType) + "."
+				+ exportType.getQualifier();
 		File file = new File(ReportConstants.GENERATED_FILES_DIR + filePath);
 		// 指定flush为true(强制刷新pdf、xls)
 		// 文件无效(不存在或过期),创建
@@ -170,22 +161,14 @@ public class PrintController {
 				throw new IllegalStateException("数据量过大,无法提供服务");
 			}
 			byte[] data = printService.export(userName, profile, reportName, whereCondition, otherParameters,
-					exportFileType);
+					exportType);
 			if (ArrayUtils.isEmpty(data)) {
 				throw new IllegalStateException("报表导出失败:" + userName + "/" + reportName + "\n");
 			}
 			FileUtils.write(file.getPath(), data);
 		}
 
-		String exportFileName = title;
-		if (StringUtils.isEmpty(exportFileName)) {
-			exportFileName = reportName;
-		}
-		if (exportFileType.equals(ReportConstants.FILE_TYPE_EXCEL_DATA)) {
-			exportFileName += "." + ReportConstants.FILE_TYPE_EXCEL;
-		} else {
-			exportFileName += "." + exportFileType;
-		}
+		String exportFileName = (!StringUtils.isEmpty(title) ? title : reportName) + "." + exportType.getQualifier();
 		fileService.rangeDownload(file, exportFileName, request, response);
 	}
 
@@ -224,8 +207,8 @@ public class PrintController {
 
 		// 相对路径,返回给前端
 		String pdfPath = ReportConstants.GENERATED_FILES_PATH + reportName + "/" + fileService
-				.generateFileName(userName, profile, whereCondition, otherParameters, ReportConstants.FILE_TYPE_PDF)
-				+ "." + ReportConstants.FILE_TYPE_PDF;
+				.generateFileName(userName, profile, whereCondition, otherParameters, ExportType.PDF.getQualifier())
+				+ "." + ExportType.PDF.getQualifier();
 		File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
 		// 指定flush为true(强制刷新pdf)
 		// 文件无效(不存在或过期),重新创建pdf文件

+ 69 - 0
src/main/java/com/uas/report/model/ExportType.java

@@ -0,0 +1,69 @@
+package com.uas.report.model;
+
+/**
+ * 导出类型
+ * 
+ * @author sunyj
+ * @since 2017年10月24日 下午4:44:54
+ */
+public enum ExportType {
+
+	/**
+	 * pdf
+	 */
+	PDF("pdf"),
+
+	/**
+	 * excel
+	 */
+	XLS("xls"),
+
+	/**
+	 * 纯数据 excel
+	 */
+	XLS_DATA("xls"),
+
+	/**
+	 * word
+	 */
+	DOC("doc"),
+
+	/**
+	 * text
+	 */
+	TXT("txt");
+
+	private String qualifier;
+
+	public String getQualifier() {
+		return qualifier;
+	}
+
+	private ExportType(String qualifier) {
+		this.qualifier = qualifier;
+	}
+
+	public static ExportType getType(String type) {
+		try {
+			return ExportType.valueOf(type.toUpperCase());
+		} catch (IllegalArgumentException e) {
+			return null;
+		}
+	}
+
+	/**
+	 * 获取导出类型
+	 * 
+	 * @param type
+	 * @return 获取的导出类型
+	 * @throws IllegalArgumentException
+	 *             失败,抛出异常
+	 */
+	public static ExportType checkType(String type) throws IllegalArgumentException {
+		ExportType exportType = getType(type);
+		if (exportType == null) {
+			throw new IllegalArgumentException("不支持该导出类型:" + type);
+		}
+		return exportType;
+	}
+}

+ 64 - 0
src/main/java/com/uas/report/model/PrintType.java

@@ -0,0 +1,64 @@
+package com.uas.report.model;
+
+/**
+ * 打印类型
+ * 
+ * @author sunyj
+ * @since 2017年10月24日 下午4:44:54
+ */
+public enum PrintType {
+
+	/**
+	 * 预览
+	 */
+	PREVIEW,
+
+	/**
+	 * 打印
+	 */
+	PRINT,
+
+	/**
+	 * 下载 pdf
+	 */
+	PDF,
+
+	/**
+	 * 下载纯数据 excel
+	 */
+	EXCEL,
+
+	/**
+	 * 下载 ord
+	 */
+	WORD,
+
+	/**
+	 * 下载 text
+	 */
+	TEXT;
+
+	public static PrintType getType(String type) {
+		try {
+			return PrintType.valueOf(type.toUpperCase());
+		} catch (IllegalArgumentException e) {
+			return null;
+		}
+	}
+
+	/**
+	 * 获取打印类型
+	 * 
+	 * @param type
+	 * @return 获取的打印类型
+	 * @throws IllegalArgumentException
+	 *             失败,抛出异常
+	 */
+	public static PrintType checkType(String type) throws IllegalArgumentException {
+		PrintType printType = getType(type);
+		if (printType == null) {
+			throw new IllegalArgumentException("不支持该打印类型:" + type);
+		}
+		return printType;
+	}
+}

+ 3 - 2
src/main/java/com/uas/report/service/PrintService.java

@@ -6,6 +6,7 @@ import java.util.Map;
 
 import org.dom4j.DocumentException;
 
+import com.uas.report.model.ExportType;
 import com.uas.report.util.Platform;
 
 import net.sf.jasperreports.engine.JRException;
@@ -32,7 +33,7 @@ public interface PrintService {
 	 * @param otherParameters
 	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
 	 *            JSON格式,数据为键值对
-	 * @param exportFileType
+	 * @param exportType
 	 *            报表导出的格式,默认为pdf
 	 * @return 导出的文件的字节数组
 	 * @throws SQLException
@@ -41,7 +42,7 @@ public interface PrintService {
 	 * @throws JRException
 	 */
 	public byte[] export(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, String exportFileType)
+			String otherParameters, ExportType exportType)
 			throws JRException, IOException, DocumentException, SQLException;
 
 	/**

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

@@ -42,6 +42,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.uas.report.SpecialProperties;
 import com.uas.report.SystemProperties;
 import com.uas.report.jasperreports.engine.export.CustomJRXlsExporter;
+import com.uas.report.model.ExportType;
 import com.uas.report.model.Master;
 import com.uas.report.service.FileService;
 import com.uas.report.service.PrintService;
@@ -98,10 +99,10 @@ public class PrintServiceImpl implements PrintService {
 
 	@Override
 	public byte[] export(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, String exportFileType)
+			String otherParameters, ExportType exportType)
 			throws JRException, IOException, DocumentException, SQLException {
-		Map<String, Object> result = print(userName, profile, reportName, whereCondition, otherParameters,
-				exportFileType, null);
+		Map<String, Object> result = print(userName, profile, reportName, whereCondition, otherParameters, exportType,
+				null);
 		if (!CollectionUtils.isEmpty(result)) {
 			return (byte[]) result.get("data");
 		}
@@ -123,7 +124,7 @@ public class PrintServiceImpl implements PrintService {
 	 * @param reportName
 	 * @param whereCondition
 	 * @param otherParameters
-	 * @param exportFileType
+	 * @param exportType
 	 * @param pageIndex
 	 * @return 页码pageSize:int,数据data:byte[]
 	 * @throws JRException
@@ -132,7 +133,7 @@ public class PrintServiceImpl implements PrintService {
 	 * @throws SQLException
 	 */
 	private Map<String, Object> print(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, String exportFileType, Integer pageIndex)
+			String otherParameters, ExportType exportType, Integer pageIndex)
 			throws JRException, IOException, DocumentException, SQLException {
 		// TODO 重新实现jasperserver接口
 		// try {
@@ -174,22 +175,21 @@ public class PrintServiceImpl implements PrintService {
 			// 从数据库获取数据填充报表
 			JasperPrint jasperPrint = null;
 			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-			// exportFileType导出文件的格式不为空,表示是导出,并非预览
-			if (!StringUtils.isEmpty(exportFileType)) {
+			// exportType导出文件的格式不为空,表示是导出,并非预览
+			if (exportType != null) {
 				logger.info("export fillReport...");
 				boolean customCellStyle = false;
 				// 只导出数据
-				if (exportFileType.equals(ReportConstants.FILE_TYPE_EXCEL_DATA)) {
+				if (exportType == ExportType.XLS_DATA) {
 					// 需自定义单元格格式
 					customCellStyle = true;
 					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
 					// 移除模板中多余元素
 					removeUnusedElements(jasperDesign);
-					exportFileType = ReportConstants.FILE_TYPE_EXCEL;
 					JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
 					jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
 
-				} else if (exportFileType.equals(ReportConstants.FILE_TYPE_WORD)) {
+				} else if (exportType == ExportType.DOC) {
 					// 导出word时需要对行距等进行调整
 					InputStream inputStream = new ByteArrayInputStream(
 							modifyLineSpacing(jrxmlFilePath).getBytes("UTF-8"));
@@ -200,11 +200,11 @@ public class PrintServiceImpl implements PrintService {
 					jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
 				}
 
-				if (exportFileType.equals(ReportConstants.FILE_TYPE_EXCEL)) {
+				if (exportType == ExportType.XLS || exportType == ExportType.XLS_DATA) {
 					exportReportToXls(jasperPrint, outputStream, customCellStyle);
-				} else if (exportFileType.equals(ReportConstants.FILE_TYPE_WORD)) {
+				} else if (exportType == ExportType.DOC) {
 					exportReportToDoc(jasperPrint, outputStream);
-				} else if (exportFileType.equals(ReportConstants.FILE_TYPE_TEXT)) {
+				} else if (exportType == ExportType.TXT) {
 					exportReportToText(jasperPrint, outputStream);
 				} else {
 					exportReportToPdf(jasperPrint, outputStream, pageIndex);

+ 0 - 55
src/main/java/com/uas/report/util/ReportConstants.java

@@ -18,61 +18,6 @@ public class ReportConstants {
 	 */
 	public static final String PARAMETER_REPORT_DIR = "REPORT_DIR";
 
-	/**
-	 * 请求类型:预览
-	 */
-	public static final String PRINT_TYPE_PREVIEW = "PREVIEW";
-
-	/**
-	 * 请求类型:打印
-	 */
-	public static final String PRINT_TYPE_PRINT = "PRINT";
-
-	/**
-	 * 请求类型:下载pdf
-	 */
-	public static final String PRINT_TYPE_PDF = "PDF";
-
-	/**
-	 * 请求类型:下载纯数据excel
-	 */
-	public static final String PRINT_TYPE_EXCEL = "EXCEL";
-
-	/**
-	 * 请求类型:下载word
-	 */
-	public static final String PRINT_TYPE_WORD = "WORD";
-
-	/**
-	 * 请求类型:下载text
-	 */
-	public static final String PRINT_TYPE_TEXT = "TEXT";
-
-	/**
-	 * 导出文件的格式:pdf
-	 */
-	public static final String FILE_TYPE_PDF = "pdf";
-
-	/**
-	 * 导出文件的格式:excel
-	 */
-	public static final String FILE_TYPE_EXCEL = "xls";
-
-	/**
-	 * 导出文件的格式:纯数据excel
-	 */
-	public static final String FILE_TYPE_EXCEL_DATA = "xls_data";
-
-	/**
-	 * 导出文件的格式:word
-	 */
-	public static final String FILE_TYPE_WORD = "doc";
-
-	/**
-	 * 导出文件的格式:text
-	 */
-	public static final String FILE_TYPE_TEXT = "txt";
-
 	/**
 	 * 生成的pdf、excel等文件的相对路径
 	 */