|
@@ -0,0 +1,126 @@
|
|
|
+package com.miniframe.bisiness.mdo;
|
|
|
+
|
|
|
+import com.itextpdf.html2pdf.HtmlConverter;
|
|
|
+import com.itextpdf.kernel.pdf.PdfDocument;
|
|
|
+import com.itextpdf.kernel.pdf.PdfWriter;
|
|
|
+import com.miniframe.core.ExecProcessFlow;
|
|
|
+import com.miniframe.core.exception.BusinessException;
|
|
|
+import com.miniframe.core.ext.UtilTools;
|
|
|
+import com.miniframe.generate.business.mdo.model.MDO0082BaseModel;
|
|
|
+import com.miniframe.httpserver.HttpServerTransFile;
|
|
|
+import com.miniframe.mdo.service.LogService;
|
|
|
+import com.miniframe.modo.temp.TemplateGenerator;
|
|
|
+import com.miniframe.tools.XIFileUtils;
|
|
|
+import freemarker.template.Configuration;
|
|
|
+import freemarker.template.Template;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.StringReader;
|
|
|
+import java.io.StringWriter;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 西工大系统,“报告导出”逻辑处理(重新生成不覆盖)。
|
|
|
+ */
|
|
|
+public class MDO0082Service extends MDO0082BaseModel implements ExecProcessFlow {
|
|
|
+
|
|
|
+ private static final long serialVersionUID = -7051358269847459502L;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 西工大系统,“报告导出”业务核心处理
|
|
|
+ */
|
|
|
+ public void transExecute() throws Exception {
|
|
|
+ String pid = this.getA_mdo0082().getPid();
|
|
|
+ Integer type =this.getA_mdo0082().getType();
|
|
|
+ String report;
|
|
|
+ ByteArrayOutputStream baos =new ByteArrayOutputStream();
|
|
|
+ try {
|
|
|
+ report=XIFileUtils.readFileAsString(LogService.BPATH+"/"+pid+"/in/outdata/OPTIMUM_RESULT_GOOD.dat");
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new BusinessException("MDO000015");
|
|
|
+ }
|
|
|
+ if(type==1){//pdf
|
|
|
+ Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
|
|
|
+ // 设置模板所在目录
|
|
|
+ cfg.setClassForTemplateLoading(TemplateGenerator.class, "/templates");
|
|
|
+ // 获取模板对象
|
|
|
+ Template template = cfg.getTemplate("report.xml");
|
|
|
+ // 定义数据模型(Map)
|
|
|
+ Map<String, Object> dataModel = new HashMap<>();
|
|
|
+ StringWriter writer = new StringWriter();
|
|
|
+ dataModel.put("report",report);
|
|
|
+ template.process(dataModel, writer);
|
|
|
+ HtmlConverter.convertToPdf(writer.toString(), baos);
|
|
|
+ // 关闭文档和写入器以释放资源
|
|
|
+ System.out.println("PDF created successfully.");
|
|
|
+ HttpServerTransFile transFile = new HttpServerTransFile(
|
|
|
+ URLEncoder.encode(pid+".pdf", "UTF-8"),
|
|
|
+ "application/octet-stream", baos);
|
|
|
+ UtilTools.setHttpServerTransFile(transFile);
|
|
|
+ }else if(type ==2){//word
|
|
|
+ Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
|
|
|
+ // 设置模板所在目录
|
|
|
+ cfg.setClassForTemplateLoading(TemplateGenerator.class, "/templates");
|
|
|
+ // 获取模板对象
|
|
|
+ Template template = cfg.getTemplate("report.xml");
|
|
|
+ // 定义数据模型(Map)
|
|
|
+ Map<String, Object> dataModel = new HashMap<>();
|
|
|
+ dataModel.put("report",report);
|
|
|
+ // 将数据模型传入模板进行处理
|
|
|
+ StringWriter writer = new StringWriter();
|
|
|
+ template.process(dataModel, writer);
|
|
|
+ baos.write(writer.toString().getBytes(StandardCharsets.UTF_8));
|
|
|
+ HttpServerTransFile transFile = new HttpServerTransFile(
|
|
|
+ URLEncoder.encode(pid+".docx", "UTF-8"),
|
|
|
+ "application/octet-stream", baos);
|
|
|
+ UtilTools.setHttpServerTransFile(transFile);
|
|
|
+ }else if(type ==3) {//html
|
|
|
+ Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
|
|
|
+ // 设置模板所在目录
|
|
|
+ cfg.setClassForTemplateLoading(TemplateGenerator.class, "/templates");
|
|
|
+ // 获取模板对象
|
|
|
+ Template template = cfg.getTemplate("report.html");
|
|
|
+ // 定义数据模型(Map)
|
|
|
+ Map<String, Object> dataModel = new HashMap<>();
|
|
|
+ StringWriter writer = new StringWriter();
|
|
|
+ dataModel.put("report",report);
|
|
|
+ template.process(dataModel, writer);
|
|
|
+ baos.write(writer.toString().getBytes(StandardCharsets.UTF_8));
|
|
|
+ HttpServerTransFile transFile = new HttpServerTransFile(
|
|
|
+ URLEncoder.encode(pid+".html", "UTF-8"),
|
|
|
+ "application/octet-stream", baos);
|
|
|
+ UtilTools.setHttpServerTransFile(transFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 西工大系统,“报告导出”业务前处理
|
|
|
+ */
|
|
|
+ public void preTransFlow() throws Exception {
|
|
|
+ this.validater();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 西工大系统,“报告导出”业务后处理
|
|
|
+ */
|
|
|
+ public void afterTransFlow() throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 西工大系统,“报告导出”逻辑入口处理方法
|
|
|
+ */
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ @Override
|
|
|
+ public Map execute(Map vars) throws Exception {
|
|
|
+ this.setTransMap(vars);
|
|
|
+ preTransFlow();// 执行业务开始的规则检查和校验
|
|
|
+ transExecute();// 执行核心业务段
|
|
|
+ afterTransFlow();// 执行核心逻辑完成后的收尾逻辑
|
|
|
+ return this.getTransMap();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|