Quellcode durchsuchen

允许配置主账套与子帐套是否共用模板

sunyj vor 9 Jahren
Ursprung
Commit
b1bc931da4

+ 14 - 0
src/main/java/com/uas/report/SpecialProperties.java

@@ -48,6 +48,12 @@ public class SpecialProperties {
 	@Value("${standardJrxmlsUrl}")
 	private String standardJrxmlsUrl;
 
+	/**
+	 * 主账套与其子帐套是否共用模板
+	 */
+	@Value("${shareJrxmlsWithSubMaster}")
+	private boolean shareJrxmlsWithSubMaster;
+
 	/**
 	 * 数据源配置信息
 	 */
@@ -102,6 +108,14 @@ public class SpecialProperties {
 		this.standardJrxmlsUrl = standardJrxmlsUrl;
 	}
 
+	public boolean shareJrxmlsWithSubMaster() {
+		return shareJrxmlsWithSubMaster;
+	}
+
+	public void setShareJrxmlsWithSubMaster(boolean shareJrxmlsWithSubMaster) {
+		this.shareJrxmlsWithSubMaster = shareJrxmlsWithSubMaster;
+	}
+
 	public String getDataSourceInformation() {
 		return dataSourceInformation;
 	}

+ 9 - 8
src/main/java/com/uas/report/controller/PrintController.java

@@ -123,7 +123,7 @@ public class PrintController {
 	public void export(String userName, String profile, String reportName, String whereCondition,
 			String otherParameters, String exportFileType, HttpServletRequest request, HttpServletResponse response) {
 		ReportUtils.checkParameters(userName, reportName);
-
+		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
 		if (StringUtils.isEmpty(exportFileType)) {
 			exportFileType = ReportConstants.PDF_FILE_TYPE;
 		}
@@ -139,7 +139,7 @@ public class PrintController {
 		}
 		File file = new File(ReportConstants.GENERATED_FILES_DIR + filePath);
 		// 文件无效(不存在或过期),创建
-		if (!fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(userName, reportName))) {
+		if (!fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(masterOfJrxml, reportName))) {
 			data = printService.export(userName, profile, reportName, whereCondition, otherParameters, exportFileType);
 			if (ArrayUtils.isEmpty(data)) {
 				throw new ReportException("报表导出失败:" + userName + "/" + reportName + "\n");
@@ -208,6 +208,7 @@ public class PrintController {
 			final String whereCondition, final String otherParameters, Integer pageIndex, HttpServletRequest request,
 			HttpServletResponse response) {
 		ReportUtils.checkParameters(userName, reportName);
+		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
 		Map<String, Object> result = new HashMap<>();
 
 		// 相对路径,返回给前端
@@ -218,7 +219,7 @@ public class PrintController {
 		final File file = new File(ReportConstants.GENERATED_FILES_DIR + pdfPath);
 		if (pageIndex == null || pageIndex == 1) {
 			// 文件无效(不存在或过期),重新创建pdf文件
-			if (!fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(userName, reportName))) {
+			if (!fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(masterOfJrxml, reportName))) {
 				// 参数pageIndex为null或1,表示是直接打印或预览第一页,
 				// 需要生成第一页(可能页数过多,提示用户不支持预览打印),再在后台开线程生成总的pdf
 				// 先生成第一页pdf
@@ -245,7 +246,7 @@ public class PrintController {
 			if (!fileService.isFileValid(
 					file.getPath().replace("." + ReportConstants.PDF_FILE_TYPE,
 							"_" + pageIndex + "." + ReportConstants.PDF_FILE_TYPE),
-					fileService.getJrxmlFilePath(userName, reportName))) {
+					fileService.getJrxmlFilePath(masterOfJrxml, reportName))) {
 				printService.createPdfFile(userName, profile, reportName, whereCondition, otherParameters,
 						file.getPath().replace("." + ReportConstants.PDF_FILE_TYPE,
 								"_" + pageIndex + "." + ReportConstants.PDF_FILE_TYPE),
@@ -288,21 +289,21 @@ public class PrintController {
 				userName, profile, reportName, whereCondition, otherParameters, ReportConstants.PDF_FILE_TYPE);
 		File file = null;
 
-		userName = printService.processMasterOfB2B(userName, reportName);
+		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
 		if (fileType.equals(ReportConstants.PDF_FILE_TYPE)) {
 			file = new File(ReportConstants.GENERATED_FILES_DIR + filePath + "." + ReportConstants.PDF_FILE_TYPE);
 			result.put("valid",
-					fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(userName, reportName)));
+					fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(masterOfJrxml, reportName)));
 			result.put("size", file.length());
 		} else if (fileType.equals(ReportConstants.EXCEL_FILE_TYPE)) {
 			file = new File(ReportConstants.GENERATED_FILES_DIR + filePath + "." + ReportConstants.EXCEL_FILE_TYPE);
 			result.put("valid",
-					fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(userName, reportName)));
+					fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(masterOfJrxml, reportName)));
 			result.put("size", file.length());
 		} else if (fileType.equals(ReportConstants.EXCEL_WITH_ONLY_DATA_FILE_TYPE)) {
 			file = new File(ReportConstants.GENERATED_FILES_DIR + filePath + "." + ReportConstants.EXCEL_FILE_TYPE);
 			result.put("valid",
-					fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(userName, reportName)));
+					fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(masterOfJrxml, reportName)));
 			result.put("size", file.length());
 		} else {
 			throw new ReportException("fileType只能为pdf、xls、xls_with_only_data");

+ 54 - 0
src/main/java/com/uas/report/model/Master.java

@@ -0,0 +1,54 @@
+package com.uas.report.model;
+
+import org.springframework.util.StringUtils;
+
+/**
+ * 账套
+ * 
+ * @author sunyj
+ * @since 2017年2月15日 下午1:57:31
+ */
+public class Master {
+
+	private String name;
+
+	private Master parent;
+
+	public Master(String name) {
+		this(name, null);
+	}
+
+	public Master(String name, Master parent) {
+		if (StringUtils.isEmpty(name)) {
+			throw new IllegalArgumentException("账套名称不能为空");
+		}
+		this.name = name;
+		this.parent = parent;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public Master getParent() {
+		return parent;
+	}
+
+	public void setParent(Master parent) {
+		this.parent = parent;
+	}
+
+	public boolean hasParent() {
+		return parent != null && !StringUtils.isEmpty(parent.getName());
+	}
+
+	@Override
+	public String toString() {
+		return "Master [name=" + name + ", parent=" + parent + "]";
+	}
+
+}

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

@@ -76,14 +76,17 @@ public interface PrintService {
 			String otherParameters, String pdfFilePath, Integer pageIndex);
 
 	/**
-	 * 处理B2B账套,如果企业没有自己的模板,则使用B2B的标准模板(返回B2B标准账套)
+	 * 获取模板对应的账套
+	 * 
+	 * 1. 处理B2B账套,如果企业没有自己的模板,则使用B2B的标准模板(返回B2B标准账套)
+	 * 2. 有些主账套和子帐套共用主账套的模板,需要进行处理,返回主账套的名称
 	 * 
 	 * @param userName
 	 *            账套名称
 	 * @param reportName
 	 *            模板名称
-	 * @return 处理后的B2B账套
+	 * @return 处理后的账套
 	 */
-	String processMasterOfB2B(String userName, String reportName);
+	public String getMasterOfJrxml(String userName, String reportName);
 
 }

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

@@ -41,9 +41,10 @@ import com.alibaba.fastjson.JSONObject;
 import com.uas.report.SpecialProperties;
 import com.uas.report.core.exception.ReportException;
 import com.uas.report.jasperreports.engine.export.CustomJRXlsExporter;
+import com.uas.report.model.Master;
 import com.uas.report.service.FileService;
 import com.uas.report.service.PrintService;
-import com.uas.report.util.DataSourceUtils;
+import com.uas.report.util.MasterManager;
 import com.uas.report.util.FileUtils;
 import com.uas.report.util.ReportConstants;
 import com.uas.report.util.ReportUtils;
@@ -86,12 +87,8 @@ public class PrintServiceImpl implements PrintService {
 	@Override
 	public byte[] export(String userName, String profile, String reportName, String whereCondition,
 			String otherParameters, String exportFileType) {
-		DataSource dataSource = DataSourceUtils.getDataSource(userName, profile);
-		if (dataSource == null) {
-			throw new ReportException("获取数据源失败");
-		}
-		Map<String, Object> result = print(userName, reportName, whereCondition, otherParameters, exportFileType, null,
-				dataSource);
+		Map<String, Object> result = print(userName, profile, reportName, whereCondition, otherParameters,
+				exportFileType, null);
 		if (!CollectionUtils.isEmpty(result)) {
 			return (byte[]) result.get("data");
 		}
@@ -101,26 +98,23 @@ public class PrintServiceImpl implements PrintService {
 	@Override
 	public Map<String, Object> preview(String userName, String profile, String reportName, String whereCondition,
 			String otherParameters, Integer pageIndex) {
-		DataSource dataSource = DataSourceUtils.getDataSource(userName, profile);
-		if (dataSource == null) {
-			throw new ReportException("获取数据源失败");
-		}
-		return print(userName, reportName, whereCondition, otherParameters, null, pageIndex, dataSource);
+		return print(userName, profile, reportName, whereCondition, otherParameters, null, pageIndex);
 	}
 
 	/**
 	 * 输出报表的数据
 	 * 
 	 * @param userName
+	 * @param profile
 	 * @param reportName
 	 * @param whereCondition
 	 * @param otherParameters
 	 * @param exportFileType
 	 * @param pageIndex
-	 * @return
+	 * @return 页码pageSize:int,数据data:byte[]
 	 */
-	private Map<String, Object> print(String userName, String reportName, String whereCondition, String otherParameters,
-			String exportFileType, Integer pageIndex, DataSource dataSource) {
+	private Map<String, Object> print(String userName, String profile, String reportName, String whereCondition,
+			String otherParameters, String exportFileType, Integer pageIndex) {
 		// TODO 重新实现jasperserver接口
 		// try {
 		// resourceService.syncResources(userName);
@@ -128,12 +122,17 @@ public class PrintServiceImpl implements PrintService {
 		// throw new ReportException(e).setDetailedMessage(e);
 		// }
 
-		userName = processMasterOfB2B(userName, reportName);
-		String jrxmlFilePath = fileService.getJrxmlFilePath(userName, reportName);
+		DataSource dataSource = MasterManager.getDataSource(userName, profile);
+		if (dataSource == null) {
+			throw new ReportException("获取数据源失败");
+		}
+
+		String masterOfJrxml = getMasterOfJrxml(userName, reportName);
+		String jrxmlFilePath = fileService.getJrxmlFilePath(masterOfJrxml, reportName);
 
 		// 向报表模板传递参数:报表路径、where条件、其他参数
 		Map<String, Object> parameters = new HashMap<>();
-		parameters.put(ReportConstants.PARAMETER_REPORT_DIR, fileService.getMasterPath(userName));
+		parameters.put(ReportConstants.PARAMETER_REPORT_DIR, fileService.getMasterPath(masterOfJrxml));
 		if (!StringUtils.isEmpty(whereCondition)) {
 			parameters.put(ReportConstants.PARAMETER_WHERE_CONDITION, whereCondition);
 		}
@@ -503,7 +502,7 @@ public class PrintServiceImpl implements PrintService {
 	}
 
 	@Override
-	public String processMasterOfB2B(String userName, String reportName) {
+	public String getMasterOfJrxml(String userName, String reportName) {
 		ReportUtils.checkParameters(userName, reportName);
 		if (userName.startsWith("B2B")) {
 			String jrxmlFilePath = fileService.getJrxmlFilePath(userName, reportName);
@@ -512,6 +511,18 @@ public class PrintServiceImpl implements PrintService {
 			if (!jrxmlFile.exists()) {
 				return specialProperties.getStandardMaster() + "/B2B";
 			}
+		} else {
+			// 如果主账套与子账套共用模板
+			if (specialProperties.shareJrxmlsWithSubMaster()) {
+				Master master = MasterManager.getMaster(userName);
+				if (master != null) {
+					// 获取账套的主账套
+					while (master.hasParent()) {
+						master = MasterManager.getMaster(master.getParent().getName());
+					}
+					userName = master.getName();
+				}
+			}
 		}
 		return userName;
 	}

+ 41 - 3
src/main/java/com/uas/report/util/DataSourceUtils.java → src/main/java/com/uas/report/util/MasterManager.java

@@ -4,6 +4,7 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -19,21 +20,27 @@ import com.alibaba.druid.pool.DruidDataSource;
 import com.alibaba.fastjson.JSONObject;
 import com.uas.report.SpecialProperties;
 import com.uas.report.core.exception.ReportException;
+import com.uas.report.model.Master;
 
 /**
- * 数据源工具类
+ * 账套及其数据源管理
  * 
  * @author sunyj
  * @since 2016年11月4日 下午4:33:56
  */
-public class DataSourceUtils {
+public class MasterManager {
 
 	/**
 	 * 数据源
 	 */
 	private static Map<String, DruidDataSource> dataSources = new ConcurrentHashMap<>();
 
-	private static Logger logger = LoggerFactory.getLogger(DataSourceUtils.class);
+	/**
+	 * 账套
+	 */
+	private static Set<Master> masters = new HashSet<>();
+
+	private static Logger logger = LoggerFactory.getLogger(MasterManager.class);
 
 	static {
 		initAllDataSources();
@@ -50,9 +57,19 @@ public class DataSourceUtils {
 		for (Entry<String, Object> entry : entrySet) {
 			DruidDataSource dataSource = JSONObject.parseObject(entry.getValue().toString(), DruidDataSource.class);
 			dataSources.put(entry.getKey(), dataSource);
+			masters.add(new Master(entry.getKey()));
 		}
 	}
 
+	/**
+	 * 获取账套对应的数据源
+	 * 
+	 * @param userName
+	 *            账套名称
+	 * @param profile
+	 *            用于识别B2B、B2C账套的环境(dev、test、prod)
+	 * @return 数据源
+	 */
 	public static DataSource getDataSource(String userName, String profile) {
 		if (StringUtils.isEmpty(userName)) {
 			return null;
@@ -69,6 +86,8 @@ public class DataSourceUtils {
 				dataSource = getDataSourceFromMainMaster(entry.getValue(), userName);
 				if (dataSource != null) {
 					dataSources.put(userName, dataSource);
+					Master parent = new Master(entry.getKey());
+					masters.add(new Master(userName, parent));
 					return dataSource;
 				}
 			}
@@ -143,4 +162,23 @@ public class DataSourceUtils {
 		}
 		return null;
 	}
+
+	/**
+	 * 根据账套名获取账套
+	 * 
+	 * @param userName
+	 *            账套名称
+	 * @return 账套
+	 */
+	public static Master getMaster(String userName) {
+		if (StringUtils.isEmpty(userName)) {
+			return null;
+		}
+		for (Master master : masters) {
+			if (master.getName().equals(userName)) {
+				return master;
+			}
+		}
+		return null;
+	}
 }