package com.eactive.eai.rms.onl.statistics.backup.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

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 org.springframework.stereotype.Service;

/**
 * <pre>
 * 1. 기능 : Daily Backup의 비즈니스 로직이 담겨져 있다.
 * 2. 처리 개요 : 
 *     - 
 * 3. 주의사항 : 
 * </pre>
 * @author  : 박기섭
 * @version : $Revision: 1.5 $
 * @since : rts 1.0
 */
@Service("monthlyBackupJob")
public class MonthlyBackupJob implements Job {

    /**
     * Logger for this class
     */
	private static final Logger logger = Logger.getLogger(MonthlyBackupJob.class);
	
	private MonthlyBackupService monthlyBackupService;

    
//  @ScheduleMethod(concurrent = false, type = ScheduleType.CRON, cronExpression = "0 0 4 1 * ?")
	@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 ;
		}
		
		monthlyBackupService = (MonthlyBackupService)appContext.getBean("monthlyBackupService");
		
    	Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        Date beforeMonth = cal.getTime();

        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        String date = format.format(beforeMonth);
        
        monthlyBackupService.doAction(date);
	}
}
