package shms.validation;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator;

public class CustomStringLengthFieldValidator extends StringLengthFieldValidator {

	/**
	 * validation check¸¦ ÇÒ¶§ 
	 * hashMap nameÀÌ view(jsp)¿¡¼­ ÀÔ·ÂµÇ¾ú´ÂÁö¸¦ ¾Ë¼ö ÀÖ°Ô ÇÏ±â À§ÇØ¼­´Â
	 * stack¿¡ getter setter·Î Á¤ÀÇµÈ hashMap(¿©±â¿¡¼­´Â reqMapÀÌ¸§À» ¾´´Ù)ÀÌ¸§À» ÁöÁ¤ÇÏ°í
	 * hashMap¿¡ ÀúÀåµÈ °ªÀ» ¸®ÅÏÇÑ´Ù.
	 */
	@Override
	protected Object getFieldValue(String name, Object object) 
	throws ValidationException{
	    ValueStack stack = ActionContext.getContext().getValueStack();

	    boolean pop = false;

	    if (!(stack.getRoot().contains(object))) {
	      stack.push(object);
	      pop = true;
	    }
	    
	    Object retVal = null;
		// HashMap reqMapÀÇ °ªÀ» stack¿¡ ³Ö¾îÁØ´Ù.
		Object obj = (Map)stack.findValue("reqMap");
		
		// HashMap reqMapÀÌ nullÀÌ ¾Æ´Ò¶§
		if(obj != null){
			Map m = (Map)obj;
			// stack¿¡ ÀÖ´Â HashMap reqMap°ªÀ» nameÀ¸·Î °¡Á®¿Í¼­ valueStack¿¡ ¿Ã·ÁÁØ´Ù.
			retVal = m.get(name);
		}
		// HashMap reqMapÀÌ nullÀÏ¶§
		else{
			// getter setterÀ» »ç¿ëÇÑ nameÀÇ °ªÀ» valueStack¿¡ ¿Ã·ÁÁØ´Ù.
			retVal = stack.findValue(name);
		}
		
		if (pop) {
			stack.pop();
		}
	    
	    return retVal;
	}
	
}

