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

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 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(rs.getString(1).trim());
                vo.setPhaseSeq(rs.getString(2).trim());
                vo.setPhaseCode(rs.getString(3).trim());
                vo.setPhaseType(rs.getString(4).trim());
                vo.setDesc(rs.getString(5).trim());
                vo.setTelegramType(rs.getString(6).trim());
                vo.setOpCode(rs.getString(7).trim());
                vo.setTrClass(rs.getString(8).trim());
                vo.setTOVal(rs.getInt(9));
                vo.setRepeat(rs.getInt(10));
                vo.setConditionTxt(rs.getString(11).trim());
                vo.setPriority(rs.getInt(12));
                vo.setCTelegramType(rs.getString(13).trim());
                vo.setCOpCode(rs.getString(14).trim());
                vo.setCTrClass(rs.getString(15).trim());
                vo.setCResCode(rs.getString(16).trim());
                vo.setTelegramID(rs.getString(17).trim());
                vo.setNextPhaseCode(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;
    }
}
