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

import javax.servlet.http.HttpServletRequest;

import jp.mosp.common.CommonConst;
import jp.mosp.common.MosPVo;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.common.dto.CmCodeDto;
import jp.mosp.common.utils.StringUtil;

/**
 * 
 */
public class CmCodeCardVo extends MosPVo {
	
	private static final long	serialVersionUID	= -1356242375010280961L;
	
	// 定数
	/**
	 * 
	 */
	public static final int		MODE_INSERT			= 0;
	/**
	 * 
	 */
	public static final int		MODE_UPDATE			= 1;
	
	// パラメータ名
	/**
	 * 
	 */
	public static final String	PRM_PLT_T_ID		= "pltTId";
	/**
	 * 
	 */
	public static final String	PRM_TXT_T_CODE		= "txtTCode";
	/**
	 * 
	 */
	public static final String	PRM_TXT_T_NAME		= "txtTName";
	/**
	 * 
	 */
	public static final String	PRM_TXT_VIEW_INDEX	= "txtViewIndex";
	/**
	 * 
	 */
	public static final String	PRM_PLT_DELETE		= "pltDelete";
	
	// フィールド
	private String				pltTId;
	private String				txtTCode;
	private String				txtTName;
	private String				txtViewIndex;
	private String				pltDelete;
	
	// プルダウンリスト
	/**
	 * 
	 */
	public String[][]			aryPltTId;
	
	// モード(0：新規登録、1：更新)
	private int					mode;
	
	// フィールド名及び制限
	/**
	 * 
	 */
	public String				NAM_CODE_ID;
	/**
	 * 
	 */
	public String				NAM_CODE;
	/**
	 * 
	 */
	public String				NAM_NAME;
	/**
	 * 
	 */
	public String				NAM_INDEX;
	/**
	 * 
	 */
	public String				NAM_DELETE;
	/**
	 * 
	 */
	public int					LEN_CODE_ID;
	/**
	 * 
	 */
	public int					LEN_CODE;
	/**
	 * 
	 */
	public int					LEN_NAME;
	/**
	 * 
	 */
	public int					LEN_INDEX;
	/**
	 * 
	 */
	public String				REG_DELETE;
	

	/**
	 * コンストラクタ
	 */
	public CmCodeCardVo() {
		super();
		mode = MODE_INSERT;
		setViewPath(getClassName());
	}
	
	@Override
	protected void setSubTitle() {
		subTitle = CommonConst.TTL_COMMON_CODE_MASTER;
		aryMenu = new String[][]{ { CMD_MENU_SYSTEM2, CommonConst.NAM_TO_MENU } };
	}
	
	@Override
	protected void setFieldsName() {
		NAM_CODE_ID = CommonConst.NAM_COMMON_CODE + CommonConst.NAM_COMMON_TYPE;
		NAM_CODE = CommonConst.NAM_COMMON_CODE;
		NAM_NAME = CommonConst.NAM_COMMON_MEISYO;
		NAM_INDEX = CommonConst.NAM_SHOW_ORDER;
		NAM_DELETE = CommonConst.NAM_DELETE_FLAG;
		LEN_CODE_ID = 10;
		LEN_CODE = 8;
		LEN_NAME = 16;
		LEN_INDEX = 3;
		REG_DELETE = CommonConst.REG_DELETE;
	}
	
	@Override
	public void setParams(HttpServletRequest request) {
		pltTId = request.getParameter(PRM_PLT_T_ID);
		txtTCode = request.getParameter(PRM_TXT_T_CODE);
		txtTName = request.getParameter(PRM_TXT_T_NAME);
		txtViewIndex = request.getParameter(PRM_TXT_VIEW_INDEX);
		pltDelete = request.getParameter(PRM_PLT_DELETE);
	}
	
	/**
	 * フィールド設定
	 * @param dto ユーザーリスト
	 */
	public void setFields(CmCodeDto dto) {
		// データの設定
		pltTId = dto.getTId();
		txtTCode = dto.getTCode();
		txtTName = dto.getTName();
		txtViewIndex = String.valueOf(dto.getViewIndex());
		pltDelete = String.valueOf(dto.getDeleteFlag());
	}
	
	/**
	 * フィールドの妥当性確認
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws MospException MosP例外処理が発生した場合
	 */
	public void validate(boolean isInsert) throws MospException {
		// 妥当性確認開始時処理
		startValidation();
		// 必須確認
		checkRequired(pltTId, NAM_CODE_ID, PRM_PLT_T_ID);
		checkRequired(txtTCode, NAM_CODE, PRM_TXT_T_CODE);
		checkRequired(txtTName, NAM_NAME, PRM_TXT_T_NAME);
		checkRequired(pltDelete, NAM_DELETE, PRM_PLT_DELETE);
		// 文字タイプ確認
		checkCode(pltTId, NAM_CODE_ID, PRM_PLT_T_ID);
		checkCode(txtTCode, NAM_CODE, PRM_TXT_T_CODE);
		checkNumber(txtViewIndex, NAM_INDEX, PRM_TXT_VIEW_INDEX);
		checkRegex(pltDelete, REG_DELETE, NAM_DELETE, PRM_PLT_DELETE);
		// 文字列長確認
		checkLength(pltTId, LEN_CODE_ID, NAM_CODE_ID, PRM_PLT_T_ID);
		checkLength(txtTCode, LEN_CODE, NAM_CODE, PRM_TXT_T_CODE);
		checkLength(txtTName, LEN_NAME, NAM_NAME, PRM_TXT_T_NAME);
		checkLength(txtViewIndex, LEN_INDEX, NAM_INDEX, PRM_TXT_VIEW_INDEX);
		// コード区分確認
		if (isInsert && (pltTId.equals(CommonConst.TID_AUTHORITY)
		/*						
		 ||
		pltTId.equals(CommonConst.TID_FIELD        ) ||
		pltTId.equals(CommonConst.TID_HOLIDAY_TYPE ) ||
		pltTId.equals(CommonConst.TID_PAYMENT_TYPE ) ||
		pltTId.equals(CommonConst.TID_OVERTIME_TYPE) ||
		pltTId.equals(CommonConst.TID_TIME         ) ||
		pltTId.equals(CommonConst.TID_TOTAL_TYPE   ) ||
		pltTId.equals(CommonConst.TID_TRAIN        ) ||
		pltTId.equals(CommonConst.TID_ACCOUNT_TYPE ) ||
		pltTId.equals(CommonConst.TID_PAYMENT_MEANS) ||
		pltTId.equals(CommonConst.TID_AMOUNT_TYPE  )
		 */
		)) {
			addErrMessage(MospUtility.getMessage(msg, CommonConst.MSG_DISABLE_TID, ""));
			addErrField(PRM_PLT_T_ID);
			isValid = false;
		}
		// 妥当性確認終了時処理
		endValidation();
	}
	
	/**
	 * フィールド設定(DTOに対するVO情報のセット)
	 * @param dto 設定対象DTO
	 */
	public void setDtoFields(CmCodeDto dto) {
		dto.setTId(pltTId);
		dto.setTCode(txtTCode);
		dto.setTName(txtTName);
		if (StringUtil.isNull(txtViewIndex)) {
			dto.setViewIndex(0);
		} else {
			dto.setViewIndex(Integer.parseInt(txtViewIndex));
		}
		dto.setDeleteFlag(Integer.parseInt(pltDelete));
	}
	
	@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();
	}
	
	/**
	 * @return pltTId
	 */
	public String getPltTId() {
		return pltTId;
	}
	
	/**
	 * @return txtTCode
	 */
	public String getTxtTCode() {
		return txtTCode;
	}
	
	/**
	 * @return txtTName
	 */
	public String getTxtTName() {
		return txtTName;
	}
	
	/**
	 * @return txtViewIndex
	 */
	public String getTxtViewIndex() {
		return txtViewIndex;
	}
	
	/**
	 * @return pltDelete
	 */
	public String getPltDelete() {
		return pltDelete;
	}
	
	/**
	 * @return mode
	 */
	public int getMode() {
		return mode;
	}
	
	/**
	 * @param pltTId セットする pltTId
	 */
	public void setPltTId(String pltTId) {
		this.pltTId = pltTId;
	}
	
	/**
	 * @param txtTCode セットする txtTCode
	 */
	public void setTxtTCode(String txtTCode) {
		this.txtTCode = txtTCode;
	}
	
	/**
	 * @param txtTName セットする txtTName
	 */
	public void setTxtTName(String txtTName) {
		this.txtTName = txtTName;
	}
	
	/**
	 * @param txtViewIndex セットする txtViewIndex
	 */
	public void setTxtViewIndex(String txtViewIndex) {
		this.txtViewIndex = txtViewIndex;
	}
	
	/**
	 * @param pltDelete セットする pltDelete
	 */
	public void setPltDelete(String pltDelete) {
		this.pltDelete = pltDelete;
	}
	
	/**
	 * @param mode セットする mode
	 */
	public void setMode(int mode) {
		this.mode = mode;
	}
	
}
