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.Calendar ; 33 import java.util.Date ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 import net.sf.jasperreports.engine.JRException; 38 import net.sf.jasperreports.engine.JRExporterParameter; 39 import net.sf.jasperreports.engine.JasperCompileManager; 40 import net.sf.jasperreports.engine.JasperExportManager; 41 import net.sf.jasperreports.engine.JasperFillManager; 42 import net.sf.jasperreports.engine.JasperPrint; 43 import net.sf.jasperreports.engine.util.JRLoader; 44 45 46 50 public class ChartsApp 51 { 52 53 54 57 private static final String TASK_FILL = "fill"; 58 private static final String TASK_PDF = "pdf"; 59 private static final String TASK_HTML = "html"; 60 private static final String TASK_WRITE_XML = "writeXml"; 61 62 private static final String [] reportNames = 63 { 64 "PieChartReport", 65 "Pie3DChartReport", 66 "BarChartReport", 67 "Bar3DChartReport", 68 "StackedBarChartReport", 69 "StackedBar3DChartReport", 70 "XYBarChartTimePeriodReport", 71 "XYBarChartTimeSeriesReport", 72 "XYBarChartReport", 73 "AreaChartReport", 74 "XYAreaChartReport", 75 "ScatterChartReport", 76 "LineChartReport", 77 "XYLineChartReport", 78 "TimeSeriesChartReport", 79 "BubbleChartReport", 80 "HighLowChartReport", 81 "CandlestickChartReport", 82 "SubDatasetChartReport", 83 "MeterChartReport", 84 "MultipleAxisChartReport", 85 "ThermometerChartReport" 86 }; 87 88 91 public static void main(String [] args) 92 { 93 String taskName = null; 94 95 if(args.length == 0) 96 { 97 usage(); 98 return; 99 } 100 101 int k = 0; 102 while ( args.length > k ) 103 { 104 if ( args[k].startsWith("-T") ) 105 taskName = args[k].substring(2); 106 107 k++; 108 } 109 110 try 111 { 112 if (TASK_FILL.equals(taskName)) 113 { 114 Map parameters = new HashMap (); 115 parameters.put("MaxOrderID", new Integer (12500)); 116 117 for(int i = 0; i < reportNames.length; i++) 118 { 119 long start = System.currentTimeMillis(); 120 JasperFillManager.fillReportToFile(reportNames[i] + ".jasper", parameters, getConnection()); 121 System.err.println("Report : " + reportNames[i] + ". Filling time : " + (System.currentTimeMillis() - start)); 122 } 123 124 System.exit(0); 125 } 126 else if (TASK_PDF.equals(taskName)) 127 { 128 for(int i = 0; i < reportNames.length; i++) 129 { 130 long start = System.currentTimeMillis(); 131 JasperExportManager.exportReportToPdfFile(reportNames[i] + ".jrprint"); 132 System.err.println("Report : " + reportNames[i] + ". PDF export time : " + (System.currentTimeMillis() - start)); 133 } 134 135 System.exit(0); 136 } 137 else if (TASK_HTML.equals(taskName)) 138 { 139 for(int i = 0; i < reportNames.length; i++) 140 { 141 long start = System.currentTimeMillis(); 142 JasperExportManager.exportReportToHtmlFile(reportNames[i] + ".jrprint"); 143 System.err.println("Report : " + reportNames[i] + ". Html export time : " + (System.currentTimeMillis() - start)); 144 } 145 146 System.exit(0); 147 } 148 else if (TASK_WRITE_XML.equals(taskName)) 149 { 150 for(int i = 0; i < reportNames.length; i++) 151 { 152 long start = System.currentTimeMillis(); 153 JasperCompileManager.writeReportToXmlFile(reportNames[i] + ".jasper", reportNames[i] + ".jasper.jrxml"); 154 System.err.println("Report : " + reportNames[i] + ". XML write time : " + (System.currentTimeMillis() - start)); 155 } 156 157 System.exit(0); 158 } 159 else 160 { 161 usage(); 162 System.exit(0); 163 } 164 } 165 catch (JRException e) 166 { 167 e.printStackTrace(); 168 System.exit(1); 169 } 170 catch (Exception e) 171 { 172 e.printStackTrace(); 173 System.exit(1); 174 } 175 } 176 177 178 181 private static void usage() 182 { 183 System.out.println( "ChartsApp usage:" ); 184 System.out.println( "\tjava ChartsApp -Ttask" ); 185 System.out.println( "\tTasks : fill | pdf | html" ); 186 } 187 188 189 192 private static Connection getConnection() throws ClassNotFoundException , SQLException  193 { 194 String driver = "org.hsqldb.jdbcDriver"; 196 String connectString = "jdbc:hsqldb:hsql://localhost"; 197 String user = "sa"; 198 String password = ""; 199 200 201 Class.forName(driver); 202 Connection conn = DriverManager.getConnection(connectString, user, password); 203 return conn; 204 } 205 206 207 208 public static final Date truncateToMonth(Date date) 209 { 210 Calendar calendar = Calendar.getInstance(); 211 calendar.setTime(date); 212 int year = calendar.get(Calendar.YEAR); 213 int month = calendar.get(Calendar.MONTH); 214 calendar.clear(); 215 calendar.set(Calendar.YEAR, year); 216 calendar.set(Calendar.MONTH, month); 217 return calendar.getTime(); 218 } 219 220 } 221
| Popular Tags
|