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

import java.sql.ResultSet;
import java.util.ArrayList;
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;

public class PhaseInfoDAO extends BaseDAO implements PhaseInfoQuery
{
    /**
     * 각 흐름규칙의 첫번째 노드를 읽어들인다.
     */

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

            while(rs.next())
            {
                vo = new PhaseInfoVO();
                vo.setRuleCode(StringUtil.nvl(rs.getString(1)).trim());
                vo.setPhaseSeq(StringUtil.nvl(rs.getString(2)).trim());
                vo.setPhaseCode(StringUtil.nvl(rs.getString(3)).trim());
                vo.setPhaseType(StringUtil.nvl(rs.getString(4)).trim());
                vo.setDesc(StringUtil.nvl(rs.getString(5)).trim());
                vo.setTelegramType(StringUtil.nvl(rs.getString(6)).trim());
                vo.setOpCode(StringUtil.nvl(rs.getString(7)).trim());
                vo.setTrClass(StringUtil.nvl(rs.getString(8)).trim());
                vo.setTOVal(rs.getInt(9));
                vo.setRepeat(rs.getInt(10));
                vo.setConditionTxt(StringUtil.nvl(rs.getString(11)).trim());
                vo.setPriority(rs.getInt(12));
                vo.setCTelegramType(StringUtil.nvl(rs.getString(13)).trim());
                vo.setCOpCode(StringUtil.nvl(rs.getString(14)).trim());
                vo.setCTrClass(StringUtil.nvl(rs.getString(15)).trim());
                vo.setCResCode(StringUtil.nvl(rs.getString(16)).trim());
                vo.setTelegramID(StringUtil.nvl(rs.getString(17)).trim());
                vo.setNextPhaseCode(StringUtil.nvl(rs.getString(18)).trim());


                ArrayList<PhaseInfoVO> al = all.get(vo.getRuleCode());
                if (al == null) {
                    al = new ArrayList<PhaseInfoVO>();
                    al.add(vo);
                    all.put(vo.getRuleCode(), al);
                } else {
                    all.remove(vo.getRuleCode());
                    al.add(vo);
                    all.put(vo.getRuleCode(), al);
                }
            }
        } catch(Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e,"BECEAIMFR024"));
        } finally {
            this.disconnect();
        }
        return all;
    }
}
