/* $Id: DerbyApplicationDAO.java,v 1.3 2007/11/27 02:06:21 knakata Exp $
 *
 * Copyright (c)ARGO 21, Corporation. 2005, 2006.  All rights reserved.
 * 
 * This file is part of Nautica Workflow Core.
 * 
 *  Nautica Workflow Core is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  Nautica Workflow Core is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Nautica Workflow Core; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  
 */
package jp.co.argo21.nautica.workflow.dataaccess.derby;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import jp.co.argo21.nautica.workflow.dataaccess.AbstractDAO;
import jp.co.argo21.nautica.workflow.dataaccess.DAOException;
import jp.co.argo21.nautica.workflow.dataaccess.ApplicationBean;
import jp.co.argo21.nautica.workflow.dataaccess.ApplicationDAO;
import jp.co.argo21.nautica.workflow.engine.LogManager;
import jp.co.argo21.nautica.workflow.util.StringManager;

/**
 * ApplicationBeanをデータとするDerby用DAOクラスである。
 * <pre><tt>
 * WF_APPLICATIONテーブル
 * +----------+-------------+------+-----+---------+-------+
 * | Field    | Type        | Null | Key | Default | Extra |
 * +----------+-------------+------+-----+---------+-------+
 * | APP_NAME | varchar(64) |      | PRI |         |       |
 * | APP_TYPE | int(3)      |      | MUL | 0       |       |
 * +----------+-------------+------+-----+---------+-------+
 * </tt></pre>
 *
 * @author  knakata(Argo 21, Corp.)
 * @version $Revision: 1.3 $
 * @since   Nautica Workflow 1.0
 */
public class DerbyApplicationDAO extends AbstractDAO implements ApplicationDAO
{
	/** エンジンログ */
	protected static Logger eLog = LogManager.getEngineLogger();

	/** アプリケーション名のカラム名 */
	public static final String APP_NAME = "APP_NAME";
	/** アプリケーションタイプのカラム名 */
	public static final String APP_TYPE = "APP_TYPE";

	private static final String SQL_SELECT_BY_PRIMARY_KEY
	= "select * from WF_APPLICATION where APP_NAME = ? ";

	private static final String SQL_INSERT
	= "insert into WF_APPLICATION ("
		+ APP_NAME + ", "
		+ APP_TYPE + ") values (?,?)";

	/**
	 * プロセス管理テーブルに対するアクセスオブジェクトを生成する。
	 *
	 * @param conn コネクション
	 */
	DerbyApplicationDAO(Connection conn)
	{
		super(conn);
	}
	
	/**
	 * 主キーにより、ApplicationBeanを取得する。
	 *
	 * @param appName アプリケーション名
	 * @return ApplicationBean
	 * @throws SQLException 任意のSQL例外
	 * @throws DAOException DAO実行中に処理としてあげる場合がある例外
	 * @see jp.co.argo21.nautica.workflow.dataaccess.ApplicationDAO#findByPrimaryKey(java.lang.String)
	 */
	public ApplicationBean findByPrimaryKey(String appName)
	throws SQLException, DAOException
	{
		PreparedStatement st = getConnection().prepareStatement(SQL_SELECT_BY_PRIMARY_KEY);
		try {
			st.setString(1, appName);

			/************** DEBUG **************/
			eLog.debug(st.toString());
			/***********************************/
			
			ResultSet rs = st.executeQuery();
			
			ApplicationBean bean = null;
			if (rs.next()) {
				bean = createBean(rs);
			}
			return bean;
		} finally {
			st.close();
		}
	}
	
	/**
	 * ApplicationBeanを挿入する。
	 *
	 * @param bean ApplicationBean
	 * @throws SQLException 任意のSQL例外
	 * @throws DAOException DAO実行中に処理としてあげる場合がある例外
	 * @see jp.co.argo21.nautica.workflow.dataaccess.ApplicationDAO#insert(jp.co.argo21.nautica.workflow.dataaccess.ApplicationBean)
	 */
	public void insert(ApplicationBean bean)
	throws SQLException, DAOException
	{
		PreparedStatement st = getConnection().prepareStatement(SQL_INSERT);
		try {
			st.setString(1, bean.getAppName());
			st.setInt(2, bean.getAppType());

			/************** DEBUG **************/
			eLog.debug(st.toString());
			/***********************************/
			
			int i = st.executeUpdate();
			
			if (i != 1) {
			    // アプリケーションレコードの挿入に失敗しました。
				String errMsg = StringManager.get("E0049");
				throw new DAOException(errMsg);
			}
		} finally {
			st.close();
		}
	}
	
	/**
	 * 結果セットからApplicationBeanを生成する。
	 *
	 * @param rs 結果セット
	 * @return ApplicationBean
	 * @throws SQLException 任意のSQL例外
	 */
	private ApplicationBean createBean(ResultSet rs)
	throws SQLException
	{
		ApplicationBean bean = new ApplicationBean();

		bean.setAppName(rs.getString(APP_NAME));
		bean.setAppType(rs.getInt(APP_TYPE));
		
		return bean;
	}
}
