From 7de98ee67fd45860f5d0eff65a9deb5db25a75bd Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Thu, 17 Jan 2008 08:23:51 +0000 Subject: [PATCH] FR [ 1873511 ] Allow jasper reports in customization.jar --- .../org/compiere/report/ReportStarter.java | 98 ++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/JasperReports/src/org/compiere/report/ReportStarter.java b/JasperReports/src/org/compiere/report/ReportStarter.java index 9ebd10c764..14c0b8bcad 100644 --- a/JasperReports/src/org/compiere/report/ReportStarter.java +++ b/JasperReports/src/org/compiere/report/ReportStarter.java @@ -1,5 +1,4 @@ /* - * Class . */ package org.compiere.report; @@ -10,6 +9,7 @@ import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.math.BigDecimal; import java.net.InetAddress; import java.net.MalformedURLException; @@ -443,6 +443,11 @@ public class ReportStarter implements ProcessCall, ClientProcess { { subreports = getAttachmentSubreports(reportPath); } + else if (reportPath.startsWith("resource:")) + { + subreports = getResourceSubreports(jasperName + "Subreport", reportPath, fileExtension); + } + // TODO: Implement file:/ lookup for subreports else { // Locate subreports from local/remote filesystem @@ -468,6 +473,9 @@ public class ReportStarter implements ProcessCall, ClientProcess { File resFile = null; if (reportPath.startsWith("attachment:") && attachment != null) { resFile = getAttachmentResourceFile(jasperName, currLang); + } else if (reportPath.startsWith("resource:")) { + resFile = getResourcesForResourceFile(jasperName, currLang); + // TODO: Implement file:/ for resources } else { File[] resources = reportDir.listFiles( new FileFilter( jasperName, reportDir, ".properties")); // try baseName + "_" + language @@ -557,6 +565,22 @@ public class ReportStarter implements ProcessCall, ClientProcess { return resFile; } + /** + * Get .property resource file from resources + * @param jasperName + * @param currLang + * @return File + */ + private File getResourcesForResourceFile(String jasperName, Language currLang) { + File resFile = null; + try { + resFile = getFileAsResource(jasperName+currLang.getLocale().getLanguage()+".properties"); + } catch (Exception e) { + // ignore exception - file couldn't exist + } + return resFile; + } + /** * Get subreports from attachment. Assume all other jasper attachment is subreport. * @param reportPath @@ -580,6 +604,39 @@ public class ReportStarter implements ProcessCall, ClientProcess { return subreportsTemp; } + /** + * Search for additional subreports deployed as resources + * @param reportName The original reportname + * @param reportPath The full path to the parent report + * @param fileExtension The file extension of the parent report + * @return An Array of File objects referencing to the downloaded subreports + */ + private File[] getResourceSubreports(String reportName, String reportPath, String fileExtension) + { + ArrayList subreports = new ArrayList(); + String remoteDir = reportPath.substring(0, reportPath.lastIndexOf("/")); + + // Currently check hardcoded for max. 10 subreports + for(int i=1; i<10; i++) + { + // Check if subreport number i exists + File subreport = null; + try { + subreport = getFileAsResource(remoteDir + "/" + reportName + i + fileExtension); + } catch (Exception e) { + // just ignore it + } + if(subreport == null) // Subreport doesn't exist, abort further approaches + break; + + subreports.add(subreport); + } + + File[] subreportsTemp = new File[0]; + subreportsTemp = subreports.toArray(subreportsTemp); + return subreportsTemp; + } + /** * @author alinv * @param reportPath @@ -620,6 +677,13 @@ public class ReportStarter implements ProcessCall, ClientProcess { log.warning(e.getLocalizedMessage()); reportFile = null; } + } else if (reportPath.startsWith("resource:")) { + try { + reportFile = getFileAsResource(reportPath); + } catch (Exception e) { + log.warning(e.getLocalizedMessage()); + reportFile = null; + } } else { reportFile = new File(REPORT_HOME, reportPath); } @@ -629,6 +693,36 @@ public class ReportStarter implements ProcessCall, ClientProcess { return reportFile; } + /** + * @param reportPath + * @return + * @throws Exception + */ + private File getFileAsResource(String reportPath) throws Exception { + File reportFile; + String name = reportPath.substring("resource:".length()).trim(); + String localName = name.replace('/', '_'); + log.info("reportPath = " + reportPath); + log.info("getting resource from = " + getClass().getClassLoader().getResource(name)); + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(name); + String localFile = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + localName; + log.info("localFile = " + localFile); + reportFile = new File(localFile); + + OutputStream out = null; + out = new FileOutputStream(reportFile); + if (out != null){ + byte buf[]=new byte[1024]; + int len; + while((len=inputStream.read(buf))>0) + out.write(buf,0,len); + out.close(); + inputStream.close(); + } + + return reportFile; + } + /** * Download db attachment * @param reportPath must of syntax attachment:filename @@ -1031,4 +1125,4 @@ public class ReportStarter implements ProcessCall, ClientProcess { } } -} +} \ No newline at end of file