package com.eactive.eai.inbound.remote;

import java.sql.ResultSet;
//import java.sql.ResultSetMetaData;
import java.util.ArrayList;

import org.slf4j.LoggerFactory;

import com.eactive.eai.batch.common.StringUtil;
import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.util.Logger;

public class RemoteDirectoryDAO extends BaseDAO implements RemoteDirectoryQuery
{

	public RemoteDirectoryDAO() {
		super();
		super.logger = Logger.getLogger(Logger.LOGGER_FILEEVENT);
	}


	//-------------------------------------------------------------------------------------------
	// REMOTE 서버정보 상세조회
	//-------------------------------------------------------------------------------------------
	public ArrayList<RemoteDirectoryVO> selectRemoteDirectory(String searchProcessCode, String searchType, String searchText) throws DAOException {

		ResultSet rs = null;
		int index    = 0;

        try {
			String query = SEARCH_REMOTE_DIRECTORY;
            if("SERVER_NAME".equals(searchType)) {
				query = query + " AND A.\"RmtSevrName\" LIKE '%" + searchText + "%'\n";
			}
			else if("DIRECTORY_NAME".equals(searchType)) {
				query = query + " AND B.\"RmtSevrJobDirName\" LIKE '%" + searchText + "%'\n";
			}
			else if("DIRECTORY_ID".equals(searchType)) {
				query = query + " AND B.\"RmtSevrJobDirID\" LIKE '" + searchText + "%'\n";
			}
			query = query + " ORDER BY B.\"RmtSevrJobDirID\" ";

            
            this.connect(query);
			logger.debug(">>> 리모트 디렉토리 검색 SQL : \n"+ query);

			index = 1;
            this.preparedStatement.setString(index++, searchProcessCode);	logger.debug(">>> 바인드("+(index-1)+") : ["+ searchProcessCode +"]");
            rs = executeQuery();

			ArrayList<RemoteDirectoryVO> alist = new ArrayList<RemoteDirectoryVO>();
			while (rs.next()) {
                RemoteServerVO sinfo = new RemoteServerVO();
                sinfo.setRemoteServerId  (StringUtil.nvlTrim(rs.getString(1 )));   // 리모트서버 ID (PK)
                sinfo.setRemoteServerName(StringUtil.nvlTrim(rs.getString(2 )));   // 리모트서버 명
                sinfo.setProcessCode     (StringUtil.nvlTrim(rs.getString(3 )));   // 업무구분코드 (FK)
                sinfo.setRemoteServerIP  (StringUtil.nvlTrim(rs.getString(4 )));   // 리모트서버 IP
                sinfo.setRemoteNdmPort   (StringUtil.nvlTrim(rs.getString(5 )));   // 리모트서버 NDM Port Number
                sinfo.setRemoteFtpPort   (StringUtil.nvlTrim(rs.getString(6 )));   // 리모트서버 FTP Port Number
                sinfo.setRemoteUserId    (StringUtil.nvlTrim(rs.getString(7 )));   // 리모트서버 FTP User ID
                sinfo.setRemotePassword  (StringUtil.nvl    (rs.getString(8 )));   // 리모트서버 FTP User Password
                sinfo.setRemoteRootDir   (StringUtil.nvlTrim(rs.getString(9 )));   // 리모트서버 Root Directory
                sinfo.setAutoTransferType(StringUtil.nvlTrim(rs.getString(10)));   // 리포트 파일 이벤트시 파일 복사 방법
                sinfo.setUseYn           (StringUtil.nvlTrim(rs.getString(11)));   // 리모트서버 사용여부
                sinfo.setCreateId        (StringUtil.nvlTrim(rs.getString(12)));   // 리모트서버 등록자ID
                sinfo.setCreateTime      (StringUtil.nvlTrim(rs.getString(13)));   // 리모트서버 등록시간
                sinfo.setUpdateId        (StringUtil.nvlTrim(rs.getString(14)));   // 리모트서버 수정자ID
                sinfo.setUpdateTime      (StringUtil.nvlTrim(rs.getString(15)));   // 리모트서버 수정시간

				//리모트 Root Directory 가 "/" 로 끝나면 "/" 제거
				if (sinfo.getRemoteRootDir().endsWith("/")) {
					sinfo.setRemoteRootDir(sinfo.getRemoteRootDir().substring(0, sinfo.getRemoteRootDir().length()-1));
				}

				RemoteDirectoryVO info = new RemoteDirectoryVO();
				info.setRemoteDirId   (StringUtil.nvlTrim(rs.getString(16)));
				info.setRemoteDirName (StringUtil.nvlTrim(rs.getString(17)));
				info.setRemoteServerId(StringUtil.nvlTrim(rs.getString(18)));
				info.setProcessCode   (StringUtil.nvlTrim(rs.getString(19)));
				info.setProcessName   (StringUtil.nvlTrim(rs.getString(20)));
				info.setOrganCode     (StringUtil.nvlTrim(rs.getString(21)));
				info.setOrganName     (StringUtil.nvlTrim(rs.getString(22)));
				info.setBizGroupCode  (StringUtil.nvlTrim(rs.getString(23)));
				info.setBizGroupName  (StringUtil.nvlTrim(rs.getString(24)));
                info.setUseYn         (StringUtil.nvlTrim(rs.getString(25)));
				info.setCreateId      (StringUtil.nvlTrim(rs.getString(26)));
				info.setCreateTime    (StringUtil.nvlTrim(rs.getString(27)));
				info.setUpdateId      (StringUtil.nvlTrim(rs.getString(28)));
				info.setUpdateTime    (StringUtil.nvlTrim(rs.getString(29)));
				info.setRemoteServerVO  (sinfo);

				alist.add(info);
            }
			logger.info(">>> 리모트 디렉토리 검색 건수 : "+ alist.size());
            return alist;

 		} catch (DAOException ex) {
			throw ex;
        } catch (Exception e) {
			String errMsg = ExceptionUtil.getErrorCode(e, "UNDEFINEDCD", new String[] {});
			throw new DAOException(errMsg);	//<--------------
        } finally {
            disconnect();
        }
    }

	//-------------------------------------------------------------------------------------------
	// REMOTE 서버정보 신규
	//-------------------------------------------------------------------------------------------
//	public int insertRemoteDirectory(RemoteDirectoryVO remoteDirectoryVO) throws DAOException {
//
//		int retValue = 0;
//		int index    = 0;
//
//		try {
//
//			String query = INSERT_REMOTE_DIRECTORY;
//            this.connect(query);
//			logger.debug(">>> 리모트 디렉토리 등록 SQL : \n"+ query);
//
//			index = 1;
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getRemoteServerId());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getRemoteServerId() +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getRemoteDirName ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getRemoteDirName () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getRemoteServerId());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getRemoteServerId() +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getProcessCode   ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getProcessCode   () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getOrganCode     ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getOrganCode     () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getBizGroupCode  ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getBizGroupCode  () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getUseYn         ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getUseYn         () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getCreateId      ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getCreateId      () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getCreateTime    ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getCreateTime    () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getUpdateId      ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getUpdateId      () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getUpdateTime    ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getUpdateTime    () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getRemoteServerId());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getRemoteServerId() +"]");
//
//			retValue = this.executeUpdate();
//			logger.info(">>> 리모트 디렉토리 등록 건수 : "+ retValue);
//
//			return retValue;
//
//		} catch (DAOException ex) {
//			throw ex;
//		} catch (Exception e) {
//			String errMsg = ExceptionUtil.getErrorCode(e, "UNDEFINEDCD", new String[] {});
//			throw new DAOException(errMsg);	//<--------------
//		} finally {
//			disconnect();
//		}
//	}
//
//
//	//-------------------------------------------------------------------------------------------
//	// REMOTE 서버정보 변경
//	//-------------------------------------------------------------------------------------------
//	public int updatetRemoteDirectory(RemoteDirectoryVO remoteDirectoryVO) throws DAOException {
//
//		int retValue = 0;
//		int index    = 0;
//
//		try {
//
//			String query = UPDATE_REMOTE_DIRECTORY;
//            this.connect(query);
//			logger.debug(">>> 리모트 디렉토리 수정 SQL : \n"+ query);
//
//			index = 1;
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getRemoteServerId());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getRemoteServerId() +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getRemoteDirName ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getRemoteDirName () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getOrganCode     ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getOrganCode     () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getBizGroupCode  ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getBizGroupCode  () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getUseYn         ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getUseYn         () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getUpdateId      ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getUpdateId      () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getUpdateTime    ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getUpdateTime    () +"]");
//			this.preparedStatement.setString(index++, remoteDirectoryVO.getRemoteDirId   ());	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirectoryVO.getRemoteDirId   () +"]");
//
//			retValue = this.executeUpdate();
//			logger.info(">>> 리모트 디렉토리 수정 건수 : "+ retValue);
//
//			return retValue;
//
//		} catch (DAOException ex) {
//			throw ex;
//		} catch (Exception e) {
//			String errMsg = ExceptionUtil.getErrorCode(e, "UNDEFINEDCD", new String[] {});
//			throw new DAOException(errMsg);	//<--------------
//		} finally {
//			disconnect();
//		}
//	}
//
//
//	//-------------------------------------------------------------------------------------------
//	// REMOTE 서버정보 삭제
//	//-------------------------------------------------------------------------------------------
//	public int deleteRemoteDirectory(String remoteDirId) throws DAOException {
//
//		int retValue = 0;
//		int index    = 0;
//
//		try {
//
//			String query = DELETE_REMOTE_DIRECTORY;
//            this.connect(query);
//			logger.debug(">>> 리모트 디렉토리 삭제 SQL : \n"+ query);
//
//			index = 1;
//			this.preparedStatement.setString(index++, remoteDirId);	logger.debug(">>> 바인드("+(index-1)+") : ["+ remoteDirId +"]");
//
//			retValue = this.executeUpdate();
//			logger.info(">>> 리모트 디렉토리 삭제 건수 : "+ retValue);
//
//			return retValue;
//
//		} catch (DAOException ex) {
//			throw ex;
//		} catch (Exception e) {
//			String errMsg = ExceptionUtil.getErrorCode(e, "UNDEFINEDCD", new String[] {});
//			throw new DAOException(errMsg);	//<--------------
//		} finally {
//			disconnect();
//		}
//	}


}
