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

package jp.co.going.xbrl.taxonomy;

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

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

/**
 * @author Going Dot Com Inc.
 */
public abstract class XbItemLabelDAO {

    // Connection変数
    protected static Connection conn = null;

    private void insert(ArrayList<XbItemLabelObj> obj)  throws XbException {
        try {
            for (int i = 0;i < obj.size();i ++) {
                XbItemLabelObj label = obj.get(i);

                // ステートメントを作成
                PreparedStatement stmt;

                StringBuffer  strb_sql = new StringBuffer();
                // 問合せの実行
                strb_sql.append("INSERT INTO ");
                strb_sql.append("xb_item_label (");
                strb_sql.append("taxonomy_group_id, ");
                strb_sql.append("taxonomy_id, ");
                strb_sql.append("taxonomy_item_id, ");
                strb_sql.append("label_lang, ");
                strb_sql.append("resource_value) ");
                strb_sql.append("VALUES ");
                strb_sql.append("(?, ?, ?, ?, ?)");

                // SQL設定
                stmt = conn.prepareStatement(strb_sql.toString());

                int index = 1;
                stmt.setString(index++, label.getTaxGrpId());
                stmt.setString(index++, label.getTaxId());
                stmt.setString(index++, label.getItemId());
                stmt.setString(index++, label.getLang());
                stmt.setString(index++, label.getValue());

                // SQL　実行
                stmt.executeUpdate();

                // ステートメントをクローズ
                stmt.close();
            }

        } catch (SQLException e) {
            throw new XbException(e, "SQL設定エラー: SQLError : " + e.getMessage());
        }
    }

    protected final void setItemLabel(long grpId) throws XbException {
        // 表示名取得
        ArrayList<XbItemLabelObj> labelList = getItemLabel(grpId);
        // 表示名登録
        this.insert(labelList);
    }

    abstract protected ArrayList<XbItemLabelObj> getItemLabel(long grpId) throws XbException;
}