package com.eactive.eai.rms.onl.manage.serviceinfo.stdmessage;

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.common.util.CommonUtil;


public class RestoreSTDMessageDeleteJob implements Job {
    /**
     * Logger for this class
     */
     private static final Logger logger = Logger.getLogger(RestoreSTDMessageDeleteJob.class);

     private transient RestoreSTDMessageDeleteService service;
     
     
     
//     @ScheduleMethod(type = ScheduleType.CRON, cronExpression = "0 0 3 * * ?")
	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 ;
		}  
		service = (RestoreSTDMessageDeleteService)appContext.getBean("restoreSTDMessageDeleteService");	
		
		
		// 프로퍼티로 뺄 것
       	//String cronYn = monitoringContext.getStringProperty("rms.EMPTY_RESTORE_STDMSG_CRON_YN", "N"); // 실행여부
      	logger.info("START RestoreSTDMessageDeleteJob");
  		String today     = CommonUtil.getToday("yyyyMMdd");
  		String targetDay = CommonUtil.getDiffHourDate(today, -24, "yyyyMMdd"); // 하루전

	  	for( DataSourceType d : DataSourceTypeManager.getOnlineDataSourceTypes() ) {
	  		DataSourceContextHolder.setDataSourceType(d);
	  		int result = service.deleteTable(d, targetDay);
	  		logger.debug("[" + d.getName() +"][" + result +"]");
	  	}
      	logger.info("END RestoreSTDMessageDeleteJob");		
	}     
}