/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2009 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.payroll.payment.vo;

import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.base.PayrollVo;
import jp.mosp.payroll.dto.CmCoBankDto;

public class CompanyCoBankVo extends PayrollVo {

	private static final long serialVersionUID = 1048045114292237926L;
	
	public static final int MODE_INSERT = 0;
	public static final int MODE_UPDATE = 1;
	
	// パラメータ名
	public static final String PRM_TXT_CO_BANK_CODE  = "txtCoBankCode"  ;
	public static final String PRM_TXT_BANK_CODE     = "txtBankCode"    ;
	public static final String PRM_TXT_BRANCH_CODE   = "txtBranchCode"  ;
	public static final String PRM_TXT_ACCOUNT_NO    = "txtAccountNo"   ;
	public static final String PRM_PLT_ACCOUNT_TYPE  = "pltAccountType" ;
	public static final String PRM_PLT_DELETE        = "pltDelete"      ;
	public static final String PRM_TXT_BANK_NAME     = "txtBankName"    ;
	public static final String PRM_TXT_BRANCH_NAME   = "txtBranchName"  ;
	public static final String PRM_LBL_BANK_NAME     = "lblBankName"    ;
	public static final String PRM_LBL_BRANCH_NAME   = "lblBranchName"  ;

	// フィールド
	private String[] aryCoBankCode  ;
	private String[] aryBankCode    ;
	private String[] aryBranchCode  ;
	private String[] aryAccountType ;
	private String[] aryAccountNo   ;
	private String[] aryDelete      ;
	private String[] aryBankName    ;
	private String[] aryBranchName  ;

	private String   txtCoBankCode  ;
	private String   txtBankCode    ;
	private String   txtBranchCode  ;
	private String   txtAccountNo   ;
	private String   pltAccountType ;
	private String   pltDelete      ;
	private String   txtBankName    ;
	private String   txtBranchName  ;
	private String   lblBankName    ;
	private String   lblBranchName  ;

	// フィールド名及び制限
	public static final String NAM_CO_BANK_CODE = "会社銀行コード" ;
	public static final String NAM_BANK_CODE    = "銀行コード"     ;
	public static final String NAM_BRANCH_CODE  = "支店コード"     ;
	public static final String TNM_ACCOUNT_TYPE = "預金区分"       ;
	public static final String NAM_ACCOUNT_NO   = "口座番号"       ;
	public static final String NAM_DELETE       = "有効/無効"      ;
	public static final String NAM_BANK_NAME    = "銀行名"         ;
	public static final String NAM_BRANCH_NAME  = "支店名"         ;
	
	public static final int    LEN_CO_BANK_CODE = 2     ;
	public static final int    LEN_BANK_CODE    = 4     ;
	public static final int    LEN_BRANCH_CODE  = 3     ;
	public static final int    LEN_ACCOUNT_NO   = 7     ;

	// ソートキー
	public final String KEY_CO_BANK_CODE = "keyCoBankCode"  ;
	public final String KEY_BANK_CODE    = "keyBankCode"    ;
	public final String KEY_BRANCH_CODE  = "keyBranchCode"  ;
	public final String KEY_ACCOUNT_TYPE = "keyAccountType" ;
	public final String KEY_ACCOUNT_NO   = "keyAccountNo"   ;
	public final String KEY_DELETE       = "keydelete"      ;
	public final String KEY_BANK_NAME    = "keyBankName"    ;
	public final String KEY_BRANCH_NAME  = "keyBranchName"  ;
	
	// プルダウンリスト
	public String[][] aryPltAccountType;
	
	// モード(0：登録、1：更新)
	private int mode;
	
	// 銀行リスト項目
	public String[][] aryPltPaidBankName;
	public String[][] aryPltPaidBranchName;


	/**
	 * コンストラクタ
	 */
	public CompanyCoBankVo() {
		super();
		txtCoBankCode  = "";
		txtBankCode    = "";
		txtBranchCode  = "";
		txtAccountNo   = "";
		pltAccountType = "";
		pltDelete      = "";
		txtBankName    = "";
		txtBranchName  = "";

		lblBankName    = "";
		lblBranchName  = "";
		
		aryCoBankCode  = new String[0];
		aryBankCode    = new String[0];
		aryBranchCode  = new String[0];
		aryAccountType = new String[0];
		aryAccountNo   = new String[0];
		aryDelete      = new String[0];	
		aryBankName    = new String[0];
		aryBranchName  = new String[0];
		setFormerKey(KEY_CO_BANK_CODE);
	}

	/* (non-Javadoc)
	 * @see jp.mosp.payroll.base.PayrollVo#setSubTitle()
	 */
	@Override
	protected void setSubTitle() {
		super.setSubTitle();
		subTitle = getName("P_COMPANY") + getName("P_BANK") + getName("P_MAINTENANCE");
		addAryMenu("U5248", getName("P_COMPANY") + getName("P_INFORMATION") + getName("P_MAINTENANCE") + getName("C_TO"));
		addAryMenu("P1002", getName("P_PAYROLL") + getName("C_MASTER") + getName("C_MENU") + getName("C_TO"));
	}
	
	/**
	 * パラメータ設定
	 * @param request リクエスト
	 */
	public void setParams(HttpServletRequest request) {
		txtCoBankCode  = request.getParameter(PRM_TXT_CO_BANK_CODE );
		txtBankCode    = request.getParameter(PRM_TXT_BANK_CODE    );
		txtBranchCode  = request.getParameter(PRM_TXT_BRANCH_CODE  );
		txtAccountNo   = request.getParameter(PRM_TXT_ACCOUNT_NO   );
		pltAccountType = request.getParameter(PRM_PLT_ACCOUNT_TYPE );
		pltDelete      = request.getParameter(PRM_PLT_DELETE       );
		txtBankName    = request.getParameter(PRM_TXT_BANK_NAME    );
		txtBranchName  = request.getParameter(PRM_TXT_BRANCH_NAME  );

	}
	
	/**
	 * フィールド設定
	 * @param dto 
	 */
	public void setFields(CmCoBankDto dto) {
		txtCoBankCode  = dto.getCoBankCode();
		txtBankCode    = dto.getBankCode();
		txtBranchCode  = dto.getBranchCode();
		txtAccountNo   = dto.getAccountNo();
		pltAccountType = String.valueOf(dto.getAccountType());
		pltDelete      = String.valueOf(dto.getDeleteFlag());
	}

	/**
	 * フィールド設定
	 * @param listCmCoBank 会社銀行登録リスト
	 * @throws MospException
	 */
	public void setFields(List listCmCoBank) throws MospException{
		// リストの設定
		list = listCmCoBank;
		// データ総数の取得
		count = listCmCoBank.size();
		// 配列の初期化
		aryCoBankCode   = new String[count];
		aryBankCode     = new String[count];
		aryBranchCode   = new String[count];
		aryAccountType  = new String[count];
		aryAccountNo    = new String[count];
		aryDelete       = new String[count];
		aryBankName     = new String[count];
		aryBranchName   = new String[count];
		// データの設定
		int i = 0;
		Iterator it = listCmCoBank.iterator();

		while (it.hasNext()) {
			CmCoBankDto dto    = (CmCoBankDto)it.next();
			aryCoBankCode  [i] = escapeHTML(dto.getCoBankCode());
			aryBankCode    [i] = escapeHTML(dto.getBankCode());
			aryBranchCode  [i] = escapeHTML(dto.getBranchCode());
			aryAccountType [i] = dto.getAccountType();
			aryAccountNo   [i] = escapeHTML(dto.getAccountNo());
			aryDelete      [i] = String.valueOf(dto.getDeleteFlag());
			
			aryAccountType[i] = "";
			for (int j = 0; j < aryPltAccountType.length; j++) {
				if (dto.getAccountType().equals(aryPltAccountType[j][0])) {
					aryAccountType[i] = escapeHTML(aryPltAccountType[j][1]);
					break;
				}
			}
			if (dto.getDeleteFlag() == MospConst.DELETE_FLAG_OFF) {
				aryDelete[i] = escapeHTML(MospConst.DELETE_FLAG_OFF_NAME);
			} else if (dto.getDeleteFlag() == MospConst.DELETE_FLAG_ON) {
				aryDelete[i] = escapeHTML(MospConst.DELETE_FLAG_ON_NAME);
			} else {
				throw new MospException(MospConst.EX_CM_USER_ILLEGAL);
			}
			i++;
		}
	}
	
	/**
	 * 銀行名を設定する。<br>
	 * 銀行名の配列に銀行名をセットする。<br>
	 * 会社銀行登録一覧で、銀行コードと一致する銀行名を設定する。
	 * @param index
	 * @param bankName
	 */
	public void setBankName(int index, String bankName) {
		aryBankName[index] = escapeHTML(bankName); 
	}
	
	/**
	 * 支店名を設定する。<br>
	 * 支店名の配列に支店名をセットする。<br>
	 * 会社銀行登録一覧で、銀行コード・支店コードと一致する支店名を設定する。
	 * @param index
	 * @param branchName
	 */
	public void setBranchName(int index, String branchName) {
		aryBranchName[index] = escapeHTML(branchName);
	}
	
	/**
	 * フィールドの妥当性確認
	 * @throws MospException
	 */
	public void validate() throws MospException {
		// 妥当性確認開始時処理
		startValidation();
		// 必須確認
		checkRequired(txtCoBankCode  , NAM_CO_BANK_CODE , PRM_TXT_CO_BANK_CODE );
		checkRequired(txtBankCode    , NAM_BANK_CODE    , PRM_TXT_BANK_CODE    );
		checkRequired(txtBranchCode  , NAM_BRANCH_CODE  , PRM_TXT_BRANCH_CODE  );
		checkRequired(pltAccountType , TNM_ACCOUNT_TYPE , PRM_PLT_ACCOUNT_TYPE );
		checkRequired(txtAccountNo   , NAM_ACCOUNT_NO   , PRM_TXT_ACCOUNT_NO   );
		checkRequired(pltDelete      , NAM_DELETE       , PRM_PLT_DELETE       );
		// 文字タイプ確認
		checkCode    (txtCoBankCode  , NAM_CO_BANK_CODE , PRM_TXT_CO_BANK_CODE );
		//checkCode    (txtBankCode    , NAM_BANK_CODE    , PRM_TXT_BANK_CODE    );
		//checkCode    (txtBranchCode  , NAM_BRANCH_CODE  , PRM_TXT_BRANCH_CODE  );
		//checkCode    (pltAccountType , TNM_ACCOUNT_TYPE , PRM_PLT_ACCOUNT_TYPE );
		//checkCode    (txtAccountNo   , NAM_ACCOUNT_NO   , PRM_TXT_ACCOUNT_NO   );
		//checkRegex   (pltDelete, REG_DELETE, NAM_DELETE , PRM_PLT_DELETE       );
		// 文字列長確認
		checkLength  (txtCoBankCode  , LEN_CO_BANK_CODE , NAM_CO_BANK_CODE , PRM_TXT_CO_BANK_CODE );
		checkLength  (txtBankCode    , LEN_BANK_CODE    , NAM_BANK_CODE    , PRM_TXT_BANK_CODE    );
		checkLength  (txtBranchCode  , LEN_BRANCH_CODE  , NAM_BRANCH_CODE  , PRM_TXT_BRANCH_CODE  );
		//checkLength  (pltAccountType , LEN_ACCOUNT_TYPE , TNM_ACCOUNT_TYPE , PRM_PLT_ACCOUNT_TYPE );
		checkLength  (txtAccountNo   , LEN_ACCOUNT_NO   , NAM_ACCOUNT_NO   , PRM_TXT_ACCOUNT_NO   );
		// 数字確認
		checkNumeric(txtBankCode , NAM_BANK_CODE     , PRM_TXT_BANK_CODE   );
		checkNumeric(txtBranchCode , NAM_BRANCH_CODE , PRM_TXT_BRANCH_CODE );
		checkNumeric(txtAccountNo , NAM_ACCOUNT_NO   , PRM_TXT_ACCOUNT_NO  );
		// 妥当性確認終了時処理
		endValidation();
	}
	
	/**
	 * 金融機関JS設定
	 * @param sb
	 */
	protected void setFinanceData(StringBuffer sb) {
		sb.append("</script>");
		sb.append("<script type=\"text/javascript\" src=\"..");
		sb.append(PayrollConst.JS_FINANCE);
		sb.append("\"></script>");
	}
	
	/**
	 * JavaScript設定
	 * @param cls 情報取得クラス
	 * @throws IllegalAccessException
	 */
	@Override
	public String getDirectJs(Class<?> cls) throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDirectJs(cls));
		sb.append("var MODE = ");
		sb.append(mode);
		sb.append(";");
		// 銀行名JS設定
		setFinanceData(sb);

		return sb.toString();
	}
	
	/**
	 * フィールド設定(DTOに対するVO情報のセット)
	 * @param dto 設定対象DTO
	 */
	public void setDtoFields(CmCoBankDto dto) {
		dto.setCoBankCode(txtCoBankCode);
		dto.setBankCode(txtBankCode);
		dto.setBranchCode(txtBranchCode);
		dto.setAccountType(pltAccountType);
		dto.setAccountNo(txtAccountNo);
		dto.setDeleteFlag(Integer.parseInt(pltDelete));
	}
	
	/**
	 * 初期化処理
	 */
	public void setInitFields() {
		txtCoBankCode  = "";
		txtBankCode    = "";
		txtBranchCode  = "";
		pltAccountType = "";
		txtAccountNo   = "";
		pltDelete      = "";
		lblBankName    = "";
		lblBranchName  = "";
	}

	// アクセサメソッド
	public void setTxtCoBankCode(String txtCoBankCode   ) { this.txtCoBankCode = txtCoBankCode   ; }
	public void setTxtBankCode(String txtBankCode       ) { this.txtBankCode = txtBankCode       ; }
	public void setTxtBranchCode(String txtBranchCode   ) { this.txtBranchCode = txtBranchCode   ; }
	public void setPltAccountType(String pltAccountType ) { this.pltAccountType = pltAccountType ; }
	public void setTxtAccountNo(String txtAccountNo     ) { this.txtAccountNo = txtAccountNo     ; }
	public void setPltDelete(String pltDelete           ) { this.pltDelete = pltDelete           ; }
	public void setMode(int mode                       ) {this.mode = mode                      ; }
	public void setLblBankName  (String lblBankName     ) { this.lblBankName   = lblBankName     ; }
	public void setLblBranchName(String lblBranchName   ) { this.lblBranchName = lblBranchName   ; }

	public String[] getEscAryCoBankCode  () { return aryCoBankCode.clone()  ; }
	public String[] getEscAryBankCode    () { return aryBankCode.clone()    ; }
	public String[] getEscAryBranchCode  () { return aryBranchCode.clone()  ; }
	public String[] getEscAryAccountType () { return aryAccountType.clone() ; }
	public String[] getEscAryAccountNo   () { return aryAccountNo.clone()   ; }
	public String[] getEscAryDelete      () { return aryDelete.clone()      ; }

	public String getTxtCoBankCode    () { return txtCoBankCode             ; }
	public String getTxtBankCode      () { return txtBankCode               ; }
	public String getTxtBranchCode    () { return txtBranchCode             ; }
	public String getTxtAccountNo     () { return txtAccountNo              ; }
	public String getPltAccountType   () { return pltAccountType            ; }
	public String getPltDelete        () { return pltDelete                 ; }
	public String getEscTxtCoBankCode () { return escapeHTML(txtCoBankCode) ; }
	public String getEscTxtBankCode   () { return escapeHTML(txtBankCode)   ; }
	public String getEscTxtBranchCode () { return escapeHTML(txtBranchCode) ; }
	public String getEscTxtAccountNo  () { return escapeHTML(txtAccountNo)  ; }
	public int	   getMode             () { return mode;                       }
	public void setPltDelete      (int pltDelete) { this.pltDelete = String.valueOf(pltDelete); }
	
	public void setTxtBankName(String txtBankName       ) { this.txtBankName = txtBankName       ; }
	public void setTxtBranchName(String txtBranchName   ) { this.txtBranchName = txtBranchName   ; }
	public String[] getEscAryBankName    () { return aryBankName.clone()    ; }
	public String[] getEscAryBranchName  () { return aryBranchName.clone()  ; }
	public String getTxtBankName      () { return txtBankName               ; }
	public String getTxtBranchName    () { return txtBranchName             ; }
	public String getEscTxtBankName   () { return escapeHTML(txtBankName)   ; }
	public String getEscTxtBranchName () { return escapeHTML(txtBranchName) ; }
	public String getEscLblBankName   () { return escapeHTML(lblBankName)   ; }
	public String getEscLblBranchName () { return escapeHTML(lblBranchName) ; }

}
