View Javadoc

1   /*
2    * JaspertReports JSF Plugin Copyright (C) 2011 A. Alonso Dominguez
3    *
4    * This library is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as published by
6    * the Free Software Foundation; either version 2.1 of the License, or (at
7    * your option) any later version. This library is distributed in the hope
8    * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9    * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   *
11   * See the GNU Lesser General Public License for more details. You should have
12   * received a copy of the GNU Lesser General Public License along with this
13   * library; if not, write to the Free Software Foundation, Inc., 59 Temple
14   * Place, Suite 330, Boston, MA 02111-1307 USA A.
15   *
16   * Alonso Dominguez
17   * alonsoft@users.sf.net
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   * Base source converter class.
46   *
47   * @author A. Alonso Dominguez
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             // Look for a UISource component that may have the string value
144             // as component id in the same view container
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     	// Try to find source component using 'sourceId' as an
168     	// absolute ID
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 }