/*
 * 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.payroll.bonus.dao;

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

import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.bonus.dto.PmPositionDto;

public class PmPositionDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String	TABLE				= "UM_POSITION";
	public static final String	KEY_1				= "POSITION_CODE";		// 職位コード
																			
	// 列名
	public static final String	COL_POSITION_CODE	= "POSITION_CODE";		// 職位コード
	public static final String	COL_POSITION_NAME	= "POSITION_NAME";		// 職位名
	public static final String	COL_GRADE			= "GRADE";				// 号数
	public static final String	COL_NOTCH			= "NOTCH";				// 等級
	public static final String	COL_BASIC_SALARY	= "BASIC_SALARY";		// 基本給
	public static final String	COL_EMPLOYMENT_TYPE	= "EMPLOYMENT_TYPE";	// 雇用区分
	public static final String	COL_DEFAULT_AUTH	= "DEFAULT_AUTH";		// デフォルトプログラム利用権限
	public static final String	COL_DEFAULT_LEVEL	= "DEFAULT_LEVEL";		// デフォルトデータ参照レベル
	public static final String	COL_DELETE_FLAG		= "DELETE_FLAG";		// 削除フラグ
																			
	
	/**
	 * コンストラクタ
	 */
	public PmPositionDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<PmPositionDto> mappingAll() throws SQLException {
		List<PmPositionDto> all = new ArrayList<PmPositionDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return PmPositionDto UM_POSITIONレコード
	 * @throws SQLException
	 */
	private PmPositionDto mapping() throws SQLException {
		PmPositionDto dto = new PmPositionDto();
		dto.setPositionCode(rs.getString(COL_POSITION_CODE)); // 職位コード
		dto.setPositionName(rs.getString(COL_POSITION_NAME)); // 職位名
		dto.setGrade(rs.getInt(COL_GRADE)); // 号数
		dto.setNotch(rs.getInt(COL_NOTCH)); // 等級
		dto.setBasicSalary(rs.getInt(COL_BASIC_SALARY)); // 基本給
		dto.setEmploymentType(rs.getString(COL_EMPLOYMENT_TYPE)); // 雇用区分
		dto.setDefaultAuth(rs.getString(COL_DEFAULT_AUTH)); // デフォルトプログラム利用権限
		dto.setDefaultLevel(rs.getInt(COL_DEFAULT_LEVEL)); // デフォルトデータ参照レベル
		dto.setDeleteFlag(rs.getInt(COL_DELETE_FLAG)); // 削除フラグ
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmPositionDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<PmPositionDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 有効データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmPositionDto> findAvailable() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = ");
			sb.append(MospConst.DELETE_FLAG_OFF);
			sb.append(" ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			executeQuery();
			List<PmPositionDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param positionCode 職位コード(検索キー)
	 * @return PmPositionDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PmPositionDto findForKey(String positionCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, positionCode);
			executeQuery();
			PmPositionDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param positionCode 職位コード(検索キー)
	 * @return PmPositionDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PmPositionDto findForUpdate(String positionCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, positionCode);
			executeQuery();
			PmPositionDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param positionCode 検索職位コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmPositionDto> findForCondition(String positionCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_POSITION_CODE);
		sb.append(" LIKE ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, positionCode);
			executeQuery();
			List<PmPositionDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param positionCode 検索所属コード
	 * @param positionName 検索所属名
	 * @param deleteFlag   削除フラグ
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmPositionDto> findForCondition(String positionCode, String positionName, String deleteFlag)
			throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_POSITION_CODE);
		sb.append(" LIKE ? AND ");
		sb.append(COL_POSITION_NAME);
		sb.append(" LIKE ?  ");
		if (!deleteFlag.equals("")) {
			sb.append(" AND  ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = ?  ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, positionCode + "%");
			setParam(index++, "%" + positionName + "%");
			if (!deleteFlag.equals("")) {
				setParam(index++, Integer.parseInt(deleteFlag));
			}
			executeQuery();
			List<PmPositionDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(PmPositionDto 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 int 更新結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int update(PmPositionDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException,
			MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getPositionCode());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ削除
	 * @param dto 更新対象DTO
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int delete(PmPositionDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException,
			MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getPositionCode());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(PmPositionDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getPositionCode()); // 職位コード
		setParam(index++, dto.getPositionName()); // 職位名
		setParam(index++, dto.getGrade()); // 号数
		setParam(index++, dto.getNotch()); // 等級
		setParam(index++, dto.getBasicSalary()); // 基本給
		setParam(index++, dto.getEmploymentType()); // 雇用区分
		setParam(index++, dto.getDefaultAuth()); // デフォルトプログラム利用権限
		setParam(index++, dto.getDefaultLevel()); // デフォルトデータ参照レベル
		setParam(index++, dto.getDeleteFlag()); // 削除フラグ
		setCommonParams(isInsert);
	}
	
	/**
	 * 低データレベル職位コード抽出SQL取得
	 * @return 低データレベル職位コード抽出SQL文字列
	 */
	public static String getQueryForLowerLevel() {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT " + COL_POSITION_CODE + " ");
		sb.append("FROM " + TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_DEFAULT_LEVEL + " < ? ");
		return sb.toString();
	}
	
}
