package com.eactive.eai.inbound.event;

import java.io.File;
import java.util.List;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

import com.eactive.eai.batch.common.FileUtil;
import com.eactive.eai.common.routing.Process;
import com.eactive.eai.common.util.Logger;

@DisallowConcurrentExecution
public class ReqFileEventJob implements Job {
    static Logger logger = Logger.getLogger(Logger.LOGGER_SCHEDULER);
	
	public void execute(JobExecutionContext context) 
	{
        String dir = context.getJobDetail().getJobDataMap().getString("DIR");
        String msg = context.getJobDetail().getJobDataMap().getString("MSG");
        
        try {
            File rootDir = new File(dir);
            if ( rootDir == null || !rootDir.isDirectory()){
            	logger.error( this.getClass().getName() + ":디렉터리 지정이 올바르지 않습니다. -" + dir);
            	return;
            }
            List<File> fileList = FileUtil.getFileList(rootDir);
             
            for (File file : fileList) {

            	String path = file.getParent();
            	String fileName = file.getName();
            	if ( file.delete() ){
    	            @SuppressWarnings("rawtypes")
    				Class cl = Class.forName(msg);
    	            Process process = (Process)cl.newInstance();
    	            process.procFileEvent(path, fileName, null);
            	}else{
            		logger.error( this.getClass().getName() + ":체크 파일을 삭제 할 수 없습니다. -" + file.getAbsolutePath());
            	}
			}

		} catch (Exception e) {
			logger.error("FileEventJob 실행도중 에러가 발생하였습니다. -" + e.getMessage());
		}
	}
	

	public void setDIR( String dir){
		return;
	}
	public void setMSG( String msg){
		return;
	}
	public void setTYPE( String type){
		return;
	}
}
