01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: ValidationRuleSameAs.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.site;
09:
10: import com.uwyn.rife.tools.BeanUtils;
11: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
12:
13: import static com.uwyn.rife.site.ValidityChecks.checkEqual;
14:
15: public class ValidationRuleSameAs extends PropertyValidationRule {
16: private String mReference = null;
17:
18: public ValidationRuleSameAs(String propertyName, String reference) {
19: super (propertyName);
20:
21: mReference = reference;
22: }
23:
24: public boolean validate() {
25: Object value;
26: Object other;
27: try {
28: value = BeanUtils.getPropertyValue(getBean(),
29: getPropertyName());
30: other = BeanUtils.getPropertyValue(getBean(), mReference);
31: } catch (BeanUtilsException e) {
32: // an error occurred when obtaining the value of the property
33: // just consider it valid to skip over it
34: return true;
35: }
36:
37: return checkEqual(value, other);
38: }
39:
40: public ValidationError getError() {
41: return new ValidationError.NOTSAMEAS(getPropertyName());
42: }
43: }
|