package com.eactive.eai.batch.job.jobItem.ksdps;

import java.io.ByteArrayOutputStream;

import com.eactive.eai.batch.job.jobItem.DefaultTelegram;
import com.eactive.eai.batch.job.jobItem.TelegramService;

import com.eactive.eai.common.exception.ExceptionUtil;

public class RECV_KSDPS_T000 extends DefaultTelegram implements TelegramService
{


    /**
     * 생성자
     */
    public RECV_KSDPS_T000() {
        //증권 예탁원 개시/종료/통신망 전문
    }

    /**
     * 전문 해체하기
     */
    public boolean doExecute() {
        if (!super.doExecute()) return false;
//        String fileName = new String ((byte[]) al_Field.get(2));
//        String fileSize = new String ((byte[]) al_Field.get(3));
//        String procType = batchDoc.getBatchMsg().getHeader().getProcessType();
//        if ( procType.equalsIgnoreCase(CommonKeys.PROCESS_REQUEST_RECEIVE) ||
//             procType.equalsIgnoreCase(CommonKeys.PROCESS_RESPONSE_RECEIVE)) {
//        	batchDoc.getBatchMsg().getHeader().setFileName(fileName);
//        	batchDoc.getBatchMsg().getHeader().setFileSize(Long.parseLong(fileSize));
//            //파일명으로 부터 거래 구분 코드 추출
//            int bizCodeStartIndex = 0;
//            int bizCodeEndIndex   = 0;
//            String processCode = batchDoc.getBatchMsg().getHeader().getProcessCode();
//            String institutionCode = batchDoc.getBatchMsg().getHeader().getInstitutionCode();
//            ArrayList<OutsideVO> organList = OutsideManager.getInstance().getOutsideInfo(processCode);
//            for (int i=0; organList!=null && i<organList.size(); i++) {
//                OutsideVO organInfo = organList.get(i);
//                if ( institutionCode.equals(organInfo.getOsdCode()) ) {
//                    bizCodeStartIndex = organInfo.getBizCdStartIdx();
//                    bizCodeEndIndex   = organInfo.getBizCdEndIdx();
//                    break;
//                }
//            }
//            if (bizCodeStartIndex <= 0 || bizCodeStartIndex >= fileName.length()) bizCodeStartIndex = 0;
//            if (bizCodeEndIndex   <= 0 || bizCodeEndIndex   >  fileName.length()) bizCodeEndIndex = fileName.length();
//            String bizCode = fileName.substring(bizCodeStartIndex, bizCodeEndIndex);
//            batchDoc.getBatchMsg().getHeader().setBizCode(bizCode);
//
//            BatchJobInfoVO bJobInfo = DirInfoManager.getInstance().getFileInfoByDstcd(processCode, bizCode);
//            batchDoc.getBatchMsg().getHeader().setJobCode(bJobInfo.getBatchCode());
//
//        }
        return true;
    }

    /**
     * 전문 Validation
     */
    public boolean doValidation()
    {
        if (!super.doValidation()) return false;

        return true;
    }

    public boolean doPostExecute() {
        if (!super.doPostExecute()) return false;
        try {
			//DB로그에 남길 전문 헤더 설정
            ByteArrayOutputStream baout = new ByteArrayOutputStream();
            for (int i=0; i<4; i++) {
                baout.write((byte[])this.al_Field.get(i));
            }
            batchDoc.getBatchMsg().setTelegramHeader(new String(baout.toByteArray()));

            batchDoc.getBatchMsg().setTelegramBody("");

            return true;

        } catch(Exception ex) {
            String[] msgArgs = new String[1];
            msgArgs[0] = this.batchDoc.getBatchMsg().getHeader().getProcessCode();
            String errCode = "BECEAIFJI002";
            String errMsg = ExceptionUtil.getErrorCode (ex, errCode, msgArgs);
            this.batchDoc.getBatchMsg().getBody().setErrorMsg(errMsg);
            logger.error( ex.getMessage(), ex);
            return false;
        }
    }

    //---------------- 특정 필드값을 구하는 부분
    // 전문종별코드 필드값 리턴
    public String getTelegramType()
    {
		return super.getFieldValueString(0);
    }
    //응답코드 필드값 리턴
    public String getResCode()
    {
    	if (super.getFieldValueString(1).equals("0000"))
    		return "000";
		return super.getFieldValueString(1).substring(1);
    }
}
