1 29 30 package it.businesslogic.ireport.data; 31 32 import bsh.Interpreter; 33 import it.businesslogic.ireport.FieldsProvider; 34 import it.businesslogic.ireport.FieldsProviderEditor; 35 import it.businesslogic.ireport.IReportConnection; 36 import it.businesslogic.ireport.gui.ReportQueryDialog; 37 import it.businesslogic.ireport.util.Misc; 38 import java.awt.Component ; 39 import java.sql.Connection ; 40 import java.sql.PreparedStatement ; 41 import java.sql.ResultSet ; 42 import java.sql.ResultSetMetaData ; 43 import java.util.ArrayList ; 44 import java.util.List ; 45 import java.util.Map ; 46 import javax.swing.JDialog ; 47 import javax.swing.JOptionPane ; 48 import net.sf.jasperreports.engine.JRDataset; 49 import net.sf.jasperreports.engine.JRException; 50 import net.sf.jasperreports.engine.JRField; 51 import net.sf.jasperreports.engine.JRParameter; 52 import net.sf.jasperreports.engine.design.JRDesignField; 53 54 58 public class SQLFieldsProvider implements FieldsProvider { 59 60 public static boolean useVisualDesigner = true; 61 62 static { 63 64 java.util.Properties p = new java.util.Properties (); 65 try { 66 java.io.InputStream is = SQLFieldsProvider.class.getClass().getResourceAsStream("/it/businesslogic/ireport/data/fieldsprovider.properties"); 67 68 p.load( is ); 69 70 if (p.getProperty("sql").equals("0")) 71 { 72 useVisualDesigner = false; 73 } 74 } catch (Exception ex) 75 { 76 ex.printStackTrace(); 77 } 78 79 } 80 81 82 83 public SQLFieldsProvider() { 84 85 86 } 87 88 95 public boolean supportsGetFieldsOperation() { 96 return true; 97 } 98 99 public JRField[] getFields(IReportConnection irConn, JRDataset reportDataset, Map parameters) throws JRException, UnsupportedOperationException { 100 101 if (irConn == null || !irConn.isJDBCConnection()) { 102 throw new JRException("The active connection is not of type JDBC. Activate a JDBC connection first."); 103 } 104 105 String query = reportDataset.getQuery().getText(); 106 String error_msg = ""; 107 Connection con = null; 108 PreparedStatement ps = null; 109 110 try { 111 java.util.List queryParams = new ArrayList (); 117 JRParameter[] jrParams = reportDataset.getParameters(); 118 for (int k=0; k<jrParams.length; ++k) 119 { 120 JRParameter parameter = jrParams[k]; 121 122 String p1 = "$P{" + parameter.getName() + "}"; 123 String p2 = "$P!{" + parameter.getName() + "}"; 124 125 Object defValue = parameters.get(parameter.getName()); 126 127 int ip1 = query.indexOf(p1); 128 while( ip1!=-1 ) { 129 if( defValue==null ) { 131 throw new IllegalArgumentException ("Please set a " + 132 "default value for the parameter '" + 133 parameter.getName() + "'" ); 134 } 135 136 String before = query.substring(0, ip1); 137 String after = query.substring(ip1+p1.length()); 138 query = before + " ? " + after; 139 queryParams.add( defValue ); 140 ip1 = query.indexOf(p1); 141 } 142 143 int ip2 = query.indexOf(p2); 144 while( ip2!=-1 ) { 145 if( defValue==null ) { 147 throw new IllegalArgumentException ("Please set a " + 148 "default value for the parameter '" 149 + parameter.getName() + "'" ); 150 } 151 152 String before = query.substring(0, ip2); 153 String after = query.substring(ip2+p2.length()); 154 query = before + "" + defValue.toString() + "" + after; 155 ip2 = query.indexOf(p2); 156 } 157 } 158 159 con = irConn.getConnection(); 160 161 ps = con.prepareStatement( query ); 163 for(int pc=0; pc<queryParams.size(); pc++ ) { 164 ps.setObject(pc+1, queryParams.get(pc) ); 165 } 166 167 try { ps.setFetchSize(0); } catch(Exception e ) {} 169 170 171 ResultSet rs = ps.executeQuery(); 172 173 175 ResultSetMetaData rsmd = rs.getMetaData(); 176 177 179 List columns = new ArrayList (); 180 for (int i=1; i <=rsmd.getColumnCount(); ++i) { 181 JRDesignField field = new JRDesignField(); 182 field.setName( rsmd.getColumnLabel(i) ); 183 field.setValueClassName( Misc.getJdbcTypeClass(rsmd, i) ); 184 field.setDescription(""); 185 columns.add( field ); 186 } 187 188 JRField[] final_fields = new JRField[columns.size()]; 189 for (int i=0; i<final_fields.length; ++i) 190 { 191 final_fields[i] = (JRField)columns.get(i); 192 } 193 194 return final_fields; 195 196 } catch( IllegalArgumentException ie ) { 197 throw new JRException( ie.getMessage() ); 198 } catch (NoClassDefFoundError ex) { 199 ex.printStackTrace(); 200 error_msg = "NoClassDefFoundError!!\nCheck your classpath!"; 201 throw new JRException( error_msg ); 202 } catch (java.sql.SQLException ex) { 203 error_msg = "SQL problems:\n"+ex.getMessage(); 204 throw new JRException( error_msg ); 205 } catch (Exception ex) { 206 ex.printStackTrace(); 207 error_msg = "General problem:\n"+ex.getMessage()+ 208 "\n\nCheck username and password; is the DBMS active ?!"; 209 throw new JRException( error_msg ); 210 } catch (Throwable t) 211 { 212 throw new JRException( t.getMessage() ); 213 } finally { 214 if(ps!=null) try { ps.close(); } catch(Exception e ) {} 215 if(con !=null) try { con.close(); } catch(Exception e ) {} 216 } 217 } 218 219 public boolean supportsAutomaticQueryExecution() { 220 return true; 221 } 222 223 public boolean hasQueryDesigner() { 224 return useVisualDesigner; 225 } 226 227 public boolean hasEditorComponent() { 228 return false; 229 } 230 231 public String designQuery(IReportConnection con, String query, ReportQueryDialog reportQueryDialog) throws JRException, UnsupportedOperationException { 232 QueryBuilderDialog qbd = new QueryBuilderDialog( (reportQueryDialog != null) ? reportQueryDialog : new JDialog (), true); 234 235 if (con.isJDBCConnection()) 236 { 237 qbd.setConnection( con.getConnection() ); 238 } 239 try { 240 241 if (query != null && query.length() > 0) 242 { 243 qbd.setQuery(query); 244 } 245 } catch (Exception ex) 246 { 247 if (reportQueryDialog != null) 248 { 249 reportQueryDialog.getJLabelStatusSQL().setText("I'm sorry, I'm unable to parse the query..."); 250 ex.printStackTrace(); 251 } 252 return null; 253 } 254 qbd.setVisible(true); 255 256 if (qbd.getDialogResult() == JOptionPane.OK_OPTION) 257 { 258 return qbd.getQuery(); 259 } 260 return null; 261 } 262 263 public FieldsProviderEditor getEditorComponent(ReportQueryDialog reportQueryDialog) { 264 return null; 265 } 266 267 } 268 | Popular Tags |