package com.eactive.eai.batch.rule.sysInfo;

import java.sql.ResultSet;
import java.util.HashMap;

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.Seed;

public class SysInfoDAO extends BaseDAO implements SysInfoQuery
{

    public HashMap<String, SysInfoVO> getAllSysInfos() throws DAOException
    {
    	HashMap<String, SysInfoVO> map = new HashMap<String, SysInfoVO>();
        ResultSet rs = null;
        SysInfoVO vo = null;
        try
        {
            this.connect(GET_ALL_SYSCODES);
            rs = this.executeQuery();

            while(rs.next())
            {
                vo = new SysInfoVO();

            	vo.setSysCd       ( StringUtil.nvl(rs.getString("sysCd")).trim());
            	vo.setSysName     ( StringUtil.nvl(rs.getString("sysName")).trim());
            	vo.setSysIp       ( StringUtil.nvl(rs.getString("sysIp")).trim());
            	vo.setSysPort     ( rs.getShort("sysPort"));
            	vo.setUserId      ( StringUtil.nvl(rs.getString("userId")).trim());
            	vo.setUserPassword( Seed.decrypt(StringUtil.nvl(rs.getString("userPassword"))));
            	vo.setSendDir     ( StringUtil.nvl(rs.getString("sendDir")).trim());
            	vo.setRecvDir     ( StringUtil.nvl(rs.getString("recvDir")).trim());
            	vo.setSftpYn      ( StringUtil.nvl(rs.getString("sftpYn")).trim());	
            	

                map.put(vo.getSysCd(), vo);
            }
        } catch(Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e,"BECEAIMFR016"));
        } finally {
            this.disconnect();
        }
        return map;
    }

    /**
     * 지정된 RuleCode와 PhaseCode에 해당 NodeInfo 리스트를 리턴한다.
     */
    public SysInfoVO getSysInfo(String sysCd ) throws DAOException
    {
        SysInfoVO vo = null;
        ResultSet   rs = null;
        try {
            this.connect(GET_SYSCODE);
            this.preparedStatement.setString(1, sysCd);
            rs = executeQuery();
            vo = new SysInfoVO();
            if (rs.next())
            {
            	vo.setSysCd       ( StringUtil.nvl(rs.getString("sysCd")).trim());
            	vo.setSysName     ( StringUtil.nvl(rs.getString("sysName")).trim());
            	vo.setSysIp       ( StringUtil.nvl(rs.getString("sysIp")).trim());
            	vo.setSysPort     ( rs.getShort("sysPort"));
            	vo.setUserId      ( StringUtil.nvl(rs.getString("userId")).trim());
            	vo.setUserPassword( Seed.decrypt(StringUtil.nvl(rs.getString("userPassword"))));
            	vo.setSendDir     ( StringUtil.nvl(rs.getString("sendDir")).trim());
            	vo.setRecvDir     ( StringUtil.nvl(rs.getString("recvDir")).trim());
            	vo.setSftpYn      ( StringUtil.nvl(rs.getString("sftpYn")).trim());	
            }
        } catch(Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e,"BECEAIMFR017", new String[] {sysCd, sysCd}));
        } finally {
            this.disconnect();
        }
        return vo;
    }
}
