/*
 * 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 com.google.appengine.api.datastore.Query;

import jp.cloudzero.mosp.exception.CloudzeroUnsupportedException;
import jp.getset.gae.bigtable.Statement.Mode;
import jp.mosp.common.common.MospException;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.bonus.dto.PsAvailableStaDto;

public class PsAvailableStaDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String	TABLE				= "US_AVAILABLE_STA";
	public static final String	KEY_1				= "K_CODE";			// 社員コード
	public static final String	KEY_2				= "STATION_CODE";		// 所属コード
																			
	// 列名
	public static final String	COL_K_CODE			= "K_CODE";			// 社員コード
	public static final String	COL_STATION_CODE	= "STATION_CODE";		// 所属コード
																			
	
	/**
	 * コンストラクタ
	 */
	public PsAvailableStaDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<PsAvailableStaDto> mappingAll() throws SQLException {
		List<PsAvailableStaDto> all = new ArrayList<PsAvailableStaDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return PsAvailableStaDto UsAvailableStaレコード
	 * @throws SQLException
	 */
	private PsAvailableStaDto mapping() throws SQLException {
		PsAvailableStaDto dto = new PsAvailableStaDto();
		dto.setKCode(rs.getString(COL_K_CODE)); // 社員コード
		dto.setStationCode(rs.getString(COL_STATION_CODE)); // 所属コード
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PsAvailableStaDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<PsAvailableStaDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @return PsAvailableStaDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PsAvailableStaDto findForKey(String kCode) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kCode);
			executeQuery();
			PsAvailableStaDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param kCode 社員コード(検索キー)
	 * @return PsAvailableStaDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PsAvailableStaDto findForUpdate(String kCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kCode);
			executeQuery();
			PsAvailableStaDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(未使用)
	 * @param kCode 検索社員コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException 
	 */
	public List<PsAvailableStaDto> findForConditaion(String kCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		throw new CloudzeroUnsupportedException();
/*		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = ? ");
		sb.append("AND ");
		sb.append(COL_STATION_CODE + " IN (");
		sb.append(PmStationDao.getBelongedStationQuery());
		sb.append(") ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			// UM_BASIS部
			setParam(index++, kCode);
			// 実行
			executeQuery();
			List<PsAvailableStaDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}*/
	}
	
	/**
	 * 条件文によるデータ取得(社員コード、所属)
	 * @param kCode 検索社員コード
	 * @param stationCode 検索所属コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException 
	 */
	public List<PsAvailableStaDto> findForKCodeStation(String kCode, String stationCode) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		if (!kCode.equals("") || !stationCode.equals("")) {
			sb.append("WHERE ");
		}
		if (!kCode.equals("")) {
			sb.append(COL_K_CODE);
			sb.append(" LIKE ? AND ");
			this.pob.appendLike(COL_K_CODE);
		}
		if (!stationCode.equals("")) {
			sb.append(COL_STATION_CODE);
			sb.append(" =? AND ");
			this.pob.appendWhere(COL_STATION_CODE, Query.FilterOperator.EQUAL);
		}
		if (sb.length() != 0) {
			sb.delete(sb.length() - 4, sb.length());
			sb.append(" ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			if (!kCode.equals("")) {
				setParam(index++, kCode + "%");
			}
			if (!stationCode.equals("")) {
				setParam(index++, stationCode);
			}
			// 実行
			executeQuery();
			List<PsAvailableStaDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(社員コード、社員名、所属)
	 * @param kCode 検索社員コード
	 * @param kName 検索社員名
	 * @param stationCode 検索所属コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException 
	 */
	public List<PsAvailableStaDto> findForKNameStation(String kCode, String kName, String stationCode)
			throws SQLException, IllegalAccessException, NoSuchFieldException {
		throw new CloudzeroUnsupportedException();
/*		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " IN");
		sb.append("(");
		sb.append(PmBasisDao.getSelectForSubQuery());
		sb.append(") ");
		if (stationCode != null && !stationCode.equals("")) {
			sb.append("AND ");
			sb.append(COL_STATION_CODE);
			sb.append(" = ? ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, kCode + "%");
			setParam(index++, "%" + kName + "%");
			setParam(index++, "%");
			if (stationCode != null && !stationCode.equals("")) {
				setParam(index++, stationCode);
			}
			// 実行
			executeQuery();
			List<PsAvailableStaDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}*/
	}
	
	/**
	 * キーによるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @param stationCode 所属コード(検索キー)
	 * @return PsAvailableStaDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PsAvailableStaDto findForKey1Key2(String kCode, String stationCode) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append(" WHERE ");
			sb.append(COL_K_CODE);
			sb.append(" =? AND ");
			sb.append(COL_STATION_CODE);
			sb.append(" =? ");
			this.pob.appendWhere(COL_K_CODE, Query.FilterOperator.EQUAL);
			this.pob.appendWhere(COL_STATION_CODE, Query.FilterOperator.EQUAL);
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			setParam(index++, stationCode);
			executeQuery();
			PsAvailableStaDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ存在チェック
	 * @param  targetMode 存在確認処理モード
	 * @param  targetCode チェックコード(検索キー)
	 * @return UmBasisDto 検索結果(DTO)
	 * @throws SQLException
	 */
	public int findForExist(String kCode, String stationCode) throws SQLException {
		int count = 0;
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT COUNT(*) FROM " + TABLE);
		sb.append(" WHERE ");
		sb.append(COL_K_CODE);
		sb.append(" =? AND ");
		sb.append(COL_STATION_CODE);
		sb.append(" =? ");
		this.setPreparedStatement(Mode.SELECT, TABLE);
		this.pob.appendOperationCount();
		this.pob.appendWhere(COL_K_CODE, Query.FilterOperator.EQUAL);
		this.pob.appendWhere(COL_STATION_CODE, Query.FilterOperator.EQUAL);
		try {
			index = 1;
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			setParam(index++, stationCode);
			executeQuery();
			rs.next();
			count = rs.getInt(1);
			return count;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(PsAvailableStaDto 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(PsAvailableStaDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException,
			MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getKCode());
			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(PsAvailableStaDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException,
			MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getKCode());
			setParam(index++, dto.getStationCode());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員によるデータ削除
	 * @param kCode 削除対象社員コード
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 */
	public int deleteForEmployee(String kCode) throws SQLException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("DELETE FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			
			this.setPreparedStatement(Mode.DELETE);
			this.pob.appendWhere(COL_K_CODE, Query.FilterOperator.EQUAL);
			
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			executeUpdate();
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(PsAvailableStaDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode()); // 社員コード
		setParam(index++, dto.getStationCode()); // 所属コード
		setCommonParams(isInsert);
	}
	
	/**
	 * 利用可能所属コード取得SQL
	 * @return 利用可能所属コード取得SQL文字列
	 */
	public String getAvailableStationQuery() {
/*		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_STATION_CODE + " ");
		sb.append("FROM ");
		sb.append(TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = ? ");
		return sb.toString();*/
		this.setPreparedStatement(Mode.SELECT, TABLE);
		this.pob.appendWhere(COL_K_CODE, Query.FilterOperator.EQUAL);
		return "";
	}
}
