package com.eactive.eai.rms.onl.common.service;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;

import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.onl.dashboard.service.Dashboard2RepositoryService;

public class DashboardBatchCountJob implements Job {

    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(DashboardBatchCountJob.class);
	
    
    /** New dashboard repository service */
    Dashboard2RepositoryService repositoryService;

    
    /** New dashboard repository service */
    DashboardTargetService dashboardTargetService;    
    
    
    //@ScheduleMethod(repeatInterval = 5000, type = ScheduleType.TIMER)
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
       // JobKey jobKey = context.getJobDetail().getKey();

		ApplicationContext appContext = null;
		try {
			appContext = (ApplicationContext)context.getScheduler().getContext().get("applicationContext");
		} catch (SchedulerException e) {
			logger.error("applicationContext get module error",e);
			return ;
		}  
		repositoryService      = (Dashboard2RepositoryService)appContext.getBean("dashboard2RepositoryService");	
		dashboardTargetService = (DashboardTargetService)appContext.getBean("DashboardTargetService");	
		
		run();
	}    
    
    /**
     * Run every 1 minute.
     * 5초마다 현재까지의 통계정보를 메모리 에 저장한다. ( 작업이 끝나고 나서 1분 후 이벤트가 발생한다. )
     *  
     */
    public void run() {
    	boolean debug = logger.isDebugEnabled();
    	if( debug ) {
    		logger.debug("START BATCH COUNT SCHEDULER !!!");
    	}
    	
    	try {
    		
    		// TOBE: JNDI 명을 공통정보 에서 얻어오는 방법은 ?
    		DataSourceType dt = DataSourceTypeManager.getDataSourceType(DataSourceTypeManager.BAP);
    		DataSourceContextHolder.setDataSourceType(dt);
        	int counts[] = null;
        	if(dt != null){  //취약점소스 수정 2020.08.25
        		counts = dashboardTargetService.getBatchProcCount( false);
        	}
        	        	
        	if( counts != null && counts.length == 6 ) {
        		repositoryService.setProcBatch( counts );
        	} else {
        		if( counts == null ) {
        			logger.error("batchProcCount : returned null !");
        		} else {
        			logger.error("batchProcCount : invalid array size (" + counts.length + ")");
        		}
        	}
        	
        	if( debug && counts != null ) {
            	for( int i = 0 ; i < counts.length ; i++ ) {
            		logger.debug("batch_counts[" + i + "]=" + counts[i] );
            	}        		
        	}
        	
        	// EAI_BATCH
        	 //취약점소스 수정 2020.08.25
    		DataSourceType dt2 = DataSourceTypeManager.getDataSourceType(DataSourceTypeManager.BAT);
    		DataSourceContextHolder.setDataSourceType(dt2);
    		int counts2[] = null;
        	
        	if(dt2 != null){
        		counts2 = dashboardTargetService.getBatchProcCount( true);
        	}
        			
        	if( counts2 != null && counts2.length == 6 ) {
        		repositoryService.setProcEaiBatch( counts2 );
        	} else {
        		if( counts2 == null ) {
        			logger.error("batchProcCount(2) : returned null !");
        		} else {
        			logger.error("batchProcCount(2) : invalid array size (" + counts2.length + ")");
        		}
        	}
        	
        	if( debug && counts2 != null ) {
            	for( int i = 0 ; i < counts2.length ; i++ ) {
            		logger.debug("batch_counts2[" + i + "]=" + counts2[i] );
            	}        		
        	}
    	} catch ( Exception e) {
    		//e.printStackTrace();
    		logger.error("BATCH COUNT SCHEDULER error", e);
    	}
    	
    	if( debug ) {
    		logger.debug("END BATCH COUNT SCHEDULER !!!");
    	}
    }



}
