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

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

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.rms.bat.loader.service.TransactionLoaderServiceImpl;
import com.eactive.eai.rms.common.base.OnlBaseService;
import com.eactive.eai.rms.common.datasource.DataSourceType;

@Service("cmBATService")
public class CmBATService extends OnlBaseService {

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

    @Autowired
    @Qualifier("batLoaderService") 
    private TransactionLoaderServiceImpl batLoaderService;    
    
  
    public HashMap<String,ArrayList<String>> insertInterfaceFile(DataSourceType dt, File interfaceDir) throws Exception{
    	HashMap<String,ArrayList<String>> result = new HashMap<String,ArrayList<String>>();
    	ArrayList<String> al = new ArrayList<String>();
    
    	String path = interfaceDir.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("CmBATService interface : fileName =" + fileName +" , data =" + data +" , path = "+ path);
    			batLoaderService.insertInterfaceLoading(dt,path , fileName);    			
    		}
    	}
    	if (al.size()>0) {
    		result.put(interfaceDir.getName(), al);
    	}    	
    	
    	return result;
    }    
 }
