|
@@ -0,0 +1,124 @@
|
|
|
|
|
+package com.uas.report.crystal;
|
|
|
|
|
+
|
|
|
|
|
+import com.crystaldecisions.sdk.occa.report.application.DatabaseController;
|
|
|
|
|
+import com.crystaldecisions.sdk.occa.report.application.OpenReportOptions;
|
|
|
|
|
+import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
|
|
|
|
|
+import com.crystaldecisions.sdk.occa.report.application.ReportSaveAsOptions;
|
|
|
|
|
+import com.crystaldecisions.sdk.occa.report.data.IDatabase;
|
|
|
|
|
+import com.crystaldecisions.sdk.occa.report.data.ITable;
|
|
|
|
|
+import com.crystaldecisions.sdk.occa.report.data.Tables;
|
|
|
|
|
+import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;
|
|
|
|
|
+import com.uas.report.util.FileUtils;
|
|
|
|
|
+import com.uas.report.util.StringUtils;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.FileFilter;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author sunyj
|
|
|
|
|
+ * @since 2017/12/22 10:42
|
|
|
|
|
+ */
|
|
|
|
|
+public class CrystalReplaceView {
|
|
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(CrystalReplaceView.class);
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
+ int length = args != null ? args.length : 0;
|
|
|
|
|
+ if (length < 3) {
|
|
|
|
|
+ logger.error("参数缺失\neg. java -jar target/crystal-replace-view-0.0.1.jar src/test/resources/rpts src/test/resources/out HUASL_");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ File src = new File(args[0]);
|
|
|
|
|
+ File destDir = new File(args[1]);
|
|
|
|
|
+ String prefix = args[2];
|
|
|
|
|
+ try {
|
|
|
|
|
+ CrystalReplaceView.replaceReports(src, destDir, prefix);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("替换失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void replaceReports(File src, File destDir, String prefix) throws IOException {
|
|
|
|
|
+ if (src == null || destDir == null || StringUtils.isEmpty(prefix)) {
|
|
|
|
|
+ throw new IllegalArgumentException("需指定参数 src, destDir, prefix");
|
|
|
|
|
+ }
|
|
|
|
|
+ FileUtils.checkFile(src);
|
|
|
|
|
+ if (src.isDirectory()) {
|
|
|
|
|
+ replaceReports(src, new File(src, "success"), destDir, prefix);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ replaceReport(src, new File(src.getParentFile(), "success"), destDir, prefix);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void replaceReports(File reportDir, File successDir, File destDir, String prefix) throws IOException {
|
|
|
|
|
+ FileUtils.checkDir(reportDir);
|
|
|
|
|
+ File[] files = reportDir.listFiles(new FileFilter() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean accept(File file) {
|
|
|
|
|
+ if (file == null || !file.exists()) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ // success路径下的文件不进行转换
|
|
|
|
|
+ String filePath = file.getAbsolutePath();
|
|
|
|
|
+ if (filePath.endsWith("success") || filePath.contains("\\success\\")
|
|
|
|
|
+ || filePath.contains("/success/")) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 文件必须是rpt或者zip格式
|
|
|
|
|
+ if (file.isFile() && !filePath.endsWith(".rpt") && !filePath.endsWith(".zip")) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ for (File file : files) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ String fileName = file.getName();
|
|
|
|
|
+ // 是文件夹的话,递归处理
|
|
|
|
|
+ if (file.isDirectory()) {
|
|
|
|
|
+ // 不可直接修改successDir和outDir,否则会造成其他文件的路径有问题
|
|
|
|
|
+ File successDirCopy = new File(successDir, fileName);
|
|
|
|
|
+ File outDirCopy = new File(destDir, fileName);
|
|
|
|
|
+ replaceReports(file, successDirCopy, outDirCopy, prefix);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ replaceReport(file, successDir, destDir, prefix);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Throwable e) {
|
|
|
|
|
+ logger.error(file.getName(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void replaceReport(File reportFile, File successDir, File destDir, String prefix) throws IOException {
|
|
|
|
|
+ logger.info("replacing... " + reportFile.getName());
|
|
|
|
|
+ FileUtils.initDir(destDir);
|
|
|
|
|
+ ReportClientDocument client = new ReportClientDocument();
|
|
|
|
|
+ try {
|
|
|
|
|
+ client.setReportAppServer(ReportClientDocument.inprocConnectionString);
|
|
|
|
|
+ client.open(reportFile, OpenReportOptions._retrieveNoReportDefinition);
|
|
|
|
|
+ DatabaseController databaseController = client.getDatabaseController();
|
|
|
|
|
+ IDatabase database = databaseController.getDatabase();
|
|
|
|
|
+ Tables tables = database.getTables();
|
|
|
|
|
+ for (int j = 0; j < tables.size(); j++) {
|
|
|
|
|
+ ITable iTable = tables.get(j);
|
|
|
|
|
+ logger.info(iTable.getName());
|
|
|
|
|
+ logger.info(iTable.getAlias());
|
|
|
|
|
+ databaseController.modifyTableAlias(iTable, prefix + iTable.getAlias());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ client.saveAs(reportFile.getName(), destDir,
|
|
|
|
|
+ ReportSaveAsOptions._overwriteExisting);
|
|
|
|
|
+ } catch (ReportSDKException e) {
|
|
|
|
|
+ logger.error("报表读取失败", e);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("报表写入失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 转换成功之后,将文件移至其他路径下
|
|
|
|
|
+ FileUtils.initDir(successDir);
|
|
|
|
|
+ FileUtils.move(reportFile, new File(successDir, reportFile.getName()));
|
|
|
|
|
+ System.out.println();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|