package com.eactive.eai.flowcontroller;

import java.util.Properties;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.eactive.eai.adapter.AdapterGroupVO;
import com.eactive.eai.adapter.AdapterManager;
import com.eactive.eai.adapter.Keys;
import com.eactive.eai.common.EAIKeys;
import com.eactive.eai.common.EAITable;
import com.eactive.eai.common.exception.ExceptionHandler;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.message.EAIMessage;
import com.eactive.eai.common.message.EAIMessageKeys;
import com.eactive.eai.common.message.MessageType;
import com.eactive.eai.common.property.PropManager;
import com.eactive.eai.common.routing.IFRouter;
import com.eactive.eai.common.routing.Process;
import com.eactive.eai.common.routing.RouteKeys;
import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.common.util.MessageUtil;
import com.eactive.eai.common.util.ObjectUtil;
import com.eactive.eai.common.util.ServiceUtil;
import com.eactive.eai.control.LogSender;
import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.service.InterfaceMapper;
import com.eactive.eai.transformer.function.util.CodeConversion;
import com.ext.eai.common.stdmessage.STDMessageKeys;

public class CompFCProcess extends Process {
	public String adptrMsgPtrnCd;

	public java.lang.String adapterGroupName;

	public int logPssSeq;

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

	public java.util.Properties callProp;

	public EAIMessage resEaiMsg;

	public com.eactive.eai.common.message.EAIMessage reqEaiMsg;

	static Logger esbLogger = Logger.getLogger(Logger.LOGGER_ESBFW);
	private int svrLogLevel;
	private static ThreadLocal local = new ThreadLocal(); // 경과시간 동기화를 위한 ThreadLocal

	String guidLogPrefix = "";

	static final long serialVersionUID = 1L;

	public EAIMessage clientReturn() {
		if (svrLogLevel >= 1) {
			try {
				long msgSndTm = System.currentTimeMillis();

				Long threadData = (Long) local.get();
				long processTm = msgSndTm - threadData.longValue();
				if (esbLogger.isInfo())
					esbLogger.info(
							guidLogPrefix + "End... - 경과시간 : " + processTm + "(ms) - EAI Server Log Level is INFO");
			} catch (Exception e) {
				logger.warn("clientReturn", e);
			}
		}
		return resEaiMsg;
	}

	public void callService(com.eactive.eai.common.message.EAIMessage reqEaiMsg, java.util.Properties callProp) {
		this.reqEaiMsg = reqEaiMsg;
		this.callProp = callProp;

		if (this.reqEaiMsg == null)
			return;
		svrLogLevel = this.reqEaiMsg.getSvrLogLvl();

//        guidLogPrefix = "BaseFCProcess] GUID["+reqEaiMsg.getKbMsg().getKBHeader().getGuIdNo()+"] UUID["+this.reqEaiMsg.getSvcOgNo()+"] ";
		guidLogPrefix = "BaseFCProcess] GUID[" + getGuid(reqEaiMsg) + "] UUID[" + this.reqEaiMsg.getSvcOgNo() + "] ";

		if (svrLogLevel >= 1) {
			try {
				long msgRcvTm = System.currentTimeMillis();
				local.set(new Long(msgRcvTm));

				if (esbLogger.isInfo())
					esbLogger.info(guidLogPrefix + " started... - EAI Server Log Level is INFO");
			} catch (Exception e) {
				logger.warn("callService", e);
			}
		}

		try {
			flow();
		} catch (Exception e) {
			printExceptionMessage(e);
			if (isSourceAsync()) {
				logSenderCtrlSend();
				callExceptionHandler();
			}
			return;
		}
	}

	public void flow() throws Exception {
		// isRealCall
		if (!isRealCall()) {
			return;
		}
		// 초기화
		init();
		// Outbound 프로세스 호출
		ifRouterProcess();
		// GUID설정
		setGUID();
//    	if (checkServiceMsgSize()){
//    		callMultiProcess();
//    	}
	}

//    public void callMultiProcess() throws Exception{
//    	
//        if (logger.isDebug()) logger.debug( guidLogPrefix + "ASYNC-SYNC CALLED");
//        //---------------------------------------------------------------
//        // AS의 경우 다음 서비스메시지(AS 응답)에 키관리메시지내용을 복사한다.
//        //---------------------------------------------------------------
//        String keyMgtMsgVl = resEaiMsg.getCurrentSvcMsg().getKeyMgtMsgVl();
//        if(logger.isDebug()) logger.debug( guidLogPrefix + "AS 응답 keyMgtMsgVl["+keyMgtMsgVl+"]");
//        
//        EAIMessage newReqEaiMsg = SerializationUtils.clone(this.reqEaiMsg);
//        newReqEaiMsg.setSvcPssSeq( newReqEaiMsg.getSvcPssSeq() + 1 );
//        newReqEaiMsg.getCurrentSvcMsg().setKeyMgtMsgVl(keyMgtMsgVl);
//        //---------------------------------------------------------------
//        
//        // 기동이 표준인 경우만
////        if(com.eactive.eai.adapter.Keys.IF_STANDARD.equals(newReqEaiMsg.getStdMsgUsgCls())){
////        	resEaiMsg = ExtMessageConverter.setAsyncSyncResponseForStandard(guidLogPrefix, reqEaiMsg, resEaiMsg);
////        }
//        
//        try {
//        	this.resEaiMsg = IFRouter.process(newReqEaiMsg, this.callProp);
//        } catch(Exception e) {
//            String[] msgArgs = new String[1];
//            msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
//            String rspErrorCode = "RECEAIFBF100";            
//            String orgErrorCode = ExceptionUtil.getErrorCode(e, rspErrorCode);
//            String rspErrorMsg = ExceptionUtil.make(rspErrorCode, orgErrorCode, msgArgs);
//            if (logger.isError()) logger.error(guidLogPrefix + rspErrorMsg);
//            this.resEaiMsg.setRspErrCd(rspErrorCode);
//            this.resEaiMsg.setRspErrMsg(rspErrorMsg);
//            //throw new RuntimeException(rspErrorCode);
//            // 2009.11.26 : 정길영과장 요청
//            // AS 응답송신 중 에러가 밸생할 경우 400 에러로그를 남기도록 수정
//            this.logPssSeq = 400;
//            LogSender.send(this.logPssSeq, this.resEaiMsg, null);
//        }
//    }

	public void ifRouterProcess() throws Exception {
		try {
			String keyMgtMsgVl = null;
			JSONObject totalResDataObject = new JSONObject();

			for (int i = 0; i < reqEaiMsg.getSvcMsgs().size(); i++) {
				EAIMessage reqMsg = (EAIMessage) ObjectUtil.deepCopy(this.reqEaiMsg);
				reqMsg.setSvcPssSeq(i + 1);
				if (keyMgtMsgVl != null)
					reqMsg.getCurrentSvcMsg().setKeyMgtMsgVl(keyMgtMsgVl);
				this.resEaiMsg = IFRouter.process(reqMsg, this.callProp);
//	        	byte[] resBytes = (byte[]) this.resEaiMsg.getExtMsg().getBizData()[0];
				String bizData = this.resEaiMsg.getStandardMessage().getBizData();

//	        	ServiceMessage svcMsg = reqMsg.getSvcMsg(i);
//	        	String encode = null;
//	        	if("1".equals(svcMsg.getBsRspCnvEn()) && StringUtils.isNotBlank(svcMsg.getBsRspCnvMsgID())) {
//	        		Layout layout = TransformManager.getManager().getTransform(svcMsg.getBsRspCnvMsgID()).getSourceLayout(0);
//	        		if("UJSON".equals(layout.getLayoutType().getName())) {
//	        			encode = "utf-8";
//	        		} else {
//	        			encode = "euc-kr";
//	        		}
//	        	}
//	        	
//	        	if(StringUtils.isBlank(encode)) {
//	        		AdapterGroupVO gvo = AdapterManager.getInstance().getAdapterGroupVO(svcMsg.getPsvSysItfTp());
//	        		encode = MessageUtil.isUTF8(gvo.getMessageType()) ? "utf-8": "ms949";
//	        	}
//	        	String resString = new String(resBytes, encode);

//	        	totalResDataObject.putAll((JSONObject)JSONValue.parse(resString));

				totalResDataObject.putAll((JSONObject) JSONValue.parse(bizData));
				keyMgtMsgVl = resEaiMsg.getCurrentSvcMsg().getKeyMgtMsgVl();
			}

			AdapterGroupVO gvo = AdapterManager.getInstance().getAdapterGroupVO(this.adapterGroupName);

//			String encode = MessageUtil.isUTF8(gvo.getMessageType()) ? "utf-8" : "ms949";
//        	this.resEaiMsg.getExtMsg().getBizData()[0] = totalResDataObject.toJSONString().getBytes(encode);
		} catch (Exception e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIFBF001";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(guidLogPrefix + rspErrorMsg);
//			this.resEaiMsg.setRspErrCd(rspErrorCode);
//			this.resEaiMsg.setRspErrMsg(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			throw new Exception(rspErrorCode);
		}
	}

	public void ifRouterProcessResponse() throws Exception {
		try {
			this.reqEaiMsg.setSvcPssSeq(2);
			this.resEaiMsg = IFRouter.process(this.reqEaiMsg, this.callProp);
//        	this.resEaiMsg.getExtMsg().setSendRecv(STDMessageKeys.SEND_RECV_CD_SEND);
			setReturnType(this.resEaiMsg, STDMessageKeys.SEND_RECV_CD_SEND);
		} catch (Exception e) {
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIFBF100";
			String orgErrorCode = ExceptionUtil.getErrorCode(e, rspErrorCode);
			String rspErrorMsg = ExceptionUtil.make(rspErrorCode, orgErrorCode, msgArgs);
			if (logger.isError())
				logger.error(guidLogPrefix + rspErrorMsg);
//			this.resEaiMsg.setRspErrCd(rspErrorCode);
//			this.resEaiMsg.setRspErrMsg(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
			// throw new RuntimeException(rspErrorCode);
			// 2009.11.26 : 정길영과장 요청
			// AS 응답송신 중 에러가 밸생할 경우 400 에러로그를 남기도록 수정
			this.logPssSeq = 400;
			LogSender.send(this.logPssSeq, this.resEaiMsg, null);
		}
	}

	public void logSenderCtrlSend() {
//        String telgmDmndDstcd = kbmessage.getKBHeader().getTelgmDmndDstcd();
		String returnType = getReturnType(reqEaiMsg);

		if (STDMessageKeys.SEND_RECV_CD_RECV.equals(returnType)) {
			this.logPssSeq = 400;
		} else {
			this.logPssSeq = 200;
		}

		try {
			LogSender.send(this.logPssSeq, this.resEaiMsg, null);
		} catch (Exception e) {
			if (logger.isError())
				logger.error(guidLogPrefix + "AA Exception Logging Error :" + e.getMessage());
		}
	}

	public boolean checkServiceMsgSize() {
		// Outbound Error일 경우
		if (MessageUtil.checkRspErrCd(this.resEaiMsg.getRspErrCd()) && reqEaiMsg.getSvcMsgs().size() > 1) {
			return true;
		} else {
			return false;
		}
	}

	public void setCurrentService() throws Exception {
		if (logger.isDebug())
			logger.debug(guidLogPrefix + "ASYNC-SYNC CALLED");
		// ---------------------------------------------------------------
		// AS의 경우 다음 서비스메시지(AS 응답)에 키관리메시지내용을 복사한다.
		// ---------------------------------------------------------------
		String keyMgtMsgVl = resEaiMsg.getCurrentSvcMsg().getKeyMgtMsgVl();
		if (logger.isDebug())
			logger.debug(guidLogPrefix + "AS 응답 keyMgtMsgVl[" + keyMgtMsgVl + "]");
		resEaiMsg.setSvcPssSeq(resEaiMsg.getSvcPssSeq() + 1);
		resEaiMsg.getCurrentSvcMsg().setKeyMgtMsgVl(keyMgtMsgVl);
		// ---------------------------------------------------------------

		// 기동이 표준인 경우만
		if (com.eactive.eai.adapter.Keys.IF_STANDARD.equals(this.reqEaiMsg.getStdMsgUsgCls())) {
//        	resEaiMsg = ExtMessageConverter.setAsyncSyncResponseForStandard(guidLogPrefix, reqEaiMsg, resEaiMsg);
			setReturnType(resEaiMsg, STDMessageKeys.SEND_RECV_CD_RECV);
		}

	}

	public boolean isOutboundCall() {
		return true;
	}

	public void init() throws Exception {
		InterfaceMapper mapper = this.reqEaiMsg.getMapper();
		StandardMessage standardMessage = this.reqEaiMsg.getStandardMessage();
		String guid = mapper.getGuid(standardMessage);
		String guidseq = mapper.getGuidSeq(standardMessage);

		if (logger.isDebug())
			logger.debug(guidLogPrefix + " REQ Current GUID [" + guid + "][" + guidseq + "]");
		mapper.nextGuidSeq(standardMessage);
		guidseq = mapper.getGuidSeq(standardMessage);
		if (logger.isDebug())
			logger.debug(guidLogPrefix + " REQ ADD PATHSEQ GUID [" + guid + "][" + guidseq + "]");

		String adapterType = "";
		this.resEaiMsg = this.reqEaiMsg;

		this.adapterGroupName = reqEaiMsg.getCurrentSvcMsg().getPsvSysItfTp();
		
		EAIServerManager eaiServerManager = EAIServerManager.getInstance();
		if (eaiServerManager.isTASEnabledEAIServer() && EAIMessageKeys.TRANTYPE_TAS.equals(reqEaiMsg.getTranType())) {
//        	this.adptrMsgPtrnCd = com.eactive.eai.adapter.Keys.IF_NONSTANDARD;
//        	adapterType = Keys.TYPE_NET2;
			PropManager manager = PropManager.getInstance();
			Properties prop = manager.getProperties(RouteKeys.SIM);

			if (reqEaiMsg.getCurrentSvcMsg().isSyncItfTp()) {
				this.adapterGroupName = prop.getProperty(RouteKeys.SIM_ADAPTER_SYNC, "_SIM_OU_HTT_SyC");
			} else {
				this.adapterGroupName = prop.getProperty(RouteKeys.SIM_ADAPTER_ASYNC, "_SIM_OU_HTT_AsC");
			}
		} else {
			AdapterManager adapterManager = AdapterManager.getInstance();
			AdapterGroupVO adptGrpVO = adapterManager.getAdapterGroupVO(this.adapterGroupName);

			// 등록된 어댑터가 실제 존재하지 않는 경우 : 2009.04.03
			if (adptGrpVO == null) {
				String[] msgArgs = new String[1];
				msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
				String rspErrorCode = "RECEAIFBF003";
				String rspErrorMsg = ExceptionUtil.make(rspErrorCode, msgArgs) + "(어댑터그룹 미등록/미사용 - "
						+ this.adapterGroupName + ")";
				if (logger.isError())
					logger.error(guidLogPrefix + rspErrorMsg);
//				this.resEaiMsg.setRspErrCd(rspErrorCode);
//				this.resEaiMsg.setRspErrMsg(rspErrorMsg);
				this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
				throw new Exception(rspErrorCode);
			}
			// failover 사용시에는 제거 되어야됨
//	        if(adptGrpVO.nextAdapterVO() == null) {
//	        	String[] msgArgs = new String[1];
//	            msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
//	            String rspErrorCode = "RECEAIFBF002";            
//	            String rspErrorMsg = ExceptionUtil.make(rspErrorCode, msgArgs) 
//	            				+  ","+this.adapterGroupName;
//	            if (logger.isError()) logger.error( guidLogPrefix + rspErrorMsg);
//	            this.resEaiMsg.setRspErrCd(rspErrorCode);
//	            this.resEaiMsg.setRspErrMsg(rspErrorMsg);
//	            throw new Exception(rspErrorCode);
//	        }
			this.adptrMsgPtrnCd = adptGrpVO.getAdptrMsgPtrnCd();
			adapterType = adptGrpVO.getType();
		}

		String svcTsmtUsgTp = ""; // 기동 호출방식
		String psvItfTp = ""; // 수동 호출방식

		// 2009.03.11
		// AA가 아니고 기동이 표준이 아닌 경우
		// Adapter의 timeout값을 설정한다.
		try {
			svcTsmtUsgTp = this.reqEaiMsg.getSvcTsmtUsgTp(); // 기동 호출방식
			psvItfTp = this.reqEaiMsg.getCurrentSvcMsg().getPsvItfTp(); // 수동 호출방식
			if (!(EAIMessageKeys.ASYNC_SVC.equals(svcTsmtUsgTp) && EAIMessageKeys.ASYNC_SVC.equals(psvItfTp))
					&& !(com.eactive.eai.adapter.Keys.IF_STANDARD.equals(this.reqEaiMsg.getStdMsgUsgCls()))) {
				// SA 또는 SNA Adapter일 경우에 는 SA Timeout값을 설정
				if ((EAIMessageKeys.SYNC_SVC.equals(svcTsmtUsgTp) && EAIMessageKeys.ASYNC_SVC.equals(psvItfTp))
						|| Keys.TYPE_SNA.equals(adapterType)) {
					int saTimeoutSec = this.reqEaiMsg.getCurrentSvcMsg().getTmoVl();
					if (logger.isDebug())
						logger.debug(guidLogPrefix + " SA TimeoutValue [" + saTimeoutSec + "]");
//	            	this.reqEaiMsg.getKbMsg().getKBHeader().setGnrzToutCrtcNmvl(""+saTimeoutSec);
					setTimeout(this.reqEaiMsg, "" + saTimeoutSec);
				} else {
					int timeout = ServiceUtil.getTimeoutValue(adapterGroupName);
					if (logger.isDebug())
						logger.debug(guidLogPrefix + " Target Adapter TimeoutValue [" + timeout + "]");
					if (timeout > 0) {
						if (logger.isDebug())
							logger.debug(guidLogPrefix + "Set Adapter TimeoutValue [" + timeout + "] to STDMessage");
//	            		this.reqEaiMsg.getKbMsg().getKBHeader().setGnrzToutCrtcNmvl(""+timeout);
						setTimeout(this.reqEaiMsg, "" + timeout);
					}
				}
			}
		} catch (Exception e) {
			if (logger.isWarn()) {
				logger.warn(guidLogPrefix + "Get Adapter TimeoutValue Error - " + e.getMessage(), e);
			}
		}

		// -----------------------------------------------------
		// TheK 요건 : SNA송신 비동기 응답거래시 수신거래코드를 EAI에서 매핑함 (20190522)
		String srcAdapterGroupName = reqEaiMsg.getSngSysItfTp();
		AdapterManager adapterManager = AdapterManager.getInstance();
		AdapterGroupVO inAdptGrpVO = adapterManager.getAdapterGroupVO(srcAdapterGroupName);
		String inAdapterMsgType = MessageType.ASC;
		if (eaiServerManager.isTASEnabledEAIServer() && EAIMessageKeys.TRANTYPE_FROMTAS.equals(reqEaiMsg.getTranType())
				&& inAdptGrpVO == null) {
			logger.info(guidLogPrefix + "- TAS 기동거래 [" + srcAdapterGroupName + "] = " + MessageType.ASC);
		} else {
			if (inAdptGrpVO == null) {
				String[] msgArgs = new String[1];
				msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
				String rspErrorCode = "RECEAIFBF003";
				String rspErrorMsg = ExceptionUtil.make(rspErrorCode, msgArgs) + "(어댑터그룹 미등록/미사용 - "
						+ srcAdapterGroupName + ")";
				if (logger.isError())
					logger.error(guidLogPrefix + rspErrorMsg);
//				this.resEaiMsg.setRspErrCd(rspErrorCode);
//				this.resEaiMsg.setRspErrMsg(rspErrorMsg);
				this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
				throw new Exception(rspErrorCode);
			}
			inAdapterMsgType = inAdptGrpVO.getMessageType();
		}
	}

	public void printExceptionMessage(Exception e) {
		// Exception e = this.context.getExceptionInfo().getException();
		if (logger.isError())
			logger.error(guidLogPrefix + "ExceptionPath : " + e.getMessage(), e);
	}

	public boolean isSourceAsync() {
		if (EAIMessageKeys.ASYNC_SVC.equals(this.reqEaiMsg.getSvcTsmtUsgTp())) {
			return true;
		} else {
			return false;
		}
	}

	public void callExceptionHandler() {
		try {
			AdapterManager adapterManager = AdapterManager.getInstance();
			AdapterGroupVO inAdptGrpVO = adapterManager.getAdapterGroupVO(this.reqEaiMsg.getSngSysItfTp());
			String inAdapterMsgType = inAdptGrpVO.getMessageType();
			if (EAITable.EBCDIC_TYPE.equals(inAdapterMsgType)) {
				String bizData = this.reqEaiMsg.getStandardMessage().getBizData();
				byte[] asc = CodeConversion.meToMixedAsc(bizData.getBytes());
				try {
					String inboundCharset = callProp.getProperty(com.eactive.eai.adapter.Keys.CHARSET_KEY_IN);
					this.reqEaiMsg.getStandardMessage().setBizData(new String(asc), inboundCharset);
				} catch (Exception e) {
					logger.error("setBizData error", e);
				}
			}
			ExceptionHandler.handle(this.reqEaiMsg);
		} catch (Exception e) {
			if (logger.isError())
				logger.error(guidLogPrefix + "ExceptionHandler Call Error", e);
		}
	}

	public boolean isRealCall() {
		if (this.callProp != null) {
			String mode = this.callProp.getProperty(EAIKeys.CALL_MODE_KEY);
			if (mode != null && EAIKeys.CALL_MODE_WARMUP.equals(mode)) {
				logger.warn(guidLogPrefix + "Warming Up Called");
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	}

//    private String getAdapterType(String adapterGroupName) {
//    	AdapterManager adapterManager = AdapterManager.getInstance();
//    	AdapterGroupVO adptGrpVO = adapterManager.getAdapterGroupVO(this.adapterGroupName);
//    	return adptGrpVO.getType();
//    }

	public void setGUID() throws Exception {
		InterfaceMapper mapper = this.resEaiMsg.getMapper();
		StandardMessage standardMessage = this.resEaiMsg.getStandardMessage();
		String guid = mapper.getGuid(standardMessage);
		String guidseq = mapper.getGuidSeq(standardMessage);

		if (logger.isDebug())
			logger.debug(guidLogPrefix + " REQ Current GUID [" + guid + "][" + guidseq + "]");
		mapper.nextGuidSeq(standardMessage);
		guidseq = mapper.getGuidSeq(standardMessage);
		if (logger.isDebug())
			logger.debug(guidLogPrefix + " REQ ADD PATHSEQ GUID [" + guid + "][" + guidseq + "]");

		String svcTsmtUsgTp = this.reqEaiMsg.getSvcTsmtUsgTp(); // 기동 호출방식
		String psvItfTp = this.reqEaiMsg.getCurrentSvcMsg().getPsvItfTp(); // 수동 호출방식

		if ((!(com.eactive.eai.adapter.Keys.IF_STANDARD.equals(this.adptrMsgPtrnCd))
				&& !(EAIMessageKeys.ASYNC_SVC.equals(svcTsmtUsgTp) && EAIMessageKeys.ASYNC_SVC.equals(psvItfTp)))
				|| ((com.eactive.eai.adapter.Keys.IF_STANDARD.equals(this.adptrMsgPtrnCd))
						&& (EAIMessageKeys.ASYNC_SVC.equals(svcTsmtUsgTp)
								&& EAIMessageKeys.SYNC_SVC.equals(psvItfTp)))) {
			String prcssRtdTranCd = mapper.getReturnServiceId(standardMessage);

			if (prcssRtdTranCd != null && prcssRtdTranCd.length() >= 14) {
				String cicsTrncd = prcssRtdTranCd.substring(0, 4);
				String telgmRecvTranCd = prcssRtdTranCd.substring(4, 14);
				mapper.setServiceId(standardMessage, telgmRecvTranCd);
			} else {
				if (logger.isInfo()) {
					logger.info(guidLogPrefix + "- PrcssRtdTranCd Length < 14 [" + prcssRtdTranCd + "]");
				}
			}
		}

		if (!(EAIMessageKeys.ASYNC_SVC.equals(svcTsmtUsgTp) && EAIMessageKeys.ASYNC_SVC.equals(psvItfTp))
				&& (com.eactive.eai.adapter.Keys.IF_STANDARD.equals(this.reqEaiMsg.getStdMsgUsgCls()))
				&& (STDMessageKeys.RECOVER_YN_YES
						.equals(getRecoverYn(this.reqEaiMsg)) ) ) {
			String reqOgtranGuIdNo = getOrgGuid(this.reqEaiMsg);
			mapper.setOrgGuid(standardMessage, reqOgtranGuIdNo);
			if (logger.isInfo()) {
				logger.info(guidLogPrefix + " OgtranGuIdNo 복원 " + this.reqEaiMsg.getSvcOgNo() + " OgtranGuIdNo ["
						+ mapper.getOrgGuid(standardMessage) + "]");
			}
		}
	}
}
