package jp.terasoluna.batch.functionsample.b003;

import jp.terasoluna.fw.batch.blogic.AbstractTransactionBLogic;
import jp.terasoluna.fw.batch.blogic.vo.BLogicParam;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 非同期関連サンプル<br>
 * 
 * 事前準備：JOB_CONTROLテーブルとJOB_CONTROL_SEQ_001テーブルを作成しておくこと。 <br>
 * <br>
 * サンプル内容：ジョブ管理テーブルへジョブを投入するサンプル。
 * 
 */
public class B003002BLogic extends AbstractTransactionBLogic {

	private Log log = LogFactory.getLog(B003002BLogic.class);

	private static final int BATCH_NORMAL_END = 0;

	private static final int BATCH_ABNORMAL_END = 255;

	@Autowired
	BatchEntryService batchEntrySvc = null;

	@Override
	public int doMain(BLogicParam param) {

		if (param == null) {
			log.error("param not found.");
			return BATCH_ABNORMAL_END;
		}

		log.info(param.toString());
		
		// パラメータ作成
		BatchEntryParam batchEntryParam = new BatchEntryParam();

		batchEntryParam.setJobAppCd(param.getJobArgNm1());
		
		// ジョブ管理テーブルを登録する
		BatchEntryResult result = batchEntrySvc.writeJobTable(batchEntryParam);
		if (result == null	|| (result != null 
				&& result.getEndCode() != BatchEntryService.BATCH_ENTRY_END_CODE_NORMAL)) {
			log.error("writeJobTable error.");
			return BATCH_ABNORMAL_END;
		}

		return BATCH_NORMAL_END;
	}

}
