package com.eactive.eai.rms.ext.cm.epb;

import java.io.File;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.bap.loader.service.TransactionBAPService;
import com.eactive.eai.rms.common.base.BapBaseAnnotationController;
import com.eactive.eai.rms.common.datasource.DataSourceType;


@Service("cmBAPService")
public class CmBAPService extends BapBaseAnnotationController {

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

    @Autowired
    @Qualifier("loaderBAPService") 
    private TransactionBAPService loaderBAPService; 
    
    public ArrayList<String> insertFIleInfoFile(DataSourceType dt, File fileinfoDir) throws Exception{
    	ArrayList<String> al = new ArrayList<String>();
    	
		String path = fileinfoDir.getAbsolutePath() + File.separator;
		File subDir = new File(path);
		String[] sublistFiles = subDir.list();
		
		if (sublistFiles != null && sublistFiles.length > 0) {
			for(String fileName : sublistFiles){ 
				String data = fileName.substring(0,fileName.length()-4);
				al.add(data);
				logger.info("CmBAPService fileName : fileName =" + fileName +" , data =" + data +" , path = "+ path);
				loaderBAPService.insertFileInfoLoading(dt, path, fileName);
			}
		}
    	return al;
    }    
    
    public boolean fileInfoMemorySync() throws Exception {
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.dirInfo.RefreshAllJobCommand");
		agentUtilService.broadcast(command);
		long currentTime = System.currentTimeMillis();
		logger.info(">> 메모리동기화   처리시간(ms) : " + (System.currentTimeMillis() - currentTime));

		return true;
	}   
}
