|
|
@@ -2,16 +2,22 @@ package com.uas.report.service.impl;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.sql.Connection;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
|
-import org.apache.commons.dbcp.BasicDataSource;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import com.alibaba.druid.pool.DruidDataSource;
|
|
|
+import com.alibaba.druid.pool.DruidDataSourceFactory;
|
|
|
import com.uas.report.service.PrintService;
|
|
|
import com.uas.report.util.ContextUtils;
|
|
|
import com.uas.report.util.ReportConstants;
|
|
|
@@ -34,13 +40,20 @@ public class PrintServiceImpl implements PrintService {
|
|
|
private static Logger logger = Logger.getLogger(PrintService.class);
|
|
|
|
|
|
@Override
|
|
|
- public byte[] print(String reportName, String whereCondition, Map<String, Object> otherParameters) {
|
|
|
+ public byte[] print(String userName, String reportName, String whereCondition,
|
|
|
+ Map<String, Object> otherParameters) {
|
|
|
byte[] results = null;
|
|
|
- if (StringUtils.isEmpty(reportName)) {
|
|
|
+ if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(reportName)) {
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
- File jrxmlFile = new File(ReportConstants.REPORT_DIR + "jrxml" + File.separator + reportName + ".jrxml");
|
|
|
+ // 报表路径为报表根路径REPORT_DIR + 当前账套用户名userName
|
|
|
+ String reportDir = new StringBuilder(ReportConstants.REPORT_DIR).append(userName).append(File.separator)
|
|
|
+ .toString();
|
|
|
+
|
|
|
+ String jrxmlFilePath = new StringBuilder(reportDir).append("jrxml").append(File.separator).append(reportName)
|
|
|
+ .append(".jrxml").toString();
|
|
|
+ File jrxmlFile = new File(jrxmlFilePath);
|
|
|
// 报表模板不存在
|
|
|
if (!jrxmlFile.exists()) {
|
|
|
logger.error("未发现模板文件: " + jrxmlFile.getPath());
|
|
|
@@ -68,8 +81,9 @@ public class PrintServiceImpl implements PrintService {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
+ // 向报表模板传递参数:报表路径、where条件、其他参数
|
|
|
Map<String, Object> parameters = new HashMap<>();
|
|
|
- parameters.put(ReportConstants.PARAMETER_REPORT_DIR, ReportConstants.REPORT_DIR);
|
|
|
+ parameters.put(ReportConstants.PARAMETER_REPORT_DIR, reportDir);
|
|
|
if (!StringUtils.isEmpty(whereCondition)) {
|
|
|
parameters.put(ReportConstants.PARAMETER_WHERE_CONDITION, whereCondition);
|
|
|
}
|
|
|
@@ -78,7 +92,12 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- Connection connection = getDataSource().getConnection();
|
|
|
+ DataSource dataSource = getDataSource(userName);
|
|
|
+ if (dataSource == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Connection connection = dataSource.getConnection();
|
|
|
+ // 打印报表,以字节数组形式导出pdf
|
|
|
results = JasperRunManager.runReportToPdf(jasperFilePath, parameters, connection);
|
|
|
connection.close();
|
|
|
} catch (SQLException e) {
|
|
|
@@ -90,11 +109,74 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 从配置文件中获取dataSource作为报表模板的数据源
|
|
|
+ * 根据当前账套用户名,从主数据库master表获取账套数据库配置信息,作为报表模板的数据源
|
|
|
*
|
|
|
+ * @param userName
|
|
|
+ * 当前账套用户名
|
|
|
* @return
|
|
|
*/
|
|
|
- private BasicDataSource getDataSource() {
|
|
|
- return ContextUtils.getApplicationContext().getBean("dataSource", BasicDataSource.class);
|
|
|
+ private DataSource getDataSource(String userName) {
|
|
|
+ // 默认数据源(主数据库)
|
|
|
+ DruidDataSource defaultDataSource = ContextUtils.getApplicationContext().getBean("defaultSob",
|
|
|
+ DruidDataSource.class);
|
|
|
+ if (defaultDataSource == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 若当前账套用户名为默认数据库用户名
|
|
|
+ if (userName.equals(defaultDataSource.getUsername())) {
|
|
|
+ return defaultDataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ try {
|
|
|
+ connection = defaultDataSource.getConnection();
|
|
|
+ // 根据当前账套用户名获取其数据库配置信息
|
|
|
+ String sql = "select * from master where MA_USER = ?";
|
|
|
+ preparedStatement = connection.prepareStatement(sql);
|
|
|
+ preparedStatement.setString(1, userName);
|
|
|
+ resultSet = preparedStatement.executeQuery();
|
|
|
+ if (resultSet.next()) {
|
|
|
+ String password = resultSet.getString("MS_PWD");
|
|
|
+ if (!StringUtils.isEmpty(password)) {
|
|
|
+ Properties properties = new Properties();
|
|
|
+ properties.put("driverClassName", defaultDataSource.getDriverClassName());
|
|
|
+ properties.put("url", defaultDataSource.getUrl());
|
|
|
+ properties.put("username", userName);
|
|
|
+ properties.put("password", password);
|
|
|
+ properties.put("testWhileIdle", "true");
|
|
|
+ properties.put("validationQuery", "select 1 from SYS.DUAL");
|
|
|
+ return DruidDataSourceFactory.createDataSource(properties);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (resultSet != null) {
|
|
|
+ resultSet.close();
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (preparedStatement != null) {
|
|
|
+ preparedStatement.close();
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ connection.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // defaultDataSource.close();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+
|
|
|
}
|
|
|
}
|