1 28 import java.io.File ; 29 import java.sql.Connection ; 30 import java.sql.DriverManager ; 31 import java.sql.SQLException ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import net.sf.jasperreports.engine.JRException; 36 import net.sf.jasperreports.engine.JRExporterParameter; 37 import net.sf.jasperreports.engine.JRParameter; 38 import net.sf.jasperreports.engine.JasperCompileManager; 39 import net.sf.jasperreports.engine.JasperExportManager; 40 import net.sf.jasperreports.engine.JasperFillManager; 41 import net.sf.jasperreports.engine.JasperPrint; 42 import net.sf.jasperreports.engine.JasperPrintManager; 43 import net.sf.jasperreports.engine.JasperRunManager; 44 import net.sf.jasperreports.engine.export.JExcelApiExporter; 45 import net.sf.jasperreports.engine.export.JRCsvExporter; 46 import net.sf.jasperreports.engine.export.JRRtfExporter; 47 import net.sf.jasperreports.engine.export.JRXlsExporter; 48 import net.sf.jasperreports.engine.export.JRXlsExporterParameter; 49 import net.sf.jasperreports.engine.util.JRLoader; 50 51 52 56 public class QueryApp 57 { 58 59 60 63 private static final String TASK_COMPILE = "compile"; 64 private static final String TASK_FILL = "fill"; 65 private static final String TASK_PRINT = "print"; 66 private static final String TASK_PDF = "pdf"; 67 private static final String TASK_RTF = "rtf"; 68 private static final String TASK_XML = "xml"; 69 private static final String TASK_XML_EMBED = "xmlEmbed"; 70 private static final String TASK_HTML = "html"; 71 private static final String TASK_XLS = "xls"; 72 private static final String TASK_JXL = "jxl"; 73 private static final String TASK_CSV = "csv"; 74 private static final String TASK_RUN = "run"; 75 private static final String TASK_FILL_IGNORE_PAGINATION = "fillIgnorePagination"; 76 77 78 81 public static void main(String [] args) 82 { 83 String fileName = null; 84 String taskName = null; 85 86 if(args.length == 0) 87 { 88 usage(); 89 return; 90 } 91 92 int k = 0; 93 while ( args.length > k ) 94 { 95 if ( args[k].startsWith("-T") ) 96 taskName = args[k].substring(2); 97 if ( args[k].startsWith("-F") ) 98 fileName = args[k].substring(2); 99 100 k++; 101 } 102 103 try 104 { 105 long start = System.currentTimeMillis(); 106 if (TASK_COMPILE.equals(taskName)) 107 { 108 JasperCompileManager.compileReportToFile(fileName); 109 System.err.println("Compile time : " + (System.currentTimeMillis() - start)); 110 System.exit(0); 111 } 112 else if (TASK_FILL.equals(taskName) || TASK_FILL_IGNORE_PAGINATION.equals(taskName)) 113 { 114 Map parameters = new HashMap (); 116 parameters.put("ReportTitle", "Address Report"); 117 parameters.put("FilterClause", "'Boston', 'Chicago', 'Oslo'"); 118 parameters.put("OrderClause", "City"); 119 120 if (TASK_FILL_IGNORE_PAGINATION.equals(taskName)) 121 { 122 parameters.put(JRParameter.IS_IGNORE_PAGINATION, Boolean.TRUE); 123 } 124 125 JasperFillManager.fillReportToFile(fileName, parameters, getConnection()); 126 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 127 System.exit(0); 128 } 129 else if (TASK_PRINT.equals(taskName)) 130 { 131 JasperPrintManager.printReport(fileName, true); 132 System.err.println("Printing time : " + (System.currentTimeMillis() - start)); 133 System.exit(0); 134 } 135 else if (TASK_PDF.equals(taskName)) 136 { 137 JasperExportManager.exportReportToPdfFile(fileName); 138 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 139 System.exit(0); 140 } 141 else if (TASK_RTF.equals(taskName)) 142 { 143 File sourceFile = new File (fileName); 144 145 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 146 147 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".rtf"); 148 149 JRRtfExporter exporter = new JRRtfExporter(); 150 151 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 152 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 153 154 exporter.exportReport(); 155 156 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start)); 157 System.exit(0); 158 } 159 else if (TASK_XML.equals(taskName)) 160 { 161 JasperExportManager.exportReportToXmlFile(fileName, false); 162 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 163 System.exit(0); 164 } 165 else if (TASK_XML_EMBED.equals(taskName)) 166 { 167 JasperExportManager.exportReportToXmlFile(fileName, true); 168 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 169 System.exit(0); 170 } 171 else if (TASK_HTML.equals(taskName)) 172 { 173 JasperExportManager.exportReportToHtmlFile(fileName); 174 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); 175 System.exit(0); 176 } 177 else if (TASK_XLS.equals(taskName)) 178 { 179 File sourceFile = new File (fileName); 180 181 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 182 183 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".xls"); 184 185 JRXlsExporter exporter = new JRXlsExporter(); 186 187 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 188 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 189 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 190 191 exporter.exportReport(); 192 193 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 194 System.exit(0); 195 } 196 else if (TASK_JXL.equals(taskName)) 197 { 198 File sourceFile = new File (fileName); 199 200 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 201 202 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls"); 203 204 JExcelApiExporter exporter = new JExcelApiExporter(); 205 206 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 207 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 208 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 209 210 exporter.exportReport(); 211 212 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 213 System.exit(0); 214 } 215 else if (TASK_CSV.equals(taskName)) 216 { 217 File sourceFile = new File (fileName); 218 219 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 220 221 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".csv"); 222 223 JRCsvExporter exporter = new JRCsvExporter(); 224 225 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 226 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 227 228 exporter.exportReport(); 229 230 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start)); 231 System.exit(0); 232 } 233 else if (TASK_RUN.equals(taskName)) 234 { 235 Map parameters = new HashMap (); 237 parameters.put("ReportTitle", "Address Report"); 238 parameters.put("FilterClause", "'Boston', 'Chicago'"); 239 parameters.put("OrderClause", "City"); 240 241 JasperRunManager.runReportToPdfFile(fileName, parameters, getConnection()); 242 System.err.println("PDF running time : " + (System.currentTimeMillis() - start)); 243 System.exit(0); 244 } 245 else 246 { 247 usage(); 248 System.exit(0); 249 } 250 } 251 catch (JRException e) 252 { 253 e.printStackTrace(); 254 System.exit(1); 255 } 256 catch (Exception e) 257 { 258 e.printStackTrace(); 259 System.exit(1); 260 } 261 } 262 263 264 267 private static void usage() 268 { 269 System.out.println( "QueryApp usage:" ); 270 System.out.println( "\tjava QueryApp -Ttask -Ffile" ); 271 System.out.println( "\tTasks : compile | fill | fillIgnorePagination | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" ); 272 } 273 274 275 278 private static Connection getConnection() throws ClassNotFoundException , SQLException 279 { 280 String driver = "org.hsqldb.jdbcDriver"; 282 String connectString = "jdbc:hsqldb:hsql://localhost"; 283 String user = "sa"; 284 String password = ""; 285 286 287 Class.forName(driver); 288 Connection conn = DriverManager.getConnection(connectString, user, password); 289 return conn; 290 } 291 292 293 } 294 | Popular Tags |