package jp.ossc.petstore.util.validator;


import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;

/**
 * 配列用 入力チェッククラス
 * @author ossc
 * @version $Revision: 1.2 $
 */
@SuppressWarnings("serial")
public class ArrayValidator extends BaseValidator{
	/**
	 * validateIntRange配列対応版<br>
	 * 配列の要素分validateIntRangeを実行する。
	 * @param bean      The bean validation is being performed on.
	 * @param va        The ValidatorAction that is currently being performed.
	 * @param field     The Field object associated with the current field being validated.
	 * @param errors    The ActionMessages object to add errors to if any validation errors occur.
	 * @param validator The Validator instance, used to access other field values.
	 * @param request   Current request object.
	 * @return True if in range, false otherwise.
	 */
	public boolean validateIntRangeArray(Object bean, ValidatorAction va, 
			Field field, ActionMessages errors,	Validator validator, HttpServletRequest request){
		boolean chkFlg = true;
		
		Object temp = ((DynaActionForm) bean).get(field.getProperty());
		String[] value2 = isString(temp) ? new String[] {(String) temp } : (String[]) temp;
		for(int i=0; i<value2.length; i++){
			bean = value2[i];
			chkFlg = super.validateIntRange(bean, va, field, errors, validator, request);
			if(chkFlg == false){
				return false;
			}
		}
		return true;
	}
	
	/**
	 * validateRequired配列対応版<br>
	 * 配列の要素分validateRequiredを実行する。
	 * @param bean      The bean validation is being performed on.
	 * @param va        The ValidatorAction that is currently being performed.
	 * @param field     The Field object associated with the current field being validated.
	 * @param errors    The ActionMessages object to add errors to if any validation errors occur.
	 * @param validator The Validator instance, used to access other field values.
	 * @param request   Current request object.
	 * @return true if meets stated requirements, false otherwise.
	 */
	public boolean validateRequiredArray(Object bean, ValidatorAction va, 
			Field field, ActionMessages errors,	Validator validator, HttpServletRequest request){
		boolean chkFlg = true;
		
		Object temp = ((DynaActionForm) bean).get(field.getProperty());
		String[] value2 = isString(temp) ? new String[] {(String) temp } : (String[]) temp;
		for(int i=0; i<value2.length; i++){
			bean = value2[i];
			chkFlg = super.validateRequired(bean, va, field, errors, validator, request);
			if(chkFlg == false) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * validateInteger配列対応版<br>
	 * 配列の要素分validateIntegerを実行する。
	 * @param bean      The bean validation is being performed on.
	 * @param va        The ValidatorAction that is currently being performed.
	 * @param field     The Field object associated with the current field being validated.
	 * @param errors    The ActionMessages object to add errors to if any validation errors occur.
	 * @param validator The Validator instance, used to access other field values.
	 * @param request   Current request object.
	 * @return true if valid, false otherwise.
	 */
	public Object validateIntegerArray(Object bean, ValidatorAction va, Field field, 
					ActionMessages errors, Validator validator, HttpServletRequest request){
		Object chkFlg = Boolean.TRUE;
		
		Object temp = ((DynaActionForm) bean).get(field.getProperty());
		String[] value2 = isString(temp) ? new String[] {(String) temp } : (String[]) temp;
		for(int i=0; i<value2.length; i++){
			bean = value2[i];
			chkFlg = super.validateInteger(bean, va, field, errors, validator, request);
			if(chkFlg.equals(false)){
				return  Boolean.FALSE;
			}
		}
		return Boolean.TRUE;
	}

}
