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

import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;

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

public class NodeInfoDAO extends BaseDAO implements NodeInfoQuery
{

    public HashMap<String, ArrayList<NodeInfoVO>> getAllNodeInfo() throws DAOException
    {
        HashMap<String, ArrayList<NodeInfoVO>> all = new HashMap<String, ArrayList<NodeInfoVO>>();
        ResultSet rs = null;
        NodeInfoVO vo = null;
        try
        {
            this.connect(GET_ALL_NODES);
            rs = this.executeQuery();

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

                vo.setRuleCode(rs.getString(1).trim());
                vo.setPhaseCode(rs.getString(2).trim());
                vo.setPhaseType(rs.getString(3).trim());
                vo.setFlowComp(rs.getString(4).trim());
                vo.setTelegramID(rs.getString(5).trim());
                vo.setLengthField(rs.getInt(6));
                vo.setDesc(rs.getString(7).trim());


                ArrayList<NodeInfoVO> al = all.get(rs.getString(1).trim());
                if (al == null) {
                    al = new ArrayList<NodeInfoVO>();
                    al.add(vo);
                    all.put(rs.getString(1).trim(), al);
                } else {
                    all.remove(rs.getString(1).trim());
                    al.add(vo);
                    all.put(rs.getString(1).trim(), al);
                }
            }
        } catch(Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e,"BECEAIMFR016"));
        } finally {
            this.disconnect();
        }
        return all;
    }

    /**
     * 지정된 RuleCode와 PhaseCode에 해당 NodeInfo 리스트를 리턴한다.
     */
    public NodeInfoVO getNodeInfo(String strRuleCode, String strPhaseCode) throws DAOException
    {
        NodeInfoVO vo = null;
        ResultSet   rs = null;
        try {
            this.connect(GET_PHASE_NODEINFO);
            this.preparedStatement.setString(1, strRuleCode);
            this.preparedStatement.setString(2, strPhaseCode);
            rs = executeQuery();
            vo = new NodeInfoVO();
            while (rs.next())
            {
                vo.setRuleCode(rs.getString(1).trim());
                vo.setPhaseCode(rs.getString(2).trim());
                vo.setPhaseType(rs.getString(3).trim());
                vo.setFlowComp(rs.getString(4).trim());
                vo.setTelegramID(rs.getString(5).trim());
                vo.setLengthField(rs.getInt(6));
                vo.setDesc(rs.getString(7).trim());
            }
        } catch(Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e,"BECEAIMFR017", new String[] {strRuleCode, strPhaseCode}));
        } finally {
            this.disconnect();
        }
        return vo;
    }

    /**
     * 지정된 노드 인포를 읽어 들이는 함수
     */
    public ArrayList<NodeInfoVO> getRuleNodeList(String strRuleCode) throws DAOException
    {
        ArrayList<NodeInfoVO> al = new ArrayList<NodeInfoVO>();
        ResultSet rs = null;
        try
        {
            this.connect(GET_NODEINFO_LIST);
            this.preparedStatement.setString(1, strRuleCode);
            rs  = executeQuery();
            NodeInfoVO vo = null;
            while(rs.next())
            {
                vo = new NodeInfoVO();

                vo.setRuleCode(rs.getString(1).trim());
                vo.setPhaseCode(rs.getString(2).trim());
                vo.setPhaseType(rs.getString(3).trim());
                vo.setFlowComp(rs.getString(4).trim());
                vo.setTelegramID(rs.getString(5).trim());
                vo.setLengthField(rs.getInt(6));
                vo.setDesc(rs.getString(7).trim());

                al.add(vo);
            }
        } catch(Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e,"BECEAIMFR018", new String[] {strRuleCode}));
        } finally {
            this.disconnect();
        }
        return al;
    }
}
