/*
 * 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.XbUnitDivideObj;

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

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

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

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

    	return unitDivide;
    }

	public void insert(XbUnitDivideObj obj) throws XbException{

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

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

			int index = 1;
			stmt.setLong(index++, obj.getUnit_id());
			stmt.setLong(index++, obj.getDivide_id());
			stmt.setInt(index++, obj.getOperator_type());
			stmt.setLong(index++, obj.getPresentation_order());
	        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<XbUnitDivideObj> selectAll(long unit_id) throws XbException{

		try {
			StringBuffer  strb_sql = new StringBuffer();
		    // 問合せの実行
			strb_sql.append("select ");
			strb_sql.append("unit_id, ");
			strb_sql.append("divide_id, ");
			strb_sql.append("operator_type, ");
			strb_sql.append("presentation_order, ");
			strb_sql.append("creation_date ");
			strb_sql.append("from xb_unit_divide ");
			strb_sql.append("where UNIT_ID=? ");
			strb_sql.append("order by presentation_order, divide_id ");

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

			int index = 1;
			stmt.setLong(index++, unit_id);

			ResultSet rs = stmt.executeQuery();
			ArrayList<XbUnitDivideObj> relist = new ArrayList<XbUnitDivideObj>();
			while(rs.next()) {
				XbUnitDivideObj obj = new XbUnitDivideObj();
				obj.setUnit_id(rs.getLong("unit_id"));
				obj.setDivide_id(rs.getLong("divide_id"));
				obj.setOperator_type(rs.getInt("operator_type"));
				obj.setPresentation_order(rs.getLong("presentation_order"));
				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 XbUnitDivideObj select(long unit_id, long divide_id) throws XbException{

		try {
			StringBuffer  strb_sql = new StringBuffer();
		    // 問合せの実行
			strb_sql.append("select ");
			strb_sql.append("unit_id, ");
			strb_sql.append("divide_id, ");
			strb_sql.append("operator_type, ");
			strb_sql.append("presentation_order, ");
			strb_sql.append("creation_date ");
			strb_sql.append("from xb_unit_divide ");
			strb_sql.append("where unit_id=? ");
			strb_sql.append("and divide_id=? ");

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

			int index = 1;
			stmt.setLong(index++, unit_id);
			stmt.setLong(index++, divide_id);

			ResultSet rs = stmt.executeQuery();
			XbUnitDivideObj obj = null;
			while(rs.next()) {
				obj = new XbUnitDivideObj();
				obj.setUnit_id(rs.getLong("unit_id"));
				obj.setDivide_id(rs.getLong("divide_id"));
				obj.setOperator_type(rs.getInt("operator_type"));
				obj.setPresentation_order(rs.getLong("presentation_order"));
				obj.setCreation_date(rs.getDate("creation_date"));
			}

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

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

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

	public long getNextId() throws XbException{

		try {
			StringBuffer  strb_sql = new StringBuffer();
		    // 問合せの実行
			strb_sql.append("select ");
			strb_sql.append("max(divide_id)+1 AS max ");
			strb_sql.append("from xb_unit_divide ");

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

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

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