Fixes issue 29745: Add on the renderJr method the option to print on plain text
authorCarlos Aristu <carlos.aristu@openbravo.com>
Wed, 03 Jun 2015 09:43:20 +0200
changeset 26866 814a9e2ca8ce
parent 26865 8fff038cc32e
child 26867 6a8a7601ff96
Fixes issue 29745: Add on the renderJr method the option to print on plain text
modules/org.openbravo.client.application/src/org/openbravo/client/application/report/ReportingUtils.java
src/org/openbravo/base/secureApp/HttpSecureAppServlet.java
src/org/openbravo/erpCommon/utility/DownloadReport.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/report/ReportingUtils.java	Tue Jun 02 19:07:56 2015 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/report/ReportingUtils.java	Wed Jun 03 09:43:20 2015 +0200
@@ -48,6 +48,7 @@
 import net.sf.jasperreports.engine.export.HtmlExporter;
 import net.sf.jasperreports.engine.export.JRCsvExporter;
 import net.sf.jasperreports.engine.export.JRPdfExporter;
+import net.sf.jasperreports.engine.export.JRTextExporter;
 import net.sf.jasperreports.engine.export.JRXlsExporter;
 import net.sf.jasperreports.engine.fill.JRSwapFileVirtualizer;
 import net.sf.jasperreports.engine.util.JRSwapFile;
@@ -58,6 +59,8 @@
 import net.sf.jasperreports.export.SimpleHtmlReportConfiguration;
 import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
 import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
+import net.sf.jasperreports.export.SimpleTextExporterConfiguration;
+import net.sf.jasperreports.export.SimpleTextReportConfiguration;
 import net.sf.jasperreports.export.SimpleWriterExporterOutput;
 import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
 import net.sf.jasperreports.export.type.HtmlSizeUnitEnum;
@@ -97,6 +100,9 @@
    * Used to set the parameter with the URI to retrieve images in HTML reports.
    */
   public static final String IMAGES_URI = "Images URI";
+
+  private static final double TEXT_CHAR_HEIGHT = 10;
+  private static final double TEXT_CHAR_WIDTH = 10;
   private static final Logger log = LoggerFactory.getLogger(ReportingUtils.class);
 
   /**
@@ -332,6 +338,9 @@
     case PDF:
       JasperExportManager.exportReportToPdfFile(jasperPrint, target.getAbsolutePath());
       break;
+    case TXT:
+      saveTxtReportToFile(jasperPrint, target);
+      break;
     case XLS:
       saveExcelReportToFile(jasperPrint, exportParameters, target);
       break;
@@ -370,6 +379,9 @@
     case PDF:
       JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
       break;
+    case TXT:
+      saveTxtReportToOutputStream(jasperPrint, outputStream);
+      break;
     case XLS:
       saveExcelReportToOutputStream(jasperPrint, exportParameters, outputStream);
       break;
@@ -643,6 +655,99 @@
   }
 
   /**
+   * Generates a plain text report from a pre-compiled report and returns it into a file.
+   * 
+   * @param jasperPrint
+   *          JasperPrint object which contains a compiled report.
+   * @param file
+   *          The file used to return the report.
+   * @throws JRException
+   *           In case there is any error generating the report an exception is thrown with the
+   *           error message.
+   */
+  private static void saveTxtReportToFile(JasperPrint jasperPrint, File file) throws JRException {
+    final JRTextExporter textExporter = new JRTextExporter();
+    SimpleExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
+    SimpleWriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(file);
+
+    // Default text configuration that can be overridden in the .jrxml template itself
+    SimpleTextExporterConfiguration textExporterConfiguration = new SimpleTextExporterConfiguration();
+    textExporterConfiguration.setOverrideHints(false);
+    textExporter.setConfiguration(textExporterConfiguration);
+    // Default item text configuration that can be overridden in the .jrxml template itself
+    SimpleTextReportConfiguration textReportConfiguration = new SimpleTextReportConfiguration();
+    textReportConfiguration.setCharHeight(new Float(TEXT_CHAR_HEIGHT));
+    textReportConfiguration.setCharWidth(new Float(TEXT_CHAR_WIDTH));
+    textReportConfiguration.setOverrideHints(false);
+    textExporter.setConfiguration(textReportConfiguration);
+
+    textExporter.setExporterInput(exporterInput);
+    textExporter.setExporterOutput(exporterOutput);
+    textExporter.exportReport();
+  }
+
+  /**
+   * Generates a plain text report from a pre-compiled report and returns it into an output stream.
+   * 
+   * @param jasperPrint
+   *          JasperPrint object which contains a compiled report.
+   * @param outputStream
+   *          The output stream used to return the report.
+   * @throws JRException
+   *           In case there is any error generating the report an exception is thrown with the
+   *           error message.
+   */
+  private static void saveTxtReportToOutputStream(JasperPrint jasperPrint, OutputStream outputStream)
+      throws JRException {
+    final JRTextExporter textExporter = new JRTextExporter();
+    SimpleExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
+    SimpleWriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outputStream);
+
+    // Default text configuration that can be overridden in the .jrxml template itself
+    SimpleTextExporterConfiguration textExporterConfiguration = new SimpleTextExporterConfiguration();
+    textExporterConfiguration.setOverrideHints(false);
+    textExporter.setConfiguration(textExporterConfiguration);
+    // Default item text configuration that can be overridden in the .jrxml template itself
+    SimpleTextReportConfiguration textReportConfiguration = new SimpleTextReportConfiguration();
+    textReportConfiguration.setCharHeight(new Float(TEXT_CHAR_HEIGHT));
+    textReportConfiguration.setCharWidth(new Float(TEXT_CHAR_WIDTH));
+    textReportConfiguration.setOverrideHints(false);
+    textExporter.setConfiguration(textReportConfiguration);
+
+    textExporter.setExporterInput(exporterInput);
+    textExporter.setExporterOutput(exporterOutput);
+    textExporter.exportReport();
+  }
+
+  /**
+   * Generates a plain text report using the SimpleExporterInput, SimpleWriterExporterOutput,
+   * SimpleTextExporterConfiguration and SimpleTextReportConfiguration received as parameters.
+   * 
+   * @param exporterInput
+   *          SimpleExporterInput object with the input data.
+   * @param exporterOutput
+   *          SimpleWriterExporterOutput object with the output data.
+   * @param textExporterConfiguration
+   *          SimpleTextExporterConfiguration with the configuration data.
+   * @param textReportConfiguration
+   *          SimpleTextReportConfiguration with the item configuration data.
+   * @throws JRException
+   *           In case there is any error generating the report an exception is thrown with the
+   *           error message.
+   */
+  public static void saveTxtReport(SimpleExporterInput exporterInput,
+      SimpleWriterExporterOutput exporterOutput,
+      SimpleTextExporterConfiguration textExporterConfiguration,
+      SimpleTextReportConfiguration textReportConfiguration) throws JRException {
+    final JRTextExporter textExporter = new JRTextExporter();
+    textExporter.setExporterInput(exporterInput);
+    textExporter.setExporterOutput(exporterOutput);
+    textExporter.setConfiguration(textExporterConfiguration);
+    textExporter.setConfiguration(textReportConfiguration);
+    textExporter.exportReport();
+  }
+
+  /**
    * Generates a SimpleXlsReportConfiguration from a parameter map.
    * 
    * This method allows backwards compatibility when generating XLS reports by using a parameter map
@@ -1181,6 +1286,15 @@
       }
     }), //
     /**
+     * TXT export type
+     */
+    @SuppressWarnings("serial")
+    TXT("txt", "100", new HashMap<String, Object>() {
+      {
+        put("IS_IGNORE_PAGINATION", false);
+      }
+    }), //
+    /**
      * XLS export type
      */
     @SuppressWarnings("serial")
@@ -1263,6 +1377,8 @@
         return ExportType.HTML;
       } else if ("PDF".equals(action)) {
         return ExportType.PDF;
+      } else if ("TXT".equals(action)) {
+        return ExportType.TXT;
       } else if ("XLS".equals(action)) {
         return ExportType.XLS;
       } else if ("XML".equals(action)) {
--- a/src/org/openbravo/base/secureApp/HttpSecureAppServlet.java	Tue Jun 02 19:07:56 2015 +0200
+++ b/src/org/openbravo/base/secureApp/HttpSecureAppServlet.java	Wed Jun 03 09:43:20 2015 +0200
@@ -1285,7 +1285,8 @@
         exportParameters.put(ReportingUtils.IMAGES_URI, localAddress + "/servlets/image?image={0}");
         ReportingUtils.exportJR(strReportName, expType, designParameters, os, false, this, data,
             exportParameters);
-      } else if (strOutputType.equals("pdf") || strOutputType.equalsIgnoreCase("xls")) {
+      } else if (strOutputType.equals("pdf") || strOutputType.equalsIgnoreCase("xls")
+          || strOutputType.equalsIgnoreCase("txt") || strOutputType.equalsIgnoreCase("csv")) {
         reportId = UUID.randomUUID();
         File outputFile = new File(globalParameters.strFTPDirectory + "/" + strFileName + "-"
             + (reportId) + "." + strOutputType);
--- a/src/org/openbravo/erpCommon/utility/DownloadReport.java	Tue Jun 02 19:07:56 2015 +0200
+++ b/src/org/openbravo/erpCommon/utility/DownloadReport.java	Wed Jun 03 09:43:20 2015 +0200
@@ -57,6 +57,8 @@
       response.setContentType("application/pdf");
     } else if (extension.equalsIgnoreCase(".csv")) {
       response.setContentType("text/csv");
+    } else if (extension.equalsIgnoreCase(".txt")) {
+      response.setContentType("text/plain");
     } else if (extension.equalsIgnoreCase(".xls")) {
       response.setContentType("application/vnd.ms-excel");
     } else {