Fixed 6833
authorStefan Hühner <stefan.huehner@openbravo.com>
Tue, 20 Jan 2009 17:31:09 +0000
changeset 2670 8c903e6470ad
parent 2669 dfa1375ef2cf
child 2671 04154345c847
Fixed 6833
Unify the jrxml file-loading copies in three places:
- HttpSecureAppServlet.renderJR, ReportManager, JasperProcess (currently unused)
- fix two copies ReportManager, JasperProcess to use the new translation handling in trunk
fixing them with language != en_US
src/org/openbravo/base/secureApp/HttpSecureAppServlet.java
src/org/openbravo/erpCommon/ad_process/JasperProcess.java
src/org/openbravo/erpCommon/utility/Utility.java
src/org/openbravo/erpCommon/utility/reporting/ReportManager.java
--- a/src/org/openbravo/base/secureApp/HttpSecureAppServlet.java	Tue Jan 20 17:00:41 2009 +0000
+++ b/src/org/openbravo/base/secureApp/HttpSecureAppServlet.java	Tue Jan 20 17:31:09 2009 +0000
@@ -14,7 +14,6 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.PrintWriter;
 import java.sql.Connection;
 import java.text.DecimalFormat;
@@ -36,17 +35,14 @@
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.JRExporterParameter;
 import net.sf.jasperreports.engine.JRParameter;
-import net.sf.jasperreports.engine.JasperCompileManager;
 import net.sf.jasperreports.engine.JasperExportManager;
 import net.sf.jasperreports.engine.JasperFillManager;
 import net.sf.jasperreports.engine.JasperPrint;
 import net.sf.jasperreports.engine.JasperReport;
-import net.sf.jasperreports.engine.design.JasperDesign;
 import net.sf.jasperreports.engine.export.JExcelApiExporter;
 import net.sf.jasperreports.engine.export.JExcelApiExporterParameter;
 import net.sf.jasperreports.engine.export.JRHtmlExporter;
 import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
-import net.sf.jasperreports.engine.xml.JRXmlLoader;
 
 import org.openbravo.authentication.AuthenticationException;
 import org.openbravo.authentication.AuthenticationManager;
@@ -63,7 +59,6 @@
 import org.openbravo.erpCommon.utility.OBError;
 import org.openbravo.erpCommon.utility.PrintJRData;
 import org.openbravo.erpCommon.utility.Utility;
-import org.openbravo.uiTranslation.TranslationHandler;
 import org.openbravo.utils.Replace;
 import org.openbravo.xmlEngine.XmlDocument;
 import org.w3c.dom.Document;
@@ -986,28 +981,11 @@
         final String strFileName = strReportName.substring(strReportName
                 .lastIndexOf("/") + 1);
 
-        final File reportFile = new File(strReportName);
-        log4j.debug("renderJR() - strReportName: " + strReportName);
-
-        InputStream reportInputStream = null;
-        if (reportFile.exists()) {
-            final TranslationHandler handler = new TranslationHandler(this);
-            handler.prepareFile(strReportName, strLanguage, reportFile);
-            reportInputStream = handler.getInputStream();
-        }
         ServletOutputStream os = null;
         try {
-            JasperDesign jasperDesign;
-            if (reportInputStream != null) {
-                log4j.debug("Jasper report being created with inputStream.");
-                jasperDesign = JRXmlLoader.load(reportInputStream);
-            } else {
-                log4j.debug("Jasper report being created with strReportName.");
-                jasperDesign = JRXmlLoader.load(strReportName);
-            }
 
-            final JasperReport jasperReport = JasperCompileManager
-                    .compileReport(jasperDesign);
+            final JasperReport jasperReport = Utility
+                    .getTranslatedJasperReport(this, strReportName, strLanguage);
             if (designParameters == null)
                 designParameters = new HashMap<String, Object>();
 
--- a/src/org/openbravo/erpCommon/ad_process/JasperProcess.java	Tue Jan 20 17:00:41 2009 +0000
+++ b/src/org/openbravo/erpCommon/ad_process/JasperProcess.java	Tue Jan 20 17:31:09 2009 +0000
@@ -12,17 +12,14 @@
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.JRExporterParameter;
 import net.sf.jasperreports.engine.JRParameter;
-import net.sf.jasperreports.engine.JasperCompileManager;
 import net.sf.jasperreports.engine.JasperExportManager;
 import net.sf.jasperreports.engine.JasperFillManager;
 import net.sf.jasperreports.engine.JasperPrint;
 import net.sf.jasperreports.engine.JasperReport;
-import net.sf.jasperreports.engine.design.JasperDesign;
 import net.sf.jasperreports.engine.export.JExcelApiExporter;
 import net.sf.jasperreports.engine.export.JExcelApiExporterParameter;
 import net.sf.jasperreports.engine.export.JRHtmlExporter;
 import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
-import net.sf.jasperreports.engine.xml.JRXmlLoader;
 
 import org.apache.log4j.Logger;
 import org.openbravo.base.ConfigParameters;
@@ -80,7 +77,7 @@
         Locale locLocale = new Locale(strLanguage.substring(0, 2), strLanguage
                 .substring(3, 5));
 
-        String strBaseDesign = getBaseDesignPath(config, strLanguage);
+        String strBaseDesign = getBaseDesignPath(config);
 
         strReportName = Replace.replace(Replace.replace(strReportName,
                 "@basedesign@", strBaseDesign), "@attach@", strAttach);
@@ -90,9 +87,9 @@
         // FIXME: os is never assigned, but used leading to an NPE
         ServletOutputStream os = null;
         try {
-            JasperDesign jasperDesign = JRXmlLoader.load(strReportName);
-            JasperReport jasperReport = JasperCompileManager
-                    .compileReport(jasperDesign);
+            JasperReport jasperReport = Utility.getTranslatedJasperReport(
+                    connection, strReportName, strLanguage);
+
             if (designParameters == null)
                 designParameters = new HashMap<String, Object>();
 
@@ -217,18 +214,13 @@
      * Returns the absolute path to the correct language subfolder within the
      * context's src-loc folder.
      * 
-     * @param language
-     *            String specifying the language folder required, e.g. es_ES
      * @return String with the absolute path on the local drive.
      */
-    protected String getBaseDesignPath(ConfigParameters config, String language) {
+    protected String getBaseDesignPath(ConfigParameters config) {
         log4j.info("*********************Base path: "
                 + config.strBaseDesignPath);
         String strNewAddBase = config.strDefaultDesignPath;
         String strFinal = config.strBaseDesignPath;
-        if (!language.equals("") && !language.equals("en_US")) {
-            strNewAddBase = language;
-        }
         if (!strFinal.endsWith("/" + strNewAddBase)) {
             strFinal += "/" + strNewAddBase;
         }
--- a/src/org/openbravo/erpCommon/utility/Utility.java	Tue Jan 20 17:00:41 2009 +0000
+++ b/src/org/openbravo/erpCommon/utility/Utility.java	Tue Jan 20 17:31:09 2009 +0000
@@ -26,6 +26,7 @@
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.sql.Connection;
@@ -44,6 +45,12 @@
 
 import javax.servlet.ServletException;
 
+import net.sf.jasperreports.engine.JRException;
+import net.sf.jasperreports.engine.JasperCompileManager;
+import net.sf.jasperreports.engine.JasperReport;
+import net.sf.jasperreports.engine.design.JasperDesign;
+import net.sf.jasperreports.engine.xml.JRXmlLoader;
+
 import org.apache.log4j.Logger;
 import org.openbravo.base.secureApp.OrgTree;
 import org.openbravo.base.secureApp.VariablesSecureApp;
@@ -53,6 +60,7 @@
 import org.openbravo.database.ConnectionProvider;
 import org.openbravo.erpCommon.reference.PInstanceProcessData;
 import org.openbravo.model.ad.ui.Window;
+import org.openbravo.uiTranslation.TranslationHandler;
 import org.openbravo.utils.Replace;
 
 /**
@@ -230,8 +238,7 @@
     /**
      * 
      * Formats a message String into a String for html presentation. Escapes the
-     * &, <, >, " and ®, and replace the \n by <br/>
-     * and \r for space.
+     * &, <, >, " and ®, and replace the \n by <br/> and \r for space.
      * 
      * IMPORTANT! : this method is designed to transform the output of
      * Utility.messageBD method, and this method replaces \n by \\n and \" by
@@ -2236,4 +2243,34 @@
         return false;
     }
 
+    public static JasperReport getTranslatedJasperReport(
+            ConnectionProvider conn, String reportName, String language)
+            throws JRException {
+
+        log4j.debug("translate report: " + reportName + " for language: "
+                + language);
+
+        File reportFile = new File(reportName);
+
+        InputStream reportInputStream = null;
+        if (reportFile.exists()) {
+            TranslationHandler handler = new TranslationHandler(conn);
+            handler.prepareFile(reportName, language, reportFile);
+            reportInputStream = handler.getInputStream();
+        }
+        JasperDesign jasperDesign;
+        if (reportInputStream != null) {
+            log4j.debug("Jasper report being created with inputStream.");
+            jasperDesign = JRXmlLoader.load(reportInputStream);
+        } else {
+            log4j.debug("Jasper report being created with strReportName.");
+            jasperDesign = JRXmlLoader.load(reportName);
+        }
+
+        JasperReport jasperReport = JasperCompileManager
+                .compileReport(jasperDesign);
+
+        return jasperReport;
+    }
+
 }
--- a/src/org/openbravo/erpCommon/utility/reporting/ReportManager.java	Tue Jan 20 17:00:41 2009 +0000
+++ b/src/org/openbravo/erpCommon/utility/reporting/ReportManager.java	Tue Jan 20 17:31:09 2009 +0000
@@ -26,13 +26,10 @@
 
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.JRParameter;
-import net.sf.jasperreports.engine.JasperCompileManager;
 import net.sf.jasperreports.engine.JasperExportManager;
 import net.sf.jasperreports.engine.JasperFillManager;
 import net.sf.jasperreports.engine.JasperPrint;
 import net.sf.jasperreports.engine.JasperReport;
-import net.sf.jasperreports.engine.design.JasperDesign;
-import net.sf.jasperreports.engine.xml.JRXmlLoader;
 
 import org.apache.log4j.Logger;
 import org.openbravo.base.secureApp.ClassInfoData;
@@ -76,19 +73,11 @@
                     _strDefaultDesignPath.length() - 1);
     }
 
-    private String getBaseDesignPath(String language) {
-        String designPath = _strDefaultDesignPath;
-        if (!language.equals("") && !language.equals("en_US"))
-            designPath = language;
-        designPath = _prefix + "/" + _strBaseDesignPath + "/" + designPath;
-
-        return designPath;
-    }
-
     public void processReport(Report report, VariablesSecureApp variables)
             throws ReportingException {
         String language = variables.getLanguage();
-        String baseDesignPath = getBaseDesignPath(language);
+        String baseDesignPath = _prefix + "/" + _strBaseDesignPath + "/"
+                + _strDefaultDesignPath;
         Locale locale = new Locale(language.substring(0, 2), language
                 .substring(3, 5));
 
@@ -104,19 +93,14 @@
 
         designParameters.put("DOCUMENT_ID", report.getDocumentId());
         designParameters.put("TEMPLATE_LOCATION", templateLocation);
-        designParameters.put("BASE_ATTACH", _strAttachmentPath); // TODO: Rename
-                                                                 // parameter to
-                                                                 // BASE_ATTACH_PATH
-        designParameters.put("BASE_WEB", _strBaseWeb); // TODO: Do not use Base
-                                                       // web, this is an url
-                                                       // and generates web
-                                                       // traffic, a local path
-                                                       // reference should be
-                                                       // used
+        // TODO: Rename parameter to BASE_ATTACH_PATH
+        designParameters.put("BASE_ATTACH", _strAttachmentPath);
+        // TODO: Do not use Base web, this is an url and generates web traffic,
+        // a local path reference should be used
+        designParameters.put("BASE_WEB", _strBaseWeb);
         try {
-            JasperDesign jasperDesign = JRXmlLoader.load(templateFile);
-            JasperReport jasperReport = JasperCompileManager
-                    .compileReport(jasperDesign);
+            JasperReport jasperReport = Utility.getTranslatedJasperReport(
+                    _connectionProvider, templateFile, language);
             if (designParameters == null)
                 designParameters = new HashMap<String, Object>();