forked from fanruan/design
fly.li
3 years ago
1 changed files with 184 additions and 0 deletions
@ -0,0 +1,184 @@
|
||||
package com.fr.nx.app.designer.utils; |
||||
|
||||
import com.fr.design.mainframe.JTemplate; |
||||
import com.fr.file.FILE; |
||||
import com.fr.file.FileNodeFILE; |
||||
import com.fr.general.ComparatorUtils; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.main.TemplateWorkBook; |
||||
import com.fr.main.impl.WorkBook; |
||||
import com.fr.nx.app.designer.cptx.io.DesignReadWritableProvider; |
||||
import com.fr.nx.app.designer.monitor.DesignerMetricRecorder; |
||||
import com.fr.nx.app.designer.toolbar.TransformResult; |
||||
import com.fr.nx.app.designer.toolbar.TransformResultInfo; |
||||
import com.fr.nx.compile.CompileStatus; |
||||
import com.fr.nx.compile.ReportCompiler; |
||||
import com.fr.nx.compile.adapter.LegacyWorkBookAdapter; |
||||
import com.fr.nx.compile.util.ReportCompileUtils; |
||||
import com.fr.nx.cptx.CptxIOManager; |
||||
import com.fr.nx.cptx.cache.CptxTemplatePool; |
||||
import com.fr.nx.cptx.entry.CptxTemplate; |
||||
import com.fr.nx.cptx.io.handle.CptxTemplateHandle; |
||||
import com.fr.nx.cptx.monitor.CompileMonitorHandler; |
||||
import com.fr.nx.cptx.utils.CptxFileUtils; |
||||
import com.fr.nx.data.layer.LayerItem; |
||||
import com.fr.nx.data.layer.LayerProps; |
||||
import com.fr.nx.template.compile.CompiledReport; |
||||
import com.fr.report.report.Report; |
||||
import com.fr.report.stable.LayerReportAttr; |
||||
import com.fr.report.worksheet.WorkSheet; |
||||
|
||||
import java.io.OutputStream; |
||||
|
||||
|
||||
public class CptCompileUtil { |
||||
public static void compile(JTemplate jtemplate) { |
||||
if (jtemplate == null || jtemplate.getEditingFILE() == null) { |
||||
return; |
||||
} |
||||
FILE file = jtemplate.getEditingFILE(); |
||||
String path = file.getPath(); |
||||
WorkBook workbook = (WorkBook) jtemplate.getTarget(); |
||||
//如果是cpt并且引擎设置正确,执行预编译。cptx文件不执行预编译
|
||||
if (!(path.endsWith(".cpt") && isNewEngine(workbook))){ |
||||
return; |
||||
} |
||||
TransformResultInfo resultInfo = compile0(workbook, file); |
||||
unSupportLog(resultInfo); |
||||
} |
||||
|
||||
public static TransformResultInfo compile0(WorkBook workbook, FILE file) { |
||||
if (!(file instanceof FileNodeFILE)) { |
||||
try { |
||||
OutputStream outputStream = file.asOutputStream(); |
||||
workbook.export(outputStream); |
||||
return TransformResultInfo.generateResult(TransformResult.SUCCESS).saved(true); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
return TransformResultInfo.generateResult(TransformResult.FAILED).saved(false); |
||||
} |
||||
} |
||||
boolean saved; |
||||
CompiledReport report = null; |
||||
CompileStatus compileStatus = CompileStatus.SUCCEED; |
||||
if (workbook == null || file == null) { |
||||
return TransformResultInfo |
||||
.generateResult(TransformResult.FAILED, "work and file must not be null") |
||||
.saved(false); |
||||
} |
||||
String failMessage = null; |
||||
// 编译
|
||||
ReportCompiler compiler; |
||||
try { |
||||
compiler = new ReportCompiler(new LegacyWorkBookAdapter(workbook)); |
||||
long startTime = System.currentTimeMillis(); |
||||
compiler.compile(); |
||||
report = compiler.getCompiledReport(); |
||||
// 折叠树埋点
|
||||
LayerProps props = ReportCompileUtils.getLayerProps(report); |
||||
LayerItem[] items; |
||||
if (props != null && (items = props.getLayerItems()) != null) { |
||||
CompileMonitorHandler.submitTreeCompileFocusPoint( |
||||
startTime, file.getPath(), items.length, |
||||
props.getExpandLayer(), true |
||||
); |
||||
} |
||||
compileStatus = compiler.getStatus(); |
||||
failMessage = compiler.getFailMessage(); |
||||
long endTime = System.currentTimeMillis(); |
||||
CompileMonitorHandler.submitCompileMessage(startTime, endTime, file.getPath(), ""); |
||||
if (compileStatus != CompileStatus.SUCCEED) { |
||||
DesignerMetricRecorder.submitUnSupportTransform( |
||||
TransformResult.UNSUPPORT, |
||||
workbook.getTemplateID(), |
||||
file.getName(), |
||||
compileStatus == CompileStatus.FAILED_UNSUPPORT ? failMessage : null |
||||
); |
||||
} |
||||
} catch (Exception exception) { |
||||
String templateID = workbook.getTemplateID(); |
||||
String fileName = file.getName(); |
||||
DesignerMetricRecorder.submitFailedTransform(TransformResult.FAILED, templateID, fileName, exception); |
||||
FineLoggerFactory.getLogger().error(exception.getMessage(), exception); |
||||
} |
||||
|
||||
// 构建编译结果,当前的 cptx template 是未经反序列化钩子处理过的 cptx 模版对象,不能直接用于模版预览
|
||||
CptxTemplate template = CptxIOManager.createCptxTemplate(workbook, report, compileStatus, failMessage); |
||||
// 保存
|
||||
DesignReadWritableProvider cptx = new DesignReadWritableProvider(file); |
||||
CptxTemplateHandle handle = CptxIOManager.create(template, cptx); |
||||
try { |
||||
saved = handle.save(); |
||||
} catch (Exception exception) { |
||||
// 存储cptx格式报错或者失败
|
||||
FineLoggerFactory.getLogger().error(exception.getMessage(), exception); |
||||
DesignerMetricRecorder.submitFailedTransform(TransformResult.FAILED, workbook.getTemplateID(), file.getName(), exception); |
||||
return TransformResultInfo.generateResult(TransformResult.FAILED, failMessage).saved(false); |
||||
} |
||||
// 读取可 web 预览模版并缓存
|
||||
try { |
||||
CptxTemplate previewCptxTemplate = CptxIOManager.open(cptx).getTemplate(); |
||||
CptxTemplatePool.getInstance().addCptxTemplate(CptxFileUtils.getFormatPath(file.getPath()), previewCptxTemplate); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
if (report == null) { |
||||
// 编译报错或者失败
|
||||
return TransformResultInfo.generateResult(TransformResult.FAILED, failMessage).saved(saved); |
||||
} |
||||
return TransformResultInfo.generateResult(TransformResult.parse(compileStatus), failMessage).saved(saved); |
||||
} |
||||
|
||||
private static boolean needDoAfterCompile(TransformResult result) { |
||||
return ComparatorUtils.equals(TransformResult.SUCCESS, result) |
||||
|| ComparatorUtils.equals(TransformResult.UNSUPPORT, result); |
||||
} |
||||
|
||||
/* |
||||
* 该另存为流程会改变文件内容,现在作为兼容的流程,以后可以随时删除该流程 |
||||
* */ |
||||
public static void changeFrEngineAttr(String oldName, JTemplate jTemplate){ |
||||
FILE editingFILE = jTemplate.getEditingFILE(); |
||||
String path = editingFILE.getPath(); |
||||
//只有在旧文件是cptx文件并且新文件是cpt文件时才会改变报表引擎属性
|
||||
if (oldName.endsWith(".cptx") && path.endsWith(".cpt")){ |
||||
WorkBook target = (WorkBook)jTemplate.getTarget(); |
||||
WorkSheet workSheet = (WorkSheet)target.getReport(0); |
||||
LayerReportAttr layerReportAttr = workSheet.getLayerReportAttr(); |
||||
if (layerReportAttr == null){ |
||||
layerReportAttr = new LayerReportAttr(); |
||||
workSheet.setLayerReportAttr(layerReportAttr); |
||||
} |
||||
layerReportAttr.setClientPaging(true); |
||||
layerReportAttr.setNewEngine(true); |
||||
} |
||||
} |
||||
|
||||
public static boolean isNewEngine(Object workBook){ |
||||
if (workBook == null || !(workBook instanceof TemplateWorkBook)){ |
||||
return false; |
||||
} |
||||
Report report = ((TemplateWorkBook) workBook).getReport(0); |
||||
if (report instanceof WorkSheet){ |
||||
WorkSheet workSheet = (WorkSheet) report; |
||||
LayerReportAttr layerReportAttr = workSheet.getLayerReportAttr(); |
||||
if (layerReportAttr!= null && layerReportAttr.isClientPaging() && layerReportAttr.isNewEngine()){ |
||||
return true; |
||||
}else{ |
||||
return false; |
||||
} |
||||
}else { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
private static void unSupportLog(TransformResultInfo transformResultInfo){ |
||||
TransformResult result = transformResultInfo.getResult(); |
||||
//这里只打印模板转换不支持的信息,没有打印模板转换失败的信息
|
||||
if (result.equals(TransformResult.UNSUPPORT)){ |
||||
FineLoggerFactory.getLogger().error(transformResultInfo.getTransformLog()); |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue