KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > config > OverrideValidator


1 package com.inversoft.verge.mvc.config;
2
3
4 import java.util.Map JavaDoc;
5
6 import com.inversoft.error.ErrorRegistry;
7 import com.inversoft.error.PropertyError;
8 import com.inversoft.verge.mvc.controller.Action;
9 import com.inversoft.verge.mvc.validator.Validator;
10 import com.inversoft.verge.util.WebBeanProperty;
11
12
13 /**
14  * <p>
15  * Because the Verge Framework does type conversion using
16  * the Java type of the properties (i.e. int getInt()), we
17  * needed a method to validate properties based on these
18  * types from the configuration file. This class instance
19  * is a Validator, but is automatically constructed and
20  * configured by the Verge framework and allows us to auto-
21  * validate properties based on types.
22  * </p>
23  *
24  * @author Brian Pontarelli
25  */

26 public class OverrideValidator implements Validator {
27
28     private PropertyConfig config;
29
30
31     /**
32      * Constructs a new <code>OverrideValidator</code> that ensures what the user
33      * typed in was of the correct type.
34      *
35      * @param config The PropertyConfig from the configuration file. This
36      * contains the type and all the error message information
37      */

38     public OverrideValidator(PropertyConfig config) {
39         this.config = config;
40     }
41
42
43     /**
44      * This method is not supported for this class and is a no-op. This class only
45      * handles bad type conversions.
46      *
47      * @param modelObjects Not used
48      * @param action Not used
49      * @return Always true
50      */

51     public boolean validate(Map JavaDoc modelObjects, Action action) {
52         // no-op
53
return true;
54     }
55
56     /**
57      * Using the initial configuration for this class, this does the validation
58      * of the property against a known primitive type. This allows us to verify
59      * that what the user typed in was of that type and also, automatically add
60      * error messages based on the configuraiton.
61      *
62      * @param model The model object that had conversion problems
63      * @param property The property that had conversion problems
64      * @param action The action that the user took (not used).
65      */

66     public void handleConversion(Object JavaDoc model, WebBeanProperty property,
67             Action action) {
68
69         Object JavaDoc[] params = null;
70         if (config.getParameters() != null) {
71             params = config.getParameters().values().toArray();
72         }
73
74         // Favor the bundle over the message
75
PropertyError error = null;
76         if (config.getKey() != null) {
77             error = ErrorRegistry.getPropertyError(config.getBundleName(),
78                 config.getKey(), null, property.getFullName(), null, params);
79         } else {
80             error = new PropertyError(property.getFullName(), config.getErrorMsg(),
81                 params);
82         }
83
84         action.getRequestContext().addError(error);
85     }
86 }
Popular Tags