/*
 * 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.vo;

import javax.servlet.http.HttpServletRequest;

import jp.mosp.common.CommonConst;
import jp.mosp.common.MosPMenuConst;
import jp.mosp.common.MosPVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.workflow.dto.PmAttendanceDto;

/**
 * @author yoshida
 *
 */
public class AttendanceRouteCardVo extends MosPVo {
	
	private static final long	serialVersionUID	= 6002665600142279869L;
	
	// 定数
	public static final int		MODE_INSERT			= 0;
	public static final int		MODE_UPDATE			= 1;
	public static final int		MODE_IMPOSSIBLE		= 2;
	
	// パラメータ名
	public static final String	PRM_HID_ROUTE_ID	= "hidRouteid";
	public static final String	PRM_PLT_OFFICE		= "pltOffice";
	public static final String	PRM_PLT_SECTION		= "pltSection";
	public static final String	PRM_TXT_K_CODE		= "txtKCode";
	public static final String	PRM_PLT_ROUTE_CODE	= "pltRouteCode";
	public static final String	PRM_PLT_DELETE		= "pltDelete";
	
	// フィールド
	private String				hidRouteid;
	private String				pltOffice;
	private String				pltSection;
	private String				txtKCode;
	private String				pltRouteCode;
	private String				pltDelete;
	
	// プルダウンリスト
	public String[][]			aryPltOffice;
	public String[][]			aryPltSection;
	public String[][]			aryPltRoute;
	public String[][]			aryUser;
	public String[][]			aryBelongSection;
	public String[][]			aryBelongOffice;
	
	// フィールド名及び制限
	public String				NAM_OFFICE;
	public String				NAM_SECTION;
	public String				NAM_K_CODE;
	public String				NAM_ROUTE_CODE;
	public String				NAM_DELETE;
	public int					LEN_CODE;
	public String				REG_DELETE;
	
	// モード
	private int					mode;
	

	/**
	 * コンストラクタ
	 */
	public AttendanceRouteCardVo() {
		super();
		mode = MODE_INSERT;
		setViewPath(getClassName());
	}
	
	@Override
	protected void setSubTitle() {
		subTitle = CommonConst.TTL_APPROVAL_ATTENDANCE;
		aryMenu = new String[][]{ { CMD_MENU_SYSTEM2, CommonConst.NAM_TO_MENU }, MosPMenuConst.getMenuApproval(), };
	}
	
	@Override
	protected void setFieldsName() {
		NAM_OFFICE = CommonConst.NAM_OFFICE;
		NAM_SECTION = CommonConst.NAM_STATION;
		NAM_K_CODE = CommonConst.NAM_K_CODE;
		NAM_ROUTE_CODE = CommonConst.NAM_APPROVAL_ROUTE;
		NAM_DELETE = CommonConst.NAM_DELETE_FLAG;
		LEN_CODE = 10;
		REG_DELETE = CommonConst.REG_DELETE;
	}
	
	/**
	 * 初期値設定
	 */
	public void setDefaultValues() {
		setHidRouteid("");
		setPltSection("");
		setTxtKCode("");
		setPltRouteCode("");
		setPltDelete(String.valueOf(MospConst.DELETE_FLAG_OFF));
	}
	
	@Override
	public void setParams(HttpServletRequest request) {
		hidRouteid = request.getParameter(PRM_HID_ROUTE_ID);
		pltOffice = request.getParameter(PRM_PLT_OFFICE);
		pltSection = request.getParameter(PRM_PLT_SECTION);
		txtKCode = request.getParameter(PRM_TXT_K_CODE);
		pltRouteCode = request.getParameter(PRM_PLT_ROUTE_CODE);
		pltDelete = request.getParameter(PRM_PLT_DELETE);
	}
	
	/**
	 * フィールド設定
	 * @param dto 対象DTO
	 */
	public void setFields(PmAttendanceDto dto) {
		hidRouteid = String.valueOf(dto.getRouteId());
		pltSection = dto.getSCode();
		txtKCode = dto.getKCode();
		pltRouteCode = dto.getRtCode();
		pltOffice = dto.getJCode();
		pltDelete = String.valueOf(dto.getDeleteFlag());
	}
	
	/**
	 * フィールド設定(DTOに対するVO情報のセット)
	 * @param dto 設定対象DTO
	 */
	public void setDtoFields(PmAttendanceDto dto) {
		dto.setSCode(pltSection);
		dto.setKCode(txtKCode);
		dto.setRtCode(pltRouteCode);
		dto.setJCode(pltOffice);
		dto.setDeleteFlag(Integer.parseInt(pltDelete));
	}
	
	/**
	 * フィールドの妥当性確認
	 * @throws MospException
	 */
	public void validate() throws MospException {
		// 妥当性確認開始時処理
		startValidation();
		// 文字列長確認
		checkLength(txtKCode, LEN_CODE, NAM_K_CODE, PRM_TXT_K_CODE);
		if (txtKCode.length() != 0) {
			// コード存在確認
			checkCodeExist(txtKCode, aryUser, NAM_K_CODE, PRM_TXT_K_CODE);
			// 所属確認
			if (pltSection.length() != 0) {
				checkBelong(txtKCode, pltSection, aryBelongSection, NAM_K_CODE, PRM_TXT_K_CODE);
			}
			// 事業所確認
			if (pltOffice.length() != 0) {
				checkBelong(txtKCode, pltOffice, aryBelongOffice, NAM_K_CODE, PRM_TXT_K_CODE);
			}
		} else if (pltSection.length() != 0 || pltOffice.length() != 0) {
			checkRequired(pltSection, NAM_SECTION, PRM_PLT_SECTION);
			checkRequired(pltOffice, NAM_OFFICE, PRM_PLT_OFFICE);
		} else {
			addErrMessage(MospUtility.getMessage(msg, "WA1010", ""));
			addErrField(PRM_TXT_K_CODE);
			addErrField(PRM_PLT_SECTION);
			isValid = false;
		}
		// 妥当性確認終了時処理
		endValidation();
	}
	
	/**
	 * 所属確認
	 * @param kCode					確認対象社員コード
	 * @param targetBelongCode		確認対象コード
	 * @param aryTarget				確認対象配列
	 * @param name 					確認対象名称
	 * @param id 					確認対象フィールドID
	 * @throws MospException
	 */
	private void checkBelong(String kCode, String targetBelongCode, String[][] aryTarget, String name, String id)
			throws MospException {
		// 社員コードの所属を確認する
		if (!targetBelongCode.equals(getCodeName(kCode, aryTarget))) {
			addErrMessage(MospUtility.getMessage(msg, CommonConst.MSG_TARGET_NOT_EXIST, name));
			addErrField(id);
			isValid = false;
		}
	}
	
	@Override
	public String getDirectJs(Class<?> cls, Object obj) throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDirectJs(cls, obj));
		sb.append("var MODE = ");
		sb.append(mode);
		sb.append(";");
		return sb.toString();
	}
	
	// アクセサメソッド
	public String getHidRouteid() {
		return hidRouteid;
	}
	
	public String getPltOffice() {
		return pltOffice;
	}
	
	public String getPltSection() {
		return pltSection;
	}
	
	public String getTxtKCode() {
		return txtKCode;
	}
	
	public String getPltRouteCode() {
		return pltRouteCode;
	}
	
	public String getPltDelete() {
		return pltDelete;
	}
	
	public String getEscTxtKCode() {
		return escapeHTML(txtKCode);
	}
	
	public void setHidRouteid(String hidRouteid) {
		this.hidRouteid = hidRouteid;
	}
	
	public void setPltOffice(String pltOffice) {
		this.pltOffice = pltOffice;
	}
	
	public void setPltSection(String pltSection) {
		this.pltSection = pltSection;
	}
	
	public void setTxtKCode(String txtKCode) {
		this.txtKCode = txtKCode;
	}
	
	public void setPltRouteCode(String pltRouteCode) {
		this.pltRouteCode = pltRouteCode;
	}
	
	public void setPltDelete(String pltDelete) {
		this.pltDelete = pltDelete;
	}
	
	public void setMode(int mode) {
		this.mode = mode;
	}
	
}
