|
@@ -41,12 +41,12 @@ import org.springframework.stereotype.Service;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.uas.report.SpecialProperties;
|
|
import com.uas.report.SpecialProperties;
|
|
|
import com.uas.report.SystemProperties;
|
|
import com.uas.report.SystemProperties;
|
|
|
-import com.uas.report.core.exception.ReportException;
|
|
|
|
|
import com.uas.report.jasperreports.engine.export.CustomJRXlsExporter;
|
|
import com.uas.report.jasperreports.engine.export.CustomJRXlsExporter;
|
|
|
import com.uas.report.model.Master;
|
|
import com.uas.report.model.Master;
|
|
|
import com.uas.report.service.FileService;
|
|
import com.uas.report.service.FileService;
|
|
|
import com.uas.report.service.PrintService;
|
|
import com.uas.report.service.PrintService;
|
|
|
import com.uas.report.util.CollectionUtils;
|
|
import com.uas.report.util.CollectionUtils;
|
|
|
|
|
+import com.uas.report.util.ExceptionUtils;
|
|
|
import com.uas.report.util.FileUtils;
|
|
import com.uas.report.util.FileUtils;
|
|
|
import com.uas.report.util.MasterManager;
|
|
import com.uas.report.util.MasterManager;
|
|
|
import com.uas.report.util.Platform;
|
|
import com.uas.report.util.Platform;
|
|
@@ -83,7 +83,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private FileService fileService;
|
|
private FileService fileService;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private SystemProperties systemProperties;
|
|
private SystemProperties systemProperties;
|
|
|
|
|
|
|
@@ -97,7 +97,8 @@ public class PrintServiceImpl implements PrintService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public byte[] export(String userName, String profile, String reportName, String whereCondition,
|
|
public byte[] export(String userName, String profile, String reportName, String whereCondition,
|
|
|
- String otherParameters, String exportFileType) {
|
|
|
|
|
|
|
+ String otherParameters, String exportFileType)
|
|
|
|
|
+ throws JRException, IOException, DocumentException, SQLException {
|
|
|
Map<String, Object> result = print(userName, profile, reportName, whereCondition, otherParameters,
|
|
Map<String, Object> result = print(userName, profile, reportName, whereCondition, otherParameters,
|
|
|
exportFileType, null);
|
|
exportFileType, null);
|
|
|
if (!CollectionUtils.isEmpty(result)) {
|
|
if (!CollectionUtils.isEmpty(result)) {
|
|
@@ -108,7 +109,8 @@ public class PrintServiceImpl implements PrintService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Map<String, Object> preview(String userName, String profile, String reportName, String whereCondition,
|
|
public Map<String, Object> preview(String userName, String profile, String reportName, String whereCondition,
|
|
|
- String otherParameters, Integer pageIndex) {
|
|
|
|
|
|
|
+ String otherParameters, Integer pageIndex)
|
|
|
|
|
+ throws JRException, IOException, DocumentException, SQLException {
|
|
|
return print(userName, profile, reportName, whereCondition, otherParameters, null, pageIndex);
|
|
return print(userName, profile, reportName, whereCondition, otherParameters, null, pageIndex);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -123,9 +125,14 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param exportFileType
|
|
* @param exportFileType
|
|
|
* @param pageIndex
|
|
* @param pageIndex
|
|
|
* @return 页码pageSize:int,数据data:byte[]
|
|
* @return 页码pageSize:int,数据data:byte[]
|
|
|
|
|
+ * @throws JRException
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ * @throws DocumentException
|
|
|
|
|
+ * @throws SQLException
|
|
|
*/
|
|
*/
|
|
|
private Map<String, Object> print(String userName, String profile, String reportName, String whereCondition,
|
|
private Map<String, Object> print(String userName, String profile, String reportName, String whereCondition,
|
|
|
- String otherParameters, String exportFileType, Integer pageIndex) {
|
|
|
|
|
|
|
+ String otherParameters, String exportFileType, Integer pageIndex)
|
|
|
|
|
+ throws JRException, IOException, DocumentException, SQLException {
|
|
|
// TODO 重新实现jasperserver接口
|
|
// TODO 重新实现jasperserver接口
|
|
|
// try {
|
|
// try {
|
|
|
// resourceService.syncResources(userName);
|
|
// resourceService.syncResources(userName);
|
|
@@ -135,7 +142,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
|
|
|
|
|
DataSource dataSource = MasterManager.getDataSource(userName, profile);
|
|
DataSource dataSource = MasterManager.getDataSource(userName, profile);
|
|
|
if (dataSource == null) {
|
|
if (dataSource == null) {
|
|
|
- throw new ReportException("获取数据源失败");
|
|
|
|
|
|
|
+ throw new SQLException("获取数据源失败");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String masterOfJrxml = getMasterOfJrxml(userName, reportName);
|
|
String masterOfJrxml = getMasterOfJrxml(userName, reportName);
|
|
@@ -214,15 +221,9 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
}
|
|
|
result.put("data", data);
|
|
result.put("data", data);
|
|
|
return result;
|
|
return result;
|
|
|
- } catch (Throwable e) {
|
|
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
|
|
} finally {
|
|
} finally {
|
|
|
if (connection != null) {
|
|
if (connection != null) {
|
|
|
- try {
|
|
|
|
|
- connection.close();
|
|
|
|
|
- } catch (SQLException e) {
|
|
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ connection.close();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -236,10 +237,11 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param connection
|
|
* @param connection
|
|
|
* @return 编译后的jasper文件路径
|
|
* @return 编译后的jasper文件路径
|
|
|
* @throws IOException
|
|
* @throws IOException
|
|
|
- * @throws FileNotFoundException
|
|
|
|
|
|
|
+ * @throws SQLException
|
|
|
|
|
+ * @throws DocumentException
|
|
|
*/
|
|
*/
|
|
|
private String maybeCompileJrxmlFile(String jrxmlFilePath, String otherParameters, Connection connection)
|
|
private String maybeCompileJrxmlFile(String jrxmlFilePath, String otherParameters, Connection connection)
|
|
|
- throws FileNotFoundException, IOException {
|
|
|
|
|
|
|
+ throws IOException, DocumentException, SQLException {
|
|
|
if (StringUtils.isEmpty(jrxmlFilePath)) {
|
|
if (StringUtils.isEmpty(jrxmlFilePath)) {
|
|
|
logger.error("参数不能为空:" + jrxmlFilePath);
|
|
logger.error("参数不能为空:" + jrxmlFilePath);
|
|
|
return null;
|
|
return null;
|
|
@@ -248,17 +250,13 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// 报表模板不存在
|
|
// 报表模板不存在
|
|
|
if (!jrxmlFile.exists()) {
|
|
if (!jrxmlFile.exists()) {
|
|
|
// 替换windows下路径中的双反斜杠为单斜杠
|
|
// 替换windows下路径中的双反斜杠为单斜杠
|
|
|
- throw new ReportException("未发现模板文件:" + jrxmlFilePath.replaceAll("\\\\", "/"));
|
|
|
|
|
|
|
+ throw new FileNotFoundException("未发现模板文件:" + jrxmlFilePath.replaceAll("\\\\", "/"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 首先解析jrxml文件,检查是否引用子报表,如果是,先(递归)编译子报表
|
|
// 首先解析jrxml文件,检查是否引用子报表,如果是,先(递归)编译子报表
|
|
|
- try {
|
|
|
|
|
- List<String> subJrxmlFilePaths = getSubJrxmlFilePath(jrxmlFilePath);
|
|
|
|
|
- for (String subJrxmlFilePath : subJrxmlFilePaths) {
|
|
|
|
|
- maybeCompileJrxmlFile(subJrxmlFilePath, otherParameters, connection);
|
|
|
|
|
- }
|
|
|
|
|
- } catch (DocumentException e) {
|
|
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
|
|
|
|
+ List<String> subJrxmlFilePaths = getSubJrxmlFilePath(jrxmlFilePath);
|
|
|
|
|
+ for (String subJrxmlFilePath : subJrxmlFilePaths) {
|
|
|
|
|
+ maybeCompileJrxmlFile(subJrxmlFilePath, otherParameters, connection);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String jasperFilePath = jrxmlFilePath.replace(".jrxml", ".jasper");
|
|
String jasperFilePath = jrxmlFilePath.replace(".jrxml", ".jasper");
|
|
@@ -276,8 +274,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
compileReportToFile(jrxmlFilePath, jasperFilePath, otherParameters, connection);
|
|
compileReportToFile(jrxmlFilePath, jasperFilePath, otherParameters, connection);
|
|
|
}
|
|
}
|
|
|
} catch (JRException e) {
|
|
} catch (JRException e) {
|
|
|
- logger.error("", e);
|
|
|
|
|
- throw new ReportException("编译报表模板失败: " + jrxmlFilePath + " " + e.getMessage()).setDetailedMessage(e);
|
|
|
|
|
|
|
+ throw new IllegalStateException("编译报表模板失败: " + jrxmlFilePath + " " + ExceptionUtils.getDetailedMessage(e));
|
|
|
}
|
|
}
|
|
|
return jasperFilePath;
|
|
return jasperFilePath;
|
|
|
}
|
|
}
|
|
@@ -303,7 +300,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
int beginIndex = text.indexOf("/jrxml/");
|
|
int beginIndex = text.indexOf("/jrxml/");
|
|
|
int endIndex = text.indexOf(".jasper");
|
|
int endIndex = text.indexOf(".jasper");
|
|
|
if (beginIndex < 0 || endIndex < 0) {
|
|
if (beginIndex < 0 || endIndex < 0) {
|
|
|
- throw new ReportException("嵌套报表源码解析失败:" + jrxmlFile.getName() + ",子报表引用格式错误:" + text);
|
|
|
|
|
|
|
+ throw new IllegalStateException("嵌套报表源码解析失败:" + jrxmlFile.getName() + ",子报表引用格式错误:" + text);
|
|
|
}
|
|
}
|
|
|
beginIndex += "/jrxml/".length();
|
|
beginIndex += "/jrxml/".length();
|
|
|
subJrxmlFilePaths.add(jrxmlFile.getParent() + "/" + text.substring(beginIndex, endIndex) + ".jrxml");
|
|
subJrxmlFilePaths.add(jrxmlFile.getParent() + "/" + text.substring(beginIndex, endIndex) + ".jrxml");
|
|
@@ -324,9 +321,12 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @throws JRException
|
|
* @throws JRException
|
|
|
* @throws IOException
|
|
* @throws IOException
|
|
|
* @throws FileNotFoundException
|
|
* @throws FileNotFoundException
|
|
|
|
|
+ * @throws DocumentException
|
|
|
|
|
+ * @throws SQLException
|
|
|
*/
|
|
*/
|
|
|
private void compileReportToFile(String jrxmlFilePath, String jasperFilePath, String otherParameters,
|
|
private void compileReportToFile(String jrxmlFilePath, String jasperFilePath, String otherParameters,
|
|
|
- Connection connection) throws JRException, FileNotFoundException, IOException {
|
|
|
|
|
|
|
+ Connection connection)
|
|
|
|
|
+ throws JRException, FileNotFoundException, IOException, DocumentException, SQLException {
|
|
|
replaceFont(jrxmlFilePath);
|
|
replaceFont(jrxmlFilePath);
|
|
|
processFields(jrxmlFilePath, otherParameters, connection);
|
|
processFields(jrxmlFilePath, otherParameters, connection);
|
|
|
JasperCompileManager.compileReportToFile(jrxmlFilePath, jasperFilePath);
|
|
JasperCompileManager.compileReportToFile(jrxmlFilePath, jasperFilePath);
|
|
@@ -338,8 +338,12 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param jrxmlFilePath
|
|
* @param jrxmlFilePath
|
|
|
* @param otherParameters
|
|
* @param otherParameters
|
|
|
* @param connection
|
|
* @param connection
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ * @throws DocumentException
|
|
|
|
|
+ * @throws SQLException
|
|
|
*/
|
|
*/
|
|
|
- private void processFields(String jrxmlFilePath, String otherParameters, Connection connection) {
|
|
|
|
|
|
|
+ private void processFields(String jrxmlFilePath, String otherParameters, Connection connection)
|
|
|
|
|
+ throws IOException, DocumentException, SQLException {
|
|
|
XMLWriter xmlWriter = null;
|
|
XMLWriter xmlWriter = null;
|
|
|
try {
|
|
try {
|
|
|
SAXReader saxReader = new SAXReader();
|
|
SAXReader saxReader = new SAXReader();
|
|
@@ -359,7 +363,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
queryString = replaceOtherParameters(queryString, otherParameters);
|
|
queryString = replaceOtherParameters(queryString, otherParameters);
|
|
|
List<String> columnNames = getColumnNames(connection, queryString);
|
|
List<String> columnNames = getColumnNames(connection, queryString);
|
|
|
if (CollectionUtils.isEmpty(columnNames)) {
|
|
if (CollectionUtils.isEmpty(columnNames)) {
|
|
|
- throw new ReportException("未查询到任何列:" + queryString);
|
|
|
|
|
|
|
+ throw new SQLException("未查询到任何列:" + queryString);
|
|
|
}
|
|
}
|
|
|
Set<String> usedFields = getUsedFields(jrxmlFilePath);
|
|
Set<String> usedFields = getUsedFields(jrxmlFilePath);
|
|
|
// 指定的field
|
|
// 指定的field
|
|
@@ -381,15 +385,9 @@ public class PrintServiceImpl implements PrintService {
|
|
|
xmlWriter.write(document);
|
|
xmlWriter.write(document);
|
|
|
xmlWriter.flush();
|
|
xmlWriter.flush();
|
|
|
}
|
|
}
|
|
|
- } catch (DocumentException | SQLException | IOException e) {
|
|
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
|
|
} finally {
|
|
} finally {
|
|
|
if (xmlWriter != null) {
|
|
if (xmlWriter != null) {
|
|
|
- try {
|
|
|
|
|
- xmlWriter.close();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ xmlWriter.close();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -627,10 +625,10 @@ public class PrintServiceImpl implements PrintService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public boolean overload(String userName, String profile, String reportName, String whereCondition,
|
|
public boolean overload(String userName, String profile, String reportName, String whereCondition,
|
|
|
- String otherParameters, Platform platform) {
|
|
|
|
|
|
|
+ String otherParameters, Platform platform) throws SQLException, IOException, DocumentException {
|
|
|
DataSource dataSource = MasterManager.getDataSource(userName, profile);
|
|
DataSource dataSource = MasterManager.getDataSource(userName, profile);
|
|
|
if (dataSource == null) {
|
|
if (dataSource == null) {
|
|
|
- throw new ReportException("获取数据源失败");
|
|
|
|
|
|
|
+ throw new SQLException("获取数据源失败");
|
|
|
}
|
|
}
|
|
|
if (platform == null) {
|
|
if (platform == null) {
|
|
|
platform = Platform.PC;
|
|
platform = Platform.PC;
|
|
@@ -659,22 +657,16 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// 报表模板不存在
|
|
// 报表模板不存在
|
|
|
if (!jrxmlFile.exists()) {
|
|
if (!jrxmlFile.exists()) {
|
|
|
// 替换windows下路径中的双反斜杠为单斜杠
|
|
// 替换windows下路径中的双反斜杠为单斜杠
|
|
|
- throw new ReportException("未发现模板文件:" + jrxmlFilePath.replaceAll("\\\\", "/"));
|
|
|
|
|
|
|
+ throw new FileNotFoundException("未发现模板文件:" + jrxmlFilePath.replaceAll("\\\\", "/"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 因为子报表数据量较小,而且其参数来自主报表,无法简单地进行判断,所以不判断子报表是否过载
|
|
// 因为子报表数据量较小,而且其参数来自主报表,无法简单地进行判断,所以不判断子报表是否过载
|
|
|
int count = getCount(jrxmlFilePath, whereCondition, otherParameters, connection);
|
|
int count = getCount(jrxmlFilePath, whereCondition, otherParameters, connection);
|
|
|
logger.info("count... " + count);
|
|
logger.info("count... " + count);
|
|
|
return count >= maxRecordSize;
|
|
return count >= maxRecordSize;
|
|
|
- } catch (Throwable e) {
|
|
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
|
|
} finally {
|
|
} finally {
|
|
|
if (connection != null) {
|
|
if (connection != null) {
|
|
|
- try {
|
|
|
|
|
- connection.close();
|
|
|
|
|
- } catch (SQLException e) {
|
|
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ connection.close();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -686,8 +678,12 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param whereCondition
|
|
* @param whereCondition
|
|
|
* @param otherParameters
|
|
* @param otherParameters
|
|
|
* @param connection
|
|
* @param connection
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ * @throws SQLException
|
|
|
|
|
+ * @throws DocumentException
|
|
|
*/
|
|
*/
|
|
|
- private int getCount(String jrxmlFilePath, String whereCondition, String otherParameters, Connection connection) {
|
|
|
|
|
|
|
+ private int getCount(String jrxmlFilePath, String whereCondition, String otherParameters, Connection connection)
|
|
|
|
|
+ throws IOException, SQLException, DocumentException {
|
|
|
XMLWriter xmlWriter = null;
|
|
XMLWriter xmlWriter = null;
|
|
|
try {
|
|
try {
|
|
|
SAXReader saxReader = new SAXReader();
|
|
SAXReader saxReader = new SAXReader();
|
|
@@ -703,15 +699,9 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
}
|
|
|
queryString = replaceOtherParameters(queryString, otherParameters);
|
|
queryString = replaceOtherParameters(queryString, otherParameters);
|
|
|
return getCount(connection, queryString);
|
|
return getCount(connection, queryString);
|
|
|
- } catch (DocumentException | SQLException e) {
|
|
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
|
|
} finally {
|
|
} finally {
|
|
|
if (xmlWriter != null) {
|
|
if (xmlWriter != null) {
|
|
|
- try {
|
|
|
|
|
- xmlWriter.close();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ xmlWriter.close();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -727,7 +717,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// 替换其他参数
|
|
// 替换其他参数
|
|
|
if (sql.contains("$P{")) {
|
|
if (sql.contains("$P{")) {
|
|
|
if (StringUtils.isEmpty(otherParameters)) {
|
|
if (StringUtils.isEmpty(otherParameters)) {
|
|
|
- throw new ReportException("未传入动态参数");
|
|
|
|
|
|
|
+ throw new IllegalArgumentException("未传入动态参数");
|
|
|
}
|
|
}
|
|
|
JSONObject parameters = JSONObject.parseObject(otherParameters);
|
|
JSONObject parameters = JSONObject.parseObject(otherParameters);
|
|
|
Pattern pattern = Pattern.compile("\\$P\\{([^\\$]*)\\}");
|
|
Pattern pattern = Pattern.compile("\\$P\\{([^\\$]*)\\}");
|
|
@@ -737,7 +727,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
String parameterName = match.substring("$P{".length(), match.length() - 1);
|
|
String parameterName = match.substring("$P{".length(), match.length() - 1);
|
|
|
Object value = parameters.get(parameterName);
|
|
Object value = parameters.get(parameterName);
|
|
|
if (value == null) {
|
|
if (value == null) {
|
|
|
- throw new ReportException("未传入动态参数:" + parameterName);
|
|
|
|
|
|
|
+ throw new IllegalArgumentException("未传入动态参数:" + parameterName);
|
|
|
}
|
|
}
|
|
|
// 如果不是数字,需以单引号括起来
|
|
// 如果不是数字,需以单引号括起来
|
|
|
if (value instanceof Number) {
|
|
if (value instanceof Number) {
|