|
|
@@ -162,7 +162,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
connection = dataSource.getConnection();
|
|
|
logger.info("dataSource.getConnection done..." + userName);
|
|
|
|
|
|
- String jasperFilePath = maybeCompileJrxmlFile(jrxmlFilePath, connection);
|
|
|
+ String jasperFilePath = maybeCompileJrxmlFile(jrxmlFilePath, otherParameters, connection);
|
|
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
byte[] data = null;
|
|
|
@@ -233,11 +233,13 @@ public class PrintServiceImpl implements PrintService {
|
|
|
/**
|
|
|
* 可能需要编译报表模板文件
|
|
|
*
|
|
|
- * @param jrxmlFile
|
|
|
+ * @param jrxmlFilePath
|
|
|
* 报表模板文件
|
|
|
+ * @param otherParameters
|
|
|
+ * @param connection
|
|
|
* @return 编译后的jasper文件路径
|
|
|
*/
|
|
|
- private String maybeCompileJrxmlFile(String jrxmlFilePath, Connection connection) {
|
|
|
+ private String maybeCompileJrxmlFile(String jrxmlFilePath, String otherParameters, Connection connection) {
|
|
|
if (StringUtils.isEmpty(jrxmlFilePath)) {
|
|
|
logger.error("参数不能为空:" + jrxmlFilePath);
|
|
|
return null;
|
|
|
@@ -253,7 +255,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
try {
|
|
|
List<String> subJrxmlFilePaths = getSubJrxmlFilePath(jrxmlFilePath);
|
|
|
for (String subJrxmlFilePath : subJrxmlFilePaths) {
|
|
|
- maybeCompileJrxmlFile(subJrxmlFilePath, connection);
|
|
|
+ maybeCompileJrxmlFile(subJrxmlFilePath, otherParameters, connection);
|
|
|
}
|
|
|
} catch (DocumentException e) {
|
|
|
throw new ReportException(e).setDetailedMessage(e);
|
|
|
@@ -266,12 +268,12 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// 报表模板未编译过
|
|
|
if (!jasperFile.exists()) {
|
|
|
logger.info("正在编译报表模板... " + jrxmlFilePath);
|
|
|
- compileReportToFile(jrxmlFilePath, jasperFilePath, connection);
|
|
|
+ compileReportToFile(jrxmlFilePath, jasperFilePath, otherParameters, connection);
|
|
|
} else {
|
|
|
// 如果在编译之后,报表模板有更改过 ,重新编译
|
|
|
if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
|
|
|
logger.info("正在重新编译报表模板... " + jrxmlFilePath);
|
|
|
- compileReportToFile(jrxmlFilePath, jasperFilePath, connection);
|
|
|
+ compileReportToFile(jrxmlFilePath, jasperFilePath, otherParameters, connection);
|
|
|
}
|
|
|
}
|
|
|
} catch (JRException e) {
|
|
|
@@ -317,13 +319,14 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* 模板文件
|
|
|
* @param jasperFilePath
|
|
|
* 编译后的文件
|
|
|
+ * @param otherParameters
|
|
|
* @param connection
|
|
|
* 数据库连接
|
|
|
* @throws JRException
|
|
|
*/
|
|
|
- private void compileReportToFile(String jrxmlFilePath, String jasperFilePath, Connection connection)
|
|
|
- throws JRException {
|
|
|
- processFields(jrxmlFilePath, connection);
|
|
|
+ private void compileReportToFile(String jrxmlFilePath, String jasperFilePath, String otherParameters,
|
|
|
+ Connection connection) throws JRException {
|
|
|
+ processFields(jrxmlFilePath, otherParameters, connection);
|
|
|
JasperCompileManager.compileReportToFile(jrxmlFilePath, jasperFilePath);
|
|
|
}
|
|
|
|
|
|
@@ -331,9 +334,10 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* 处理模板中的field(与数据库中列名进行比较,删除表中无对应列名的field)
|
|
|
*
|
|
|
* @param jrxmlFilePath
|
|
|
+ * @param otherParameters
|
|
|
* @param connection
|
|
|
*/
|
|
|
- private void processFields(String jrxmlFilePath, Connection connection) {
|
|
|
+ private void processFields(String jrxmlFilePath, String otherParameters, Connection connection) {
|
|
|
XMLWriter xmlWriter = null;
|
|
|
try {
|
|
|
SAXReader saxReader = new SAXReader();
|
|
|
@@ -351,6 +355,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
else if (queryString.toUpperCase().contains("WHERE")) {
|
|
|
queryString = queryString.substring(0, queryString.toUpperCase().indexOf("WHERE")) + "where rownum = 0";
|
|
|
}
|
|
|
+ queryString = replaceOtherParameters(queryString, otherParameters);
|
|
|
List<String> columnNames = getColumnNames(connection, queryString);
|
|
|
if (CollectionUtils.isEmpty(columnNames)) {
|
|
|
throw new ReportException("未查询到任何列:" + queryString);
|
|
|
@@ -699,29 +704,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
if (queryString.contains("$P!{WHERE_CONDITION}") && !StringUtils.isEmpty(whereCondition)) {
|
|
|
queryString = queryString.replace("$P!{WHERE_CONDITION}", whereCondition);
|
|
|
}
|
|
|
- // 替换其他参数
|
|
|
- if (queryString.contains("$P{")) {
|
|
|
- if (StringUtils.isEmpty(otherParameters)) {
|
|
|
- throw new ReportException("未传入动态参数");
|
|
|
- }
|
|
|
- JSONObject parameters = JSONObject.parseObject(otherParameters);
|
|
|
- Pattern pattern = Pattern.compile("\\$P\\{([^\\$]*)\\}");
|
|
|
- Matcher matcher = pattern.matcher(queryString);
|
|
|
- while (matcher.find()) {
|
|
|
- String match = matcher.group();
|
|
|
- String parameterName = match.substring("$P{".length(), match.length() - 1);
|
|
|
- Object value = parameters.get(parameterName);
|
|
|
- if (value == null) {
|
|
|
- throw new ReportException("未传入动态参数:" + parameterName);
|
|
|
- }
|
|
|
- // 如果不是数字,需以单引号括起来
|
|
|
- if (value instanceof Number) {
|
|
|
- queryString = queryString.replace(match, value.toString());
|
|
|
- } else {
|
|
|
- queryString = queryString.replace(match, "'" + value.toString() + "'");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ queryString = replaceOtherParameters(queryString, otherParameters);
|
|
|
return getCount(connection, queryString);
|
|
|
} catch (DocumentException | SQLException e) {
|
|
|
throw new ReportException(e).setDetailedMessage(e);
|
|
|
@@ -736,6 +719,40 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 替换sql语句中的动态参数
|
|
|
+ *
|
|
|
+ * @param sql
|
|
|
+ * @param otherParameters
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String replaceOtherParameters(String sql, String otherParameters) {
|
|
|
+ // 替换其他参数
|
|
|
+ if (sql.contains("$P{")) {
|
|
|
+ if (StringUtils.isEmpty(otherParameters)) {
|
|
|
+ throw new ReportException("未传入动态参数");
|
|
|
+ }
|
|
|
+ JSONObject parameters = JSONObject.parseObject(otherParameters);
|
|
|
+ Pattern pattern = Pattern.compile("\\$P\\{([^\\$]*)\\}");
|
|
|
+ Matcher matcher = pattern.matcher(sql);
|
|
|
+ while (matcher.find()) {
|
|
|
+ String match = matcher.group();
|
|
|
+ String parameterName = match.substring("$P{".length(), match.length() - 1);
|
|
|
+ Object value = parameters.get(parameterName);
|
|
|
+ if (value == null) {
|
|
|
+ throw new ReportException("未传入动态参数:" + parameterName);
|
|
|
+ }
|
|
|
+ // 如果不是数字,需以单引号括起来
|
|
|
+ if (value instanceof Number) {
|
|
|
+ sql = sql.replace(match, value.toString());
|
|
|
+ } else {
|
|
|
+ sql = sql.replace(match, "'" + value.toString() + "'");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sql;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前查询语句的结果数目
|
|
|
*
|