1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.form.validator; |
16 | |
|
17 | |
import org.apache.tapestry.IMarkupWriter; |
18 | |
import org.apache.tapestry.IRequestCycle; |
19 | |
import org.apache.tapestry.form.FormComponentContributorContext; |
20 | |
import org.apache.tapestry.form.IFormComponent; |
21 | |
import org.apache.tapestry.form.ValidationMessages; |
22 | |
import org.apache.tapestry.json.JSONLiteral; |
23 | |
import org.apache.tapestry.json.JSONObject; |
24 | |
import org.apache.tapestry.valid.ValidationConstants; |
25 | |
import org.apache.tapestry.valid.ValidationConstraint; |
26 | |
import org.apache.tapestry.valid.ValidationStrings; |
27 | |
import org.apache.tapestry.valid.ValidatorException; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class Identity extends BaseValidator { |
39 | |
|
40 | |
private String _fieldName; |
41 | |
private int _matchType; |
42 | |
|
43 | |
private static final int DIFFER = 0; |
44 | |
private static final int MATCH = 1; |
45 | |
|
46 | |
public Identity() |
47 | |
{ |
48 | 0 | super(); |
49 | 0 | } |
50 | |
|
51 | |
public Identity(String initializer) |
52 | |
{ |
53 | 0 | super(initializer); |
54 | 0 | } |
55 | |
|
56 | |
public String toString(IFormComponent field, Object value) |
57 | |
{ |
58 | 0 | if (value == null) |
59 | 0 | return null; |
60 | |
|
61 | 0 | return value.toString(); |
62 | |
} |
63 | |
|
64 | |
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
65 | |
throws ValidatorException |
66 | |
{ |
67 | 0 | IFormComponent referent = (IFormComponent) field.getContainer().getComponent(_fieldName); |
68 | 0 | Object referentValue = referent.getBinding("value").getObject(); |
69 | |
|
70 | |
|
71 | 0 | boolean notEq = notEqual(referentValue, object); |
72 | |
|
73 | 0 | if (_matchType == MATCH ? notEq : !notEq) |
74 | 0 | throw new ValidatorException(buildIdentityMessage(messages, field, referent), |
75 | |
ValidationConstraint.CONSISTENCY); |
76 | 0 | } |
77 | |
|
78 | |
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
79 | |
FormComponentContributorContext context, IFormComponent field) |
80 | |
{ |
81 | 0 | if (field.isDisabled()) |
82 | 0 | return; |
83 | |
|
84 | 0 | IFormComponent referent = (IFormComponent) field.getContainer().getComponent(_fieldName); |
85 | |
|
86 | 0 | JSONObject profile = context.getProfile(); |
87 | |
|
88 | 0 | if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
89 | 0 | profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
90 | |
} |
91 | 0 | JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
92 | |
|
93 | 0 | String func = (_matchType == MATCH) ? |
94 | |
"tapestry.form.validation.isEqual" : |
95 | |
"tapestry.form.validation.isNotEqual"; |
96 | |
|
97 | 0 | accumulateProperty(cons, field.getClientId(), |
98 | |
new JSONLiteral("[" + func + ",\"" |
99 | |
+ referent.getClientId() + "\"]")); |
100 | |
|
101 | |
|
102 | 0 | accumulateProfileProperty(field, profile, |
103 | |
ValidationConstants.CONSTRAINTS, buildIdentityMessage(context, field, referent)); |
104 | 0 | } |
105 | |
|
106 | |
public String getMatch() |
107 | |
{ |
108 | 0 | return _fieldName; |
109 | |
} |
110 | |
|
111 | |
public void setMatch(String field) |
112 | |
{ |
113 | 0 | _fieldName = field; |
114 | 0 | _matchType = MATCH; |
115 | |
|
116 | 0 | } |
117 | |
|
118 | |
public String getDiffer() |
119 | |
{ |
120 | 0 | return _fieldName; |
121 | |
} |
122 | |
|
123 | |
public void setDiffer(String field) |
124 | |
{ |
125 | 0 | _fieldName = field; |
126 | 0 | _matchType = DIFFER; |
127 | 0 | } |
128 | |
|
129 | |
protected String buildIdentityMessage(ValidationMessages messages, IFormComponent field, IFormComponent referent) |
130 | |
{ |
131 | 0 | Object[] parameters = new Object[]{ |
132 | |
field.getDisplayName(), new Integer(_matchType), referent.getDisplayName() |
133 | |
}; |
134 | |
|
135 | 0 | return messages.formatValidationMessage(getMessage(), |
136 | |
ValidationStrings.INVALID_FIELD_EQUALITY, parameters); |
137 | |
} |
138 | |
|
139 | |
private boolean notEqual(Object o1, Object o2) |
140 | |
{ |
141 | 0 | if (o1 == null && o2 == null) |
142 | 0 | return false; |
143 | 0 | if (o1 == null || o2 == null) |
144 | 0 | return true; |
145 | |
|
146 | 0 | return !o1.equals(o2); |
147 | |
} |
148 | |
|
149 | |
} |