apzl_leasing/src_tenwa/com/tenwa/makeContract/util/BqCreateOfficeService.java

172 lines
6.5 KiB
Java

package com.tenwa.makeContract.util;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jbo.com.tenwa.entity.comm.officetempalte.BF_TEMPLATE;
import com.amarsoft.app.util.XMLDataUtil;
import com.amarsoft.are.ARE;
import com.amarsoft.are.jbo.BizObject;
import com.amarsoft.are.jbo.BizObjectManager;
import com.amarsoft.are.jbo.BizObjectQuery;
import com.amarsoft.are.jbo.JBOFactory;
import com.amarsoft.are.jbo.JBOTransaction;
import com.amarsoft.are.log.Log;
import com.amarsoft.awe.util.ASResultSet;
import com.amarsoft.awe.util.SqlObject;
import com.amarsoft.awe.util.Transaction;
import com.tenwa.comm.exception.BusinessException;
import com.tenwa.officetempalte.service.BaseParamService;
import com.tenwa.officetempalte.service.CreateOfficeCallBackService;
import com.tenwa.officetempalte.util.FileOperatorUtil;
/**
* 根据模板创建文件
* 外部调用 createOfficeByTemplateNo方法
* */
public class BqCreateOfficeService {
public static Log logger=ARE.getLog();
JBOTransaction tx;
Transaction Sqlca =null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
public BqCreateOfficeService(JBOTransaction tx)throws Exception{
this.tx=tx;
Sqlca=Transaction.createTransaction(tx);
}
/**
* 根据模板编号和前台参数创建文件
* @param templateNo 模板编号
* @param model 前台参数
*
* */
@SuppressWarnings("unchecked")
public void createOfficeByTemplateNo(String templateNo,Map<String,String>model) throws Exception{
try{
//获取模板配置
BizObject templateConfig=this.getTemplateConfigByNo(templateNo);
if(templateConfig==null){
throw new BusinessException("根据模板编号"+templateNo+"没有找到模板");
}
//根据模板号和前台参数获取模板参数值
BaseParamService paramSer=new BaseParamService(tx);
//模板处理类
Class<BqCreateOfficeCallBackService> callback=null;
//获取模板处理类
if(templateConfig.getAttribute("callback").getString().length()>0){
try{
callback=(Class<BqCreateOfficeCallBackService>) Class.forName(templateConfig.getAttribute("callback").getString());
}catch(Exception e){
throw new BusinessException(templateConfig.getAttribute("templatename").getString()+"模板处理类未找到");
}
}else if(templateConfig.getAttribute("templatetype").getString().equals("word")){//word默认模板处理类
callback=(Class<BqCreateOfficeCallBackService>) Class.forName("com.tenwa.makeContract.util.BqBaseWordCallBackServiceImpl");
}else if(templateConfig.getAttribute("templatetype").getString().equals("excel")){//excel 默认模板处理类
callback=(Class<BqCreateOfficeCallBackService>) Class.forName("com.tenwa.makeContract.util.BqBaseWordCallBackServiceImpl");
}else{
throw new BusinessException(templateConfig.getAttribute("templatename").getString()+"模板处理类未找到");
}
//获取模板处理的方法
BqCreateOfficeCallBackService ser=callback.newInstance();
//所有运行前方法
ser.runAllBefore(templateConfig, model, tx);
//获取模板创建条件
List<Map<String,String>> creatParam=this.getCreateTempalteParm(templateConfig, model);
if(creatParam==null)//创建条件是空 默认创建一个文件
{ Map<String,String> paramMap=new HashMap<String, String>();
Map<String,String> pmap=paramSer.getParamValueByTemplateNo(templateConfig.getAttribute("id").getString(), model);
paramMap.putAll(model);
paramMap.putAll(pmap);
ser.runBefore(templateConfig, paramMap, tx);
ser.run(templateConfig, paramMap, tx);
ser.runAfter(templateConfig, paramMap, tx);
}else{//根据创建条件循环创建多个文件
Map<String,String> map=new HashMap<String, String>();
for(int i=0;i<creatParam.size();i++){
map.clear();
map.putAll(model);
map.putAll(creatParam.get(i));
//Map<String,String> pmap=paramSer.getParamValueByTemplateNo(templateConfig.getAttribute("id").getString(), map);
//创建条件得到的数据放到参数池里 标签获取数据可从参数池获取
ser.runBefore(templateConfig, map, tx);
ser.run(templateConfig, map, tx);
ser.runAfter(templateConfig, map, tx);
}
}
}catch(BusinessException e){
e.printStackTrace();
throw e;
}
}
/**
* 获取模板配置
* */
public BizObject getTemplateConfigByNo(String templateNo)throws Exception{
BizObjectManager tempalteManger = JBOFactory.getBizObjectManager(BF_TEMPLATE.CLASS_NAME);
tx.join(tempalteManger);
BizObjectQuery tempalteMangerQuery = tempalteManger.createQuery("id=:id ");
tempalteMangerQuery.setParameter("id",templateNo);
BizObject tempalte = null;
tempalte = tempalteMangerQuery.getSingleResult(true);
return tempalte;
}
/**
* 根据模板配置和参数 获取模板创建条件
* @throws Exception
* */
public List<Map<String,String>> getCreateTempalteParm(BizObject templateConfig,Map<String,String> model) throws Exception{
//获取创建条件
String sql=templateConfig.getAttribute("CreateCondition").getString();
if(null==sql||sql.length()>10)
{
List<Map<String,String>> params=new ArrayList<Map<String,String>>();
if(sql.indexOf(".xml")>0)//创建条件是xml 获取xml中的sql
{
String xmlPath=ARE.getProperty("PRD_HOME").replace("/WEB-INF", "")+sql;
Map<String,String> xmlfile=XMLDataUtil.readTableInfoFromXmlFile(xmlPath);
sql=xmlfile.get("table_sql");
}
Map<String,String>paramMap=new HashMap<String,String>();
//替换sql参数
sql=FileOperatorUtil.replaceSQLFixedParam(sql,model);
paramMap=FileOperatorUtil.getSqlParam(sql, model);
sql=sql.replaceAll("[{]", "");
sql=sql.replaceAll("[}]", "");
ASResultSet rs = null;
try {
SqlObject asql = new SqlObject(sql);
if(null!=paramMap && paramMap.keySet().size()>0){
for(String key :paramMap.keySet()){
asql.setParameter(key, paramMap.get(key));
}
}
rs = Sqlca.getASResultSet(asql);
} catch (Exception e1) {
throw new BusinessException("加载数据执行"+sql+"时出错");
}
while (rs.next()) {
Map<String,String> dataMap=FileOperatorUtil.getASResultSetToMap(rs);
params.add(dataMap);
}
if(params.size()==0)
{
logger.info("模板"+templateConfig.getAttribute("templatename").getString()+"根据创建条件未生成文档");
}
return params;
}
else
{
return null;
}
}
}