IDEMPIERE-2877 Adapt jasper reporting to latest (actually using deprecated methods) / thanks to Tiago Ceridorio (tifece) for the patch

This commit is contained in:
Carlos Ruiz 2017-10-13 17:40:12 +02:00
parent 68fec48868
commit 840673e4e5
3 changed files with 131 additions and 65 deletions

View File

@ -17,6 +17,7 @@ Import-Package: javax.activation,
net.sf.jasperreports.engine,
net.sf.jasperreports.engine.export,
net.sf.jasperreports.engine.util,
net.sf.jasperreports.export,
org.adempiere.report.jasper,
org.apache.commons.codec.binary,
org.apache.ecs,

View File

@ -3,24 +3,10 @@ package org.adempiere.webui.window;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import javax.activation.FileDataSource;
import javax.servlet.http.HttpServletRequest;
import net.sf.jasperreports.engine.DefaultJasperReportsContext;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRCsvExporterParameter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.util.LocalJasperReportsContext;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.webui.LayoutUtils;
@ -44,7 +30,6 @@ import org.compiere.util.Msg;
import org.compiere.util.Util;
import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
@ -59,6 +44,23 @@ import org.zkoss.zul.Separator;
import org.zkoss.zul.Tab;
import org.zkoss.zul.Toolbar;
import net.sf.jasperreports.engine.DefaultJasperReportsContext;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
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.JRXlsExporter;
import net.sf.jasperreports.engine.util.LocalJasperReportsContext;
import net.sf.jasperreports.export.SimpleCsvExporterConfiguration;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleHtmlReportConfiguration;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCloseHandler {
/**
*
@ -66,6 +68,8 @@ public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCl
private static final long serialVersionUID = -7047317766671393738L;
private JasperPrint jasperPrint;
private java.util.List<JasperPrint> jasperPrintList;
private boolean isList;
private Listbox previewType = new Listbox();
private Iframe iframe;
private AMedia media;
@ -87,6 +91,18 @@ public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCl
this.setTitle(title);
m_title = title;
this.jasperPrint = jasperPrint;
this.isList = false;
m_WindowNo = SessionManager.getAppDesktop().registerWindow(this);
setAttribute(IDesktop.WINDOWNO_ATTRIBUTE, m_WindowNo);
init();
}
public ZkJRViewer(java.util.List<JasperPrint> jasperPrintList, String title) {
super();
this.setTitle(title);
m_title = title;
this.jasperPrintList = jasperPrintList;
this.isList = true;
m_WindowNo = SessionManager.getAppDesktop().registerWindow(this);
setAttribute(IDesktop.WINDOWNO_ATTRIBUTE, m_WindowNo);
init();
@ -123,7 +139,7 @@ public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCl
previewType.setMold("select");
attachment = null; // Added by Martin Augustine - Ntier software Services 09/10/2013
if (isCanExport) {
if (isCanExport && !isList) {
previewType.appendItem("PDF", "PDF");
previewType.appendItem("HTML", "HTML");
previewType.appendItem("Excel", "XLS");
@ -309,14 +325,17 @@ public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCl
}
File file = File.createTempFile(prefix, ".html", new File(path));
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE, file);
// Make images available for the HTML output
exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, Executions.getCurrent().getDesktop().getSession().getWebApp().getRealPath("/images/report/"));
HttpServletRequest request = (HttpServletRequest)Executions.getCurrent().getNativeRequest();
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath()+"/images/report/");
HtmlExporter exporter = new HtmlExporter();
SimpleHtmlReportConfiguration htmlConfig = new SimpleHtmlReportConfiguration();
htmlConfig.setEmbedImage(true);
htmlConfig.setAccessibleHtml(true);
if (!isList){
jasperPrintList = new ArrayList<>();
jasperPrintList.add(jasperPrint);
}
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
exporter.setExporterOutput(new SimpleHtmlExporterOutput(file));
exporter.setConfiguration(htmlConfig);
exporter.exportReport();
media = new AMedia(m_title, "html", "text/html", file, false);
} else if ("XLS".equals(reportType)) {
@ -331,9 +350,16 @@ public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCl
// coding For Excel:
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, fos);
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
SimpleXlsReportConfiguration xlsConfig = new SimpleXlsReportConfiguration();
xlsConfig.setOnePagePerSheet(false);
if (!isList){
jasperPrintList = new ArrayList<>();
jasperPrintList.add(jasperPrint);
}
exporterXLS.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
exporterXLS.setExporterOutput(new SimpleOutputStreamExporterOutput(fos));
exporterXLS.setConfiguration(xlsConfig);
exporterXLS.exportReport();
media = new AMedia(m_title + ".xls", "xls", "application/vnd.ms-excel", file, true);
@ -347,8 +373,12 @@ public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCl
File file = File.createTempFile(prefix, ".csv", new File(path));
FileOutputStream fos = new FileOutputStream(file);
JRCsvExporter exporter= new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
if (!isList){
jasperPrintList = new ArrayList<>();
jasperPrintList.add(jasperPrint);
}
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
exporter.setExporterOutput(new SimpleWriterExporterOutput(fos));
exporter.exportReport();
media = new AMedia(m_title + ".csv", "csv", "application/csv", file, true);
@ -363,9 +393,15 @@ public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCl
File file = File.createTempFile(prefix, ".ssv", new File(path));
FileOutputStream fos = new FileOutputStream(file);
JRCsvExporter exporter= new JRCsvExporter();
exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER, ";");
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
SimpleCsvExporterConfiguration csvConfig = new SimpleCsvExporterConfiguration();
csvConfig.setFieldDelimiter(";");
if (!isList){
jasperPrintList = new ArrayList<>();
jasperPrintList.add(jasperPrint);
}
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
exporter.setExporterOutput(new SimpleWriterExporterOutput(fos));
exporter.setConfiguration(csvConfig);
exporter.exportReport();
media = new AMedia(m_title, "ssv", "application/ssv", file, true);
@ -380,7 +416,13 @@ public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCl
private File getPDF() throws IOException, JRException {
String path = System.getProperty("java.io.tmpdir");
String prefix = makePrefix(jasperPrint.getName());
String prefix = null;
if (isList)
prefix = makePrefix(jasperPrintList.get(0).getName())+"_List";
else
prefix = makePrefix(jasperPrint.getName());
if (log.isLoggable(Level.FINE))
{
log.log(Level.FINE, "Path="+path + " Prefix="+prefix);
@ -389,8 +431,12 @@ public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCl
LocalJasperReportsContext context = new LocalJasperReportsContext(DefaultJasperReportsContext.getInstance());
context.setClassLoader(JRPdfExporter.class.getClassLoader());
JRPdfExporter exporter = new JRPdfExporter(context);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, file.getAbsolutePath());
if (!isList){
jasperPrintList = new ArrayList<>();
jasperPrintList.add(jasperPrint);
}
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
exporter.exportReport();
return file;
}

View File

@ -3,6 +3,8 @@ package org.adempiere.webui.window;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import java.util.List;
import org.adempiere.report.jasper.JRViewerProvider;
import org.adempiere.webui.apps.AEnv;
import org.adempiere.webui.component.Window;
@ -28,4 +30,21 @@ public class ZkJRViewerProvider implements JRViewerProvider {
AEnv.executeAsyncDesktopTask(runnable);
}
public void openViewer(final List<JasperPrint> jasperPrintList, final String title)
throws JRException {
Runnable runnable = new Runnable() {
@Override
public void run() {
Window viewer = new ZkJRViewer(jasperPrintList, title);
viewer.setAttribute(Window.MODE_KEY, Window.MODE_EMBEDDED);
viewer.setAttribute(Window.INSERT_POSITION_KEY, Window.INSERT_NEXT);
viewer.setAttribute(WindowContainer.DEFER_SET_SELECTED_TAB, Boolean.TRUE);
SessionManager.getAppDesktop().showWindow(viewer);
}
};
AEnv.executeAsyncDesktopTask(runnable);
}
}