/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2009 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.payroll.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jp.mosp.common.common.MospException;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.dto.CmBranchDto;

public class CmBranchDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String TABLE = "CM_BRANCH";   // 支店マスタ
	public static final String KEY_1 = "BANK_CODE";   // 銀行コード
	public static final String KEY_2 = "BRANCH_CODE"; // 支店コード
	// 列名
	public static final String COL_BANK_CODE   = "BANK_CODE"   ;	// 銀行コード
	public static final String COL_BRANCH_CODE = "BRANCH_CODE" ;	// 支店コード
	public static final String COL_BRANCH_NAME = "BRANCH_NAME" ;	// 支店名
	public static final String COL_BRANCH_KANA = "BRANCH_KANA" ;	// 支店名フリガナ
	
	/**
	 * コンストラクタ
	 */
	public CmBranchDao(){
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<CmBranchDto> mappingAll() throws SQLException {
		List<CmBranchDto> all = new ArrayList<CmBranchDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}

	/**
	 * 検索結果取得(DTO)
	 * @return CmBranchDto CM_BRANCHレコード
	 * @throws SQLException
	 */
	private CmBranchDto mapping() throws SQLException {
		CmBranchDto dto = new CmBranchDto();
		dto.setBankCode    (rs.getString(COL_BANK_CODE    ));	// 銀行コード
		dto.setBranchCode  (rs.getString(COL_BRANCH_CODE  ));	// 支店コード
		dto.setBranchName  (rs.getString(COL_BRANCH_NAME  ));	// 支店名
		dto.setBranchKana  (rs.getString(COL_BRANCH_KANA  ));	// 支店名フリガナ
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * キーによるデータの取得。<br>
	 * @param bankCode 銀行コード
	 * @param branchCode 支店コード
	 * @return 検索条件に一致する支店マスタのレコード(一致するレコードが無い場合はNULL)
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	public CmBranchDto findForKey(
			String bankCode,
			String branchCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, bankCode);
			setParam(index++, branchCode);
			executeQuery();
			CmBranchDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param bankCode 銀行コード(検索キー)
	 * @param branchCode 支店コード(検索キー)
	 * @return 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmBranchDto findForUpdate(
			String bankCode,
			String branchCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, bankCode);
			setParam(index++, branchCode);
			executeQuery();
			CmBranchDto CmBranchDto = null;
			if (rs.next()) {
				CmBranchDto = mapping();
			}
			return CmBranchDto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CmBranchDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<CmBranchDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(CmBranchDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getInsertQuery(getClass()));
			setParams(dto, true);
			executeUpdate();
			chkInsert(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ更新
	 * @param dto 更新対象DTO
	 * @return 更新結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int update(CmBranchDto dto,  boolean needLog) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getBankCode());
			setParam(index++, dto.getBranchCode());
			executeUpdate(needLog);
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 銀行コードによる配列データ取得
	 * @return CmBranchDto 検索結果(DTO)
	 * @throws SQLException
	 */
	public List<CmBranchDto> findForEmployeeList() throws SQLException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("SELECT " + " * " + " FROM " + "CM_BANK " + "left join ");
			sb.append(TABLE +" ");
			sb.append("on");
			sb.append(" CM_BANK."+ COL_BANK_CODE + " = ");
			sb.append(TABLE +"." + COL_BANK_CODE);
			prepareStatement(sb.toString());
			executeQuery();
			List<CmBranchDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param bankCode 　　 　	検索銀行コード
	 * @param bankKana			検索銀行カナ
	 * @param branchCode 　　  検索支店コード
	 * @param branchKana 　　  検索支店カナ
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CmBranchDto> findForCondition(
			String bankCode,
			String bankKana,
			String branchCode,
			String branchKana
	) throws SQLException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append("* ");
		sb.append("FROM ");
		sb.append("CM_BANK ");
		sb.append("left join ");
		sb.append(TABLE);
		sb.append(" on ");
		sb.append("CM_BANK."+ COL_BANK_CODE + " = ");
		sb.append(TABLE +".");
		sb.append(COL_BANK_CODE);
		if (
				bankCode         !=  null && !bankCode.equals("")
				|| bankKana      !=  null && !bankKana.equals("")
				|| branchCode    !=  null && !branchCode.equals("")
				|| branchKana    !=  null && !branchKana.equals("")
			) {
		sb.append(" WHERE ");
		}
		if (bankCode             !=  null && !bankCode.equals("")) {
			sb.append("CM_BANK." + COL_BANK_CODE  + " like ? ");
			if (
				bankKana         !=  null && !bankKana.equals("")
				|| branchCode    !=  null && !branchCode.equals("")
				|| branchKana    !=  null && !branchKana.equals("")
			) {
				sb.append(" AND ");
			}
		}
		if (    bankKana        !=  null && !bankKana.equals("")) {
			sb.append("CM_BANK.BANK_KANA" + " like ? ");
			if (
				branchCode       !=  null && !branchCode.equals("")
				|| branchKana    !=  null && !branchKana.equals("")
			) {
				sb.append(" AND ");
			}
		}
		if (    branchCode        !=  null && !branchCode.equals("")) {
			sb.append(TABLE +"." + COL_BRANCH_CODE + " like ? ");
			if (
				branchKana    !=  null && !branchKana.equals("")
			) {
				sb.append(" AND ");
			}
		}
		if (    branchKana        !=  null && !branchKana.equals("")) {
			sb.append(TABLE +"." + COL_BRANCH_KANA + " like ? ");
		}
		try {
			prepareStatement(sb.toString());
			if (bankCode   !=  null && !bankCode.equals("")) {
			setParam(index++, bankCode + "%" );
			}
			if (bankKana   !=  null && !bankKana.equals("")) {
			setParam(index++, "%" + bankKana   + "%" );
			}
			if (branchCode !=  null && !branchCode.equals("")) {
			setParam(index++, "%" + branchCode + "%" );
			}
			if (branchKana !=  null && !branchKana.equals("")) {
			setParam(index++, "%" + branchKana + "%" );
			}
			executeQuery();
			List<CmBranchDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(CmBranchDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getBankCode    ());	// 銀行コード
		setParam(index++, dto.getBranchCode  ());   // 支店コード
		setParam(index++, dto.getBranchName  ());   // 支店名
		setParam(index++, dto.getBranchKana  ());   // 支店フリガナ
		setCommonParams(isInsert);
	}
	
}
