package com.eactive.eai.batch.job.jobModule.Component;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.net.Socket;

import com.eactive.eai.batch.job.jobModule.ReSendReqSend;

import com.eactive.eai.batch.common.CommonKeys;
import com.eactive.eai.batch.doc.BatchDoc;
import com.eactive.eai.batch.running.BatchOpenFileManager;
import com.eactive.eai.batch.running.BatchRunningJobManager;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.secure.inisafe.IniSafeManager;
import com.eactive.eai.common.util.UCMsgSenderUtil;

public class EEND extends JobComponent implements JobComponentService
{

    // 소켓을 구한다.
    BatchRunningJobManager bs_man   = BatchRunningJobManager.getInstance();
    Socket             socket ;

    public BatchDoc execute() throws Exception {
        FileInputStream fin = BatchOpenFileManager.getInstance().getFile(batchDoc.getBatchMsg().getHeader().getUUID() );
        if ( fin != null){
            try{
                fin.close();
                BatchOpenFileManager.getInstance().removeFile(batchDoc.getBatchMsg().getHeader().getUUID() );
            } 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);
            }
        }

        BufferedReader fbr = BatchOpenFileManager.getInstance().getFbr(batchDoc.getBatchMsg().getHeader().getUUID() );
        if ( fbr != null){
            try{
                fbr.close();
                BatchOpenFileManager.getInstance().removeFile(batchDoc.getBatchMsg().getHeader().getUUID() );
            } 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);
            }
        }

        BufferedInputStream bis = BatchOpenFileManager.getInstance().getBis(batchDoc.getBatchMsg().getHeader().getUUID());
        if (bis != null){
            try {
                bis.close();
                BatchOpenFileManager.getInstance().removeFile(batchDoc.getBatchMsg().getHeader().getUUID());
            } 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);
            }
        }

        IniSafeManager.getInstance().removeManager(batchDoc);
        
        logger.info("{} EEND.execute>> Batch job end with error.. ", logHeader);
        logger.debug("{} 1. Node Count = " + batchDoc.getBatchMsg().getBody().getNodeCount(), logHeader);
        if (batchDoc.getBatchMsg().getHeader().getProcessType().equalsIgnoreCase(CommonKeys.PROCESS_REQUEST_SEND)
                && batchDoc.getBatchMsg().getHeader().getReqSendRetryFlag()==true){ //요구송신이면서 재처리 설정되어 있으면..
            batchDoc = ReSendReqSend.insertJobToQueue(batchDoc);
        }else{
        	UCMsgSenderUtil.sendUCMsg(batchDoc);
        }
        return batchDoc;
    }
}