Browse Source

SAAS报表完善,1:模板路劲问题处理 2:完善url返回问题

guq 7 years ago
parent
commit
35a77342b7

+ 91 - 51
src/main/java/com/uas/report/controller/PrintController.java

@@ -4,21 +4,28 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import com.uas.report.model.PrintParameter;
+import com.uas.report.model.PrintType;
 import com.uas.report.util.*;
 import org.dom4j.DocumentException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
+import org.springframework.util.Assert;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 
@@ -48,6 +55,15 @@ public class PrintController {
 
 	private static Logger logger = LoggerFactory.getLogger(PrintController.class);
 
+
+
+
+	/**
+	 * 缓存的打印参数
+	 */
+	private ConcurrentMap<String, PrintParameter> printParameters = new ConcurrentHashMap<>();
+
+
 	/**
 	 * 为UAS系统打印提供服务, 根据printType进行预览、打印、下载pdf、下载纯数据excel等操作
 	 * 
@@ -79,56 +95,63 @@ public class PrintController {
 	public void print(String userName, String profile, String reportName, String whereCondition, String otherParameters,
 			String printType, String title, HttpServletRequest request, HttpServletResponse response)
 			throws JRException, IOException, DocumentException, SQLException, ServletException {
+		if(!StringUtils.isEmpty(reportName)){
+			reportName = URLDecoder.decode(reportName, "UTF-8");
+		}
+		if(!StringUtils.isEmpty(whereCondition)){
+			whereCondition = URLDecoder.decode(whereCondition, "UTF-8");
+		}
+		if(!StringUtils.isEmpty(otherParameters)){
+			otherParameters = URLDecoder.decode(otherParameters, "UTF-8");
+		}
 		// printType为空,默认进入预览页
 		if (StringUtils.isEmpty(printType)) {
 			printType = ReportConstants.PRINT_TYPE_PREVIEW;
 		}
+		// printType为空,默认进入预览页
+		PrintType type = StringUtils.isEmpty(printType) ? PrintType.PREVIEW : PrintType.checkType(printType);
+		//生成requestId
+		String requestId = UUID.randomUUID().toString().replace("-", "");
+		logger.info("generated id... " + requestId);
+		printParameters.put(requestId, new PrintParameter(userName, profile, reportName, whereCondition, otherParameters, type, title));
 
-		// 预览或打印
-		if (printType.equals(ReportConstants.PRINT_TYPE_PREVIEW)
-				|| printType.equals(ReportConstants.PRINT_TYPE_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,
-					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,
-					true, title, request, response);
-		}
-		// 下载word
-		else if (printType.equals(ReportConstants.PRINT_TYPE_WORD)) {
-			export(userName, profile, reportName, whereCondition, otherParameters, ReportConstants.FILE_TYPE_WORD, true,
-					title, request, response);
-		} else {
-			throw new IllegalArgumentException("printType不合法");
+		switch (type) {
+			// 预览或打印
+			case PREVIEW:
+			case PRINT:
+				String previewUrl = String.format("preview?id=%s&printType=%s", requestId, type);
+				if(!StringUtils.isEmpty(reportName)){
+					previewUrl += "&reportName=" + URLEncoder.encode(reportName, "UTF-8");
+				}
+				response.sendRedirect(previewUrl);
+				break;
+			case PDF:
+				export(requestId, ReportConstants.FILE_TYPE_PDF, true,
+						 request, response);
+				break;
+			// 该下载接口供 UAS 系统使用,应其要求,printType 为{@link PrintType.EXCEL}时,下载纯数据的 excel
+			case EXCEL:
+				export(requestId, ReportConstants.FILE_TYPE_EXCEL_DATA,
+						true, request, response);
+				break;
+			case WORD:
+				export(requestId, ReportConstants.FILE_TYPE_WORD, true, request, response);
+				break;
+			default:
+				throw new IllegalArgumentException("printType不合法");
 		}
+
 	}
 
 	/**
 	 * 导出报表
-	 * 
-	 * @param userName
-	 *            不为null;当前账套名称
-	 * @param profile
-	 *            可选(UAS等系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
-	 * @param reportName
-	 *            不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
-	 * @param whereCondition
-	 *            可为null;where之后的条件(包括where)
-	 * @param otherParameters
-	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
+
 	 *            JSON格式,数据为键值对
 	 * @param exportFileType
 	 *            报表导出的格式,默认为pdf
 	 * @param flush
 	 *            是否强制刷新pdf、xls
-	 * @param title
-	 *            导出为文件时的名称
+
 	 * @param request
 	 * @param response
 	 * @throws SQLException
@@ -138,9 +161,15 @@ public class PrintController {
 	 */
 	@RequestMapping("/export")
 	@ResponseBody
-	public void export(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, String exportFileType, Boolean flush, String title, HttpServletRequest request,
+	public void export(String id, String exportFileType, Boolean flush, HttpServletRequest request,
 			HttpServletResponse response) throws JRException, IOException, DocumentException, SQLException {
+		PrintParameter printParameter = getPrintParameter(id, request, response);
+		String userName = printParameter.getUserName();
+		String profile = printParameter.getProfile();
+		String reportName = printParameter.getReportName();
+		String whereCondition = printParameter.getWhereCondition();
+		String otherParameters = printParameter.getOtherParameters();
+		String title = printParameter.getTitle();
 		ReportUtils.checkParameters(userName, reportName);
 		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
 		if (StringUtils.isEmpty(exportFileType)) {
@@ -194,20 +223,26 @@ public class PrintController {
 		}
 	}
 
+
+	/**
+	 * 获取缓存的打印参数
+	 *
+	 * @param id       预打印时获取的 id
+	 * @return 缓存的打印参数
+	 */
+	@RequestMapping(value = "/parameter")
+	@ResponseBody
+	public PrintParameter getPrintParameter(String id, HttpServletRequest request, HttpServletResponse response) {
+		Assert.hasText(id, "Required String parameter 'id' is not present");
+		PrintParameter printParameter = printParameters.get(id);
+		Assert.notNull(printParameter, "id 不存在");
+		return printParameter;
+	}
+
+
 	/**
 	 * 报表预览时获取pdf相对路径
-	 * 
-	 * @param userName
-	 *            不为null;当前账套名称
-	 * @param profile
-	 *            可选(UAS等系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
-	 * @param reportName
-	 *            不为null;需要预览的报表的名称,不带任何后缀(如预览采购单,即为"Purchase")
-	 * @param whereCondition
-	 *            可为null;where之后的条件(包括where)
-	 * @param otherParameters
-	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
-	 *            JSON格式,数据为键值对
+	 *
 	 * @param flush
 	 *            是否强制刷新pdf、xls
 	 * @param request
@@ -220,9 +255,14 @@ public class PrintController {
 	 */
 	@RequestMapping(value = "/pdfPath")
 	@ResponseBody
-	public String getPdfPath(String userName, final String profile, final String reportName,
-			final String whereCondition, final String otherParameters, Boolean flush, HttpServletRequest request,
+	public String getPdfPath(String id, Boolean flush, HttpServletRequest request,
 			HttpServletResponse response) throws JRException, IOException, DocumentException, SQLException {
+		PrintParameter printParameter = getPrintParameter(id, request, response);
+		String userName = printParameter.getUserName();
+		String profile = printParameter.getProfile();
+		String reportName = printParameter.getReportName();
+		String whereCondition = printParameter.getWhereCondition();
+		String otherParameters = printParameter.getOtherParameters();
 		ReportUtils.checkParameters(userName, reportName);
 		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
 

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

@@ -0,0 +1,79 @@
+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"),
+
+    /**
+     * excel
+     */
+    XLSX("xlsx"),
+
+    /**
+     * 纯数据 excel
+     */
+    XLSX_DATA("xlsx"),
+
+    /**
+     * word
+     */
+    DOC("doc"),
+
+    /**
+     * text
+     */
+    TXT("txt");
+
+    private String qualifier;
+
+    public String getQualifier() {
+        return qualifier;
+    }
+
+    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;
+    }
+}

+ 99 - 0
src/main/java/com/uas/report/model/PrintParameter.java

@@ -0,0 +1,99 @@
+package com.uas.report.model;
+import java.io.Serializable;
+
+/**
+ * 打印参数
+ *
+ * @author sunyj
+ * @since 2018/1/23 14:13
+ */
+public class PrintParameter implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 当前账套名称
+     */
+    private String userName;
+
+    /**
+     * 用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev(UAS等系统不必传递该参数)
+     */
+    private String profile;
+
+    /**
+     * 需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
+     */
+    private String reportName;
+
+    /**
+     * where 之后的条件(包括 where)
+     */
+    private String whereCondition;
+
+    /**
+     * 其他参数,区别于 whereCondition,报表某些字段的值取决于这些参数;JSON格式,数据为键值对
+     */
+    private String otherParameters;
+
+    /**
+     * 打印类型,可为 PREVIEW_PRINT_TYPE, PRINT_PRINT_TYPE, PDF_PRINT_TYPE, EXCEL_PRINT_TYPE
+     */
+    private PrintType printType;
+
+    /**
+     * 导出为文件时的名称
+     */
+    private String title;
+
+    public PrintParameter(String userName, String profile, String reportName, String whereCondition, String otherParameters, PrintType printType, String title) {
+        this.userName = userName;
+        this.profile = profile;
+        this.reportName = reportName;
+        this.whereCondition = whereCondition;
+        this.otherParameters = otherParameters;
+        this.printType = printType;
+        this.title = title;
+    }
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public String getProfile() {
+        return profile;
+    }
+
+    public String getReportName() {
+        return reportName;
+    }
+
+    public String getWhereCondition() {
+        return whereCondition;
+    }
+
+    public String getOtherParameters() {
+        return otherParameters;
+    }
+
+    public PrintType getPrintType() {
+        return printType;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    @Override
+    public String toString() {
+        return "PrintParameter{" +
+                "userName='" + userName + '\'' +
+                ", profile='" + profile + '\'' +
+                ", reportName='" + reportName + '\'' +
+                ", whereCondition='" + whereCondition + '\'' +
+                ", otherParameters='" + otherParameters + '\'' +
+                ", printType=" + printType +
+                ", title='" + title + '\'' +
+                '}';
+    }
+}

+ 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;
+    }
+}

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

@@ -619,6 +619,7 @@ public class PrintServiceImpl implements PrintService {
 			 *     /jrxml
 			 *     /Picture
 			 */
+			userName = userName.substring(userName.indexOf("/") + 1, userName.length());
 			String jrxmlFilePath = fileService.getJrxmlFilePath(userName, reportName);
 			File jrxmlFile = new File(jrxmlFilePath);
 			// 报表模板不存在,返回SAAS标准模板账套