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 net.sf.jasperreports.jsf.engine.JRDataSourceWrapper;
22  import net.sf.jasperreports.jsf.engine.ConnectionWrapper;
23  import java.sql.Connection;
24  import java.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  import java.sql.SQLException;
27  import java.util.logging.Level;
28  import java.util.logging.LogRecord;
29  import java.util.logging.Logger;
30  
31  import javax.faces.component.UIComponent;
32  import javax.faces.component.UIParameter;
33  import javax.faces.context.FacesContext;
34  
35  import net.sf.jasperreports.engine.JRDataSource;
36  import net.sf.jasperreports.engine.JREmptyDataSource;
37  import net.sf.jasperreports.engine.JRResultSetDataSource;
38  import net.sf.jasperreports.jsf.Constants;
39  import net.sf.jasperreports.jsf.engine.Source;
40  import net.sf.jasperreports.jsf.engine.SourceException;
41  
42  import static net.sf.jasperreports.jsf.util.ComponentUtil.*;
43  
44  /**
45   * Base converter class for source converters which can obtain
46   * a JDBC connection from the value to be converted.
47   *
48   * @author A. Alonso Dominguez
49   */
50  public abstract class DatabaseSourceConverter
51          extends SourceConverterBase {
52  
53      /**
54  	 * 
55  	 */
56  	private static final long serialVersionUID = 5189066786528032421L;
57  	private static final Logger logger = Logger.getLogger(
58              DatabaseSourceConverter.class.getPackage().getName(),
59              Constants.LOG_MESSAGES_BUNDLE);
60  
61      @Override
62      protected Source createSource(FacesContext context,
63              UIComponent component, Object value)
64      throws SourceException {
65          Connection connection = getConnection(context, component);
66          if (connection == null) {
67              if (logger.isLoggable(Level.WARNING)) {
68                  String clientId = component.getClientId(context);
69                  logger.log(Level.WARNING, "JRJSF_0020", clientId);
70              }
71              JRDataSource ds = new JREmptyDataSource();
72              return new JRDataSourceWrapper(ds);
73          } else {
74              Source reportSource;
75              final String query = getStringAttribute(component, "query", null);
76              if (query != null && query.length() > 0) {
77                  ResultSet rs = executeQuery(context, component, connection, query);
78                  JRDataSource dataSource = new JRResultSetDataSource(rs);
79                  reportSource = new JRDataSourceWrapper(dataSource);
80              } else {
81                  reportSource = new ConnectionWrapper(connection);
82              }
83              return reportSource;
84          }
85      }
86  
87      protected abstract Connection getConnection(
88              FacesContext context, UIComponent component)
89      throws SourceException;
90  
91      /**
92       * Execute query.
93       *
94       * @param conn the conn
95       *
96       * @return the result set
97       *
98       * @throws ReportSourceException if some error happens when executing
99       *         the sql statement
100      */
101     protected ResultSet executeQuery(final FacesContext context,
102             final UIComponent component, final Connection conn,
103             final String query)
104             throws SourceException {
105         PreparedStatement st;
106         try {
107             st = conn.prepareStatement(query);
108         } catch (SQLException ex) {
109             throw new UnpreparedStatementException(query, ex);
110         }
111 
112         int paramIdx = 1;
113         for (final UIComponent kid : component.getChildren()) {
114             if (!(kid instanceof UIParameter)) {
115                 continue;
116             }
117 
118             final UIParameter param = (UIParameter) kid;
119             try {
120                 st.setObject(paramIdx++, param.getValue());
121             } catch (Exception e) {
122                 if (logger.isLoggable(Level.WARNING)) {
123                     LogRecord record = new LogRecord(
124                             Level.WARNING, "JRJSF_0028");
125                     record.setParameters(new Object[]{
126                     		paramIdx, param.getName(), query
127                     });
128                     record.setThrown(e);
129                     record.setResourceBundleName(logger.getResourceBundleName());
130                     record.setResourceBundle(logger.getResourceBundle());
131                     logger.log(record);
132                 }
133             }
134         }
135 
136 
137         try {
138             return st.executeQuery();
139         } catch (SQLException e) {
140             throw new QueryExecutionException(query, e);
141         } finally {
142             try {
143                 st.close();
144             } catch (final SQLException e) { ; }
145         }
146 
147     }
148 
149 }