/*
 * 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 jp.co.going.xbrl.common.XbException;

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

    // singleton作成
    private static XbTaxonomyDAO tax = null;
    // Connection変数
    private static Connection conn = null;

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

    // インスタンス取得
    public static XbTaxonomyDAO getInstance(Connection connection) {
        // インスタンスを生成
        if (tax == null) {
            conn = connection;
            tax = new XbTaxonomyDAO();
        }

        return tax;
    }

    protected void insert(XbTaxonomyObj obj)
        throws XbException {

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

    		StringBuffer  strb_sql = new StringBuffer();
    	    // 問合せの実行
        	strb_sql.append("insert into ");
        	strb_sql.append("xb_taxonomy ");
        	strb_sql.append("values ");
        	strb_sql.append("(?, ?, ?, ?, ?, ?, ?, ?)");

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

    		int index = 1;
    		stmt.setLong(index++, obj.getTaxonomyId());
    		stmt.setLong(index++, obj.getTaxonomyGroupId());
    		stmt.setString(index++, obj.getTaxonomyName());
    		stmt.setString(index++, obj.getTaxonomyFile());
    		stmt.setString(index++, obj.getNamespace());
    		stmt.setString(index++, obj.getNamespaceUrl());
    		stmt.setInt(index++, obj.getTerminatedFlag());
            stmt.setTimestamp(index++, new java.sql.Timestamp(obj.getCreationDate().getTime()));

    	   	// SQL　実行
    	   	stmt.executeUpdate();

    	    // ステートメントをクローズ
    	    stmt.close();
        } catch (SQLException e) {
            throw new XbException(e, "SQLError : " + e.getMessage());
        }
	}
}