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

package jp.co.going.xbrl.sample.dao;

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;
import jp.co.going.xbrl.sample.dao.obj.XbEtcObj;

/**
 * @author Masako Okayasu
 */
public class XbEtcDao {

	// singleton作成
    private static XbEtcDao etc = null;
    // Connection変数
    private static Connection conn = null;
	// ステートメントを作成
    private PreparedStatement stmt;

    // コンストラクタ
    public XbEtcDao(){
    }

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

        return etc;
    }

	public void insert(XbEtcObj obj) throws XbException{

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

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

			int index = 1;
			stmt.setInt(index++, obj.getEtc_type());
			stmt.setLong(index++, obj.getEtc_id());
			stmt.setString(index++, obj.getEtc_name());
			stmt.setString(index++, obj.getName());
			stmt.setString(index++, obj.getValue());
	        stmt.setTimestamp(index++, new java.sql.Timestamp(obj.getCreation_date().getTime()));

		   	// SQL　実行
		   	stmt.executeUpdate();

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

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

	public ArrayList<XbEtcObj> selectAll(int etc_type) throws XbException{

		try {
			StringBuffer  strb_sql = new StringBuffer();
		    // 問合せの実行
			strb_sql.append("select ");
			strb_sql.append("etc_type, ");
			strb_sql.append("etc_id, ");
			strb_sql.append("etc_name, ");
			strb_sql.append("name, ");
			strb_sql.append("value, ");
			strb_sql.append("creation_date ");
			strb_sql.append("from xb_etc ");
			strb_sql.append("where etc_type=? ");
			strb_sql.append("order by etc_id ");

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

			int index = 1;
			stmt.setInt(index++, etc_type);

			ResultSet rs = stmt.executeQuery();
			ArrayList<XbEtcObj> relist = new ArrayList<XbEtcObj>();
			while(rs.next()) {
				XbEtcObj obj = new XbEtcObj();
				obj.setEtc_type(rs.getInt("etc_type"));
				obj.setEtc_id(rs.getLong("etc_id"));
				obj.setEtc_name(rs.getString("etc_name"));
				obj.setName(rs.getString("name"));
				obj.setValue(rs.getString("value"));
				obj.setCreation_date(rs.getDate("creation_date"));
				relist.add(obj);
			}

		    // 結果セットをクローズ
		    rs.close();

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

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

	public XbEtcObj select(int etc_type, long etc_id) throws XbException{

		try {
			StringBuffer  strb_sql = new StringBuffer();
		    // 問合せの実行
			strb_sql.append("select ");
			strb_sql.append("etc_type, ");
			strb_sql.append("etc_id, ");
			strb_sql.append("etc_name, ");
			strb_sql.append("name, ");
			strb_sql.append("value, ");
			strb_sql.append("creation_date ");
			strb_sql.append("from xb_etc ");
			strb_sql.append("where etc_type=? and etc_id=? ");

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

			int index = 1;
			stmt.setInt(index++, etc_type);
			stmt.setLong(index++, etc_id);

			ResultSet rs = stmt.executeQuery();
			XbEtcObj obj = null;
			while(rs.next()) {
				obj = new XbEtcObj();
				obj.setEtc_type(rs.getInt("etc_type"));
				obj.setEtc_id(rs.getLong("etc_id"));
				obj.setEtc_name(rs.getString("etc_name"));
				obj.setName(rs.getString("name"));
				obj.setValue(rs.getString("value"));
				obj.setCreation_date(rs.getDate("creation_date"));
			}

		    // 結果セットをクローズ
		    rs.close();

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

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

	public long getNextId(int etc_type) throws XbException{

		try {
			StringBuffer  strb_sql = new StringBuffer();
		    // 問合せの実行
			strb_sql.append("select ");
			strb_sql.append("max(etc_id)+1 as max ");
			strb_sql.append("from xb_etc ");
			strb_sql.append("where etc_type=? ");

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

			int index = 1;
			stmt.setInt(index++, etc_type);

			ResultSet rs = stmt.executeQuery();
			rs.next();

			return (rs.getLong("max")==0 ? 1 : rs.getLong("max"));
		} catch (SQLException e) {
            throw new XbException(e, "SQL設定エラー: SQLError : " + e.getMessage());
        }
	}
}