1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package nl.openedge.baritus.validation.impl;
32
33 import nl.openedge.baritus.FormBeanContext;
34 import nl.openedge.baritus.validation.AbstractFormValidator;
35 import nl.openedge.baritus.validation.ValidationActivationRule;
36
37 import ognl.Ognl;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.infohazard.maverick.flow.ControllerContext;
42
43 /***
44 * Checks whether the form contains a non null property with the name of property
45 * propertyName.
46 *
47 * @author Eelco Hillenius
48 */
49 public class PropertyNotNullFormValidator extends AbstractFormValidator
50 {
51
52 private String propertyName;
53
54 private String errorMessageKey = "object.not.found";
55
56 private Log log = LogFactory.getLog(PropertyNotNullFormValidator.class);
57
58 /***
59 * construct
60 */
61 public PropertyNotNullFormValidator()
62 {
63
64 }
65
66 /***
67 * construct with errorMessageKey
68 * @param errorMessageKey
69 */
70 public PropertyNotNullFormValidator(String errorMessageKey)
71 {
72 setErrorMessageKey(errorMessageKey);
73 }
74
75 /***
76 * construct with rule
77 * @param rule validation rule
78 */
79 public PropertyNotNullFormValidator(ValidationActivationRule rule)
80 {
81 setValidationRule(rule);
82 }
83
84 /***
85 * construct with error message and rule
86 * @param errorMessageKey message key
87 * @param rule validation rule
88 */
89 public PropertyNotNullFormValidator(
90 String errorMessageKey,
91 ValidationActivationRule rule)
92 {
93 setErrorMessageKey(errorMessageKey);
94 setValidationRule(rule);
95 }
96
97 /***
98 * construct with property name and errorMessageKey
99 * @param propertyName name of property
100 * @param errorMessageKey message key
101 */
102 public PropertyNotNullFormValidator(String propertyName, String errorMessageKey)
103 {
104 setPropertyName(propertyName);
105 setErrorMessageKey(errorMessageKey);
106 }
107
108 /***
109 * check whether the form contains a not null property with the name of property
110 * propertyName
111 * @return boolean true if property in form with name of property propertyName exists
112 * and is not null, false otherwise
113 * @see nl.openedge.baritus.validation.FormValidator#isValid(org.infohazard.maverick.flow.ControllerContext, nl.openedge.baritus.FormBeanContext)
114 */
115 public boolean isValid(ControllerContext cctx, FormBeanContext formBeanContext)
116 {
117 Object bean = formBeanContext.getBean();
118 boolean valid = false;
119 try
120 {
121 Object property = Ognl.getValue(propertyName, bean);
122 valid = (property != null);
123 }
124 catch (Exception e)
125 {
126 log.error(e.getMessage(), e);
127 }
128
129 if(!valid)
130 {
131 setErrorMessage(formBeanContext, propertyName, getErrorMessageKey(),
132 new Object[]{getFieldName(formBeanContext, propertyName)});
133 }
134
135 return valid;
136
137 }
138
139 /***
140 * get the name of the property
141 * @return String name of property
142 */
143 public String getPropertyName()
144 {
145 return propertyName;
146 }
147
148 /***
149 * set the name of the property
150 * @param string name of the property
151 */
152 public void setPropertyName(String string)
153 {
154 propertyName = string;
155 }
156
157 /***
158 * Get key of error message.
159 * @return String key of error message
160 */
161 public String getErrorMessageKey()
162 {
163 return errorMessageKey;
164 }
165
166 /***
167 * Set key of error message.
168 * @param string key of error message
169 */
170 public void setErrorMessageKey(String string)
171 {
172 errorMessageKey = string;
173 }
174
175 }