1 13 package org.pentaho.plugin.jasperreports; 14 15 import java.io.File ; 16 import java.io.OutputStream ; 17 import java.sql.Connection ; 18 import java.sql.DriverManager ; 19 import java.sql.SQLException ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import javax.naming.NamingException ; 29 import javax.sql.DataSource ; 30 import net.sf.jasperreports.engine.JRException; 31 import net.sf.jasperreports.engine.JRExporter; 32 import net.sf.jasperreports.engine.JRExporterParameter; 33 import net.sf.jasperreports.engine.JRParameter; 34 import net.sf.jasperreports.engine.JasperCompileManager; 35 import net.sf.jasperreports.engine.JasperFillManager; 36 import net.sf.jasperreports.engine.JasperPrint; 37 import net.sf.jasperreports.engine.JasperReport; 38 import net.sf.jasperreports.engine.export.JRCsvExporter; 39 import net.sf.jasperreports.engine.export.JRHtmlExporter; 40 import net.sf.jasperreports.engine.export.JRHtmlExporterParameter; 41 import net.sf.jasperreports.engine.export.JRPdfExporter; 42 import net.sf.jasperreports.engine.export.JRXlsExporter; 43 import net.sf.jasperreports.engine.export.JRXlsExporterParameter; 44 import net.sf.jasperreports.engine.export.JRXmlExporter; 45 import net.sf.jasperreports.engine.util.JRLoader; 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 import org.pentaho.core.repository.IContentItem; 49 import org.pentaho.core.runtime.IActionParameter; 50 import org.pentaho.core.solution.IActionResource; 51 import org.pentaho.core.system.PentahoSystem; 52 import org.pentaho.core.util.DatasourceHelper; 53 import org.pentaho.messages.Messages; 54 import org.pentaho.plugin.ComponentBase; 55 import org.pentaho.plugin.core.StandardSettings; 56 57 78 public class JasperReportsComponent extends ComponentBase { 79 80 private static final String REMOVE_EMPTY_ROWS = "remove-empty-rows"; 82 private static final String IMAGE_URL = "image-url"; 84 private static final String IMAGE_DIR = "image-dir"; 86 private static final String REPORT_OUTPUT = "report-output"; 88 private static final long serialVersionUID = -4422766007912720969L; 89 90 94 public final static String JASPER_REPORTS_DESIGN_EXTENSION = ".jrxml"; 96 private static Log logger = LogFactory.getLog(JasperReportsComponent.class); 97 98 101 public final static String COMPILED_JASPER_REPORTS_EXTENSION = ".jasper"; 103 public Log getLogger() { 104 return logger; 105 } 106 107 114 protected boolean validateSystemSettings() { 115 119 String imageUrl = PentahoSystem.getSystemSetting("jasperreports/jasperreports-conf.xml", "jasperreports/imageHandling/imageUrl", null); String imageDir = PentahoSystem.getSystemSetting("jasperreports/jasperreports-conf.xml", "jasperreports/imageHandling/imageDir", null); String removeEmptyRows = PentahoSystem.getSystemSetting("jasperreports/jasperreports-conf.xml", "jasperreports/htmlExportOptions/removeEmptySpaceBetweenRows", "false"); 124 if (debug) { 125 debug(Messages.getString("JasperReport.DEBUG_IMAGE_URL") + imageUrl); debug(Messages.getString("JasperReport.DEBUG_IMAGE_DIRECTORY") + imageDir); debug(Messages.getString("JasperReport.DEBUG_REMOVE_EMPTRY_ROWS") + removeEmptyRows); } 129 130 if (imageUrl == null) { 131 error(Messages.getErrorString("JasperReport.ERROR_0001_IMAGE_URL_NOT_DEFINED")); return false; 133 } 134 if (imageDir == null) { 135 error(Messages.getErrorString("JasperReport.ERROR_0002_IMAGE_DIRECTORY_INVALID")); return false; 137 } 138 139 saveSetting(IMAGE_URL, imageUrl); 140 saveSetting(IMAGE_DIR, imageDir); 141 saveSetting(REMOVE_EMPTY_ROWS, removeEmptyRows); 142 143 return true; 146 } 147 148 153 public boolean validateAction() { 154 155 159 boolean jndiDefined = isDefinedInput(StandardSettings.JNDI); 161 162 if (!jndiDefined) { 164 if (!isDefinedInput(StandardSettings.DRIVER)) { 165 error(Messages.getErrorString("JasperReport.ERROR_0003_JDBC_DRIVER_NOT_SPECIFIED")); return false; 167 } 168 if (!isDefinedInput(StandardSettings.CONNECTION)) { 169 error(Messages.getErrorString("JasperReport.ERROR_0004_JDBC_CONNECTION_NOT_SPECIFIED")); return false; 171 } 172 if (!isDefinedInput(StandardSettings.USERID)) { 173 error(Messages.getErrorString("JasperReport.ERROR_0005_JDBC_USER_NOT_SPECIFIED")); return false; 175 } 176 } 177 if (!isDefinedInput("output-type")) { error(Messages.getErrorString("JasperReport.ERROR_0006_OUTPUT_TYPE_NOT_SPECIFIED")); return false; 181 } 182 183 if (getResourceNames().isEmpty()) { 185 error(Messages.getErrorString("JasperReport.ERROR_0007_REPORT_DEFINITION_NOT_SPECIFIED")); return false; 187 } 188 189 return true; 190 } 191 192 209 private JRExporter getExporter(String outputType, String reportName) { 210 JRExporter exporter = null; 211 if ("html".equals(outputType)) { String removeEmptyRows = getStringSetting("removeEmptyRows"); exporter = new JRHtmlExporter(); 214 exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, new Boolean (false)); 215 if (removeEmptyRows != null && "true".equalsIgnoreCase(removeEmptyRows)) { exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, new Boolean (true)); 217 } 218 String imageUrl = PentahoSystem.getApplicationContext().getBaseUrl() + getStringSetting(IMAGE_URL); 219 String imagePath = PentahoSystem.getApplicationContext().getSolutionPath(getStringSetting(IMAGE_DIR)); 220 if (!imagePath.endsWith(File.separator)) 221 imagePath = imagePath + File.separator; 222 imagePath = imagePath + reportName + File.separator; 223 File imageDir = new File (imagePath); 224 imageDir.mkdirs(); 225 exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR, imageDir); 226 exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageUrl + reportName + "/"); exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, new Boolean (true)); 230 if (debug) 231 debug(Messages.getString("JasperReport.DEBUG_IMAGE_DIRECTORY", imagePath)); } else if ("pdf".equals(outputType)) { exporter = new JRPdfExporter(); 234 } else if ("xls".equals(outputType)) { exporter = new JRXlsExporter(); 236 exporter.setParameter(JRXlsExporterParameter.IS_AUTO_DETECT_CELL_TYPE, new Boolean (true)); 238 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, new Boolean (true)); 239 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, new Boolean (true)); 240 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 241 } else if ("csv".equals(outputType)) { exporter = new JRCsvExporter(); 243 } else if ("xml".equals(outputType)) { exporter = new JRXmlExporter(); 245 } 246 return exporter; 247 } 248 249 252 public boolean executeAction() { 253 254 String reportOutputType = getInputStringValue("output-type"); if (debug) 257 debug(Messages.getString("JasperReport.DEBUG_OUTPUT_TYPE", reportOutputType)); String mimeType = null; 259 if ("html".equals(reportOutputType)) { mimeType = "text/html"; } else if ("pdf".equals(reportOutputType)) { mimeType = "application/pdf"; } else if ("xls".equals(reportOutputType)) { mimeType = "application/vnd.ms-excel"; } else if ("csv".equals(reportOutputType)) { mimeType = "text/text"; } else if ("xml".equals(reportOutputType)) { mimeType = "text/xml"; } 270 String extension = "." + reportOutputType; if (mimeType == null) { 272 error(Messages.getErrorString("JasperReport.ERROR_0011_OUTPUT_TYPE_INVALID")); return false; 274 } 275 276 HashMap allReportParameters = new HashMap (); 278 279 String resourceName = new String (); 282 283 Set resources = getResourceNames(); 284 HashSet compiledReportPaths = new HashSet (); 285 286 Iterator resourcesIterator = resources.iterator(); 287 288 while (resourcesIterator.hasNext()) { 289 resourceName = (String ) resourcesIterator.next(); 290 IActionResource resource = getResource(resourceName); 291 292 String reportDefinitionPath; 294 if (resource.getSourceType() == IActionResource.SOLUTION_FILE_RESOURCE) { 295 reportDefinitionPath = PentahoSystem.getApplicationContext().getSolutionPath(resource.getAddress()); 296 } else { 297 reportDefinitionPath = resource.getAddress(); 298 } 299 if (reportDefinitionPath == null) { 300 error(Messages.getErrorString("JasperReport.ERROR_0008_REPORT_DEFINITION_UNREADABLE")); return false; 302 } 303 304 if (debug) 305 debug(Messages.getString("JasperReport.DEBUG_RUNNING_REPORT", reportDefinitionPath)); 307 String compiledReportPath = null; 311 312 File sourceFile = new File (reportDefinitionPath); 314 String reportBaseName = sourceFile.getName(); 315 int extensionIdx = reportBaseName.lastIndexOf("."); if (extensionIdx > 0) 317 reportBaseName = reportBaseName.substring(0, extensionIdx); 318 319 if (reportDefinitionPath.endsWith(COMPILED_JASPER_REPORTS_EXTENSION)) { 322 compiledReportPath = reportDefinitionPath; 323 } else { 324 if (!sourceFile.exists()) { 326 error(Messages.getErrorString("JasperReport.ERROR_0009_REPORT_DEFINITION_MISSING", reportDefinitionPath)); return false; 328 } 329 if (debug) 330 debug(Messages.getString("JasperReport.DEBUG_REPORT_FILE_FOUND")); String reportDirectory = sourceFile.getParent(); 334 if (!reportDirectory.endsWith(File.separator)) { 335 reportDirectory = reportDirectory + File.separator; 336 } 337 compiledReportPath = reportDirectory + reportBaseName + COMPILED_JASPER_REPORTS_EXTENSION; 338 compiledReportPaths.add(compiledReportPath); 339 340 if (debug) 341 debug(Messages.getString("JasperReport.DEBUG_COMPILED_REPORT_LOCATION", compiledReportPath)); File compiledReportFile = new File (compiledReportPath); 344 345 if (!compiledReportFile.exists() || sourceFile.lastModified() > compiledReportFile.lastModified()) { 348 if (debug) 349 debug(Messages.getString("JasperReport.DEBUG_COMPILING_REPORT")); 354 System.setProperty("jasper.reports.compiler.class", "net.sf.jasperreports.engine.design.JRJdtCompiler"); 357 try { 359 JasperCompileManager.compileReportToFile(reportDefinitionPath, compiledReportPath); 360 } catch (JRException jre) { 361 error(Messages.getErrorString("JasperReport.ERROR_0010_UNABLE_TO_COMPILE", reportDefinitionPath, compiledReportPath), jre); return false; 363 } 364 if (debug) 365 debug(Messages.getString("JasperReport.DEBUG_COMPILED_OK")); } 367 } 368 369 371 Map reportParameters = new HashMap (); 375 376 if (debug) 378 debug(Messages.getString("JasperReport.DEBUG_LOADING_REPORT_DESIGN")); JasperReport jrreport = null; 380 try { 381 jrreport = (JasperReport) JRLoader.loadObject(compiledReportPath); 382 } catch (JRException jre) { 383 error(Messages.getErrorString("JasperReport.ERROR_0012_REPORT_DESIGN_NO_LOADABLE", compiledReportPath), jre); return false; 385 } 386 387 JRParameter[] jrparams = jrreport.getParameters(); 388 if (debug) 389 debug(Messages.getString("JasperReport.DEBUG_LOADED_DESIGN", Integer.toString(jrparams.length))); 391 boolean needToPrompt = false; 394 for (int i = 0; i < jrparams.length; i++) { 395 JRParameter param = jrparams[i]; 396 if (param.isSystemDefined()) 397 continue; 398 String parameterName = param.getName(); 399 String parameterValue = null; 400 if (isDefinedInput(parameterName)) { 401 parameterValue = getInputStringValue(parameterName); 402 } 403 if (parameterValue != null && parameterValue.length() != 0) { 404 if (debug) 406 debug(Messages.getString("JasperReport.DEBUG_ADDING_PARAMETER", parameterName, parameterValue)); reportParameters.put(parameterName, parameterValue); 408 } 409 else if (param.isForPrompting()) { 411 if (debug) 412 debug(Messages.getString("JasperReport.DEBUG_PARAMETER_NEEDED", parameterName)); if (feedbackAllowed()) { 415 416 IActionParameter paramParameter = getInputParameter(parameterName); 417 if (paramParameter.getPromptStatus() != IActionParameter.PROMPT_PENDING) { 418 String displayName = param.getDescription(); 419 if (displayName == null || displayName.trim().length() == 0) 420 displayName = parameterName; 421 String defaultValue = ""; createFeedbackParameter(parameterName, displayName, "", defaultValue, true); } 424 needToPrompt = true; 425 } 426 } 427 } 428 429 if (needToPrompt) { 433 setFeedbackMimeType("text/html"); return true; 440 } 441 else if (getRuntimeContext().isPromptPending() ) { 442 return true; 443 } 444 allReportParameters.put(compiledReportPath, reportParameters); 445 } 446 447 OutputStream outputStream = getDefaultOutputStream(); 450 if (isDefinedOutput(REPORT_OUTPUT)) { 451 outputStream = getOutputStream(REPORT_OUTPUT, mimeType, extension); 452 } else if (getOutputNames().size() == 1) { 453 String outputName = (String ) getOutputNames().iterator().next(); 454 IContentItem contentItem = getOutputContentItem(outputName); 455 contentItem.setMimeType(mimeType); 456 try { 457 outputStream = contentItem.getOutputStream(getActionName()); 458 } catch (Exception e) { 459 } 460 } 461 if (getOutputNames().size() == 0) { 462 warn(Messages.getString("Base.WARN_NO_OUTPUT_STREAM")); outputStream = getDefaultOutputStream(); 467 if (outputStream != null) { 468 setOutputMimeType(mimeType); 469 } 470 } 471 if (outputStream == null) { 472 error(Messages.getErrorString("JasperReport.ERROR_0013_OUTPUT_STREAM_INVALID")); return false; 474 } 475 Connection conn = getConnection(); 476 if (conn == null) 477 return false; 478 try { 479 480 String reportBaseName = new String (); 481 List jasperPrintList = new ArrayList (); 482 483 Iterator compiledReportPathIterator = compiledReportPaths.iterator(); 484 485 while(compiledReportPathIterator.hasNext()) { 486 String compiledReportPath = (String ) compiledReportPathIterator.next(); 487 488 Map reportParameters = (Map ) allReportParameters.get(compiledReportPath); 491 JasperPrint jrprint = JasperFillManager.fillReport(compiledReportPath, reportParameters, conn); 492 jasperPrintList.add(jrprint); 494 495 String lastReportBaseName = compiledReportPath; 497 int extensionIdx = lastReportBaseName.lastIndexOf("."); if (extensionIdx > 0) 499 lastReportBaseName = lastReportBaseName.substring(0, extensionIdx); 500 501 reportBaseName = reportBaseName.concat(lastReportBaseName); 502 } 503 504 JRExporter exporter = getExporter(reportOutputType, reportBaseName); 505 506 List jasperPrintListReversed = new ArrayList (); 508 for (int i = jasperPrintList.size() - 1; i >= 0; i--) { 509 jasperPrintListReversed.add(jasperPrintList.get(i)); 510 } 511 512 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintListReversed); 514 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); 515 516 exporter.exportReport(); 518 } catch (JRException jre) { 519 error(Messages.getErrorString("JasperReport.ERROR_0014_REPORT_EXECUTION_FAILED"), jre); return false; 521 } finally { 522 try { 523 conn.close(); 524 } catch (SQLException ignored) { 525 } 526 } 527 return true; 528 } 529 530 public boolean init() { 531 533 return true; 534 } 535 536 public void done() { 537 } 539 540 private Connection getConnection() { 541 try { 542 String jndiUrl = getInputStringValue(StandardSettings.JNDI); 547 Connection conn = null; 548 if (jndiUrl != null) { 549 DataSource ds = DatasourceHelper.getDataSourceFromJndi(jndiUrl); 550 return ds.getConnection(); 551 } else { 552 String driver = getInputStringValue(StandardSettings.DRIVER); 553 String connectString = getInputStringValue(StandardSettings.CONNECTION); 554 String user = getInputStringValue(StandardSettings.USERID); 555 String password = getInputStringValue(StandardSettings.PASSWORD); 556 Class.forName(driver); 557 conn = DriverManager.getConnection(connectString, user, password); 558 } 559 return conn; 560 } catch (ClassNotFoundException cnfe) { 561 error(Messages.getErrorString("JasperReport.ERROR_0015_JDBC_DRIVER_LOAD_FAILED")); } catch (SQLException se) { 563 error(Messages.getErrorString("JasperReport.ERROR_0016_DATABASE_CONNECTION_FAILED"), se); } catch (NamingException ne) { 565 error(Messages.getErrorString("JasperReport.ERROR_0016_DATABASE_CONNECTION_FAILED"), ne); } 567 return null; 568 } 569 } 570 | Popular Tags |