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 java.sql.Connection;
22 import java.sql.SQLException;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.faces.component.NamingContainer;
26
27 import javax.faces.component.UIComponent;
28 import javax.faces.component.UIViewRoot;
29 import javax.faces.context.FacesContext;
30 import javax.sql.DataSource;
31
32 import net.sf.jasperreports.engine.JRDataSource;
33 import net.sf.jasperreports.engine.JREmptyDataSource;
34 import net.sf.jasperreports.jsf.Constants;
35 import net.sf.jasperreports.jsf.component.UIReport;
36 import net.sf.jasperreports.jsf.component.UISource;
37 import net.sf.jasperreports.jsf.convert.ConverterException;
38 import net.sf.jasperreports.jsf.convert.SourceConverter;
39 import net.sf.jasperreports.jsf.engine.Source;
40 import net.sf.jasperreports.jsf.engine.SourceException;
41 import net.sf.jasperreports.jsf.engine.ConnectionWrapper;
42 import net.sf.jasperreports.jsf.engine.JRDataSourceWrapper;
43
44
45
46
47
48
49 public class SourceConverterBase implements SourceConverter {
50
51
52
53
54 private static final long serialVersionUID = -7915218134982872003L;
55
56 private static final Logger logger = Logger.getLogger(
57 SourceConverterBase.class.getPackage().getName(),
58 Constants.LOG_MESSAGES_BUNDLE);
59
60 protected static final Source NULL_SOURCE =
61 new JRDataSourceWrapper(new JREmptyDataSource());
62
63 public Source convertFromValue(FacesContext context,
64 UIComponent component, Object value)
65 throws ConverterException {
66 if (context == null) {
67 throw new IllegalArgumentException("'context'");
68 }
69 if (component == null) {
70 throw new IllegalArgumentException("'context'");
71 }
72
73 if (value == null) {
74 return NULL_SOURCE;
75 }
76
77 Source source = null;
78 if (value instanceof Source) {
79 source = (Source) value;
80 } else if (value instanceof Connection) {
81 source = new ConnectionWrapper((Connection) value);
82 } else if (value instanceof DataSource) {
83 try {
84 source = new ConnectionWrapper(
85 ((DataSource) value).getConnection());
86 } catch (SQLException e) {
87 throw new ConverterException(e);
88 }
89 } else if (value instanceof JRDataSource) {
90 source = new JRDataSourceWrapper((JRDataSource) value);
91 } else {
92 try {
93 source = resolveSource(context, component, value);
94 } catch (SourceException e) {
95 throw new ConverterException(e);
96 }
97 }
98
99 if (source == null) {
100 throw new ConverterException("Couldn't convert value '" + value +
101 "' to a source object for component: "
102 + component.getClientId(context));
103 }
104
105 return source;
106 }
107
108 public Object convertFromSource(FacesContext context,
109 UIComponent component, Source source)
110 throws ConverterException {
111 if (context == null) {
112 throw new IllegalArgumentException("'context'");
113 }
114 if (component == null) {
115 throw new IllegalArgumentException("'context'");
116 }
117
118 if (source == null) {
119 return null;
120 }
121
122 if (source instanceof ConnectionWrapper) {
123 return ((ConnectionWrapper) source).getConnection();
124 } else if (source instanceof JRDataSourceWrapper) {
125 return ((JRDataSourceWrapper) source).getDataSource();
126 } else {
127 throw new ConverterException("Unrecognized source type: " + source.
128 getClass().getName());
129 }
130 }
131
132 protected Source createSource(FacesContext context,
133 UIComponent component, Object value)
134 throws SourceException {
135 return NULL_SOURCE;
136 }
137
138 private Source resolveSource(FacesContext context, UIComponent component,
139 Object value)
140 throws SourceException {
141 Source result = null;
142 if ((value instanceof String) && (component instanceof UIReport)) {
143
144
145 UISource source = resolveSourceId(context,
146 (UIReport) component,
147 (String) value);
148 if (source != null) {
149 result = source.getSubmittedSource();
150 if (logger.isLoggable(Level.FINE)) {
151 logger.log(Level.FINE, "JRJSF_0036", new Object[]{
152 component.getClientId(context),
153 source.getClientId(context)
154 });
155 }
156 }
157 }
158
159 if (result == null) {
160 result = createSource(context, component, value);
161 }
162 return result;
163 }
164
165 private UISource resolveSourceId(FacesContext context,
166 UIReport report, String sourceId) {
167
168
169 UIViewRoot viewRoot = context.getViewRoot();
170 UISource result = (UISource) viewRoot.findComponent(sourceId);
171 if (result == null) {
172 UIComponent component;
173
174 do {
175 component = getNamingContainer(report);
176 String id = component.getClientId(context) + ":" + sourceId;
177 UIComponent found = component.findComponent(id);
178 if ((found != null) && (found instanceof UISource)) {
179 result = (UISource) found;
180 break;
181 }
182 } while (!(component instanceof UIViewRoot));
183 }
184 return result;
185 }
186
187 private UIComponent getNamingContainer(UIComponent child) {
188 UIComponent component = child;
189 do {
190 component = component.getParent();
191 } while (!(component instanceof NamingContainer));
192 return component;
193 }
194 }