package com.eactive.eai.rms.client.agent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.TimeZone;

import com.eactive.eai.common.monitor.EAIServiceMonitor;
import com.eactive.eai.common.monitor.MonitorVO;
import com.eactive.eai.rms.client.SocketClient;
import com.eactive.eai.rms.onl.vo.TransactionSendVo;

/**
 * The Class TransactionTrackingAgent.
 * <ul>
 * <li>1. Function :</li>
 * <li>2. Summary :</li>
 * <li>3. Notification :</li>
 * </ul>
 * 
 * @author  : $Author: amjang $
 * @version : $Revision: 1.4 $
 */
public class TransactionTrackingAgent extends AbstractScheduledAgent {

    /** The socket client. */
    private SocketClient socketClient;

    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        EAIServiceMonitor monitor = EAIServiceMonitor.getInstance();
        sendTransactionOriginData( monitor );
    }

    /**
     * 
     * @param monitor F/W 의 모니터링 정보
     */
    private void sendTransactionOriginData( EAIServiceMonitor monitor ) {
    	try {
	    	// 2010.01.25  YUN - 
	        // 만일 오늘 날짜와 모니터링 날짜가 다르면 (거래건수 초기화 수행중 )
	        // 데이터를 전송하지 않는다.
	        // 주의 : Framework.jar 파일의 버전에 추가된 함수를 사용하므로, 함께 배포되어야 한다. 
	        DateFormat df = new SimpleDateFormat("yyyyMMdd");
	        TimeZone tz = TimeZone.getTimeZone("Asia/Seoul");
	        df.setTimeZone(tz);
	
	        String today   = df.format(new Date());
	        String monDate = monitor.getMonitorDate();
	        if( ! today.equals(monDate) ) {
	        	logger.debug("DEBUG_sendTransactionOriginData_today | monDate : " + today + "|" + monDate);
	            return ;
	        }
	        
	        TransactionSendVo sendVo = (TransactionSendVo) applicationContext.getBean("transactionSendVo");
	        
	        String allBwkClsKey[] = monitor.getAllBwkClsKey();
	        if(allBwkClsKey == null) {
	        	logger.debug("DEBUG_sendTransactionOriginData_allBwkClsKey_isNull");
	        	return; // 2011.02.27 가비지에 의한 에러처리
	        }
	        for (int bwkClsCount = 0; bwkClsCount < allBwkClsKey.length; bwkClsCount++) {
	            // 서비스 코드별 vo 맵
	            HashMap<String, MonitorVO> voByBwk = monitor.getBwkCls(allBwkClsKey[bwkClsCount]);
	            if(voByBwk == null) logger.debug("DEBUG_sendTransactionOriginData_voByBwk_isNull");
	            if (voByBwk != null) {
	                for( MonitorVO vo : voByBwk.values() ) {
	                	if(vo == null) {
	                		logger.debug("DEBUG_sendTransactionOriginData_MonitorVO_isNull");
	                		continue; // 2011.02.27 가비지에 의한 에러처리
	                	}
	    	            
	                	sendVo.setCmd("DATA");
	                    sendVo.setKey1(allBwkClsKey[bwkClsCount]);
	                    sendVo.setKey2(vo.getEaiServiceCode());
	                    sendVo.setVo( vo );
	                    socketClient.send( sendVo );
	                    
	                    if( logger.isDebugEnabled() ) {
	                        logger.debug( "RMS SEND NEW TRANSACTION(" + sendVo.getCmd() + ":key1=" + sendVo.getKey1() + ", key2=" + sendVo.getKey2());
	                    }
	                }
	            }	        	
	        }
	        
	        // 분할된 데이터 전송이 끝났음을  RMS 서버에 알린다.
	        if( logger.isDebugEnabled() ) {
	            logger.debug( "RMS SEND NEW TRANSACTION(END)" );
	        }
	
	        // cmd 객체에 모니터링 날짜를 담아서 보낸다.
	        sendVo.setKey1(monDate);
	        sendVo.setCmd("END");
	        socketClient.send( sendVo );
	        
	        logger.debug("SEND TransactionTrackingAgent : " + sendVo );
    	} catch (Exception e) {
    		logger.error("DEBUG_sendTransactionOriginData_Exception",e);
    		//e.printStackTrace();
    	}
    }

    /**
     * Sets the socket client.
     * 
     * @param socketClient the new socket client
     */
    public void setSocketClient(SocketClient socketClient) {
        this.socketClient = socketClient;
    }

}
