package jp.terasoluna.batch.functionsample.b001;

import jp.terasoluna.fw.batch.blogic.AbstractTransactionBLogic;
import jp.terasoluna.fw.batch.blogic.vo.BLogicParam;
import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

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

/**
 * DB関連ジョブのサンプル<br>
 * <br>
 * 
 * 事前準備：EMPLOYEEテーブルを作成しておくこと<br>
 * <br>
 * サンプル内容：参照・更新処理を行う。トランザクション管理はフレームワークに任せるサンプル。
 * 
 * 
 */
public class B001001BLogic extends AbstractTransactionBLogic {

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

	private static final int BATCH_NORMAL_END = 0;

	@Autowired
	@Qualifier("queryDAO")
	private QueryDAO queryDAO = null;

	@Autowired
	@Qualifier("updateDAO")
	private UpdateDAO updateDAO = null;

	@Override
	public int doMain(BLogicParam arg0) {

		int count = queryDAO.executeForObject("B001001.selectEmployeeCount",
				null, Integer.class);

		B001001Param newUser = new B001001Param();

		newUser.setId(count + 1);
		newUser.setFamilyName("山田");
		newUser.setFirstName("次郎");
		newUser.setAge(45);

		updateDAO.execute("B001001.insertEmployee", newUser);
		log.info("以下のユーザをEMPLOYEEテーブルに追加しました");
		log.info("id : " + newUser.getId() + " familyName : "
				+ newUser.getFamilyName() + " firstName : "
				+ newUser.getFirstName() + " age : " + newUser.getAge());

		return BATCH_NORMAL_END;
	}

}
