package com.eactive.eai.adapter.wca.cfg;

import com.eactive.eai.adapter.wca.jdbc.BaseJDBC;
import com.eactive.eai.adapter.wca.jdbc.JDBCException;
import com.eactive.eai.adapter.wca.util.DateUtil;
import com.eactive.eai.common.util.Logger;

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

/** 
* 1. 기능 : LU 정보 테이블을 접근하기 위한 JDBC 클래스
* 2. 처리 개요 :
* 3. 주의사항 
*
* @author  :  
* @version : v 1.0.0
* @see     : LUInfoQuery.java
* @since   : 
*/
public class LUInfoJDBCEx extends BaseJDBC implements LUInfoQueryEx
{     
    Logger trc_logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
    
   /** 
    * 1. 기능 :  노드이름에 해당하는 모든 LU 정보 반환
    * 2. 처리 개요 : 검색된 LU 정보를 LUInfoData Object 저장한다.
    * 3. 주의사항 
    *
    * @params       nodeName        노드 이름
    * @return       HashMap         노드에 해당하는 모든 LU 정보를 저장한다.
    * @exception    JDBCException   DB 작업시 발행하는 예외 발생 정보 (SQLException 에러코드와 메세지 포함)
    **/
   /*
    public HashMap getLUInfos(String nodeName) throws JDBCException {
        ResultSet rs = null;
        
        try {
            getDsConnection();
            setStatement(GET_ALL_LU_INFO);         
            preparedStatement.setString(1, nodeName);
            rs = executeSelect();
            
            HashMap     luinfos   = new HashMap();
            LUInfoData  luinfo         = null;
            while(rs.next()) {
                luinfo = new LUInfoData(rs.getString(1));
                luinfo.setAdapterGroupName(rs.getString(2));
                luinfo.setGroupName(rs.getString(3));
                luinfo.setPUName(rs.getString(4));
                luinfo.setLUName(rs.getString(5));               
                luinfos.put(luinfo.getLUName(), luinfo);
            }
            trc_logger.debug("getLUInfos] luinfos size = " + luinfos.size());            
            return luinfos;
        } catch(Exception e) {
            e.printStackTrace();
            throw new JDBCException("LUInfoJDBC","getLUInfos",e);
        } finally {
            this.close(rs);
        }
    }
    */
   public HashMap getLUInfos(String nodeName) throws JDBCException {
        ResultSet rs = null;
        
        try {
            getDsConnection();
            
            // 20080827 Start
            /*
            setStatement(GET_ALL_LU_INFO);         
            preparedStatement.setString(1, nodeName);
            */
            setStatement(GET_ALL_NODE_LU_INFO);
            // 20080827 End
            rs = executeSelect();
            
            // LU 정보 저장
            HashMap     luInfos          = new HashMap();
            
            LUInfoDataEx  	luinfo    = null;
            String      	sTranMode = null;
            String      	sGroupName = "";
            
            /*
            public static final String GET_ALL_NODE_LU_INFO  
            = "SELECT \"AdptrGstatSevrName\", \"AdptrGroupName\", \"LUGroupName\", \"PUName\", \"LUName\", \"SndrcvDstcd\" " +
       		"FROM " + TableInfoEx.T_LU_INFO + 
       		" ORDER BY \"LUGroupName\", \"AdptrGstatSevrName\", \"AdptrGroupName\" ";
            */
            
            while (rs.next()) {               
                sGroupName = rs.getString(3);
                sTranMode = rs.getString(6);
                luinfo = new LUInfoDataEx(rs.getString(1));
                luinfo.setAdapterGroupName(rs.getString(2));
                luinfo.setGroupName(sGroupName);
                luinfo.setPUName(rs.getString(4));
                luinfo.setLUName(rs.getString(5));
                luinfo.setTranMode(sTranMode);
               
                luInfos.put(luinfo.getLUName(), luinfo);
            }
            
            if(trc_logger.isDebug()) {
            	trc_logger.debug("getLUInfos] luinfos size = " + luInfos.size());
            }
            
            return luInfos;
        } 
        catch (Exception e) {
        	trc_logger.error("LUInfoJDBC", e);
            throw new JDBCException("LUInfoJDBC","getLUInfos",e);
        } 
        finally {
            this.close(rs);
        }
    }
   /** 
    * 1. 기능 :  LU 정보 추가
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @param        data    LUInfoData Object
    * @return       추가된 레코드 수 
    * @exception    JDBCException   DB 작업시 발행하는 예외 발생 정보 (SQLException 에러코드와 메세지 포함)
    **/
    public int insertLUInfo(LUInfoDataEx data) throws JDBCException {
        try {
            DateUtil dateUtil = new DateUtil();            
            String sDateTime = dateUtil.getDateMilliTime();
            getDsConnection();
            
            setStatement(INSERT_LU_INFO);               
            this.preparedStatement.setString(1,data.getNodeName());
            this.preparedStatement.setString(2,data.getAdapterGroupName());
            this.preparedStatement.setString(3,data.getGroupName());
            this.preparedStatement.setString(4,data.getPUName());
            this.preparedStatement.setString(5,data.getLUName());
            this.preparedStatement.setString(6,data.getTranMode());
            this.preparedStatement.setString(7,data.getApplyID());
            this.preparedStatement.setString(8,data.getBackupLUYN());
            this.preparedStatement.setString(9,data.getLogYN());
            this.preparedStatement.setString(10,data.getStringLogLevel());
            this.preparedStatement.setInt(11,data.getLogSize());
            this.preparedStatement.setString(12, sDateTime);
            this.preparedStatement.setString(13, sDateTime);
            
            // 20080827 Start
            /*
            // 20080527 Start
            this.preparedStatement.setString(14,data.getRmtLUYN());
            // 20080527 End
            */
            // 20080827 End
            
            return executeUpdate();
        } 
        catch (SQLException e) {
            throw new JDBCException("LUInfoJDBC", "insertLUInfo", e);
        } 
        finally {
            this.close();   
        }
    }
    
    /** 
    * 1. 기능 :  LU 정보 삭제
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @param        data    LUInfoData Object
    * @return       삭제된 레코드 수 
    * @exception    JDBCException   DB 작업시 발행하는 예외 발생 정보 (SQLException 에러코드와 메세지 포함)
    **/
    public int deleteLUInfo(LUInfoDataEx data) throws JDBCException {
        try {
            getDsConnection();            
            setStatement(DELETE_LU_INFO);                     
            this.preparedStatement.setString(1,data.getNodeName());
            this.preparedStatement.setString(2,data.getAdapterGroupName());
            this.preparedStatement.setString(3,data.getGroupName());
            this.preparedStatement.setString(4,data.getPUName());
            this.preparedStatement.setString(5,data.getLUName());
            
            return executeUpdate();
        } 
        catch (SQLException e) {
            throw new JDBCException("LUInfoJDBC", "deleteLUInfo", e);
        } 
        finally {
            this.close();   
        }
    }
    
   /** 
    * 1. 기능 :  LU 정보 수정
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @param        data    LUInfoData Object
    * @return       수정된 레코드 수 
    * @exception    JDBCException   DB 작업시 발행하는 예외 발생 정보 (SQLException 에러코드와 메세지 포함)
    **/
    public int updateLUInfo(LUInfoDataEx data) throws JDBCException {
        try {
            DateUtil dateUtil = new DateUtil();
            
            getDsConnection();            
            setStatement(UPDATE_LU_INFO);            
            this.preparedStatement.setString(1,data.getTranMode());
            this.preparedStatement.setString(2,data.getApplyID());
            this.preparedStatement.setString(3,data.getBackupLUYN());            
            this.preparedStatement.setString(4,data.getLogYN());
            this.preparedStatement.setString(5,data.getStringLogLevel());
            this.preparedStatement.setInt(6,data.getLogSize());      
            this.preparedStatement.setString(7, dateUtil.getDateMilliTime());     
            
            // 20080827 Start
            // 20080527 Start
            /*
            this.preparedStatement.setString(8,data.getNodeName());
            this.preparedStatement.setString(9,data.getAdapterGroupName());
            this.preparedStatement.setString(10,data.getGroupName());
            this.preparedStatement.setString(11,data.getPUName());
            this.preparedStatement.setString(12,data.getLUName());
            */
            /*
            this.preparedStatement.setString(8,data.getRmtLUYN());
            this.preparedStatement.setString(9,data.getNodeName());
            this.preparedStatement.setString(10,data.getAdapterGroupName());
            this.preparedStatement.setString(11,data.getGroupName());
            this.preparedStatement.setString(12,data.getPUName());
            this.preparedStatement.setString(13,data.getLUName());
            */            
            // 20080527 End
            this.preparedStatement.setString(8,data.getNodeName());
            this.preparedStatement.setString(9,data.getAdapterGroupName());
            this.preparedStatement.setString(10,data.getGroupName());
            this.preparedStatement.setString(11,data.getPUName());
            this.preparedStatement.setString(12,data.getLUName());            
            // 20080827 End
            return executeUpdate();
        } 
        catch (SQLException e) {
            throw new JDBCException("LUInfoJDBC", "updateLUInfo", e);
        } 
        finally {
            this.close();   
        }
    }
}
