1 7 package com.inversoft.verge.mvc.validator; 8 9 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 import com.inversoft.beans.BeanException; 14 import com.inversoft.error.PropertyError; 15 import com.inversoft.util.ReflectionException; 16 import com.inversoft.util.typeconverter.TypeConversionException; 17 import com.inversoft.util.typevalidator.TypeValidator; 18 import com.inversoft.util.typevalidator.TypeValidatorRegistry; 19 import com.inversoft.verge.mvc.MVCException; 20 import com.inversoft.verge.mvc.MVCRequest; 21 import com.inversoft.verge.mvc.config.BaseConfigStruct; 22 import com.inversoft.verge.mvc.config.BaseValidatorConfig; 23 import com.inversoft.verge.mvc.config.PropertyConfig; 24 import com.inversoft.verge.mvc.controller.Action; 25 import com.inversoft.verge.util.WebBean; 26 import com.inversoft.verge.util.WebBeanProperty; 27 28 29 38 public abstract class AbstractValidatorHandler implements ValidatorHandler { 39 40 50 protected boolean validate(MVCRequest mvcRequest, BaseConfigStruct struct) 51 throws MVCException { 52 Action action = mvcRequest.getAction(); 53 if (action == null) { 54 action = new Action(null, mvcRequest.getRequest(), 55 mvcRequest.getResponse(), mvcRequest.getRequestContext()); 56 } 57 58 List validators = struct.baseFormConfig.getValidators(); 59 if (validators == null) { 60 return true; 61 } 62 63 Iterator iter = validators.iterator(); 64 BaseValidatorConfig validatorConfig; 65 Validator validator; 66 boolean valid = true; 67 68 while (iter.hasNext()) { 69 validatorConfig = (BaseValidatorConfig) iter.next(); 70 71 if (validatorConfig.isValidating()) { 73 try { 74 validator = validatorConfig.newValidator(); 75 } catch (ReflectionException re) { 76 throw new MVCException(re); 77 } 78 79 valid &= validator.validate(mvcRequest.getModelObjects(), action); 80 } 81 82 if (!validatorConfig.hasProperties()) { 83 continue; 84 } 85 86 Iterator propIter = validatorConfig.getProperties().iterator(); 88 PropertyConfig propConfig; 89 Object value; 90 WebBean webBean; 91 while(propIter.hasNext()) { 92 propConfig = (PropertyConfig) propIter.next(); 93 94 webBean = struct.baseFormConfig.getFormBean(propConfig.getID()); 95 Object formBean = null; 96 try { 97 formBean = webBean.getInstance(mvcRequest.getRequest()); 98 } catch (BeanException be) { 99 throw new MVCException(be); 100 } 101 102 try { 103 value = webBean.getPropertyValue(propConfig.getProperty(), 104 formBean); 105 } catch (BeanException be) { 106 throw new MVCException(be); 107 } 108 109 String type = propConfig.getType(); 110 TypeValidator typeValidator = 111 TypeValidatorRegistry.lookupTypeValidator(type); 112 if (typeValidator == null) { 113 throw new MVCException("During validation of form named: " + 114 struct.baseFormConfig.getName() + ", unable to locate " + 115 "type validator named: " + type); 116 } 117 118 PropertyError error = null; 119 if (propConfig.getKey() != null) { 120 error = typeValidator.validate(propConfig.getName(), value, 121 propConfig.getParameters(), propConfig.getBundleName(), 122 propConfig.getKey(), null, new Object []{value}, null); 123 } else { 124 error = typeValidator.validate(propConfig.getName(), value, 125 propConfig.getParameters(), propConfig.getErrorMsg(), 126 new Object []{value}); 127 } 128 129 if (error != null) { 130 mvcRequest.getRequestContext().addError(error); 131 valid &= false; 132 } 133 } 134 135 if (!valid && validatorConfig.getFailureDefinition() != null && 137 struct.failedValidatorConfig == null) { 138 struct.failedValidatorConfig = validatorConfig; 139 } 140 } 141 142 return valid; 143 } 144 145 146 150 public void handle(MVCRequest mvcRequest, TypeConversionException tce, 151 Object model, WebBeanProperty wbp) 152 throws MVCException { 153 Action action = mvcRequest.getAction(); 154 if (action == null) { 155 action = new Action(null, mvcRequest.getRequest(), 156 mvcRequest.getResponse(), mvcRequest.getRequestContext()); 157 } 158 159 Object config = mvcRequest.getConfiguration(); 160 if (config != null && config instanceof BaseConfigStruct) { 161 BaseConfigStruct struct = (BaseConfigStruct) mvcRequest.getConfiguration(); 162 if (!struct.baseFormConfig.isValidating()) { 163 throw new MVCException(tce); 164 } 165 166 ValidatorTools.callHandle(struct, model, wbp, action); 167 } 168 } 169 } | Popular Tags |