/*
 * 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 Affero General Public License
 * as published by the Free Software Foundation, either version 3
 * 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package jp.mosp.platform.dao.workflow.impl;

import java.util.ArrayList;
import java.util.List;

import jp.mosp.framework.base.BaseDto;
import jp.mosp.framework.base.BaseDtoInterface;
import jp.mosp.framework.base.MospException;
import jp.mosp.platform.base.PlatformDao;
import jp.mosp.platform.constant.PlatformConst;
import jp.mosp.platform.dao.workflow.WorkflowDaoInterface;
import jp.mosp.platform.dto.workflow.WorkflowDtoInterface;
import jp.mosp.platform.dto.workflow.impl.PftWorkflowDto;

/**
 * ワークフローDAOクラス。
 */
public class PftWorkflowDao extends PlatformDao implements WorkflowDaoInterface {
	
	/**
	 * ワークフロー。
	 */
	public static final String	TABLE				= "pft_workflow";
	
	/**
	 * レコード識別ID。
	 */
	public static final String	COL_PFT_WORKFLOW_ID	= "pft_workflow_id";
	
	/**
	 * ワークフロー番号。
	 */
	public static final String	COL_WORKFLOW		= "workflow";
	
	/**
	 * 段階。
	 */
	public static final String	COL_WORKFLOW_STAGE	= "workflow_stage";
	
	/**
	 * 状況。
	 */
	public static final String	COL_WORKFLOW_STATUS	= "workflow_status";
	
	/**
	 * ルートコード。
	 */
	public static final String	COL_ROUTE_CODE		= "route_code";
	
	/**
	 * 機能コード。
	 */
	public static final String	COL_FUNCTION_CODE	= "function_code";
	
	/**
	 * 承認者個人ID。
	 */
	public static final String	COL_APPROVER_ID		= "approver_id";
	
	/**
	 * キー。
	 */
	public static final String	KEY_1				= COL_PFT_WORKFLOW_ID;
	

	/**
	 * コンストラクタ。
	 */
	public PftWorkflowDao() {
		// 処理無し
	}
	
	@Override
	public void initDao() {
		// 処理無し
	}
	
	@Override
	public BaseDto mapping() throws MospException {
		PftWorkflowDto dto = new PftWorkflowDto();
		dto.setPftWorkflowId(getLong(COL_PFT_WORKFLOW_ID));
		dto.setWorkflow(getLong(COL_WORKFLOW));
		dto.setWorkflowStage(getInt(COL_WORKFLOW_STAGE));
		dto.setWorkflowStatus(getString(COL_WORKFLOW_STATUS));
		dto.setRouteCode(getString(COL_ROUTE_CODE));
		dto.setFunctionCode(getString(COL_FUNCTION_CODE));
		dto.setApproverId(getString(COL_APPROVER_ID));
		mappingCommonInfo(dto);
		return dto;
	}
	
	@Override
	public List<WorkflowDtoInterface> mappingAll() throws MospException {
		List<WorkflowDtoInterface> all = new ArrayList<WorkflowDtoInterface>();
		while (next()) {
			all.add(castDto(mapping()));
		}
		return all;
	}
	
	@Override
	public int update(BaseDtoInterface baseDto) throws MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(baseDto, false);
			WorkflowDtoInterface dto = castDto(baseDto);
			setParam(index++, dto.getPftWorkflowId());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (Throwable e) {
			throw new MospException(e);
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	@Override
	public int delete(BaseDtoInterface baseDto) throws MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			WorkflowDtoInterface dto = castDto(baseDto);
			setParam(index++, dto.getPftWorkflowId());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (Throwable e) {
			throw new MospException(e);
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	@Override
	public void setParams(BaseDtoInterface baseDto, boolean isInsert) throws MospException {
		WorkflowDtoInterface dto = castDto(baseDto);
		setParam(index++, dto.getPftWorkflowId());
		setParam(index++, dto.getWorkflow());
		setParam(index++, dto.getWorkflowStage());
		setParam(index++, dto.getWorkflowStatus());
		setParam(index++, dto.getRouteCode());
		setParam(index++, dto.getFunctionCode());
		setParam(index++, dto.getApproverId());
		setCommonParams(baseDto, isInsert);
	}
	
	@Override
	public WorkflowDtoInterface findForKey(long workflow) throws MospException {
		try {
			index = 1;
			StringBuffer sb = getSelectQuery(getClass());
			sb.append(where());
			sb.append(deleteFlagOff());
			sb.append(and());
			sb.append(equal(COL_WORKFLOW));
			prepareStatement(sb.toString());
			setParam(index++, workflow);
			executeQuery();
			WorkflowDtoInterface dto = null;
			if (next()) {
				dto = castDto(mapping());
			}
			return dto;
		} catch (Throwable e) {
			throw new MospException(e);
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	@Override
	public long findForMaxWorkflow() throws MospException {
		try {
			String max = "";
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append(selectMax(COL_WORKFLOW));
			sb.append(from(TABLE));
			prepareStatement(sb.toString());
			executeQuery();
			if (next()) {
				max = String.valueOf(rs.getLong(1));
			}
			return Long.parseLong(max);
		} catch (Throwable e) {
			throw new MospException(e);
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	@Override
	public List<WorkflowDtoInterface> findForApproverId(String functionCode, String approverId) throws MospException {
		try {
			index = 1;
			StringBuffer sb = getSelectQuery(getClass());
			sb.append(where());
			sb.append(deleteFlagOff());
			if (functionCode != null && functionCode.isEmpty() == false) {
				sb.append(and());
				sb.append(equal(COL_FUNCTION_CODE));
			}
			sb.append(and());
			sb.append(like(COL_APPROVER_ID));
			prepareStatement(sb.toString());
			if (functionCode != null && functionCode.isEmpty() == false) {
				setParam(index++, functionCode);
			}
			setParam(index++, containsParam(approverId));
			executeQuery();
			return mappingAll();
		} catch (Throwable e) {
			throw new MospException(e);
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	@Override
	public List<WorkflowDtoInterface> findForRoute(int workflowStage, String workflowStatus, String routeCode,
			String functionCode) throws MospException {
		try {
			index = 1;
			StringBuffer sb = getSelectQuery(getClass());
			sb.append(where());
			sb.append(deleteFlagOff());
			sb.append(and());
			sb.append(equal(COL_WORKFLOW_STAGE));
			if (workflowStatus != null && !workflowStatus.isEmpty()) {
				sb.append(and());
				sb.append(equal(COL_WORKFLOW_STATUS));
			}
			sb.append(and());
			sb.append(equal(COL_ROUTE_CODE));
			if (functionCode != null && !functionCode.isEmpty()) {
				sb.append(and());
				sb.append(equal(COL_FUNCTION_CODE));
			}
			prepareStatement(sb.toString());
			setParam(index++, workflowStage);
			if (workflowStatus != null && !workflowStatus.isEmpty()) {
				setParam(index++, workflowStatus);
			}
			setParam(index++, routeCode);
			if (functionCode != null && !functionCode.isEmpty()) {
				setParam(index++, functionCode);
			}
			executeQuery();
			return mappingAll();
		} catch (Throwable e) {
			throw new MospException(e);
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	@Override
	public List<WorkflowDtoInterface> findForRoute(String workflowStatus, String routeCode, String functionCode)
			throws MospException {
		try {
			index = 1;
			StringBuffer sb = getSelectQuery(getClass());
			sb.append(where());
			sb.append(deleteFlagOff());
			if (workflowStatus != null && !workflowStatus.isEmpty()) {
				sb.append(and());
				sb.append(equal(COL_WORKFLOW_STATUS));
			}
			sb.append(and());
			sb.append(equal(COL_ROUTE_CODE));
			if (functionCode != null && !functionCode.isEmpty()) {
				sb.append(and());
				sb.append(equal(COL_FUNCTION_CODE));
			}
			prepareStatement(sb.toString());
			if (workflowStatus != null && !workflowStatus.isEmpty()) {
				setParam(index++, workflowStatus);
			}
			setParam(index++, routeCode);
			if (functionCode != null && !functionCode.isEmpty()) {
				setParam(index++, functionCode);
			}
			executeQuery();
			return mappingAll();
		} catch (Throwable e) {
			throw new MospException(e);
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	@Override
	public List<WorkflowDtoInterface> findForHistory(long workflow) throws MospException {
		try {
			index = 1;
			StringBuffer sb = getSelectQuery(getClass());
			sb.append(where());
			sb.append(equal(COL_WORKFLOW));
			prepareStatement(sb.toString());
			setParam(index++, workflow);
			executeQuery();
			return mappingAll();
		} catch (Throwable e) {
			throw new MospException(e);
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	@Override
	public String getSubQueryForSetting(boolean useStage, boolean useStatus, boolean useRoute, boolean useFunction) {
		StringBuffer sb = new StringBuffer();
		sb.append(select());
		sb.append(COL_WORKFLOW);
		sb.append(from(TABLE));
		sb.append(where());
		sb.append(deleteFlagOff());
		if (useStage) {
			sb.append(and());
			sb.append(equal(COL_WORKFLOW_STAGE));
		}
		if (useStatus) {
			sb.append(and());
			sb.append(equal(COL_WORKFLOW_STATUS));
		}
		if (useRoute) {
			sb.append(and());
			sb.append(equal(COL_ROUTE_CODE));
		}
		if (useFunction) {
			sb.append(and());
			sb.append(equal(COL_FUNCTION_CODE));
		}
		return sb.toString();
	}
	
	@Override
	public String getSubQueryForNotEqualDraft() {
		StringBuffer sb = new StringBuffer();
		sb.append(select());
		sb.append(COL_WORKFLOW);
		sb.append(from(TABLE));
		sb.append(where());
		sb.append(deleteFlagOff());
		sb.append(and());
		sb.append(notEqual(COL_WORKFLOW_STATUS, PlatformConst.CODE_STATUS_DRAFT));
		return sb.toString();
	}
	
	@Override
	public String getSubQueryForNotEqualWithdrawn() {
		StringBuffer sb = new StringBuffer();
		sb.append(select());
		sb.append(COL_WORKFLOW);
		sb.append(from(TABLE));
		sb.append(where());
		sb.append(deleteFlagOff());
		sb.append(and());
		sb.append(notEqual(COL_WORKFLOW_STATUS, PlatformConst.CODE_STATUS_WITHDRAWN));
		return sb.toString();
	}
	
	/**
	 * DTOインスタンスのキャストを行う。<br>
	 * @param baseDto 対象DTO
	 * @return キャストされたDTO
	 */
	protected WorkflowDtoInterface castDto(BaseDtoInterface baseDto) {
		return (WorkflowDtoInterface)baseDto;
	}
	
}
