//package com.eactive.eai.batch.eventscheduler.util;
//
//import java.io.IOException;
//import java.io.OutputStream;
//import java.util.Properties;
//import java.util.Vector;
//
//import org.apache.commons.net.ftp.FTPClient;
//import org.apache.commons.net.ftp.FTPConnectionClosedException;
//import org.apache.commons.net.ftp.FTPFile;
//import org.apache.commons.net.ftp.FTPReply;
//
//import com.eactive.eai.batch.common.StringUtil;
//import com.eactive.eai.batch.eventscheduler.management.SchedulerEventVO;
//import com.eactive.eai.batch.eventscheduler.management.SchedulerKeys;
//import com.eactive.eai.common.exception.ExceptionUtil;
//import com.eactive.eai.common.property.PropManager;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import com.eactive.eai.inbound.remote.RemoteServerVO;
//
//public class RemoteFTPUtil {
//	
//	private Vector<String[]> fileInfos;
//	
//    
//    private FTPClient ftp = null;
//    
//    private OutputStream output = null;
//    private String propIp = "";
//    private String propPort = "21";
//    private String propUser = "";
//    private String propPassword = "";
//    private String propRemoteDir = "";
//    private String propPassive = "active";
//    private String propArchiveDir = "";
//    private String searchFileName = "";
//    private String posActionType = "";
//
//    private String delimiter ="";
//    
//    static Logger logger = LoggerFactory.getLogger(ElinkLogger.LOGGER_SCHEDULER);
//    
//    public RemoteFTPUtil() {
//    	fileInfos = new Vector<String[]>();
//    }
//	
//    public Vector<String[]> searchFileInfo(SchedulerEventVO vo, String searchFile) throws Exception {
//        
//    	searchFileName = searchFile;
//        //Properties mapProp = vo.getEventPropMap();
//    	String propGroup = vo.getPropGroupName();
//		Properties mapProp = null;
//		
//		try{
//    		if(propGroup !=null && propGroup.length() > 0){
//    			PropManager pmanager = PropManager.getInstance();
//    			mapProp = pmanager.getPropGroupVO(propGroup).getProperties();    		
//    		}
//    		
//		}catch (Exception e){
//			e.printStackTrace();
//			logger.error("RemoteFTPUtil ] searchFileInfo - 프로퍼티 정보를 설정하는데 오류가 발생했습니다. - "+e.getMessage());
//		}
//        try {
//        	
//        	//RemoteDirectoryVO rdvo = vo.getRemoteDirectoryVO();
//        	RemoteServerVO rsvo = vo.getRemoteServerVO();
//        	
//        	propPassive = (String)(mapProp.get(SchedulerKeys.PASSIVE_MODE)==null?"active":mapProp.get(SchedulerKeys.PASSIVE_MODE));
//        	
//        	propIp = rsvo.getRemoteServerIP();
//        	propPort = rsvo.getRemoteFtpPort();
//        	propUser = rsvo.getRemoteUserId();
//        	propPassword = rsvo.getRemotePassword();
//        	//propRemoteDir = rdvo.getRemoteDirName();
//        	propRemoteDir = mapProp.getProperty(SchedulerKeys.REMOTE_DIR);
//        	
//        	/*
//        	propIp = mapProp.getProperty(SchedulerKeys.REMOTE_IP);
//        	propUser = mapProp.getProperty(SchedulerKeys.USER);
//        	propPassword = mapProp.getProperty(SchedulerKeys.PASSWORD);
//        	propRemoteDir = mapProp.getProperty(SchedulerKeys.REMOTE_DIR);
//        	propPort = mapProp.getProperty(SchedulerKeys.REMOTE_PORT);
//        	*/
//        	delimiter = StringUtil.getDirDelimiter(propRemoteDir);
//        	posActionType = (String)(mapProp.get(SchedulerKeys.POST_ACTION_TYPE)==null?SchedulerKeys.POST_ACTION_NONE:mapProp.get(SchedulerKeys.POST_ACTION_TYPE));
//        	propArchiveDir = (String)mapProp.get(SchedulerKeys.ARCH_DIR);
//        	
//        	
//            
//            if (propRemoteDir == null || propRemoteDir.length() <=0){
//            	throw new Exception(ExceptionUtil.getErrorCode("BECEAIMEU011"));
//            }
//            
//            String remoteFileName = "";
//   
//            logger.debug("RemoteFTPUtil ] propIp:propPort:propUser:propPassword - "+propIp+":"+propPort+":"+propUser+":"+propPassword );
//            ftp = connect(propIp, Integer.parseInt(propPort), propUser, propPassword, Boolean.getBoolean(propPassive));
//            
//            // 디렉토리 변경
//			boolean result = ftp.changeWorkingDirectory(propRemoteDir); 
//			
//			if (!result){
//				String arg[] = new String[1];
//				arg[0] = propRemoteDir;
//				throw new Exception(ExceptionUtil.getErrorCode("BECEAIMEU012", arg));
//			}
//
//			FTPFile[] ftpFiles = ftp.listFiles(propRemoteDir);
//            
//            if ((ftpFiles != null) && (ftpFiles.length != 0)) {
//            	for (int i=0; i<ftpFiles.length; i++) {
//	                remoteFileName = ftpFiles[i].getName();                  
//	                
//	                if (remoteFileName.length() > propRemoteDir.length()) {
//	                    remoteFileName = remoteFileName.substring(propRemoteDir.getBytes().length);
//	                }
//	
//	                // 첫번째 목록이 파일 타입인지 확인한다.
//	                if (ftpFiles[i].isFile()){
//	                	String[] fileInfo = new String[2];
//	                	
//	                	if (searchFileName !=null && searchFileName.length()>0){
//	                		if (remoteFileName.indexOf(searchFileName)>0){		//탐색하고자 하는 파일명(BJ01의 BjobTranDstcdName값 참조
//	                			
//	                			fileInfo[0] = propRemoteDir;
//	                			fileInfo[1] = remoteFileName;
//	                			fileInfos.add(fileInfo);
//	                			
//	                			logger.debug("RemoteFTPUtil ] 지정된 파일을 찾았습니다.  searchFileName - ["+searchFileName+"] - remoteFileName ["+remoteFileName+"]");
//	                			
//	                			postAction(ftp, propRemoteDir, remoteFileName);
//
//	                		}
//	                	}else {
//	                		fileInfo[0] = propRemoteDir;
//	                		fileInfo[1] = remoteFileName;
//	                		fileInfos.add(fileInfo);
//	                		
//	                		postAction(ftp, propRemoteDir, remoteFileName);
//
//	                	}	
//	                }else {
//	                	reDirectory(ftp,  propRemoteDir+delimiter+remoteFileName);
//	                }
//            	}
//            }
//            ftp.logout();
//            return fileInfos;
//        } catch (FTPConnectionClosedException e) {
//            e.printStackTrace();
//            String arg[] = new String[2];
//            arg[0] = propIp;
//            arg[1] = ""+propPort;
//            throw new IOException(ExceptionUtil.getErrorCode(e, "BECEAIMEU013", arg));
//        } catch (IOException e) {
//        	e.printStackTrace();
//        	throw new IOException(ExceptionUtil.getErrorCode(e, "BECEAIMEU014"));
//        } catch (Exception e) {
//        	e.printStackTrace();
//        	throw new Exception(ExceptionUtil.getErrorCode(e, "BECEAIMEU014"));
//        } finally {
//            if (output != null) {
//                try {
//                    output.close();
//                } catch (Exception e) {
//                    // do nothing
//                }
//            }
//            if (ftp != null && ftp.isConnected()) {
//                try {
//                    ftp.disconnect();
//                } catch (IOException f) {
//                    // do nothing
//                }
//            }
//            ftp = null;
//        }
//    }
//    
//
//    private FTPClient connect(String server, int port, String username,
//            String password, boolean passive) throws IOException {
//        FTPClient ftpClient = new FTPClient();
//
//        try {
//        	
//            if (passive) {
//               ftpClient.enterLocalPassiveMode();
//            } else {
//                ftpClient.enterLocalActiveMode();
//            }
//            
//        	ftpClient.setControlEncoding("euc-kr");
//
//            int reply;
//            ftpClient.connect(server, port);
//
//            reply = ftpClient.getReplyCode();
//
//            if (!FTPReply.isPositiveCompletion(reply)) {
//            	ftpClient.disconnect();
//                throw new IOException(ExceptionUtil.getErrorCode("BECEAIMEU015"));
//            }
//
//            if (!ftpClient.login(username, password)) {
//            	ftpClient.logout();
//            	String arg[] = new String[0];
//            	arg[0] = username;
//            	throw new IOException(ExceptionUtil.getErrorCode("BECEAIMEU016", arg));
//            }
//            return ftpClient;
//        } catch (IOException ie) {
//            try {
//                String arg[] = new String[2];
//                arg[0] = server;
//                arg[1] = ""+port;
//                throw new IOException(ExceptionUtil.getErrorCode("BECEAIMEU017", arg));
//            } catch (Exception ex) {
//            }
//            throw ie;
//        } finally {
//            try {
//             if (ftp.isConnected()) {
//             ftp.disconnect();
//             }
//             } catch (Exception f) {
//             // do nothing
//             }
//        }
//    }
//    
//
//    private void reDirectory (FTPClient ftp, String currentDir) throws Exception {                                                                                            
//        
//    	
//    	try {                                                                                                                                       
//        	 // 디렉토리 변경                                                                                                                          
//			boolean isChange = ftp.changeWorkingDirectory(currentDir);                                                                                  
//			if (!isChange) {   
//				String arg[] = new String[1];
//				arg[0]=currentDir;
//				throw new Exception(ExceptionUtil.getErrorCode("BECEAIMEU018", arg));
//			}                                                                                                                                              
//                                                                                                                                       
//			FTPFile[] subFile = ftp.listFiles(currentDir);
//			                                                                                                                                                                                                         
//            String remoteFile = "";
//                                                                                                                                   
//            if ((subFile != null) && (subFile.length != 0)) {                                                                                       
//                for (int i=0; i<subFile.length; i++) {                                                                                              
//                    remoteFile = subFile[i].getName();                                                                                                                                    
//                    if (remoteFile.length() > currentDir.length()) {                                                                               
//                        remoteFile = remoteFile.substring(currentDir.getBytes().length);                                                       
//                    }                                                                                                                               
//                                                                                                                                 
//                    // 파일 타입인지 확인한다.                                                                                                      
//                    if(subFile[i].isDirectory()) {	//directory type이면
//                    	reDirectory(ftp,  currentDir+delimiter+remoteFile);                                                                                                 
//                    	                                                                                                                               
//                    } else if(subFile[i].isFile()) {  
//                    	String[] fileInfo = new String[2];
//                    	
//                    	
//                    	if (searchFileName !=null && searchFileName.length()>0){
//                    		if (remoteFile.indexOf(searchFileName)>0){		//탐색하고자 하는 파일명(BJ01의 BjobTranDstcdName값 참조
//                    			fileInfo[0] = currentDir;
//                    			fileInfo[1] = remoteFile;
//                    			fileInfos.add(fileInfo);
//                    			
//                    			logger.debug("RemoteFTPUtil ] 지정된 파일을 찾았습니다.  searchFileName - ["+searchFileName+"] - remoteFileName ["+remoteFile+"]");
//                    			
//                    			postAction(ftp, currentDir, remoteFile );
//                    		}
//                    	}else {
//                    		fileInfo[0] = currentDir;
//                    		fileInfo[1] = remoteFile;
//                    		fileInfos.add(fileInfo);
//                    		
//                    		postAction(ftp, currentDir, remoteFile );
//                    	}
//                    	
//                    } 
//                }
//            }
//            subFile = null;
//            
//        } catch (Exception f) {
//        	String arg[] = new String[1];
//			arg[0]=currentDir;
//			throw new Exception(ExceptionUtil.getErrorCode(f, "BECEAIMEU019", arg));
//        }
//    }
//    
//    private void postAction(FTPClient ftp, String dirName, String fileName) throws Exception{
//    	if (SchedulerKeys.POST_ACTION_ARCH.equals(posActionType)){
//    		
//    		//if (!ftp.rename(dirName+delimiter+fileName, propArchiveDir+delimiter+fileName+"_" +CalendarUtil.getCurrentTimeNoDash())){
//    		if (!ftp.rename(dirName+delimiter+fileName, propArchiveDir+delimiter+fileName)){
//    			logger.error("RemoteFTPUtil ] File remane failed");
//    			logger.error("                : org -["+dirName+delimiter+fileName+"], new-["+propArchiveDir+delimiter+fileName+"]");
//    		}else {
//    			logger.debug("RemoteFTPUtil ] File rename : org -["+dirName+delimiter+fileName+"], new-["+propArchiveDir+delimiter+fileName+"]");
//    		}
//    	}else if (SchedulerKeys.POST_ACTION_DELETE.equals(posActionType)){                  		
//    		ftp.deleteFile(dirName+delimiter+fileName);  
//    	}
//    	
//    }
//
//}
