package com.eactive.eai.rms.common.meta;

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;

public class MetaSyncJob implements Job {
	private static final Logger logger = Logger.getLogger(MetaSyncJob.class);

	public void execute(JobExecutionContext context) throws JobExecutionException {

		ApplicationContext appContext = null;
		try {
			appContext = (ApplicationContext)context.getScheduler().getContext().get("applicationContext");
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			return ;
		}
		if (appContext == null){
			logger.info("[MetaSyncJob.metaSync ]appContext is null");
		}
		if (appContext.getBean("metaSyncService") == null){
			logger.info("[MetaSyncJob.metaSync ]metaSyncService is null");
		}
		
		MetaSyncService service = (MetaSyncService)appContext.getBean("metaSyncService");		
		
		
		
		logger.info("[MetaSyncJob.metaSync START]----------");
    	//1. Meta Á¤º¸ Á¶È¸
    	//int iCnt = 0;
    	//int tCnt = 0;
    	try {
        	for(DataSourceType dataSourceType : DataSourceTypeManager.getOnlineDataSourceTypes()){
        		DataSourceContextHolder.setDataSourceType(dataSourceType);
        		int cnt=0;
        		cnt = service.syncItem();
        		logger.info("[MetaSyncJob.metaSync] item="+cnt);
        		cnt = service.syncTelegram();
        		logger.info("[MetaSyncJob.metaSync] telegram="+cnt);
        		cnt = service.syncStdErrMsg();
        		logger.info("[MetaSyncJob.metaSync] std error message="+cnt);
        		cnt = service.syncCode();
        		logger.info("[MetaSyncJob.metaSync] code="+cnt);
        		cnt = service.syncActnMsg();
        		logger.info("[MetaSyncJob.metaSync] actn message="+cnt);
        	}

    	} catch(Exception e) {
    		logger.error(e.getMessage(), e);
    	} finally {
    		logger.info("[MetaSyncJob.metaSync END]----------");
    	}

        
	}
}
