package com.tenwa.makeContract.util; import java.io.File; import java.io.InputStream; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import jbo.sys.CODE_LIBRARY; import org.dom4j.Document; import org.dom4j.io.SAXReader; import com.amarsoft.are.jbo.BizObject; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.jbo.JBOTransaction; import com.amarsoft.ars.zip.ZipEntry; import com.amarsoft.ars.zip.ZipFile; import com.tenwa.comm.exception.BusinessException; import com.tenwa.officetempalte.service.BaseLableDataService; import com.tenwa.officetempalte.serviceImp.BaseWordLableReplaceServiceImp; import com.tenwa.officetempalte.util.FileOperatorUtil; import com.tenwa.officetempalte.util.WordBookMarkXmlUtil; public class BqBaseWordCallBackServiceImpl extends BqCreateOfficeCallBackService{ @Override public void run(BizObject templateConfig, Map paramMap, JBOTransaction tx) throws Exception { BaseLableDataService dataSer=new BaseLableDataService(tx); //获取模板配置 List exportConfig=dataSer.getTemplateExportConfig(templateConfig.getAttribute("id").getString()); Map tempModel=new HashMap(); tempModel.putAll(paramMap); //获取生成文件实际路径 String fileName=""; String filePath=""; String fullPath=""; fileName=FileOperatorUtil.getFileNameByFormual(templateConfig.getAttribute("templateshowname").getString(), tempModel); String uuid=UUID.randomUUID().toString().replaceAll("-", ""); fullPath=FileOperatorUtil.getuploadFileDir(paramMap.get("fileSavePath")) +uuid+"_"+fileName; filePath=fullPath.replace(paramMap.get("fileSavePath"), ""); //获取模板文件 String tempName=templateConfig.getAttribute("templatepath").getString(); String rootPath=this.getClass().getResource("/").getPath(); rootPath=rootPath.substring(0, rootPath.indexOf("WEB-INF")-1); tempName=rootPath+"/fileTemplate/"+tempName; String suffix = tempName.substring(tempName.lastIndexOf(".")); File file = new File(tempName); if (file.isFile()) { if (suffix.indexOf("doc") > 0) { try { //生成word this.writeDatatoTemplateWordbyBookMark(tempName, paramMap, fullPath,exportConfig,tx); } catch (Exception e) { e.printStackTrace(); throw new BusinessException("写入word模板时出错"+e.getMessage()); } } } else { throw new BusinessException(templateConfig.getAttribute("templateshowname").getString() + "没有模板"); } //生成成功 维护关系进附件表 this.createRelative(templateConfig, paramMap, fileName, filePath, fullPath, tx); } public void runBefore(BizObject templateConfig, Map paramMap, JBOTransaction tx) throws Exception { // TODO Auto-generated method stub } @Override public void runAllBefore(BizObject templateConfig, Map paramMap, JBOTransaction tx) throws Exception { // TODO Auto-generated method stub } //生成word文档 public void writeDatatoTemplateWordbyBookMark(String tempfile,Map model, String targetFile,List exportConfig,JBOTransaction tx) throws Exception{ BaseWordLableReplaceServiceImp wordSer=new BaseWordLableReplaceServiceImp(); BaseLableDataService lableSer=new BaseLableDataService(tx); Document document = null; File file = new File(tempfile); ZipFile docxFile = new ZipFile(file); ZipEntry documentXML = docxFile.getEntry("word/document.xml"); InputStream documentXMLIS = docxFile.getInputStream(documentXML); SAXReader saxReader = new SAXReader(); document = saxReader.read(documentXMLIS); Map documents=new HashMap(); documents.put("miandocument", document); Map footmap=WordBookMarkXmlUtil.getfooter(docxFile); documents.putAll(footmap); Map operMap=new HashMap(); operMap.put("default", wordSer); Map configData=new HashMap(); for(BizObject config:exportConfig){ configData=lableSer.getLableDataByConfig(config, model); Class oper=null; Method replaceString=null; Method replaceList=null; Method replaceTable=null; String operator_class=config.getAttribute("operatorclass").getString(); if(operator_class.length()==0||operator_class.equals("word_default_class")){ oper=operMap.get("default").getClass(); }else if(operMap.containsKey(operator_class)){ oper=(Class)operMap.get(operator_class); }else{ BizObject code=JBOFactory.getBizObjectManager(CODE_LIBRARY.CLASS_NAME).createQuery("itemno=:itemno and codeno='lable_operator_class'") .setParameter("itemno", operator_class).getSingleResult(false); oper=Class.forName(code.getAttribute("itemattribute").getString()); operMap.put(operator_class, oper); } replaceString=oper.getMethod("replaceStringLable",Map.class,BizObject.class,Map.class); replaceList=oper.getMethod("replaceListLable",Map.class,BizObject.class,Map.class); replaceTable=oper.getMethod("replaceTableLable",Map.class,BizObject.class,Map.class); if(config.getAttribute("tagtype").getString().equals("tag_type1")){//字符串 replaceString.invoke(oper.newInstance(),documents,config,configData); }else if(config.getAttribute("tagtype").getString().equals("tag_type2")){//数组 replaceList.invoke(oper.newInstance(),documents,config,configData); }else if(config.getAttribute("tagtype").getString().equals("tag_type3")){//表格 replaceTable.invoke(oper.newInstance(),documents,config,configData); } } //保存文档 WordBookMarkXmlUtil.saveWordXML(docxFile,document,footmap,targetFile); } @Override public void runAfter(BizObject templateConfig, Map paramMap, JBOTransaction tx) throws Exception { // TODO Auto-generated method stub } }