/*
 * 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 XbTupleDataDAO {

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

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

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

        return instance;
    }

    protected ArrayList<XbTupleDataObj> getTupleData(String groupId, String contextId) throws XbException {
        try {
            // ステートメントを作成
            PreparedStatement stmt;
            StringBuffer  strb_sql = new StringBuffer();

            // メインリスト
            strb_sql.append("select ");
            strb_sql.append("tuple.item_order as item_order ");
            strb_sql.append(", tuple.taxonomy_group_id as taxonomy_group_id ");
            strb_sql.append(", tuple.taxonomy_item_id as taxonomy_item_id ");
            strb_sql.append(", tuple.taxonomy_item_seq as taxonomy_item_seq ");
            strb_sql.append(", tuple.parent_taxonomy_item_id as parent_taxonomy_item_id ");
            strb_sql.append(", tuple.parent_taxonomy_item_seq as parent_taxonomy_item_seq ");
            strb_sql.append(", tuple.item_value as item_value ");
            strb_sql.append(", map.item_precision as item_precision ");
            strb_sql.append(", map.item_decimal as item_decimal ");
            strb_sql.append(", sort.context_type as context_type ");
            strb_sql.append(", foot.parent_etc_id as parent_etc_id ");
            strb_sql.append(", attr.value as value ");
            strb_sql.append("from ");
            strb_sql.append("(((xb_tuple_data tuple ");
            strb_sql.append("left outer join xb_mapping map ");
            strb_sql.append("on tuple.taxonomy_group_id = map.taxonomy_group_id ");
            strb_sql.append("and tuple.taxonomy_item_id = map.taxonomy_item_id) ");
            strb_sql.append("inner join xb_item sort ");
            strb_sql.append("on tuple.item_order = sort.item_order ");
            strb_sql.append("and tuple.taxonomy_group_id = sort.taxonomy_group_id ");
            strb_sql.append("and tuple.taxonomy_item_id = sort.taxonomy_item_id) ");
            strb_sql.append("left outer join xb_item_footnote foot ");
            strb_sql.append("on map.user_item_id = foot.main_id ");
            strb_sql.append("and map.etc_id = foot.etc_id) ");
            strb_sql.append("left outer join xb_etc_attributes attr ");
            strb_sql.append("on foot.etc_id = attr.etc_id ");
            strb_sql.append("and attr.etc_type = 3 ");
            strb_sql.append("and attr.name = 'href' ");
            strb_sql.append("where ");
            strb_sql.append("tuple.taxonomy_group_id = ? ");
            strb_sql.append("and tuple.context_id = ? ");
            strb_sql.append("group by tuple.item_order, tuple.taxonomy_group_id, tuple.taxonomy_item_id, tuple.taxonomy_item_seq, tuple.parent_taxonomy_item_id, tuple.parent_taxonomy_item_seq, tuple.item_value, map.item_precision, map.item_decimal, sort.context_type, foot.parent_etc_id, attr.value");
            // SQL設定
            stmt = conn.prepareStatement(strb_sql.toString());
            int index = 1;

            stmt.setString(index++, groupId);
            stmt.setString(index++, contextId);

            ResultSet rs = stmt.executeQuery();

            ArrayList<XbTupleDataObj> relist = new ArrayList<XbTupleDataObj>();
            while(rs.next()) {
                // サブリスト
                XbTupleDataObj obj = new XbTupleDataObj();
                obj.setItemOrder(rs.getString("item_order"));
                obj.setTaxonomyGroupId(rs.getString("taxonomy_group_id"));
                obj.setTaxonomyItemId(rs.getString("taxonomy_item_id"));
                obj.setTaxonomyItemSeq(rs.getString("taxonomy_item_seq"));
                obj.setParentTaxonomyItemId(rs.getString("parent_taxonomy_item_id"));
                obj.setParentTaxonomyItemSeq(rs.getString("parent_taxonomy_item_seq"));
                obj.setItemValue(rs.getString("item_value"));
                obj.setItemPrecision(rs.getString("item_precision"));
                obj.setItemDecimal(rs.getString("item_decimal"));
                obj.setContextType(rs.getString("context_type"));
                obj.setParentEtcId(rs.getString("parent_etc_id"));
                obj.setValue(rs.getString("value"));
                relist.add(obj);
            }
            // 結果セットをクローズ
            rs.close(); 
            // ステートメントをクローズ
            stmt.close(); 

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