码迷,mamicode.com
首页 > 其他好文 > 详细

ESB 客户端调用 处理类

时间:2015-09-10 17:17:27      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

esb

package com.isoftstone.synchronize.entrance;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.apache.log4j.Logger;
import org.jdom.Document;

import com.isoftstone.core.common.tools.DWTransformTool;
import com.isoftstone.core.common.tools.XmlTool;
import com.isoftstone.core.dto.RequestBody;
import com.isoftstone.core.dto.RequestBusinessObject;
import com.isoftstone.core.dto.RequestHead;
import com.isoftstone.core.dto.ResponseBusinessObject;
import com.isoftstone.fwk.vo.BusinessVo;
import com.isoftstone.httpclient.ESBClient;
import com.isoftstone.iaeap.datawindow.metadata.DataWindowMetaDataMgr;
import com.isoftstone.synchronize.dao.TempBrmTaxpaymentCodeDAO;
import com.isoftstone.synchronize.dao.TempBrmUwClaimDAO;
import com.isoftstone.synchronize.domain.TempBrmTaxpaymentCode;
import com.isoftstone.synchronize.domain.TempBrmUwClaim;
import com.isoftstone.synchronize.domain.request.SynchronizeRequestBase;
import com.isoftstone.synchronize.domain.response.SynchronizeResponseBase;
import com.isoftstone.synchronize.domain.response.SynchronizeResponseClaim;
import com.isoftstone.synchronize.domain.response.SynchronizeResponseTaxpaymentCode;
import com.isoftstone.synchronize.tool.SpringBeanTool;
import com.isoftstone.synchronize.tool.TableOperation;

public class SynchronizeEntrance {
    Logger logger = Logger.getLogger(SynchronizeEntrance.class);
    
    
    public  void synchronizeTableTaxpaymentCodeWithCore(Date startDate) throws Exception{//与核心 同步 Taxpayment表
        ResponseBusinessObject responseObject = connectToCore( "3", "0", /*start_tm*/startDate) ;//约定该表固定传"3"
        TempBrmTaxpaymentCodeDAO tempBrmTaxpaymentCodeDAO= (TempBrmTaxpaymentCodeDAO) SpringBeanTool.getBean("TempBrmTaxpaymentCodeDAO");
//        ResponseBusinessObject responseObject = generateTestResponse("D:\\application\\eclipse64ee\\workspace\\ilogSynchronized\\src\\syncTaxpaymentCode.txt") ;
//        log.info("同步处理业务数据-开始initSynchronizeResult");
        Map<String, Object> returnBatchesMap = transferResponseBusinessObject2ReturnMap(responseObject);
        List<TempBrmTaxpaymentCode> tempTaxpaymentCodeList = (List<TempBrmTaxpaymentCode>) returnBatchesMap.get("tempTaxpaymentCodeList");
        TableOperation.deleteTable(TableOperation.TRUNCATE_TEMP_T_BRM_TAXPAYMENT_CODE);//删除临时表
        tempBrmTaxpaymentCodeDAO.addTempBrmTaxpaymentCodes(tempTaxpaymentCodeList); //插入临时表
        StringBuilder sb  = new StringBuilder();
        TableOperation.deleteTable(TableOperation.DELETE_T_BRM_TAXPAYMENT_CODE_WITH_TEMP);//根据临时表的主键删除真实表中的行
        int effectCount = TableOperation.insertTable(TableOperation.INSERT_INTO_T_BRM_TAXPAYMENT_CODE);//把临时表中数据插入到真实表中
        TableOperation.deleteTable(TableOperation.TRUNCATE_TEMP_T_BRM_TAXPAYMENT_CODE);//删除临时表
        logger.info("更新T_BRM_TAXPAYMENT_CODE表总行数: "+ effectCount+"\n");
        
    }
    
    
    /**
     * 1    第一次取<核心数据>,得到需要传输的次数
     * 2    删除临时表
     * 3    循环次数,再取<核心数据>,插入到所有数据到临时表
     * 4    根据临时表的主键删除真实表中的行
     * 5    把临时表中数据插入到真实表中
     * 6    删除临时表
     * @throws Exception
     */
    public  void synchronizeTableClaimWithCore(Date startTime) throws Exception{//与核心 同步 Claim表

        
        ResponseBusinessObject responseObject = connectToCore( "1", " 0", /*start_tm*/ startTime ) ;
//        ResponseBusinessObject responseObject = generateTestResponse("D:\\application\\eclipse64ee\\workspace\\ilogSynchronized\\src\\syncClaim.txt") ;
        
//        log.info("同步处理业务数据-开始initSynchronizeResult");
        Map<String, Object> returnBatchesMap = transferResponseBusinessObject2ReturnMap(responseObject);
        SynchronizeResponseBase synchronizeResponseBase = (SynchronizeResponseBase) returnBatchesMap.get("synchronizeResponseBase");
        List<TempBrmUwClaim> tempClaimList = (List<TempBrmUwClaim>) returnBatchesMap.get("tempClaimList");

        TableOperation.deleteTable(TableOperation.TRUNCATE_TEMP_T_BRM_UW_CLAIM);//删除临时表
        StringBuilder sb  = new StringBuilder();
        TempBrmUwClaimDAO tempBrmUwClaimDAO= (TempBrmUwClaimDAO) SpringBeanTool.getBean("TempBrmUwClaimDAO");
        if(synchronizeResponseBase != null && synchronizeResponseBase.getBatches() != null ){
            int i = Integer.parseInt(synchronizeResponseBase.getBatches());//总批次号
            if(i >= 1){
                for(int j = 1 ; j <= i ; j++){
                    ResponseBusinessObject responseBatchesObject = connectToCore("2", ""+j, /*start_tm*/  startTime  ) ;
//                    ResponseBusinessObject responseBatchesObject =  generateTestResponse("D:\\application\\eclipse64ee\\workspace\\ilogSynchronized\\src\\syncClaim.txt") ;  // connectToCore("2", ""+j, /*start_tm*/DateUtil.stringToDate(("2012-12-29 12:00:00" == null ? "" :"2012-12-29 12:00:00"), DateUtil.FORMAT_0)) ;
                    Map<String, Object> claimsMap = transferResponseBusinessObject2ReturnMap(responseBatchesObject);
                    tempClaimList = (List<TempBrmUwClaim>) claimsMap.get("tempClaimList");
                    sb.append(String.format("第 %d 次传输 %d 行 ------ \n",j,tempClaimList.size()));
                    boolean flag = tempBrmUwClaimDAO.addTempBrmUwClaims(tempClaimList);
                    if( flag == false){
                        ResponseBusinessObject responseBatchesObjectAgain = // generateTestResponse("D:\\application\\eclipse64ee\\workspace\\ilogSynchronized\\src\\syncClaim.txt") ; 
                         connectToCore("2", ""+j, /*start_tm*/ startTime/*,DateUtil.stringToDate(("2012-12-29 12:00:00" == null ? "" :"2012-12-29 12:00:00"), DateUtil.FORMAT_0)*/ ) ;
                        Map<String, Object> claimsMapAgain = transferResponseBusinessObject2ReturnMap(responseBatchesObject);
                        tempClaimList = (List<TempBrmUwClaim>) claimsMapAgain.get("tempClaimList");
                        sb.append(String.format("第 %d 次保存数据失败, 重新传输 , 传输 %d 行\n",j,tempClaimList.size()));
                        flag = tempBrmUwClaimDAO.addTempBrmUwClaims(tempClaimList);
                        if(flag == false) {
                            sb.append(String.format("第 %d 次保存数据失败, 不再重新传输了,直接退出\n",j));
                            break;
                        }
                    }
                }
            }
        }
        logger.info(sb.toString());
        TableOperation.deleteTable(TableOperation.DELETE_T_BRM_UW_CLAIM_WITH_TEMP);//根据临时表的主键删除真实表中的行
        int effectCount = TableOperation.insertTable(TableOperation.INSERT_INTO_T_BRM_UW_CLAIM);//把临时表中数据插入到真实表中
        TableOperation.deleteTable(TableOperation.TRUNCATE_TEMP_T_BRM_UW_CLAIM);//删除临时表
        logger.info("更新T_BRM_UW_CLAIM表总行数: "+ effectCount+"\n");
    }
    
    /**接收响应对象以转换Map
     * @param responseBusinessObject 响应对象
     * @return
     * @throws Exception
     */
    public Map<String, Object> transferResponseBusinessObject2ReturnMap(ResponseBusinessObject responseBusinessObject) throws Exception{
        Map<String, Object> returnMap = new HashMap<String, Object>();
        HashMap<String, BusinessVo> map = new HashMap<String, BusinessVo>();
        
        List<SynchronizeResponseBase> baseList = new ArrayList<SynchronizeResponseBase>();
        List<SynchronizeResponseClaim> claimList = new ArrayList<SynchronizeResponseClaim>();//刚取到的Claim列表
        List<SynchronizeResponseTaxpaymentCode> taxpaymentCodeList = new ArrayList<SynchronizeResponseTaxpaymentCode>();//刚取到的TaxpaymentCode列表
        
        List<TempBrmUwClaim> tempClaimList = new ArrayList<TempBrmUwClaim>();//转换完成后的用于临时表的Claim
        List<TempBrmTaxpaymentCode> tempTaxpaymentCodeList = new ArrayList<TempBrmTaxpaymentCode>();//转换完成后的用于临时表的TaxpaymentCode
        
//        DataWindowMetaDataMgr.getInstance().init(this.getClass().getResource("/").toString().split("WEB-INF")[0]+"template/");//不依整于容器   设置web-inf路径
        DataWindowMetaDataMgr.getInstance().init(this.getClass().getClassLoader().getResource("/").getPath().split("WEB-INF")[0]+"template/");//设置web-inf路径
//        logger.info("------------------------------"+this.getClass().getClassLoader().getResource("/").getPath());
//        logger.info("______________________________"+SynchronizeEntrance.class.getResource("/").toString());
//        DataWindowMetaDataMgr.getInstance().init(this.getClass().getClassLoader().getResource("/").getPath() + "core/conf/datawindow/");//取固定路径
//        DataWindowMetaDataMgr.getInstance().init("WebContent/template/");//取固定路径
        if(responseBusinessObject != null){//机构部门同步
            if(responseBusinessObject.getResponseHead().getErrorCode().equals("0000")){
                map = DWTransformTool.getBusinessVoMap(responseBusinessObject);
                baseList.addAll(DWTransformTool.getDataObjListWithBusinessVo("synchronize.response.BASE", new SynchronizeResponseBase(), map));
                claimList.addAll(DWTransformTool.getDataObjListWithBusinessVo("synchronize.response.CLAIM", new SynchronizeResponseClaim(), map));
                taxpaymentCodeList.addAll(DWTransformTool.getDataObjListWithBusinessVo("synchronize.response.TAXPAYMENTCODE", new SynchronizeResponseTaxpaymentCode(), map));
                Date createDate = new Date();
                for(int i = 0;i < baseList.size(); i++){
                    Object objTemp = baseList.get(i);
                    if (objTemp instanceof SynchronizeResponseBase){
                        returnMap.put("synchronizeResponseBase",(SynchronizeResponseBase)objTemp);
                    }
                }
                for(int i = 0;i < claimList.size(); i++){
                    Object objTemp = claimList.get(i);
                    if (objTemp instanceof SynchronizeResponseClaim){
                        SynchronizeResponseClaim syncResponseClaim = (SynchronizeResponseClaim)objTemp;//刚取到的Claim
                        syncResponseClaim.setRecCreateDate(createDate);
                        syncResponseClaim.setDptCode(syncResponseClaim.getDptCode().substring(0, 2));
                        TempBrmUwClaim tempBrmUwClaim = new TempBrmUwClaim(syncResponseClaim);//转换完成后的用于临时表的Claim
                        tempClaimList.add(tempBrmUwClaim);
                    }
                }
                
                for(int i = 0;i < taxpaymentCodeList.size(); i++){
                    Object objTemp = taxpaymentCodeList.get(i); 
                    if (objTemp instanceof SynchronizeResponseTaxpaymentCode){
                        SynchronizeResponseTaxpaymentCode syncResponseTaxpaymentCode = (SynchronizeResponseTaxpaymentCode)objTemp;//刚取到的TaxpaymentCode
                        syncResponseTaxpaymentCode.setCrtTm(createDate);
                        TempBrmTaxpaymentCode tempBrmUwClaim = new TempBrmTaxpaymentCode(syncResponseTaxpaymentCode);//转换完成后的用于临时表的TaxpaymentCode
                        tempTaxpaymentCodeList.add(tempBrmUwClaim);
                    }
                }
                
                returnMap.put("tempClaimList",tempClaimList);
                returnMap.put("tempTaxpaymentCodeList",tempTaxpaymentCodeList);
            }
        }
        return returnMap;
    }
    
    /**从核心提取当前批次的信息
     * @param updateType 更新方式,1、获取总批次数据,2、分批更新
     * @param batches 批次号
     * @param startTime 起时
     * @return
     * @throws Exception
     */
    public ResponseBusinessObject connectToCore(String updateType, String batches,Date startTime ) throws Exception{
//        log.info("同步初始业务数据-开始initSynchronizePara");
        RequestBusinessObject requestObject = new RequestBusinessObject();
        RequestHead head = new RequestHead();
        head.setRequestType("0000-36");
        head.setSysName("ILOG");//外围系统名
        head.setSysCode("04");//外围系统代码
        head.setUserCde("100009999");//操作用户代码 核心用到,识别用户
        head.setDptCde("00");//
        head.setServiceNo("0101030205");//
        head.setServiceName("synchronized");//request response文件夹上一层目录名
        head.setUserId("50000005"); //esb 分配的用户ID,仅限于esb用来区分不同的接入系统
        
        requestObject.setRequestHead(head);
        HashMap<String, BusinessVo> map = new HashMap<String, BusinessVo>();
        DataWindowMetaDataMgr.getInstance().init(this.getClass().getClassLoader().getResource("/").getPath().split("WEB-INF")[0]+"template/");
//        DataWindowMetaDataMgr.getInstance().init("WebContent/template/");//取固定路径
//        DataWindowMetaDataMgr.getInstance().init(this.getClass().getClassLoader().getResource("/").getPath() + "core/conf/datawindow/");
        RequestBody body = new RequestBody();
        SynchronizeRequestBase baseVo = new SynchronizeRequestBase();
        baseVo.setUpdate_type(updateType);//更新方式,1、获取总批次数据,2、分批更新
        baseVo.setBatches(batches);
        baseVo.setStart_tm(startTime);
//        baseVo.setStart_tm(DateUtil.stringToDate(("2012-12-29 12:00:00" == null ? "" :"2012-12-29 12:00:00"), DateUtil.FORMAT_0));//传固定值uwclaim
//        baseVo.setStart_tm(DateUtil.stringToDate(("2013-09-18 00:00:00" == null ? "" :"2013-09-18 00:00:00"), DateUtil.FORMAT_0));//传固定值taxpayment
        DWTransformTool.addToMap(map, "synchronize.request.BASE", baseVo);
        DWTransformTool.addToBusinessVoMap(map, body);
        requestObject.setRequestBody(body);
        ESBClient client = new ESBClient(100000,100000,100000); 
//        String xml1 = XmlTool.convertToXml(requestObject);
        ResponseBusinessObject responseObject = client.invoke(requestObject);//该方法会致使程序执行完后不关闭
//        String xml2 = XmlTool.convertToXml(responseObject);
        return responseObject;
    }
    
    public ResponseBusinessObject  generateTestResponse(String fileNameInSrc) throws Exception{
//        log.info("同步初始业务数据-开始initSynchronizePara");
        RequestBusinessObject requestObject = new RequestBusinessObject();
        RequestHead head = new RequestHead();
        head.setRequestType("0000-36");
        head.setSysName("ILOG");//外围系统名
        head.setSysCode("04");//外围系统代码
        head.setUserCde("100009999");//操作用户代码 核心用到,识别用户
        head.setDptCde("00");//
        head.setServiceNo("0101030205");//
        head.setServiceName("synchronized");//request response文件夹上一层目录名
        head.setUserId("50000005"); //esb 分配的用户ID,仅限于esb用来区分不同的接入系统
        
        requestObject.setRequestHead(head);
        
        File file = new File(fileNameInSrc);
//        File file = new File("src/"+fileNameInSrc);
        logger.info(file.getAbsolutePath());
        Scanner s = new Scanner(file);
        StringBuilder sb = new StringBuilder();
        while(s.hasNext()){
            sb.append(s.nextLine());
        }
        Document document = XmlTool.getDocument(sb.toString(), "GBK");
        ResponseBusinessObject response = XmlTool.getFromResponseXml(document, requestObject);
        return response;
    }
    

}

 

ESB 客户端调用 处理类

标签:

原文地址:http://www.cnblogs.com/bobo1713930654/p/4798457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!