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

import java.security.NoSuchAlgorithmException;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import jp.mosp.common.common.BaseVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.common.dto.CmAspUserDto;
import jp.mosp.common.dto.CmUserDto;

/**
 * パスワード変更画面情報を格納する。<br><br>
 * パスワード変更画面に関するフィールドとそのアクセサメソッドを有する。<br>
 */
public class PasswordVo extends BaseVo {
	
	private static final long serialVersionUID = 5173131320694134432L;

	// 定数
	public static final int MODE_SHOW         = 0;
	public static final int MODE_FORCE_UPDATE = 1;

	// パラメータ名
	public static final String PRM_TXT_OLD_PASSWORD = "txtOldPassword";
	public static final String PRM_TXT_NEW_PASSWORD = "txtNewPassword";
	public static final String PRM_TXT_CONFIRM_PASS = "txtConfirmPass";
	public static final String PRM_HDN_OLD_PASSWORD = "hdnOldPassword";
	public static final String PRM_HDN_NEW_PASSWORD = "hdnNewPassword";
	public static final String PRM_HDN_CONFIRM_PASS = "hdnConfirmPass";
	public static final String PRM_HID_USER_ID      = "hidUserId";

	public static final int    LEN_PASSWORD      = MospConst.LEN_PASSWORD;
	public static final int    LEN_MIN_PASSWORD  = MospConst.LEN_MIN_PASSWORD;
	
	// フィールド
	private String cmdMenu;
	private String hdnOldPassword;
	private String hdnNewPassword;
	private String hdnConfirmPass;

	// モード
	private int mode;

	// フィールド名及び制限
	public String NAM_PASSWORD	 ;
	public String NAM_OLD_PASSWORD;
	public String NAM_NEW_PASSWORD;
	public String NAM_CONFIRM_PASS;

	/**
	 * VOの初期設定を行う。<br>
	 * <ul><li>
	 * {@link BaseVo#BaseVo(Properties, Properties, Properties, String, CmAspUserDto, CmUserDto)}
	 * </li><li>
	 * {@link #needDirectJs}設定(true)<br>
	 * </li><li>
	 * {@link #setViewPath(String)}
	 * </li></ul>
	 * @param cfg     設定ファイル
	 * @param msg     メッセージファイル
	 * @param nam     文言ファイル
	 * @param cmd     コマンド
	 * @param aspUser ASPユーザー
	 * @param user    ユーザー
	 */
	public PasswordVo(
			Properties   cfg,
			Properties   msg,
			Properties   nam,
			String       cmd,
			CmAspUserDto aspUser,
			CmUserDto    user
	) {
		super(cfg, msg, nam, cmd, aspUser, user);
		needDirectJs  = true;
		setViewPath(getClassName());
		mode = 0;
	}
	
	/**
	 * 項目名称を設定する。
	 */
	@Override
	protected void setFieldsName() {
		super.setFieldsName();
		NAM_PASSWORD     = getName("C_PASSWORD");
		NAM_OLD_PASSWORD = getName("C_OLD") + getName("C_PASSWORD");
		NAM_NEW_PASSWORD = getName("C_NEW") + getName("C_PASSWORD");
		NAM_CONFIRM_PASS = getName("C_PASSWORD") + getName("C_CONFIRM");
	}
	
	/* (non-Javadoc)
	 * @see jp.mosp.payroll.base.PayrollVo#setSubTitle()
	 */
	@Override
	protected void setSubTitle() {
		super.setSubTitle();
		subTitle = getName("C_PASSWORD") + getName("C_CHANGE");
		aryMenu = new String[1][2];
		aryMenu[0][0] = MospConst.CMD_MENU;
		aryMenu[0][1] = getName("C_MENU") + getName("C_TO");
	}
	
	/**
	 * パラメータを取得、設定する。
	 * @param request リクエスト
	 */
	public void setParams(HttpServletRequest request) {
		hdnOldPassword = request.getParameter(PRM_HDN_OLD_PASSWORD);
		hdnNewPassword = request.getParameter(PRM_HDN_NEW_PASSWORD);
		hdnConfirmPass = request.getParameter(PRM_HDN_CONFIRM_PASS);
	}

	/**
	 * 項目の妥当性確認を行う。
	 * @param currentPassword 現在のパスワード
	 * @throws NoSuchAlgorithmException
	 * @throws MospException
	 */
	public void validate(String currentPassword) throws NoSuchAlgorithmException, MospException {
		// 妥当性確認開始時処理
		startValidation();
		// 必須確認
		checkRequired(hdnOldPassword, NAM_OLD_PASSWORD, PRM_TXT_OLD_PASSWORD);
		checkRequired(hdnNewPassword, NAM_NEW_PASSWORD, PRM_TXT_NEW_PASSWORD);
		checkRequired(hdnConfirmPass, NAM_CONFIRM_PASS, PRM_TXT_CONFIRM_PASS);
		// 文字列長確認
		checkLength  (hdnOldPassword, LEN_PASSWORD, NAM_OLD_PASSWORD, PRM_TXT_OLD_PASSWORD);
		checkLength  (hdnNewPassword, LEN_PASSWORD, NAM_NEW_PASSWORD, PRM_TXT_NEW_PASSWORD);
		checkLength  (hdnConfirmPass, LEN_PASSWORD, NAM_CONFIRM_PASS, PRM_TXT_CONFIRM_PASS);
		// 旧パスワード確認
		checkPassword(MospUtility.getDigest(hdnOldPassword), currentPassword, PRM_TXT_OLD_PASSWORD);
		// 変更パスワード確認
		checkCompare(hdnNewPassword, hdnConfirmPass, 0, NAM_PASSWORD, PRM_TXT_NEW_PASSWORD, PRM_TXT_CONFIRM_PASS);
		// 妥当性確認終了時処理
		endValidation();
	}

	/* (non-Javadoc)
	 * @see jp.mosp.payroll.base.PayrollVo#getDirectJs(Class)
	 */
	@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 getHdnOldPassword() { return hdnOldPassword; }
	public String getHdnNewPassword() { return hdnNewPassword; }
	public String getHdnConfirmPass() { return hdnConfirmPass; }
	public void setHdnOldPassword(String hdnOldPassword) { this.hdnOldPassword = hdnOldPassword; }
	public void setHdnNewPassword(String hdnNewPassword) { this.hdnNewPassword = hdnNewPassword; }
	public void setHdnConfirmPass(String hdnConfirmPass) { this.hdnConfirmPass = hdnConfirmPass; }
	public String getCmdMenu() { return cmdMenu; }
	public void setCmdMenu(String cmdMenu) { this.cmdMenu = cmdMenu; }

	public void setMode(int mode) { this.mode = mode; }
	public int getMode() { return mode; }

}
