1 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 36 import mondrian.olap.Connection; 37 import mondrian.olap.DriverManager; 38 import net.sf.jasperreports.engine.JRException; 39 import net.sf.jasperreports.engine.JRExporterParameter; 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.JasperPrintManager; 44 import net.sf.jasperreports.engine.JasperRunManager; 45 import net.sf.jasperreports.engine.export.JExcelApiExporter; 46 import net.sf.jasperreports.engine.export.JRCsvExporter; 47 import net.sf.jasperreports.engine.export.JRRtfExporter; 48 import net.sf.jasperreports.engine.export.JRXlsExporter; 49 import net.sf.jasperreports.engine.export.JRXlsExporterParameter; 50 import net.sf.jasperreports.engine.util.JRLoader; 51 import net.sf.jasperreports.olap.JRMondrianQueryExecuterFactory; 52 53 54 58 public class MondrianApp 59 { 60 61 62 65 private static final String TASK_FILL = "fill"; 66 private static final String TASK_PRINT = "print"; 67 private static final String TASK_PDF = "pdf"; 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_RTF = "rtf"; 72 private static final String TASK_XLS = "xls"; 73 private static final String TASK_JXL = "jxl"; 74 private static final String TASK_CSV = "csv"; 75 private static final String TASK_RUN = "run"; 76 77 78 81 public static void main(String [] args) 82 { 83 String fileName = null; 84 String taskName = null; 85 String propertiesFileName = null; 86 87 if(args.length == 0) 88 { 89 usage(); 90 return; 91 } 92 93 int k = 0; 94 while ( args.length > k ) 95 { 96 if ( args[k].startsWith("-T") ) 97 taskName = args[k].substring(2); 98 if ( args[k].startsWith("-F") ) 99 fileName = args[k].substring(2); 100 if ( args[k].startsWith("-P") ) 101 propertiesFileName = args[k].substring(2); 102 103 k++; 104 } 105 106 try 107 { 108 long start = System.currentTimeMillis(); 109 if (TASK_FILL.equals(taskName)) 110 { 111 Connection conn = getConnection(propertiesFileName); 112 try 113 { 114 Map parameters = new HashMap (); 115 parameters.put(JRMondrianQueryExecuterFactory.PARAMETER_MONDRIAN_CONNECTION, conn); 116 117 JasperFillManager.fillReportToFile(fileName, parameters); 118 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 119 } 120 finally 121 { 122 conn.close(); 123 } 124 } 125 else if (TASK_PRINT.equals(taskName)) 126 { 127 JasperPrintManager.printReport(fileName, true); 128 System.err.println("Printing time : " + (System.currentTimeMillis() - start)); 129 } 130 else if (TASK_PDF.equals(taskName)) 131 { 132 JasperExportManager.exportReportToPdfFile(fileName); 133 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 134 } 135 else if (TASK_XML.equals(taskName)) 136 { 137 JasperExportManager.exportReportToXmlFile(fileName, false); 138 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 139 } 140 else if (TASK_XML_EMBED.equals(taskName)) 141 { 142 JasperExportManager.exportReportToXmlFile(fileName, true); 143 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 144 } 145 else if (TASK_HTML.equals(taskName)) 146 { 147 JasperExportManager.exportReportToHtmlFile(fileName); 148 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); 149 } 150 else if (TASK_RTF.equals(taskName)) 151 { 152 File sourceFile = new File (fileName); 153 154 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 155 156 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".rtf"); 157 158 JRRtfExporter exporter = new JRRtfExporter(); 159 160 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 161 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 162 163 exporter.exportReport(); 164 165 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start)); 166 } 167 else if (TASK_XLS.equals(taskName)) 168 { 169 File sourceFile = new File (fileName); 170 171 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 172 173 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".xls"); 174 175 JRXlsExporter exporter = new JRXlsExporter(); 176 177 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 178 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 179 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 180 181 exporter.exportReport(); 182 183 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 184 } 185 else if (TASK_JXL.equals(taskName)) 186 { 187 File sourceFile = new File (fileName); 188 189 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 190 191 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls"); 192 193 JExcelApiExporter exporter = new JExcelApiExporter(); 194 195 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 196 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 197 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 198 199 exporter.exportReport(); 200 201 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 202 System.exit(0); 203 } 204 else if (TASK_CSV.equals(taskName)) 205 { 206 File sourceFile = new File (fileName); 207 208 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 209 210 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".csv"); 211 212 JRCsvExporter exporter = new JRCsvExporter(); 213 214 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 215 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 216 217 exporter.exportReport(); 218 219 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start)); 220 } 221 else if (TASK_RUN.equals(taskName)) 222 { 223 Connection conn = getConnection(propertiesFileName); 224 try 225 { 226 Map parameters = new HashMap (); 227 parameters.put(JRMondrianQueryExecuterFactory.PARAMETER_MONDRIAN_CONNECTION, conn); 228 229 JasperRunManager.runReportToPdfFile(fileName, parameters); 230 System.err.println("PDF running time : " + (System.currentTimeMillis() - start)); 231 } 232 finally 233 { 234 conn.close(); 235 } 236 } 237 else 238 { 239 usage(); 240 } 241 } 242 catch (JRException e) 243 { 244 e.printStackTrace(); 245 System.exit(1); 246 } 247 catch (Exception e) 248 { 249 e.printStackTrace(); 250 System.exit(1); 251 } 252 } 253 254 255 258 private static void usage() 259 { 260 System.out.println( "MondrianApp usage:" ); 261 System.out.println( "\tjava MondrianApp -Ttask -Ffile -Pproperties" ); 262 System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | csv | run" ); 263 System.out.println( "\tproperties : properties file for Mondrian connection" ); 264 } 265 266 267 private static Connection getConnection(String propertiesFileName) throws FileNotFoundException , IOException 268 { 269 if (propertiesFileName == null) { 270 throw new RuntimeException ("connection properties file not set"); 271 } 272 ConnectionData data = getConnectionData(propertiesFileName); 273 Connection connection = 274 DriverManager.getConnection( 275 "Provider=mondrian;" + 276 "JdbcDrivers=" + data.getJdbcDrivers() + ";" + 277 "Jdbc=" + data.getJdbcUrl() + ";" + 278 "JdbcUser=" + data.getJdbcUser() + ";" + 279 "JdbcPassword=" + data.getJdbcPassword() + ";" + 280 "Catalog=" + data.getCatalogUri() + ";", 281 null, false); 282 283 return connection; 284 } 285 286 private static Properties loadConnectionProperties(String path) throws FileNotFoundException , IOException 287 { 288 File connectionFile = new File (path); 289 Properties properties = new Properties (); 290 properties.load(new FileInputStream (connectionFile)); 291 return properties; 292 } 293 294 private static ConnectionData getConnectionData(String propertiesFileName) throws IOException 295 { 296 Properties properties = loadConnectionProperties(propertiesFileName); 297 298 ConnectionData data = (new MondrianApp()).new ConnectionData(); 299 String prop; 300 301 String dataSourceName = properties.getProperty("dataSourceName"); 302 if (dataSourceName == null || dataSourceName.length() == 0) { 303 prop = properties.getProperty("jdbcDrivers"); 304 if (prop != null && prop.length() > 0) 305 data.setJdbcDrivers(prop); 306 else 307 throw new RuntimeException ("Invalid JDBC driver"); 308 309 prop = properties.getProperty("jdbcUrl"); 310 if (prop != null && prop.length() > 0) 311 data.setJdbcUrl(prop); 312 else 313 throw new RuntimeException ("Invalid JDBC URL"); 314 315 prop = properties.getProperty("jdbcUser"); 316 if (prop != null && prop.length() > 0) 317 data.setJdbcUser(prop); 318 else 319 data.setJdbcUser(""); 320 321 prop = properties.getProperty("jdbcPassword"); 322 if (prop != null && prop.length() > 0) 323 data.setJdbcPassword(prop); 324 else 325 data.setJdbcPassword(""); 326 } 327 else 328 data.setDataSourceName(dataSourceName); 329 330 prop = properties.getProperty("catalogUri"); 331 if (prop != null && prop.length() > 0) { 332 data.setCatalogUri(prop); 333 } else 334 throw new RuntimeException ("Invalid catalog URI"); 335 336 return data; 337 } 338 339 340 344 private class ConnectionData 345 { 346 private String jdbcDrivers; 347 private String jdbcUrl; 348 private String jdbcUser; 349 private String jdbcPassword; 350 private String dataSourceName; 351 private String catalogUri; 352 353 public ConnectionData() 354 { 355 } 356 357 public String getCatalogUri() 358 { 359 return catalogUri; 360 } 361 362 public void setCatalogUri(String catalogUri) 363 { 364 this.catalogUri = catalogUri; 365 } 366 367 public String getDataSourceName() 368 { 369 return dataSourceName; 370 } 371 372 public void setDataSourceName(String dataSourceName) 373 { 374 this.dataSourceName = dataSourceName; 375 } 376 377 public String getJdbcDrivers() 378 { 379 return jdbcDrivers; 380 } 381 382 public void setJdbcDrivers(String jdbcDrivers) 383 { 384 this.jdbcDrivers = jdbcDrivers; 385 } 386 387 public String getJdbcPassword() 388 { 389 return jdbcPassword; 390 } 391 392 public void setJdbcPassword(String jdbcPassword) 393 { 394 this.jdbcPassword = jdbcPassword; 395 } 396 397 public String getJdbcUrl() 398 { 399 return jdbcUrl; 400 } 401 402 public void setJdbcUrl(String jdbcUrl) 403 { 404 this.jdbcUrl = jdbcUrl; 405 } 406 407 public String getJdbcUser() 408 { 409 return jdbcUser; 410 } 411 412 public void setJdbcUser(String jdbcUser) 413 { 414 this.jdbcUser = jdbcUser; 415 } 416 } 417 } 418 | Popular Tags |