/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：JCCOracleSeqUtil
*   ソースファイル名：JCCOracleSeqUtil.java
*   作成者          ：富士通
*   日付            ：2011年04月01日
*＜機能概要＞
*   Oracleシーケンス定義から採番値を取得する部品です。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2011/04/01   FJ          新規作成
*
**********************************************************************/
package eo.common.util;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;

/**
 * Oracleシーケンス定義から採番値を取得する部品です。<p>
 * Oracleで該当するシーケンス定義が作成されていること。<p>
 * <BR>
 * @ author 富士通
 */
public class JCCOracleSeqUtil {

	/**
	 * Oracleシーケンスの採番値を取得します。
	 * @param con DBコネクション
	 * @param seqName シーケンス定義名
	 * @throws Exception 採番値の取得に失敗した場合
	 */
	public static String getNextSeq(Connection con, String seqName) throws Exception {
		
		// コネクションチェック
		if (con == null) {
			throw new Exception("Connectionがnullです。");
		}
		
		String nextVal = null;
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ");
		sql.append(seqName);
		sql.append(".NEXTVAL ");
		sql.append("AS SEQ_NEXTVAL ");
		sql.append("FROM DUAL");
		
		Statement stmt = null;
		ResultSet rs = null;
		// コネクションからステートメントを取得する
		try {
			stmt = con.createStatement();
			
			// SQLを発行
			rs = stmt.executeQuery(sql.toString());
			
			if (rs.next()) {
				// 値の取得
				nextVal = rs.getString("SEQ_NEXTVAL");
			} else {
				throw new Exception(seqName+":採番値取得処理で例外が発生しました。");
			}
			
		} catch (SQLException e) {
			throw new Exception(seqName+":採番値取得処理で例外が発生しました。",e);
		} finally {
			
			if (stmt != null) {
				stmt.close();
			}
			if (rs != null) {
				rs.close();
			}
			
		}
		
		return nextVal;
		
	}
	
	/**
	 * Oracleシーケンスの採番値を取得し、指定された接頭辞の付与と接頭辞と採番値の間のゼロ埋めを行います。<br>
	 * 特記事項：<br>
	 * 桁の指定が、実際のOracleシーケンス値より大きい場合、Oracleシーケンス値が優先されます。<br>
	 * 例：桁が2でOracleシーケンスが100であった場合、接頭辞+100を返します。
	 * @param con DBコネクション
	 * @param seqName シーケンス定義名
	 * @param prefix 接頭辞
	 * @param keta シーケンス定義の桁（接頭辞を含まない）
	 * @return String 接頭辞+ゼロ埋めしたOracleシーケンスの採番値（例：D00000000001等）
	 * @throws Exception 採番値の取得に失敗した場合
	 */
	public static String getFormatedNextSeq(Connection con, String seqName, String prefix, int keta) throws Exception {
		
		// コネクションチェック
		if (con == null) {
			throw new Exception("Connectionがnullです。");
		}
		
		// 接頭辞チェック
		if (prefix == null) {
			throw new Exception("接頭辞がnullです。");
		}
		
		// 接頭辞
		//StringBuffer sb = new StringBuffer(prefix);
		StringBuffer sb = new StringBuffer();
		// ゼロ埋め
		for (int i = 0; i < keta; i++) {
			sb.append("0");
		}
		
		// フォーマット調整用インスタンス		
		DecimalFormat df = new DecimalFormat(sb.toString());
		
		String nextVal = null;
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ");
		sql.append(seqName);
		sql.append(".NEXTVAL ");
		sql.append("AS SEQ_NEXTVAL ");
		sql.append("FROM DUAL");
		
		Statement stmt = null;
		ResultSet rs = null;
		// コネクションからステートメントを取得する
		try {
			stmt = con.createStatement();
			
			// SQLを発行
			rs = stmt.executeQuery(sql.toString());
			
			if (rs.next()) {
				// 値の取得
				nextVal = rs.getString("SEQ_NEXTVAL");
			} else {
				throw new Exception(seqName+":採番値取得処理で例外が発生しました。");
			}
			nextVal = df.format(new BigDecimal(nextVal));
			nextVal = prefix + nextVal;
			
		} catch (SQLException e) {
			throw new Exception(seqName+":採番値取得処理で例外が発生しました。",e);
		} finally {
			
			if (stmt != null) {
				stmt.close();
			}
			if (rs != null) {
				rs.close();
			}
			
		}
		
		return nextVal;
		
	}
	
}
