KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > validator > AbstractValidatorHandler


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.validator;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
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 /**
30  * <p>
31  * This class contains abstract validation logic. This logic
32  * is currently only useful for the action flow and form
33  * based MVC systems.
34  * </p>
35  *
36  * @author Brian Pontarelli
37  */

38 public abstract class AbstractValidatorHandler implements ValidatorHandler {
39
40     /**
41      * This method is generic between the ActionFlow system and the Form-Based MVC
42      * system for calling validators and properties for validation.
43      *
44      * @param mvcRequest The MVCRequest to get the action from
45      * @param struct The BaseConfigStruct that contains the BaseFormConfig and
46      * can store the failed BaseValidatorConfig if applicable
47      * @return True if validation succeeded, false otherwise
48      * @throws MVCException If something went wrong trying to validate
49      */

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 JavaDoc validators = struct.baseFormConfig.getValidators();
59         if (validators == null) {
60             return true;
61         }
62
63         Iterator JavaDoc 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 the config defined a validator class, call it
72
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             // Handle the auto-validation of properties
87
Iterator JavaDoc propIter = validatorConfig.getProperties().iterator();
88             PropertyConfig propConfig;
89             Object JavaDoc value;
90             WebBean webBean;
91             while(propIter.hasNext()) {
92                 propConfig = (PropertyConfig) propIter.next();
93
94                 webBean = struct.baseFormConfig.getFormBean(propConfig.getID());
95                 Object JavaDoc 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 JavaDoc 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 JavaDoc[]{value}, null);
123                 } else {
124                     error = typeValidator.validate(propConfig.getName(), value,
125                         propConfig.getParameters(), propConfig.getErrorMsg(),
126                         new Object JavaDoc[]{value});
127                 }
128
129                 if (error != null) {
130                     mvcRequest.getRequestContext().addError(error);
131                     valid &= false;
132                 }
133             }
134
135             // Send the correct result to the controller system
136
if (!valid && validatorConfig.getFailureDefinition() != null &&
137                     struct.failedValidatorConfig == null) {
138                 struct.failedValidatorConfig = validatorConfig;
139             }
140         }
141
142         return valid;
143     }
144
145
146     /**
147      * This method does the work of handling the action flow MVC conversion error
148      * exceptions that are thrown during model handling.
149      */

150     public void handle(MVCRequest mvcRequest, TypeConversionException tce,
151             Object JavaDoc 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 JavaDoc 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