/*********************************************************************
*	All Right reserved,Copyright (c) Fujitsu, 2010
**********************************************************************
*＜プログラム内容＞
*	システム名		：Futurity
*	モジュール名	：JBSbatBusinessService
*	ソースファイル名：JBSbatBusinessService.java
*	作成者			：富士通
*	日付			：2011年02月28日
*＜機能概要＞
*	業務サービススーパークラス
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2011/02/28	富士通		新規作成
*
**********************************************************************/

package eo.business.common;


import eo.framework.application.JBSbatBusinessBase;
import eo.framework.db.JBSbatSQLAccess;
import eo.framework.item.JBSbatCommonDBInterface;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.log.JBSbatLogPrintControl;


public abstract class JBSbatBusinessService {
	
	/**
	 * バッチ更新者ＩＤ
	 */
	protected String batchUserId = null;

	/**
	 * バッチ運用日
	 */
	protected String opeDate = null;
	
	/**
	 * オンライン運用日
	 */
	protected String onlineOpeDate;

//	/**
//	 * システム日付(8桁)
//	 */
//	protected String sysDate;
//
//	/**
//	 * システム日付(17桁)
//	 */
//	protected String sysDateTime;

	/**
	 * システムコード
	 */
	protected String systemCode = null;
	
	/**
	 *  ログ出力制御オブジェクト
	 */
	protected JBSbatLogPrintControl logPrint = null;
	
	/**
	 * フリー項目
	 */
	protected String freeItem = null;

	/**
	 * ジョブＩＤ
	 */
	protected String jobid = null;

	/**
	 * 共通電文
	 */
	protected JBSbatCommonItem commonItem = null;
	
	/**
	 * 業務サービス初期処理。
	 * @param commonItem 共通パラメータ
	 * @throws Exception
	 */
	public abstract void initial(JBSbatCommonItem commonItem) throws Exception;
	

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public abstract void terminal() throws Exception;

	/**
	 * 共通パラメータ情報を設定する
	 * @param item 共通パラメータオブジェクト
	 */
	protected void setCommonInfo(JBSbatCommonItem item) {
		// 共通パラメータを設定する
		this.opeDate = item.getOpeDate();        // 運用日
		this.onlineOpeDate = item.getOnlineOpeDate(); // オンライン運用日
		this.batchUserId = item.getBatchUserId(); //バッチ更新者ＩＤ
//		this.sysDate = item.getSysDate();        // システム日付(8桁)
//		this.sysDateTime = item.getSysDateTime();// システム日付(17桁)
		this.systemCode = item.getSystemCode();  // システムコード
		this.logPrint = item.getLogPrint();      // ログ出力制御オブジェクト
		this.jobid = item.getJobid();            //ジョブＩＤ
		this.freeItem = item.getFreeItem();      // フリー項目
		this.commonItem = item;                  // 共通電文
		JBSbatInterface.commonItem = item;       //共通部品クラスに共通電文を設定

		// ログ出力制御オブジェクトにプログラムＩＤを設定する
		this.setPgidToLogPrintObj();
	}
	
	/**
	 * ログ出力制御オブジェクトにプログラムＩＤを設定する。
	 *
	 */
	protected void setPgidToLogPrintObj() {
		// 実行クラス名を取得する
		String className = this.getClassName();
		// ログ出力オブジェクトにプログラムＩＤ（クラス名）を設定する
		this.logPrint.setProgramId(className);		
	}
	
	/**
	 * クラス名（パッケージ修飾なし）を取得する。
	 * @return String クラス名
	 */
	protected String getClassName() {
		String className = this.getClass().getName();
		int index = className.lastIndexOf(".");
		if (index <= -1) {
			index = 0;
		} else {
			index++;
		}
		return className.substring(index);
	}
	
	/**
	 * データベースのコミットを行う。
	 * 業務サービスで独自に分割コミットを行う場合に使用する。
	 */
	protected void commit() throws Exception{
		//リカバリポイントの更新
		if("1".equals(commonItem.getRecovery())){
			setRecoveryPoint(this.jobid,commonItem.getInputCount(),commonItem.getInputCount2());
		}

		//コミット
		commonItem.getConnection().commit();
//		commonItem.getConnection().close();
		logPrint.printLogMsg(JBSbatBusinessBase.DIVISION_COMMIT);
		// データベースへの再接続
//		JBSbatBusinessBase.dbcon = new JBSbatDBConnection();
//		commonItem.setConnection(JBSbatBusinessBase.dbcon.conn);
	}

	/**
	 * リカバリポイントを設定します。
	 * @param String ジョブＩＤ
	 * @param int リカバリポイント１
	 * @param int リカバリポイント２ 
	 */
	protected void setRecoveryPoint(String jobId, int point1, int point2) throws Exception{
		// DBアクセスを生成する
		JBSbatSQLAccess	dbAccess = new JBSbatSQLAccess(commonItem,"CC_T_RECOVERY_POINT");
		
		// システム日付を取得
		String sysDate = JCCBatCommon.getSysDateTimeStamp();
		
		// 条件を設定する
		JBSbatCommonDBInterface paramList = new JBSbatCommonDBInterface();
		paramList.setValue(point1); 
		paramList.setValue(point2); 
		// 2012/03/08 更新日時とアカウントを登録 start
		paramList.setValue(sysDate); 
		paramList.setValue(commonItem.getBatchUserId()); 
		// 2012/03/08 更新日時とアカウントを登録 end
		paramList.setValue(jobId);
		
		//SQL実行
		int count = dbAccess.executeBySqlDefine(paramList, "UPDATE");
		//0件更新の場合は登録する。（初回のみ）
		if(count==0){
			/** TODO 設定項目精査**/
			paramList = new JBSbatCommonDBInterface();
			paramList.setValue(jobId); 
			paramList.setValue(point1); 
			paramList.setValue(point2); 
			//paramList.setValue(commonItem.getOpeDate());
			paramList.setValue(sysDate);
			paramList.setValue(commonItem.getBatchUserId()); 
			//paramList.setValue(commonItem.getOpeDate()); 
			paramList.setValue(sysDate); 
			paramList.setValue(commonItem.getBatchUserId()); 
			paramList.setValue(""); 
			paramList.setValue(""); 
			//paramList.setValue("1");
			paramList.setValue("0");
			dbAccess.executeBySqlDefine(paramList,"INSERT");
		}
		dbAccess.close();
	}
	
}
