/*
 * 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.workflow.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.mosp.common.CommonConst;
import jp.mosp.common.common.BaseDao;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.workflow.dto.AmRouteDto;

/**
 * @author yoshida
 *
 */
public class AmRouteDao extends BaseDao {
	
	// テーブル名及びキー列名
	/**
	 * AM_ROUTE
	 */
	public static final String	TABLE			= "AM_ROUTE";
	/**
	 * RTCODE
	 */
	public static final String	KEY_1			= "RTCODE";	//ルートコード
																
	// 列名
	/**
	 * 
	 */
	public static final String	COL_RT_CODE		= "RTCODE";	//ルートコード
	/**
	 * LAYER1
	 */
	public static final String	COL_LAYER1		= "LAYER1";	//１次承認
	/**
	 * 
	 */
	public static final String	COL_LAYER2		= "LAYER2";	//２次承認
	/**
	 * LAYER3
	 */
	public static final String	COL_LAYER3		= "LAYER3";	//３次承認
	/**
	 * LAYER4
	 */
	public static final String	COL_LAYER4		= "LAYER4";	//４次承認
	/**
	 * LAYER5
	 */
	public static final String	COL_LAYER5		= "LAYER5";	//５次承認
	/**
	 * LASTLAYER
	 */
	public static final String	COL_LAST_LAYER	= "LASTLAYER";	//最終承認
	/**
	 * DELETEFLAG
	 */
	public static final String	COL_DELETE_FLAG	= "DELETEFLAG"; //削除フラグ
																
	
	/**
	 * コンストラクタ
	 */
	public AmRouteDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException SQL例外が発生した場合
	 */
	private List<AmRouteDto> mappingAll() throws SQLException {
		List<AmRouteDto> all = new ArrayList<AmRouteDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return AmRouteDto am_routeレコード
	 * @throws SQLException SQL例外が発生した場合
	 */
	private AmRouteDto mapping() throws SQLException {
		AmRouteDto dto = new AmRouteDto();
		dto.setRtCode(rs.getString(COL_RT_CODE)); //ルートコード
		dto.setLayer1(rs.getString(COL_LAYER1)); //１次承認
		dto.setLayer2(rs.getString(COL_LAYER2)); //２次承認
		dto.setLayer3(rs.getString(COL_LAYER3)); //３次承認
		dto.setLayer4(rs.getString(COL_LAYER4)); //４次承認
		dto.setLayer5(rs.getString(COL_LAYER5)); //５次承認
		dto.setLastLayer(rs.getString(COL_LAST_LAYER)); //最終承認
		dto.setDeleteFlag(rs.getInt(COL_DELETE_FLAG)); //削除フラグ
		return dto;
	}
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<AmRouteDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<AmRouteDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param rtcode ルートコード(検索キー)
	 * @return AmRouteDto 検索結果(DTO)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public AmRouteDto findForKey(String rtcode) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, rtcode);
			executeQuery();
			AmRouteDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param rtcode ルートコード(検索キー)
	 * @return AmRouteDto 検索結果(DTO)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public AmRouteDto findForUpdate(String rtcode) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, rtcode);
			executeQuery();
			AmRouteDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param rtCode 検索ルートコード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<AmRouteDto> findForCondition(String rtCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_RT_CODE);
		sb.append(" LIKE ? ");
		
		this.pob.appendLike(COL_RT_CODE);
		
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, rtCode);
			executeQuery();
			List<AmRouteDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param rtCode	検索ルートコード
	 * @param unitCode	検索ユニットコード
	 * @param kCode		検索社員コード
	 * @param kName		検索社員名
	 * @return	list	検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<AmRouteDto> findForCondition(String rtCode, String unitCode, String kCode, String kName)
			throws SQLException, IllegalAccessException, NoSuchFieldException {
		throw new CloudzeroUnsupportedException();
/*		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + CommonConst.DEFAULT_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_RT_CODE);
		sb.append(" LIKE ? ");
		
		this.pob.appendWhere(COL_DELETE_FLAG, Query.FilterOperator.EQUAL, CommonConst.DEFAULT_FLAG_OFF);
		this.pob.appendLike(COL_RT_CODE);
		
		if (!unitCode.equals("")) {
			sb.append(" AND ");
			sb.append(" ( ");
			sb.append(COL_LAYER1 + " LIKE ? ");
			sb.append(" OR ");
			sb.append(COL_LAYER2 + " LIKE ? ");
			sb.append(" OR ");
			sb.append(COL_LAYER3 + " LIKE ? ");
			sb.append(" OR ");
			sb.append(COL_LAYER4 + " LIKE ? ");
			sb.append(" OR ");
			sb.append(COL_LAYER5 + " LIKE ? ");
			sb.append(" OR ");
			sb.append(COL_LAST_LAYER + " LIKE ? ");
			sb.append(" ) ");
		}
		if (!kCode.equals("")) {
			sb.append(" AND ");
			sb.append(" ( ");
			sb.append(COL_LAYER1 + " IN (" + AmUnitDao.getSearchKCodeSubQuery() + ") ");
			sb.append(" OR ");
			sb.append(COL_LAYER2 + " IN (" + AmUnitDao.getSearchKCodeSubQuery() + ") ");
			sb.append(" OR ");
			sb.append(COL_LAYER3 + " IN (" + AmUnitDao.getSearchKCodeSubQuery() + ") ");
			sb.append(" OR ");
			sb.append(COL_LAYER4 + " IN (" + AmUnitDao.getSearchKCodeSubQuery() + ") ");
			sb.append(" OR ");
			sb.append(COL_LAYER5 + " IN (" + AmUnitDao.getSearchKCodeSubQuery() + ") ");
			sb.append(" OR ");
			sb.append(COL_LAST_LAYER + " IN (" + AmUnitDao.getSearchKCodeSubQuery() + ") ");
			sb.append(" ) ");
		}
		if (!kName.equals("")) {
			sb.append(" AND ");
			sb.append(" ( ");
			sb.append(COL_LAYER1 + " IN (" + AmUnitDao.getSearchKNameSubQuery() + ") ");
			sb.append(" OR ");
			sb.append(COL_LAYER2 + " IN (" + AmUnitDao.getSearchKNameSubQuery() + ") ");
			sb.append(" OR ");
			sb.append(COL_LAYER3 + " IN (" + AmUnitDao.getSearchKNameSubQuery() + ") ");
			sb.append(" OR ");
			sb.append(COL_LAYER4 + " IN (" + AmUnitDao.getSearchKNameSubQuery() + ") ");
			sb.append(" OR ");
			sb.append(COL_LAYER5 + " IN (" + AmUnitDao.getSearchKNameSubQuery() + ") ");
			sb.append(" OR ");
			sb.append(COL_LAST_LAYER + " IN (" + AmUnitDao.getSearchKNameSubQuery() + ") ");
			sb.append(" ) ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, rtCode + "%");
			if (!unitCode.equals("")) {
				setParam(index++, unitCode + "%");
				setParam(index++, unitCode + "%");
				setParam(index++, unitCode + "%");
				setParam(index++, unitCode + "%");
				setParam(index++, unitCode + "%");
				setParam(index++, unitCode + "%");
			}
			if (!kCode.equals("")) {
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
				setParam(index++, kCode + "%");
			}
			if (!kName.equals("")) {
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
				setParam(index++, "%" + kName + "%");
			}
			executeQuery();
			List<AmRouteDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}*/
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param kCode 社員コード
	 * @param startDate 開始年月日
	 * @return	list	検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<AmRouteDto> findForKCodeAvailable(String kCode, String startDate) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		throw new CloudzeroUnsupportedException();
/*		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + CommonConst.DEFAULT_FLAG_OFF);
		sb.append(" AND ");
		sb.append(" ( ");
		sb.append(COL_LAYER1 + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(" OR ");
		sb.append(COL_LAYER2 + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(" OR ");
		sb.append(COL_LAYER3 + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(" OR ");
		sb.append(COL_LAYER4 + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(" OR ");
		sb.append(COL_LAYER5 + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(" OR ");
		sb.append(COL_LAST_LAYER + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(" ) ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, kCode);
			setParam(index++, kCode);
			setParam(index++, startDate);
			setParam(index++, kCode);
			setParam(index++, kCode);
			setParam(index++, startDate);
			setParam(index++, kCode);
			setParam(index++, kCode);
			setParam(index++, startDate);
			setParam(index++, kCode);
			setParam(index++, kCode);
			setParam(index++, startDate);
			setParam(index++, kCode);
			setParam(index++, kCode);
			setParam(index++, startDate);
			setParam(index++, kCode);
			setParam(index++, kCode);
			setParam(index++, startDate);
			executeQuery();
			List<AmRouteDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}*/
	}
	
	/**
	 * 条件文によるデータ取得
	 * @return list 検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<AmRouteDto> findForAttendanceRoute() throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_LAYER1);
		sb.append(" != '' ");
		sb.append(" AND ");
		sb.append(COL_LAYER2);
		sb.append(" = '' ");
		sb.append(" AND ");
		sb.append(COL_LAYER3);
		sb.append(" = '' ");
		sb.append(" AND ");
		sb.append(COL_LAYER4);
		sb.append(" = '' ");
		sb.append(" AND ");
		sb.append(COL_LAYER5);
		sb.append(" = '' ");
		sb.append(" AND ");
		sb.append(COL_LAST_LAYER);
		sb.append(" != '' ");
		
		this.pob.appendWhere(COL_DELETE_FLAG, Query.FilterOperator.EQUAL, MospConst.DELETE_FLAG_OFF);
		this.pob.appendWhere(COL_LAYER1, Query.FilterOperator.NOT_EQUAL, "");
		this.pob.appendWhere(COL_LAYER2, Query.FilterOperator.EQUAL, "");
		this.pob.appendWhere(COL_LAYER3, Query.FilterOperator.EQUAL, "");
		this.pob.appendWhere(COL_LAYER4, Query.FilterOperator.EQUAL, "");
		this.pob.appendWhere(COL_LAYER5, Query.FilterOperator.EQUAL, "");
		this.pob.appendWhere(COL_LAST_LAYER, Query.FilterOperator.NOT_EQUAL, "");
		
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			executeQuery();
			List<AmRouteDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param unitcode 検索ユニットコード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<AmRouteDto> findForAttendanceRouteArray(String unitcode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		throw new CloudzeroUnsupportedException();
/*		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_LAYER2);
		sb.append(" = '' ");
		sb.append(" AND ");
		sb.append(COL_LAYER3);
		sb.append(" = '' ");
		sb.append(" AND ");
		sb.append(COL_LAYER4);
		sb.append(" = '' ");
		sb.append(" AND ");
		sb.append(COL_LAYER5);
		sb.append(" = '' ");
		sb.append(" AND ");
		sb.append(" ( ");
		sb.append(COL_LAYER1);
		sb.append(" = ? ");
		sb.append(" OR ");
		sb.append(COL_LAST_LAYER);
		sb.append(" = ? ");
		sb.append(" ) ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, unitcode);
			setParam(index++, unitcode);
			executeQuery();
			List<AmRouteDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}*/
	}
	
	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 * @throws MospException 	MosP例外処理が発生した場合
	 */
	public int insert(AmRouteDto 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 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 * @throws MospException 	MosP例外処理が発生した場合
	 */
	public int update(AmRouteDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getRtCode());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ削除
	 * @param dto 更新対象DTO
	 * @return int 削除結果(件数)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 * @throws MospException 	MosP例外処理が発生した場合
	 */
	public int delete(AmRouteDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getRtCode());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException SQL例外が発生した場合
	 */
	private void setParams(AmRouteDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getRtCode()); //ルートコード
		setParam(index++, dto.getLayer1()); //１次承認
		setParam(index++, dto.getLayer2()); //２次承認
		setParam(index++, dto.getLayer3()); //３次承認
		setParam(index++, dto.getLayer4()); //４次承認
		setParam(index++, dto.getLayer5()); //５次承認
		setParam(index++, dto.getLastLayer()); //最終承認
		setParam(index++, dto.getDeleteFlag()); //削除フラグ
		setCommonParams(isInsert);
	}
	
}
