package com.eactive.eai.inbound;

import com.eactive.eai.batch.doc.BatchDoc;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.routing.Process;
import com.eactive.eai.common.util.Logger;

public class ResponseHandler
{
    public static final String ADAPTER_GROUP_NAME     = "ADAPTER_GROUP_NAME";
    public static final String ADAPTER_NAME           = "ADAPTER_NAME";

    public static final String BATCH_UUID             = "BATCH_UUID";
    public static final String BATCH_PROCESS_CODE     = "BATCH_PROCESS_CODE";
    public static final String BATCH_PROCESS_SER_NO   = "BATCH_PROCESS_SER_NO";
    public static final String BATCH_TELEGRAM_TIMEOUT = "BATCH_TELEGRAM_TIMEOUT";
    public static final String BATCH_SERVER_PORT      = "BATCH_SERVER_PORT";

    static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

    public BatchDoc execute(BatchDoc batchMsgDoc)
    {
        logger.debug("[ELINK]################ ResponseHandler.execute() 호출됨.");

        long srtTime = 0;
        srtTime = System.currentTimeMillis();


        //응답송수신 Process 호출
        //*** 응답송수신 스케쥴러 Process SKIP! 응답송수신 스케쥴러의 기능이 명확치 않음. 추후 기능정의가 되었을때 추가 예정. <----------
        try {
        	
        	//  JPD호출 변환
//            FlowController process = (FlowController)JpdProxy.create(FlowController.class, FlowController.SERVICE_URI);

        	//Process process = 
            logger.debug("[ELINK]################ 응답 송수신 JPD 호출 전 BatchMessageDoc: \n"+ batchMsgDoc.toString());
    		String className = "com.eactive.eai.batch.job.jobHandle.FlowController";
            @SuppressWarnings("rawtypes")
			Class cl = Class.forName(className);
            Process process = (Process)cl.newInstance();
            batchMsgDoc = process.clientRequestwithReturn(batchMsgDoc);
            logger.debug("[ELINK]################ 응답 송수신 JPD 호출 후 BatchMessageDoc: \n"+ batchMsgDoc.toString());

            //Process 호출 오류시 오류전문 리턴
        } catch (Exception ex) {
            logger.debug("[ELINK]★★★★★★★★ ResponseProcessHandler JPD 호출 시 예외발생 !! ★★★★★★★★");
            logger.debug(ex.getMessage());
            //오류전문 설정 및 DB로깅
            //RTMPSResponseUtil.setOutsideStageErrorTelegram(batchMsgDoc, null, srtTime, ex);
            String errMsg = ExceptionUtil.getErrorCode(ex, "BECEAIFAR004");	//응답수신 예외전문 생성 후 DB로그 저장시 오류가 발생하였습니다.
            logger.error(errMsg, ex);
        }
        logger.debug("[ELINK]################ FLOW_PHASE_CODE: "+ batchMsgDoc.getBatchMsg().getBody().getSubLayerCode());
        logger.debug("[ELINK]################ ResponseHandler.execute() 종료. (수행 시간: "+ ((System.currentTimeMillis() - srtTime)/1000.0) +" Sec)");
        return batchMsgDoc;
    }

}
