1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary.validation;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
20 import org.kuali.rice.krad.datadictionary.validation.capability.CaseConstrainable;
21 import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
22 import org.kuali.rice.krad.datadictionary.validation.capability.MustOccurConstrainable;
23 import org.kuali.rice.krad.datadictionary.validation.capability.PrerequisiteConstrainable;
24 import org.kuali.rice.krad.datadictionary.validation.capability.SimpleConstrainable;
25 import org.kuali.rice.krad.datadictionary.validation.capability.ValidCharactersConstrainable;
26 import org.kuali.rice.krad.datadictionary.validation.constraint.CaseConstraint;
27 import org.kuali.rice.krad.datadictionary.validation.constraint.MustOccurConstraint;
28 import org.kuali.rice.krad.datadictionary.validation.constraint.PrerequisiteConstraint;
29 import org.kuali.rice.krad.datadictionary.validation.constraint.SimpleConstraint;
30 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
31 import org.kuali.rice.krad.uif.UifConstants;
32 import org.kuali.rice.krad.uif.UifPropertyPaths;
33 import org.kuali.rice.krad.uif.lifecycle.ViewPostMetadata;
34 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
35 import org.kuali.rice.krad.uif.view.ViewModel;
36
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41
42
43
44
45
46
47 public class ViewAttributeValueReader extends BaseAttributeValueReader {
48 private ViewModel form;
49
50 private List<Constrainable> inputFields = new ArrayList<Constrainable>();
51 private Map<String, InputFieldConstrainableInfo> inputFieldMap = new HashMap<String, InputFieldConstrainableInfo>();
52
53
54
55
56
57
58
59 public ViewAttributeValueReader(ViewModel form) {
60 this.form = form;
61
62 ViewPostMetadata viewPostMetadata = form.getViewPostMetadata();
63
64
65 for (String id : viewPostMetadata.getInputFieldIds()) {
66 InputFieldConstrainableInfo info = new InputFieldConstrainableInfo();
67
68 Object label = viewPostMetadata.getComponentPostData(id, UifConstants.PostMetadata.LABEL);
69 if (label != null) {
70 info.setLabel((String) label);
71 }
72
73 Object name = viewPostMetadata.getComponentPostData(id, UifConstants.PostMetadata.PATH);
74 if (name != null) {
75 info.setName((String) name);
76 }
77
78 Object validCharactersConstraint = viewPostMetadata.getComponentPostData(id,
79 UifConstants.PostMetadata.VALID_CHARACTER_CONSTRAINT);
80 if (validCharactersConstraint != null) {
81 info.setValidCharactersConstraint((ValidCharactersConstraint) validCharactersConstraint);
82 }
83
84 Object caseConstraint = viewPostMetadata.getComponentPostData(id,
85 UifConstants.PostMetadata.CASE_CONSTRAINT);
86 if (caseConstraint != null) {
87 info.setCaseConstraint((CaseConstraint) caseConstraint);
88 }
89
90 Object prerequisiteConstraints = viewPostMetadata.getComponentPostData(id,
91 UifConstants.PostMetadata.PREREQ_CONSTSTRAINTS);
92 if (prerequisiteConstraints != null) {
93 info.setPrerequisiteConstraints((List<PrerequisiteConstraint>) prerequisiteConstraints);
94 }
95
96 Object mustOccurConstraints = viewPostMetadata.getComponentPostData(id,
97 UifConstants.PostMetadata.MUST_OCCUR_CONSTRAINTS);
98 if (mustOccurConstraints != null) {
99 info.setMustOccurConstraints((List<MustOccurConstraint>) mustOccurConstraints);
100 }
101
102 Object simpleConstraint = viewPostMetadata.getComponentPostData(id,
103 UifConstants.PostMetadata.SIMPLE_CONSTRAINT);
104 if (simpleConstraint != null) {
105 info.setSimpleConstraint((SimpleConstraint) simpleConstraint);
106 }
107
108 inputFields.add(info);
109 inputFieldMap.put(info.getName(), info);
110 }
111 }
112
113
114
115
116 @Override
117 public Constrainable getDefinition(String attributeName) {
118 InputFieldConstrainableInfo field = inputFieldMap.get(attributeName);
119 if (field != null) {
120 return field;
121 } else {
122 return null;
123 }
124 }
125
126
127
128
129
130
131 @Override
132 public List<Constrainable> getDefinitions() {
133 return inputFields;
134 }
135
136
137
138
139
140
141
142 @Override
143 public String getLabel(String attributeName) {
144 InputFieldConstrainableInfo field = inputFieldMap.get(attributeName);
145 if (field != null) {
146 return field.getLabel();
147 } else {
148 return "";
149 }
150 }
151
152
153
154
155
156
157 @Override
158 public Object getObject() {
159 return form;
160 }
161
162
163
164
165
166
167 @Override
168 public Constrainable getEntry() {
169 return null;
170 }
171
172
173
174
175
176
177 @Override
178 public String getPath() {
179 return this.attributeName;
180 }
181
182
183
184
185
186
187
188 @Override
189 public Class<?> getType(String attributeName) {
190 Object fieldValue = ObjectPropertyUtils.getPropertyValue(form, attributeName);
191 return fieldValue.getClass();
192 }
193
194
195
196
197
198
199
200 @Override
201 public boolean isReadable() {
202 if (attributeName != null && attributeName.contains(UifPropertyPaths.NEW_COLLECTION_LINES)) {
203 return false;
204 }
205 return true;
206 }
207
208
209
210
211
212
213
214
215 @Override
216 public <X> X getValue() throws AttributeValidationException {
217 X fieldValue = null;
218 if (StringUtils.isNotBlank(this.attributeName)) {
219 fieldValue = ObjectPropertyUtils.<X>getPropertyValue(form, this.attributeName);
220 }
221 return fieldValue;
222 }
223
224
225
226
227
228
229
230
231
232 @Override
233 public <X> X getValue(String attributeName) throws AttributeValidationException {
234 X fieldValue = null;
235 if (StringUtils.isNotBlank(attributeName)) {
236 fieldValue = ObjectPropertyUtils.<X>getPropertyValue(form, this.attributeName);
237 }
238 return fieldValue;
239 }
240
241
242
243
244
245
246 @Override
247 public AttributeValueReader clone() {
248 ViewAttributeValueReader clone = new ViewAttributeValueReader(form);
249 clone.setAttributeName(this.attributeName);
250 return clone;
251 }
252
253
254
255
256
257
258
259 public class InputFieldConstrainableInfo implements SimpleConstrainable, CaseConstrainable, PrerequisiteConstrainable, MustOccurConstrainable, ValidCharactersConstrainable {
260
261 private String label;
262 private String name;
263 private ValidCharactersConstraint validCharactersConstraint;
264 private CaseConstraint caseConstraint;
265 private List<PrerequisiteConstraint> prerequisiteConstraints;
266 private List<MustOccurConstraint> mustOccurConstraints;
267 private SimpleConstraint simpleConstraint;
268
269
270
271
272
273
274 public String getLabel() {
275 return label;
276 }
277
278
279
280
281 public void setLabel(String label) {
282 this.label = label;
283 }
284
285
286
287
288 @Override
289 public String getName() {
290 return name;
291 }
292
293
294
295
296 public void setName(String name) {
297 this.name = name;
298 }
299
300
301
302
303 @Override
304 public ValidCharactersConstraint getValidCharactersConstraint() {
305 return validCharactersConstraint;
306 }
307
308
309
310
311 public void setValidCharactersConstraint(ValidCharactersConstraint validCharactersConstraint) {
312 this.validCharactersConstraint = validCharactersConstraint;
313 }
314
315
316
317
318 @Override
319 public CaseConstraint getCaseConstraint() {
320 return caseConstraint;
321 }
322
323
324
325
326 public void setCaseConstraint(CaseConstraint caseConstraint) {
327 this.caseConstraint = caseConstraint;
328 }
329
330
331
332
333 @Override
334 public List<PrerequisiteConstraint> getPrerequisiteConstraints() {
335 return prerequisiteConstraints;
336 }
337
338
339
340
341 public void setPrerequisiteConstraints(List<PrerequisiteConstraint> prerequisiteConstraints) {
342 this.prerequisiteConstraints = prerequisiteConstraints;
343 }
344
345
346
347
348 @Override
349 public List<MustOccurConstraint> getMustOccurConstraints() {
350 return mustOccurConstraints;
351 }
352
353
354
355
356 public void setMustOccurConstraints(List<MustOccurConstraint> mustOccurConstraints) {
357 this.mustOccurConstraints = mustOccurConstraints;
358 }
359
360
361
362
363 @Override
364 public SimpleConstraint getSimpleConstraint() {
365 return simpleConstraint;
366 }
367
368
369
370
371 public void setSimpleConstraint(SimpleConstraint simpleConstraint) {
372 this.simpleConstraint = simpleConstraint;
373 }
374 }
375
376 }