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 |
1 |
super(); |
49 |
1 |
} |
50 |
|
|
51 |
|
public Identity(String initializer) |
52 |
|
{ |
53 |
4 |
super(initializer); |
54 |
4 |
} |
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 |
3 |
IFormComponent referent = (IFormComponent) field.getContainer().getComponent(_fieldName); |
68 |
3 |
Object referentValue = referent.getBinding("value").getObject(); |
69 |
|
|
70 |
|
|
71 |
3 |
boolean notEq = notEqual(referentValue, object); |
72 |
|
|
73 |
3 |
if (_matchType == MATCH ? notEq : !notEq) |
74 |
2 |
throw new ValidatorException(buildIdentityMessage(messages, field, referent), |
75 |
|
ValidationConstraint.CONSISTENCY); |
76 |
1 |
} |
77 |
|
|
78 |
|
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
79 |
|
FormComponentContributorContext context, IFormComponent field) |
80 |
|
{ |
81 |
1 |
if (field.isDisabled()) |
82 |
0 |
return; |
83 |
|
|
84 |
1 |
IFormComponent referent = (IFormComponent) field.getContainer().getComponent(_fieldName); |
85 |
|
|
86 |
1 |
JSONObject profile = context.getProfile(); |
87 |
|
|
88 |
1 |
if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
89 |
1 |
profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
90 |
|
} |
91 |
1 |
JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
92 |
|
|
93 |
1 |
String func = (_matchType == MATCH) ? |
94 |
|
"tapestry.form.validation.isEqual" : |
95 |
|
"tapestry.form.validation.isNotEqual"; |
96 |
|
|
97 |
1 |
accumulateProperty(cons, field.getClientId(), |
98 |
|
new JSONLiteral("[" + func + ",\"" |
99 |
|
+ referent.getClientId() + "\"]")); |
100 |
|
|
101 |
|
|
102 |
1 |
accumulateProfileProperty(field, profile, |
103 |
|
ValidationConstants.CONSTRAINTS, buildIdentityMessage(context, field, referent)); |
104 |
1 |
} |
105 |
|
|
106 |
|
public String getMatch() |
107 |
|
{ |
108 |
0 |
return _fieldName; |
109 |
|
} |
110 |
|
|
111 |
|
public void setMatch(String field) |
112 |
|
{ |
113 |
3 |
_fieldName = field; |
114 |
3 |
_matchType = MATCH; |
115 |
|
|
116 |
3 |
} |
117 |
|
|
118 |
|
public String getDiffer() |
119 |
|
{ |
120 |
0 |
return _fieldName; |
121 |
|
} |
122 |
|
|
123 |
|
public void setDiffer(String field) |
124 |
|
{ |
125 |
1 |
_fieldName = field; |
126 |
1 |
_matchType = DIFFER; |
127 |
1 |
} |
128 |
|
|
129 |
|
protected String buildIdentityMessage(ValidationMessages messages, IFormComponent field, IFormComponent referent) |
130 |
|
{ |
131 |
3 |
Object[] parameters = new Object[]{ |
132 |
|
field.getDisplayName(), new Integer(_matchType), referent.getDisplayName() |
133 |
|
}; |
134 |
|
|
135 |
3 |
return messages.formatValidationMessage(getMessage(), |
136 |
|
ValidationStrings.INVALID_FIELD_EQUALITY, parameters); |
137 |
|
} |
138 |
|
|
139 |
|
private boolean notEqual(Object o1, Object o2) |
140 |
|
{ |
141 |
3 |
if (o1 == null && o2 == null) |
142 |
0 |
return false; |
143 |
3 |
if (o1 == null || o2 == null) |
144 |
0 |
return true; |
145 |
|
|
146 |
3 |
return !o1.equals(o2); |
147 |
|
} |
148 |
|
|
149 |
|
} |