/*
 * Copyright (c) 2007, Going Dot Com Inc. All rights reserved.
 */

package jp.co.going.xbrl.instance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import jp.co.going.xbrl.common.XbException;

/**
 * @author Going Dot Com Inc.
 */
class XbScenarioDAO extends XbEtcDAO {

    // singleton作成
    private static XbScenarioDAO instance = null;
    private static Connection conn = null;

    // コンストラクタ
    private XbScenarioDAO() {
    }

    // インスタンス取得
    protected static synchronized XbScenarioDAO getInstance(Connection connection) {
        // インスタンスを生成
        if (instance == null) {
            conn = connection;
            instance = new XbScenarioDAO();
        }

        return instance;
    }

    protected ArrayList<XbEtcObj> getEtc(String mainId, String pEtcId) throws XbException {
        try {
            // ステートメントを作成
            PreparedStatement stmt;
            StringBuffer  strb_sql = new StringBuffer();

            // メインリスト
            strb_sql.append("select ");
            strb_sql.append("sce.etc_type as etc_type ");
            strb_sql.append(", sce.etc_id as etc_id ");
            strb_sql.append(", sce.parent_etc_id as parent_etc_id ");
            strb_sql.append(", sce.prefix as prefix ");
            strb_sql.append(", sce.uri as uri ");
            strb_sql.append(", etc.name as name ");
            strb_sql.append(", etc.value as value ");
            strb_sql.append("from ");
            strb_sql.append("xb_context_scenario sce ");
            strb_sql.append(", xb_etc etc ");
            strb_sql.append("where ");
            strb_sql.append("sce.main_id = ? ");
            strb_sql.append("and sce.etc_type = etc.etc_type ");
            strb_sql.append("and sce.etc_id = etc.etc_id ");
            strb_sql.append("and sce.parent_etc_id = ?");
            // SQL設定
            stmt = conn.prepareStatement(strb_sql.toString());
            int index = 1;

            stmt.setString(index++, mainId);
            stmt.setString(index++, pEtcId);

            ResultSet rs = stmt.executeQuery();

            ArrayList<XbEtcObj> relist = new ArrayList<XbEtcObj>();
            while(rs.next()) {
                // サブリスト
                XbEtcObj obj = new XbEtcObj();
                obj.setEtcId(rs.getString("etc_id"));
                obj.setParentEtcId(rs.getString("parent_etc_id"));
                obj.setPrefix(rs.getString("prefix"));
                obj.setUri(rs.getString("uri"));
                obj.setName(rs.getString("name"));
                obj.setValue(rs.getString("value"));
                obj.setAttrList(super.getEtcAttributes(conn, rs.getString("etc_type"), rs.getString("etc_id")));
                relist.add(obj);
            }
            // 結果セットをクローズ
            rs.close(); 
            // ステートメントをクローズ
            stmt.close(); 

            return relist;
        } catch (SQLException e) {
            throw new XbException(e, "SQLError : " + e.getMessage());
        }
    }
}