/*
 * MosP - Mind Open Source Project    http://www.mosp.jp/
 * Copyright (C) MIND Co., Ltd.       http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package jp.mosp.common.dao;

import jp.getset.gae.driver.java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import jp.mosp.common.common.BaseDao;
import jp.mosp.common.dto.CmAspUserDto;
import jp.mosp.common.dto.CmUserDto;
import jp.mosp.common.dto.CtAccountDto;

/**
 * アカウントテーブルを操作をする機能を提供する。<br><br>
 * {@link CtAccountDto}を用いて、DBとJavaプログラム間でレコードを共有する。<br>
 */
public class CtAccountDao extends BaseDao {
	
	// テーブル名及びキー列名
	/**
	 *  アカウントテーブル名。<br>
	 */
	public static final String TABLE  = "CT_ACCOUNT";
	
	/**
	 *  アカウントテーブル、主キー。<br>
	 */
	public static final String KEY_01 = "USER_ID";
	
	// 列名
	/**
	 *  アカウントテーブル、列名(ユーザーID)。<br>
	 */
	public static final String COL_USER_ID = "USER_ID";

	/**
	 *  アカウントテーブル、列名(試行回数)。<br>
	 */
	public static final String COL_ATTEMPT = "ATTEMPT";
	
	/**
	 * コンストラクタ。<br>
	 */
	public CtAccountDao() {
		super();
		INSERT_DATE = "";
		INSERT_USER = "";
		UPDATE_DATE = "UPDATE_DATE";
		UPDATE_USER = "";
	}
	
	/**
	 * コンストラクタ。<br>
	 * @param cfg     設定ファイル
	 * @param cmd     コマンド
	 * @param aspUser ASPユーザー
	 * @param user    ユーザー
	 * @param conn    DBコネクション
	 */
	public CtAccountDao(
			Properties   cfg,
			String       cmd,
			CmAspUserDto aspUser,
			CmUserDto    user,
			Connection   conn
	) {
		this();
		initDao(cfg, cmd, aspUser, user, conn);
	}
	
	/**
	 * 検索結果取得(DTOのList)。<br>
	 * @return 検索リスト
	 * @throws SQLException
	 */
	private List<CtAccountDto> mappingAll() throws SQLException {
		List<CtAccountDto> all = new ArrayList<CtAccountDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	
	/**
	 * 検索結果取得(DTO)。<br>
	 * @return
	 * @throws SQLException
	 */
	private CtAccountDto mapping() throws SQLException {
		CtAccountDto dto = new CtAccountDto();
		dto.setUserId(rs.getString(COL_USER_ID));
		dto.setAttempt(rs.getInt(COL_ATTEMPT));
		mappingCommonInfo(dto);
		return dto;
	}
	
	
	/**
	 * 全データ取得。<br>
	 * @return 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CtAccountDto> findAllCtAccount() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()));
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	
	/**
	 * キーによるデータ取得。<br>
	 * @param userId ユーザーID(検索キー)
	 * @return 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CtAccountDto findCtAccountForUserId(String userId) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, userId);
			executeQuery();
			CtAccountDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	
	/**
	 * データ挿入。<br>
	 * @param dto 挿入対象DTO
	 * @return 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public int insert(CtAccountDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getInsertQuery(getClass()));
			setParam(index++, dto.getUserId());
			setParam(index++, dto.getAttempt());
			setCommonParams(true);
			executeUpdate(false);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	
	/**
	 * データ更新。<br>
	 * @param dto 更新対象DTO
	 * @return 更新結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public int update(CtAccountDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParam(index++, dto.getUserId());
			setParam(index++, dto.getAttempt());
			setCommonParams(false);
			setParam(index++, dto.getUserId());
			executeUpdate(false);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
}
