1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jasperreports.jsf.engine.converters;
20
21 import javax.faces.context.FacesContext;
22 import net.sf.jasperreports.engine.JasperReport;
23 import net.sf.jasperreports.jsf.component.UIReport;
24 import net.sf.jasperreports.jsf.context.JRFacesContext;
25 import net.sf.jasperreports.jsf.convert.ConverterException;
26 import net.sf.jasperreports.jsf.convert.ReportConverter;
27 import net.sf.jasperreports.jsf.resource.Resource;
28
29
30
31
32
33 public class ReportConverterBase implements ReportConverter {
34
35 public boolean accepts(Object value) {
36 if (value == null) return true;
37 return (true);
38 }
39
40 public final JasperReport convertFromValue(FacesContext context,
41 UIReport component, Object value)
42 throws ConverterException {
43 if (context == null) {
44 throw new IllegalArgumentException("context can't be null");
45 }
46 if (component == null) {
47 throw new IllegalArgumentException("component can't be null");
48 }
49
50 Object aValue = component.getValue();
51 if (aValue == null) {
52 return null;
53 }
54
55 JasperReport aReport;
56 if (aValue instanceof JasperReport) {
57 aReport = (JasperReport) aValue;
58 } else {
59 String valueStr;
60 if (aValue instanceof String) {
61 valueStr = (String) aValue;
62 } else {
63 valueStr = aValue.toString();
64 }
65
66 Resource resource = getJRFacesContext().createResource(
67 context, component, valueStr);
68
69 aReport = loadFromResource(context, component, resource);
70 }
71 return aReport;
72 }
73
74 protected JasperReport loadFromResource(FacesContext context,
75 UIReport component, Resource resource)
76 throws ConverterException {
77 return null;
78 }
79
80 protected final FacesContext getFacesContext() {
81 FacesContext context = FacesContext.getCurrentInstance();
82 if (context == null) {
83 throw new IllegalStateException("No faces context");
84 }
85 return context;
86 }
87
88 protected final JRFacesContext getJRFacesContext() {
89 return JRFacesContext.getInstance(getFacesContext());
90 }
91
92 }