1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jasperreports.jsf.component;
20
21 import javax.el.ELException;
22 import javax.el.ValueExpression;
23 import javax.faces.FacesException;
24 import javax.faces.application.FacesMessage;
25 import javax.faces.component.UIComponentBase;
26 import javax.faces.context.FacesContext;
27 import javax.faces.validator.Validator;
28 import javax.faces.validator.ValidatorException;
29
30 import net.sf.jasperreports.jsf.Constants;
31 import net.sf.jasperreports.jsf.context.JRFacesContext;
32 import net.sf.jasperreports.jsf.convert.SourceConverter;
33 import net.sf.jasperreports.jsf.engine.Source;
34 import net.sf.jasperreports.jsf.validation.MissingAttributeException;
35 import net.sf.jasperreports.jsf.validation.SourceValidatorBase;
36
37 import static net.sf.jasperreports.jsf.util.MessagesFactory.*;
38
39
40
41
42
43
44
45 public class UISource extends UIComponentBase {
46
47
48 public static final String COMPONENT_FAMILY =
49 Constants.PACKAGE_PREFIX + ".Source";
50
51
52 public static final String COMPONENT_TYPE =
53 Constants.PACKAGE_PREFIX + ".Source";
54
55
56
57
58 private String query = null;
59
60
61 private String type = null;
62
63 private boolean typeSet = false;
64
65
66 private boolean valid = true;
67
68
69 private Object value;
70
71 private boolean valueSet = false;
72
73
74 private SourceConverter converter;
75
76
77 private Validator validator;
78
79
80 private Source submittedSource;
81
82
83
84
85 public UISource() {
86 super();
87 setRendererType(null);
88 }
89
90
91
92
93
94
95
96
97 public final String getQuery() {
98 if (query != null) {
99 return query;
100 }
101 final ValueExpression ve = getValueExpression("query");
102 if (ve != null) {
103 try {
104 return (String) ve.getValue(
105 getFacesContext().getELContext());
106 } catch (final ELException e) {
107 throw new FacesException(e);
108 }
109 } else {
110 return query;
111 }
112 }
113
114
115
116
117
118
119 public final void setQuery(final String query) {
120 this.query = query;
121 }
122
123
124
125
126
127
128 public final String getType() {
129 if (typeSet) {
130 return type;
131 }
132 final ValueExpression ve = getValueExpression("type");
133 if (ve != null) {
134 try {
135 return (String) ve.getValue(
136 getFacesContext().getELContext());
137 } catch (final ELException e) {
138 throw new FacesException(e);
139 }
140 } else {
141 return type;
142 }
143 }
144
145
146
147
148
149
150 public final void setType(final String type) {
151 this.type = type;
152 typeSet = true;
153 }
154
155
156
157
158
159
160 public final boolean isValid() {
161 return valid;
162 }
163
164
165
166
167
168
169 protected final void setValid(final boolean valid) {
170 this.valid = valid;
171 }
172
173
174
175
176
177
178 public final Object getValue() {
179 if (valueSet) {
180 return value;
181 }
182 final ValueExpression ve = getValueExpression("value");
183 if (ve != null) {
184 try {
185 return ve.getValue(
186 getFacesContext().getELContext());
187 } catch (final ELException e) {
188 throw new FacesException(e);
189 }
190 } else {
191 return value;
192 }
193 }
194
195
196
197
198
199
200 public final void setValue(final Object value) {
201 this.value = value;
202 valueSet = true;
203 }
204
205
206
207
208
209
210 public final SourceConverter getConverter() {
211 if (converter != null) {
212 return converter;
213 }
214 ValueExpression ve = getValueExpression("converter");
215 if (ve != null) {
216 try {
217 return (SourceConverter) ve.getValue(
218 getFacesContext().getELContext());
219 } catch (ELException e) {
220 throw new FacesException(e);
221 }
222 } else {
223 return converter;
224 }
225 }
226
227
228
229
230
231
232 public final void setConverter(final SourceConverter converter) {
233 this.converter = converter;
234 }
235
236 public Validator getValidator() {
237 if (validator != null) {
238 return validator;
239 }
240 ValueExpression ve = getValueExpression("validator");
241 if (ve != null) {
242 try {
243 return (Validator) ve.getValue(
244 getFacesContext().getELContext());
245 } catch (ELException e) {
246 throw new FacesException(e);
247 }
248 } else {
249 return validator;
250 }
251 }
252
253 public void setValidator(Validator validator) {
254 this.validator = validator;
255 }
256
257 public Source getSubmittedSource() {
258 return submittedSource;
259 }
260
261 public void setSubmittedSource(Source submittedSource) {
262 this.submittedSource = submittedSource;
263 }
264
265
266
267
268
269
270
271
272 @Override
273 public String getFamily() {
274 return COMPONENT_FAMILY;
275 }
276
277 public void resetValue() {
278 setValue(null);
279 setValid(true);
280 setSubmittedSource(null);
281 valueSet = false;
282 }
283
284
285
286
287
288
289
290
291 @Override
292 public void restoreState(final FacesContext context, final Object state) {
293 final Object[] values = (Object[]) state;
294 super.restoreState(context, values[0]);
295 query = (String) values[1];
296 type = (String) values[2];
297 typeSet = ((Boolean) values[3]).booleanValue();
298 converter = (SourceConverter) values[4];
299 validator = (Validator) values[5];
300 valid = ((Boolean) values[6]).booleanValue();
301 }
302
303
304
305
306
307
308
309 @Override
310 public Object saveState(final FacesContext context) {
311 final Object[] values = new Object[7];
312 values[0] = super.saveState(context);
313 values[1] = query;
314 values[2] = type;
315 values[3] = Boolean.valueOf(typeSet);
316 values[4] = converter;
317 values[5] = validator;
318 values[6] = valid;
319 return values;
320 }
321
322 @Override
323 public void processDecodes(FacesContext context) {
324 if (context == null) {
325 throw new IllegalArgumentException();
326 }
327
328 setValid(false);
329 setSubmittedSource(null);
330
331 super.processDecodes(context);
332
333 try {
334 executeValidate(context);
335 decodeValue(context);
336 } catch (RuntimeException e) {
337 context.renderResponse();
338 throw e;
339 }
340 }
341
342 @Override
343 public void processUpdates(FacesContext context) {
344 if (context == null) {
345 throw new IllegalArgumentException();
346 }
347
348 super.processUpdates(context);
349
350 try {
351 updateModel(context);
352 } catch(RuntimeException e) {
353 context.renderResponse();
354 throw e;
355 }
356
357 if (!isValid()) {
358 context.renderResponse();
359 }
360 }
361
362 @Override
363 public void processValidators(FacesContext context) {
364 if (context == null) {
365 throw new IllegalArgumentException();
366 }
367
368 super.processValidators(context);
369 executeValidate(context);
370 }
371
372 public void decodeValue(FacesContext context) {
373 if (context == null) {
374 throw new IllegalArgumentException();
375 }
376
377 SourceConverter aConverter = getConverter();
378 if (aConverter == null) {
379 aConverter = getJRFacesContext()
380 .createSourceConverter(context, this);
381 }
382
383 Source source = aConverter.convertFromValue(
384 context, this, getValue());
385 setSubmittedSource(source);
386 }
387
388 public void updateModel(FacesContext context) {
389 if (context == null) {
390 throw new IllegalArgumentException();
391 }
392
393 if (!isValid()) {
394 return;
395 }
396
397 ValueExpression ve = getValueExpression("value");
398 if (valueSet && (ve != null) && (getSubmittedSource() != null)) {
399 try {
400 ve.setValue(getFacesContext().getELContext(),
401 getSubmittedSource());
402 } catch (ELException e) {
403 throw new FacesException(e);
404 }
405 } else {
406
407 String clientId = getClientId(context);
408 context.getExternalContext().getRequestMap()
409 .put(clientId, submittedSource);
410 }
411 }
412
413 public void validate(FacesContext context) throws ValidatorException {
414 if (context == null) {
415 throw new IllegalArgumentException();
416 }
417
418 String aType = getType();
419 if (aType == null) {
420 FacesMessage message = createMessage(context,
421 FacesMessage.SEVERITY_FATAL, "MISSING_ATTRIBUTE", "type");
422 throw new MissingAttributeException(message);
423 }
424
425 Object aValue = getValue();
426 if (aValue == null) {
427 FacesMessage message = createMessage(context,
428 FacesMessage.SEVERITY_FATAL, "MISSING_ATTRIBUTE", "value");
429 throw new MissingAttributeException(message);
430 }
431
432 Validator aValidator = getValidator();
433 if (aValidator == null) {
434 aValidator = getFacesContext().getApplication()
435 .createValidator(SourceValidatorBase.VALIDATOR_TYPE);
436 }
437
438 if (aValidator != null) {
439 aValidator.validate(context, this, getValue());
440 }
441 }
442
443 protected void executeValidate(FacesContext context)
444 throws ValidatorException {
445
446 if (!isValid()) {
447 try {
448 validate(context);
449 setValid(true);
450 } catch(ValidatorException e) {
451 setValid(false);
452 context.renderResponse();
453 throw e;
454 }
455 }
456 }
457
458
459
460
461
462
463 protected final JRFacesContext getJRFacesContext() {
464 return JRFacesContext.getInstance(getFacesContext());
465 }
466
467 }