|
|
@@ -1,5 +1,7 @@
|
|
|
package com.uas.report.service.impl;
|
|
|
|
|
|
+import com.alibaba.druid.pool.DruidDataSource;
|
|
|
+import com.alibaba.druid.util.JdbcUtils;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.uas.report.SpecialProperties;
|
|
|
import com.uas.report.SystemProperties;
|
|
|
@@ -107,7 +109,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// throw new ReportException(e).setDetailedMessage(e);
|
|
|
// }
|
|
|
|
|
|
- DataSource dataSource = MasterManager.getDataSource(userName, profile);
|
|
|
+ DruidDataSource dataSource = MasterManager.getDataSource(userName, profile);
|
|
|
if (dataSource == null) {
|
|
|
throw new SQLException("获取数据源失败");
|
|
|
}
|
|
|
@@ -137,7 +139,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
Connection connection = null;
|
|
|
try {
|
|
|
connection = dataSource.getConnection();
|
|
|
- String jasperFilePath = maybeCompileJrxmlFile(jrxmlFilePath, false, otherParameters, connection);
|
|
|
+ String jasperFilePath = maybeCompileJrxmlFile(jrxmlFilePath, false, otherParameters, connection, dataSource.getDbType());
|
|
|
if(StringUtils.isEmpty(jasperFilePath)){
|
|
|
throw new IllegalStateException("编译报表模板失败: " + jrxmlFilePath);
|
|
|
}
|
|
|
@@ -222,13 +224,14 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* 是否为子报表
|
|
|
* @param otherParameters
|
|
|
* @param connection
|
|
|
+ * @param dbType 数据库类型
|
|
|
* @return 编译后的jasper文件路径
|
|
|
* @throws IOException
|
|
|
* @throws SQLException
|
|
|
* @throws DocumentException
|
|
|
*/
|
|
|
private String maybeCompileJrxmlFile(String jrxmlFilePath, boolean isSub, String otherParameters,
|
|
|
- Connection connection) throws IOException, DocumentException, SQLException {
|
|
|
+ Connection connection, String dbType) throws IOException, DocumentException, SQLException {
|
|
|
if (StringUtils.isEmpty(jrxmlFilePath)) {
|
|
|
logger.error("参数不能为空:" + jrxmlFilePath);
|
|
|
return null;
|
|
|
@@ -243,7 +246,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// 首先解析jrxml文件,检查是否引用子报表,如果是,先(递归)编译子报表
|
|
|
List<String> subJrxmlFilePaths = getSubJrxmlFilePath(jrxmlFilePath);
|
|
|
for (String subJrxmlFilePath : subJrxmlFilePaths) {
|
|
|
- maybeCompileJrxmlFile(subJrxmlFilePath, true, otherParameters, connection);
|
|
|
+ maybeCompileJrxmlFile(subJrxmlFilePath, true, otherParameters, connection, dbType);
|
|
|
}
|
|
|
|
|
|
String jasperFilePath = jrxmlFilePath.replace(".jrxml", ".jasper");
|
|
|
@@ -253,15 +256,15 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// 报表模板未编译过
|
|
|
if (!jasperFile.exists()) {
|
|
|
logger.info("正在编译报表模板... " + jrxmlFilePath);
|
|
|
- compileReportToFile(jrxmlFilePath, isSub, jasperFilePath, otherParameters, connection);
|
|
|
+ compileReportToFile(jrxmlFilePath, isSub, jasperFilePath, otherParameters, connection, dbType);
|
|
|
}
|
|
|
// 如果在编译之后,报表模板有更改过 ,重新编译
|
|
|
else if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
|
|
|
logger.info("正在重新编译报表模板... " + jrxmlFilePath);
|
|
|
- compileReportToFile(jrxmlFilePath, isSub, jasperFilePath, otherParameters, connection);
|
|
|
+ compileReportToFile(jrxmlFilePath, isSub, jasperFilePath, otherParameters, connection, dbType);
|
|
|
}
|
|
|
} catch (JRException e) {
|
|
|
- throw new IllegalStateException("编译报表模板失败: " + jrxmlFilePath + " " + ExceptionUtils.getDetailedMessage(e));
|
|
|
+ throw new IllegalStateException("编译报表模板失败: " + jrxmlFilePath, e);
|
|
|
}
|
|
|
return jasperFilePath;
|
|
|
}
|
|
|
@@ -307,6 +310,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param otherParameters
|
|
|
* @param connection
|
|
|
* 数据库连接
|
|
|
+ * @param dbType 数据库类型
|
|
|
* @throws JRException
|
|
|
* @throws IOException
|
|
|
* @throws FileNotFoundException
|
|
|
@@ -314,12 +318,12 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @throws SQLException
|
|
|
*/
|
|
|
private void compileReportToFile(String jrxmlFilePath, boolean isSub, String jasperFilePath, String otherParameters,
|
|
|
- Connection connection)
|
|
|
+ Connection connection, String dbType)
|
|
|
throws JRException, FileNotFoundException, IOException, DocumentException, SQLException {
|
|
|
replaceFont(jrxmlFilePath);
|
|
|
// 不处理子报表,因为其参数来自主报表,无法处理
|
|
|
if (!isSub) {
|
|
|
- processFields(jrxmlFilePath, otherParameters, connection);
|
|
|
+ processFields(jrxmlFilePath, otherParameters, connection, dbType);
|
|
|
}
|
|
|
JasperCompileManager.compileReportToFile(jrxmlFilePath, jasperFilePath);
|
|
|
}
|
|
|
@@ -330,11 +334,12 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param jrxmlFilePath
|
|
|
* @param otherParameters
|
|
|
* @param connection
|
|
|
+ * @param dbType
|
|
|
* @throws IOException
|
|
|
* @throws DocumentException
|
|
|
* @throws SQLException
|
|
|
*/
|
|
|
- private void processFields(String jrxmlFilePath, String otherParameters, Connection connection)
|
|
|
+ private void processFields(String jrxmlFilePath, String otherParameters, Connection connection, String dbType)
|
|
|
throws IOException, DocumentException, SQLException {
|
|
|
XMLWriter xmlWriter = null;
|
|
|
try {
|
|
|
@@ -346,7 +351,13 @@ public class PrintServiceImpl implements PrintService {
|
|
|
String queryString = queryStringElement.getText();
|
|
|
// 如果查询语句中含有WHERE_CONDITION参数,需将其替换掉
|
|
|
if (queryString.contains("$P!{WHERE_CONDITION}")) {
|
|
|
- queryString = queryString.replace("$P!{WHERE_CONDITION}", "where rownum = 0");
|
|
|
+ if (JdbcUtils.MYSQL.equals(dbType)) {
|
|
|
+ queryString = queryString.replace("$P!{WHERE_CONDITION}", "limit 0, 1");
|
|
|
+ } else if (JdbcUtils.ORACLE.equals(dbType)) {
|
|
|
+ queryString = queryString.replace("$P!{WHERE_CONDITION}", "where rownum = 0");
|
|
|
+ } else{
|
|
|
+ throw new IllegalStateException("不支持的数据库类型:" + dbType);
|
|
|
+ }
|
|
|
}
|
|
|
queryString = replaceOtherParameters(queryString, otherParameters);
|
|
|
// 因为获取列名只需要在编译模版时执行一次,所以这里获取数据速度虽然较慢,但是可以接受,
|