/*
 * 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.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.bonus.dto.PmStationDto;

public class PmStationDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String	TABLE					= "UM_STATION";
	public static final String	KEY_1					= "STATION_CODE";		//所属コード
																				
	// 列名
	public static final String	COL_STATION_CODE		= "STATION_CODE";		// 所属コード
	public static final String	COL_STATION_NAME		= "STATION_NAME";		// 所属名
	public static final String	COL_STATION_ABB_NAME	= "STATION_ABB_NAME";	// 所属略称
	public static final String	COL_BUSINESS_TYPE		= "BUSINESS_TYPE";		// 業務形態
	public static final String	COL_CALENDAR_TYPE		= "CALENDAR_TYPE";		// カレンダー区分
	public static final String	COL_EFFORT_TYPE			= "EFFORT_TYPE";		// 成果率区分
	public static final String	COL_BELONG_CODE			= "BELONG_CODE";		// 上位所属コード
	public static final String	COL_DELETE_FLAG			= "DELETE_FLAG";		// 削除フラグ
																				
	
	/**
	 * コンストラクタ
	 */
	public PmStationDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<PmStationDto> mappingAll() throws SQLException {
		List<PmStationDto> all = new ArrayList<PmStationDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return PmStationDto UM_STATIONレコード
	 * @throws SQLException
	 */
	private PmStationDto mapping() throws SQLException {
		PmStationDto dto = new PmStationDto();
		dto.setStationCode(rs.getString(COL_STATION_CODE)); // 所属コード
		dto.setStationName(rs.getString(COL_STATION_NAME)); // 所属名
		dto.setStationAbbName(rs.getString(COL_STATION_ABB_NAME)); // 所属略称
		dto.setBusinessType(rs.getString(COL_BUSINESS_TYPE)); // 業務形態
		dto.setCalendarType(rs.getString(COL_CALENDAR_TYPE)); // カレンダー区分
		dto.setEffortType(rs.getString(COL_EFFORT_TYPE)); // 成果率区分
		dto.setBelongCode(rs.getString(COL_BELONG_CODE)); // 上位所属コード
		dto.setDeleteFlag(rs.getInt(COL_DELETE_FLAG)); // 削除フラグ
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmStationDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<PmStationDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 有効データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmStationDto> findNotDeleted() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_DELETE_FLAG + " = " + MospConst.DELETE_FLAG_OFF + " ");
			this.pob.appendWhere(COL_DELETE_FLAG, Query.FilterOperator.EQUAL, MospConst.DELETE_FLAG_OFF);
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			executeQuery();
			List<PmStationDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 有効データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmStationDto> findAvailable() throws SQLException, IllegalAccessException, NoSuchFieldException {
		throw new CloudzeroUnsupportedException();
/*		try {
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_STATION_CODE + " IN (");
			sb.append(getBelongedStationQuery());
			sb.append(") ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			executeQuery();
			List<PmStationDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}*/
	}
	
	/**
	 * 利用可能データ取得
	 * @param kCode       社員コード
	 * @param stationCode 所属コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmStationDto> findAvailable(String kCode, String stationCode) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		throw new CloudzeroUnsupportedException();
/*		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_STATION_CODE + " IN (");
			sb.append(getBelongedStationQuery());
			sb.append(") AND (");
			sb.append(COL_STATION_CODE + " IN (");
			sb.append(getStationQuery());
			sb.append(") OR ");
			sb.append(COL_STATION_CODE + " IN (");
			sb.append(PsAvailableStaDao.getAvailableStationQuery());
			sb.append(")) ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, stationCode);
			setParam(index++, kCode);
			executeQuery();
			List<PmStationDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}*/
	}
	
	/**
	 * 利用可能データ取得
	 * @param kCode       社員コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmStationDto> findAvailable(String kCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		throw new CloudzeroUnsupportedException();
/*		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_STATION_CODE + " IN (");
			sb.append(PsAvailableStaDao.getAvailableStationQuery());
			sb.append(") ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, kCode);
			executeQuery();
			List<PmStationDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}*/
	}
	
	/**
	 * 所属(除エリア)データ取得
	 * @param kCode       社員コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmStationDto> findExceptArea() throws SQLException, IllegalAccessException, NoSuchFieldException {
		throw new CloudzeroUnsupportedException();
/*		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_STATION_CODE + " NOT IN (");
			sb.append(getAreaStationQuery());
			sb.append(") AND ");
			sb.append(COL_DELETE_FLAG + " = " + MospConst.DELETE_FLAG_OFF + " ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			executeQuery();
			List<PmStationDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}*/
	}
	
	/**
	 * キーによるデータ取得
	 * @param stationCode 所属コード(検索キー)
	 * @return PmStationDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PmStationDto findForKey(String stationCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, stationCode);
			executeQuery();
			PmStationDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param stationCode 所属コード(検索キー)
	 * @return PmStationDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PmStationDto findForUpdate(String stationCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, stationCode);
			executeQuery();
			PmStationDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param stationCode 検索所属コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmStationDto> findForCondition(String stationCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_STATION_CODE);
		sb.append(" LIKE ? AND ");
		this.pob.appendLike(COL_STATION_CODE);
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, stationCode);
			executeQuery();
			List<PmStationDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param stationCode 検索所属コード
	 * @param stationName 検索所属名
	 * @param deleteFlag   削除フラグ
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmStationDto> findForCondition(String stationCode, String stationName, String deleteFlag)
			throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_STATION_CODE);
		sb.append(" LIKE ? AND ");
		sb.append(COL_STATION_NAME);
		sb.append(" LIKE ?  ");
		this.pob.appendLike(COL_STATION_CODE);
		this.pob.appendLike(COL_STATION_NAME);
		if (!deleteFlag.equals("")) {
			sb.append(" AND  ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = ?  ");
			this.pob.appendWhere(COL_DELETE_FLAG, Query.FilterOperator.EQUAL);
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, stationCode + "%");
			setParam(index++, "%" + stationName + "%");
			if (!deleteFlag.equals("")) {
				setParam(index++, Integer.parseInt(deleteFlag));
			}
			executeQuery();
			List<PmStationDto> 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(PmStationDto 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(PmStationDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException,
			MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getStationCode());
			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(PmStationDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException,
			MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getStationCode());
			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(PmStationDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getStationCode()); // 所属コード
		setParam(index++, dto.getStationName()); // 所属名
		setParam(index++, dto.getStationAbbName()); // 事業部
		setParam(index++, dto.getBusinessType()); // 業務形態
		setParam(index++, dto.getCalendarType()); // カレンダー区分
		setParam(index++, dto.getEffortType()); // 成果率区分
		setParam(index++, dto.getBelongCode()); // 上位所属コード
		setParam(index++, dto.getDeleteFlag()); // 削除フラグ
		setCommonParams(isInsert);
	}
	
	/**
	 * 所属(下位所属含)コード取得SQL
	 * @return 所属(下位所属含)コード取得SQL文字列
	 */
	public static final String getStationQuery() {
		throw new CloudzeroUnsupportedException();
/*		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_STATION_CODE + " ");
		sb.append("FROM ");
		sb.append(TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_DELETE_FLAG + " = " + MospConst.DELETE_FLAG_OFF + " ");
		sb.append("AND ");
		sb.append(COL_BELONG_CODE + " IN (");
		sb.append("SELECT " + COL_BELONG_CODE + " ");
		sb.append("FROM " + TABLE + " ");
		sb.append("WHERE " + COL_STATION_CODE + " = ? ");
		sb.append(") ");
		return sb.toString();*/
	}
	
	/**
	 * 所属(下位所属除)コード取得SQL
	 * @return 所属(下位所属除)コード取得SQL文字列
	 */
	public String getBelongedStationQuery() {
/*		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_STATION_CODE + " ");
		sb.append("FROM ");
		sb.append(TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_STATION_CODE + " = " + COL_BELONG_CODE + " ");
		sb.append("AND ");
		sb.append(COL_DELETE_FLAG + " = " + MospConst.DELETE_FLAG_OFF + " ");
		return sb.toString();*/
		this.setPreparedStatement(Mode.SELECT, TABLE);
		this.pob.appendWhere(COL_STATION_CODE, Query.FilterOperator.EQUAL, COL_BELONG_CODE);
		this.pob.appendWhere(COL_DELETE_FLAG, Query.FilterOperator.EQUAL, MospConst.DELETE_FLAG_OFF);
		return "";
	}
	
	/**
	 * 上位所属コード取得SQL
	 * @return 上位所属コード取得SQL文字列
	 */
	private String getAreaStationQuery() {
/*		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_BELONG_CODE + " ");
		sb.append("FROM ");
		sb.append(TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_STATION_CODE + " <> " + COL_BELONG_CODE + " ");
		return sb.toString();
		this.setPreparedStatement(Mode.SELECT, TABLE);*/
		this.pob.appendWhere(COL_STATION_CODE, Query.FilterOperator.NOT_EQUAL, COL_BELONG_CODE);
		return "";
	}
	
}
