1 28 import java.io.File ; 29 import java.sql.Date ; 30 import java.util.Calendar ; 31 import java.util.HashMap ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import javax.persistence.EntityManager; 36 import javax.persistence.EntityManagerFactory; 37 import javax.persistence.Persistence; 38 import javax.persistence.Query; 39 40 import net.sf.jasperreports.engine.JRException; 41 import net.sf.jasperreports.engine.JRExporterParameter; 42 import net.sf.jasperreports.engine.JasperExportManager; 43 import net.sf.jasperreports.engine.JasperFillManager; 44 import net.sf.jasperreports.engine.JasperPrint; 45 import net.sf.jasperreports.engine.JasperPrintManager; 46 import net.sf.jasperreports.engine.JasperRunManager; 47 import net.sf.jasperreports.engine.export.JExcelApiExporter; 48 import net.sf.jasperreports.engine.export.JRCsvExporter; 49 import net.sf.jasperreports.engine.export.JRRtfExporter; 50 import net.sf.jasperreports.engine.export.JRXlsExporter; 51 import net.sf.jasperreports.engine.export.JRXlsExporterParameter; 52 import net.sf.jasperreports.engine.query.JRJpaQueryExecuterFactory; 53 import net.sf.jasperreports.engine.util.JRLoader; 54 55 56 60 public class EjbqlApp { 61 62 private static final String TASK_FILL = "fill"; 63 private static final String TASK_PRINT = "print"; 64 private static final String TASK_PDF = "pdf"; 65 private static final String TASK_XML = "xml"; 66 private static final String TASK_XML_EMBED = "xmlEmbed"; 67 private static final String TASK_HTML = "html"; 68 private static final String TASK_RTF = "rtf"; 69 private static final String TASK_XLS = "xls"; 70 private static final String TASK_JXL = "jxl"; 71 private static final String TASK_CSV = "csv"; 72 private static final String TASK_RUN = "run"; 73 74 public static void main(String [] args) { 75 String fileName = null; 76 String taskName = null; 77 78 if(args.length == 0) { 79 usage(); 80 return; 81 } 82 83 int k = 0; 84 while ( args.length > k ) { 85 if ( args[k].startsWith("-T") ) 86 taskName = args[k].substring(2); 87 if ( args[k].startsWith("-F") ) 88 fileName = args[k].substring(2); 89 90 k++; 91 } 92 93 try { 94 long start = System.currentTimeMillis(); 95 if (TASK_FILL.equals(taskName)) { 96 EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu1", new HashMap ()); 98 EntityManager em = emf.createEntityManager(); 99 100 try 101 { 102 Map parameters = getParameters(em); 103 104 JasperFillManager.fillReportToFile(fileName, parameters); 105 106 em.close(); 107 108 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 109 } 110 finally 111 { 112 if (em.isOpen()) 113 em.close(); 114 if (emf.isOpen()) 115 emf.close(); 116 } 117 System.exit(0); 118 } 119 else if (TASK_PRINT.equals(taskName)) { 120 JasperPrintManager.printReport(fileName, true); 121 System.err.println("Printing time : " + (System.currentTimeMillis() - start)); 122 System.exit(0); 123 } 124 else if (TASK_PDF.equals(taskName)) { 125 JasperExportManager.exportReportToPdfFile(fileName); 126 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 127 System.exit(0); 128 } 129 else if (TASK_XML.equals(taskName)) { 130 JasperExportManager.exportReportToXmlFile(fileName, false); 131 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 132 System.exit(0); 133 } 134 else if (TASK_XML_EMBED.equals(taskName)) { 135 JasperExportManager.exportReportToXmlFile(fileName, true); 136 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 137 System.exit(0); 138 } 139 else if (TASK_HTML.equals(taskName)) { 140 JasperExportManager.exportReportToHtmlFile(fileName); 141 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); 142 System.exit(0); 143 } 144 else if (TASK_RTF.equals(taskName)) { 145 File sourceFile = new File (fileName); 146 147 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 148 149 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".rtf"); 150 151 JRRtfExporter exporter = new JRRtfExporter(); 152 153 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 154 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 155 156 exporter.exportReport(); 157 158 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start)); 159 System.exit(0); 160 } 161 else if (TASK_XLS.equals(taskName)) { 162 File sourceFile = new File (fileName); 163 164 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 165 166 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".xls"); 167 168 JRXlsExporter exporter = new JRXlsExporter(); 169 170 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 171 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 172 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 173 174 exporter.exportReport(); 175 176 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 177 System.exit(0); 178 } 179 else if (TASK_JXL.equals(taskName)) 180 { 181 File sourceFile = new File (fileName); 182 183 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 184 185 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls"); 186 187 JExcelApiExporter exporter = new JExcelApiExporter(); 188 189 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 190 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 191 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 192 193 exporter.exportReport(); 194 195 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 196 System.exit(0); 197 } 198 else if (TASK_CSV.equals(taskName)) { 199 File sourceFile = new File (fileName); 200 201 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 202 203 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".csv"); 204 205 JRCsvExporter exporter = new JRCsvExporter(); 206 207 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 208 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 209 210 exporter.exportReport(); 211 212 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start)); 213 System.exit(0); 214 } 215 else if (TASK_RUN.equals(taskName)) { 216 EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu1", new HashMap ()); 218 EntityManager em = emf.createEntityManager(); 219 220 try 221 { 222 Map parameters = getParameters(em); 223 224 JasperRunManager.runReportToPdfFile(fileName, parameters); 225 226 em.close(); 227 228 System.err.println("PDF running time : " + (System.currentTimeMillis() - start)); 229 } 230 finally 231 { 232 if (em.isOpen()) 233 em.close(); 234 if (emf.isOpen()) 235 emf.close(); 236 } 237 System.exit(0); 238 } 239 else { 240 usage(); 241 System.exit(0); 242 } 243 } 244 catch (JRException e) { 245 e.printStackTrace(); 246 System.exit(1); 247 } 248 catch (Exception e) { 249 e.printStackTrace(); 250 System.exit(1); 251 } 252 } 253 254 private static Map getParameters(EntityManager em) { 255 Map parameters = new HashMap (); 256 parameters.put(JRJpaQueryExecuterFactory.PARAMETER_JPA_ENTITY_MANAGER, em); 257 parameters.put("ReportTitle", "JRMDb - The JasperReports Movie Database"); 258 Calendar calendar = Calendar.getInstance(); 259 calendar.set(1990, 1, 1); 260 parameters.put("DateFrom", new Date (calendar.getTimeInMillis())); 261 parameters.put("DateTo", new Date (System.currentTimeMillis())); 262 parameters.put("OrderClause", "m.genre, m.title"); 263 264 return parameters; 265 } 266 267 private static void usage() { 268 System.out.println( "EjbqlApp usage:" ); 269 System.out.println( "\tjava EjbqlApp -Ttask -Ffile" ); 270 System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | csv | run" ); 271 } 272 } 273 | Popular Tags |