|
|
@@ -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);
|
|
|
|