From dd2766d88c7f345379995d83e576a7e992488320 Mon Sep 17 00:00:00 2001 From: Deepak Pansheriya Date: Wed, 27 Nov 2019 13:22:49 +0530 Subject: [PATCH] IDEMPIERE-3721 : Adding Archive toolbar button on Jasper viewer --- .../src/org/compiere/report/JasperViewer.java | 16 ++-- .../report/SwingJRViewerProvider.java | 5 +- .../report/jasper/JRViewerProvider.java | 4 +- .../report/jasper/JRViewerProviderList.java | 4 +- .../report/jasper/ReportStarter.java | 8 +- .../adempiere/webui/window/ZkJRViewer.java | 81 ++++++++++++++++++- .../webui/window/ZkJRViewerProvider.java | 9 ++- 7 files changed, 107 insertions(+), 20 deletions(-) diff --git a/org.adempiere.report.jasper.swing/src/org/compiere/report/JasperViewer.java b/org.adempiere.report.jasper.swing/src/org/compiere/report/JasperViewer.java index b4759f60ac..9ff7892c35 100644 --- a/org.adempiere.report.jasper.swing/src/org/compiere/report/JasperViewer.java +++ b/org.adempiere.report.jasper.swing/src/org/compiere/report/JasperViewer.java @@ -18,13 +18,14 @@ import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import org.compiere.db.CConnection; - +import org.compiere.model.PrintInfo; public class JasperViewer extends javax.swing.JFrame { private static final long serialVersionUID = 1192807883081180999L; private String m_title; + private PrintInfo m_printInfo; /** Creates new form JasperViewer */ /** @@ -32,11 +33,12 @@ public class JasperViewer extends javax.swing.JFrame { * @param frameTitle Title to be displayed * @throws JRException */ - protected JasperViewer(JasperPrint jasperPrint,String frameTitle) throws JRException { + protected JasperViewer(JasperPrint jasperPrint,String frameTitle, PrintInfo printInfo) throws JRException { this.m_title = frameTitle; initComponents(); JasperReportViewer viewer = new JasperReportViewer(this, jasperPrint); this.pnlMain.add(viewer, BorderLayout.CENTER); + m_printInfo = printInfo; } @@ -85,12 +87,12 @@ public class JasperViewer extends javax.swing.JFrame { /** * */ - public static void viewReport(JasperPrint jasperPrint) throws JRException { - JasperViewer jasperViewer = new JasperViewer(jasperPrint,"JasperReport"); + public static void viewReport(JasperPrint jasperPrint, PrintInfo printInfo) throws JRException { + JasperViewer jasperViewer = new JasperViewer(jasperPrint,"JasperReport", printInfo); jasperViewer.setVisible(true); } - public static void viewReport(JasperPrint jasperPrint,String frameTitle) throws JRException { - JasperViewer jasperViewer = new JasperViewer(jasperPrint,frameTitle); + public static void viewReport(JasperPrint jasperPrint,String frameTitle, PrintInfo printInfo) throws JRException { + JasperViewer jasperViewer = new JasperViewer(jasperPrint,frameTitle, printInfo); jasperViewer.setVisible(true); } @@ -105,7 +107,7 @@ public class JasperViewer extends javax.swing.JFrame { try { JasperReport myjasperReport = (JasperReport) JasperCompileManager.compileReport(args[0] ); JasperPrint myjasperPrint = JasperFillManager.fillReport( myjasperReport, new HashMap(), getConnection()); - JasperViewer.viewReport(myjasperPrint); + JasperViewer.viewReport(myjasperPrint, null); } catch (Exception e) { e.printStackTrace(); diff --git a/org.adempiere.report.jasper.swing/src/org/compiere/report/SwingJRViewerProvider.java b/org.adempiere.report.jasper.swing/src/org/compiere/report/SwingJRViewerProvider.java index b02603e47e..2d0a4820d2 100644 --- a/org.adempiere.report.jasper.swing/src/org/compiere/report/SwingJRViewerProvider.java +++ b/org.adempiere.report.jasper.swing/src/org/compiere/report/SwingJRViewerProvider.java @@ -1,14 +1,15 @@ package org.compiere.report; import org.adempiere.report.jasper.JRViewerProvider; +import org.compiere.model.PrintInfo; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperPrint; public class SwingJRViewerProvider implements JRViewerProvider { - public void openViewer(JasperPrint jasperPrint, String title) throws JRException { - JasperViewer jasperViewer = new JasperViewer( jasperPrint, title); + public void openViewer(JasperPrint jasperPrint, String title, PrintInfo printInfo) throws JRException { + JasperViewer jasperViewer = new JasperViewer( jasperPrint, title, printInfo); jasperViewer.setExtendedState(jasperViewer.getExtendedState() | javax.swing.JFrame.MAXIMIZED_BOTH); jasperViewer.setVisible(true); } diff --git a/org.adempiere.report.jasper/src/org/adempiere/report/jasper/JRViewerProvider.java b/org.adempiere.report.jasper/src/org/adempiere/report/jasper/JRViewerProvider.java index 323486b96c..aa1f647019 100644 --- a/org.adempiere.report.jasper/src/org/adempiere/report/jasper/JRViewerProvider.java +++ b/org.adempiere.report.jasper/src/org/adempiere/report/jasper/JRViewerProvider.java @@ -1,9 +1,11 @@ package org.adempiere.report.jasper; +import org.compiere.model.PrintInfo; + import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperPrint; public interface JRViewerProvider { - public void openViewer(JasperPrint jasperPrint, String title) throws JRException; + public void openViewer(JasperPrint jasperPrint, String title, PrintInfo printInfo) throws JRException; } diff --git a/org.adempiere.report.jasper/src/org/adempiere/report/jasper/JRViewerProviderList.java b/org.adempiere.report.jasper/src/org/adempiere/report/jasper/JRViewerProviderList.java index 5718a81331..a6ad5b4f4e 100644 --- a/org.adempiere.report.jasper/src/org/adempiere/report/jasper/JRViewerProviderList.java +++ b/org.adempiere.report.jasper/src/org/adempiere/report/jasper/JRViewerProviderList.java @@ -2,10 +2,12 @@ package org.adempiere.report.jasper; import java.util.List; +import org.compiere.model.PrintInfo; + import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperPrint; public interface JRViewerProviderList { - public void openViewer(List jasperPrintList, String title) throws JRException; + public void openViewer(List jasperPrintList, String title, PrintInfo printInfo) throws JRException; } diff --git a/org.adempiere.report.jasper/src/org/adempiere/report/jasper/ReportStarter.java b/org.adempiere.report.jasper/src/org/adempiere/report/jasper/ReportStarter.java index 3295a2a258..90194647e8 100644 --- a/org.adempiere.report.jasper/src/org/adempiere/report/jasper/ReportStarter.java +++ b/org.adempiere.report.jasper/src/org/adempiere/report/jasper/ReportStarter.java @@ -722,13 +722,15 @@ public class ReportStarter implements ProcessCall, ClientProcess } } } else { - if (reportPathList.length == 1) { + if (printInfo == null) + printInfo = new PrintInfo(pi); + if (reportPathList.length == 1) { if (log.isLoggable(Level.INFO)) log.info( "ReportStarter.startProcess run report -"+jasperPrint.getName()); JRViewerProvider viewerLauncher = Service.locator().locate(JRViewerProvider.class).getService(); if (!Util.isEmpty(processInfo.getReportType())) { jasperPrint.setProperty("IDEMPIERE_REPORT_TYPE", processInfo.getReportType()); } - viewerLauncher.openViewer(jasperPrint, pi.getTitle()); + viewerLauncher.openViewer(jasperPrint, pi.getTitle(), printInfo); } else { jasperPrintList.add(jasperPrint); if (idx+1 == reportPathList.length) { @@ -736,7 +738,7 @@ public class ReportStarter implements ProcessCall, ClientProcess if (viewerLauncher == null) { throw new AdempiereException("Can not find a viewer provider for multiple jaspers"); } - viewerLauncher.openViewer(jasperPrintList, pi.getTitle()); + viewerLauncher.openViewer(jasperPrintList, pi.getTitle(), printInfo); } } } diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/ZkJRViewer.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/ZkJRViewer.java index 8f8288b291..519b4d4c75 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/ZkJRViewer.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/ZkJRViewer.java @@ -1,6 +1,8 @@ package org.adempiere.webui.window; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; @@ -21,9 +23,11 @@ import org.adempiere.webui.panel.ITabOnCloseHandler; import org.adempiere.webui.session.SessionManager; import org.adempiere.webui.theme.ThemeManager; import org.adempiere.webui.util.ZKUpdateUtil; +import org.compiere.model.MArchive; import org.compiere.model.MRole; import org.compiere.model.MSysConfig; import org.compiere.model.MUser; +import org.compiere.model.PrintInfo; import org.compiere.tools.FileUtil; import org.compiere.util.CLogger; import org.compiere.util.Env; @@ -91,10 +95,12 @@ public class ZkJRViewer extends Window implements EventListener, ITabOnCl private KeyEvent prevKeyEvent; private String m_title; // local title - embedded windows clear the title + protected ToolBarButton bArchive = new ToolBarButton(); + private PrintInfo m_printInfo; private int mediaVersion = 0; - public ZkJRViewer(JasperPrint jasperPrint, String title) { + public ZkJRViewer(JasperPrint jasperPrint, String title, PrintInfo printInfo) { super(); this.setTitle(title); m_title = title; @@ -102,10 +108,11 @@ public class ZkJRViewer extends Window implements EventListener, ITabOnCl this.isList = false; m_WindowNo = SessionManager.getAppDesktop().registerWindow(this); setAttribute(IDesktop.WINDOWNO_ATTRIBUTE, m_WindowNo); + m_printInfo = printInfo; init(); } - public ZkJRViewer(java.util.List jasperPrintList, String title) { + public ZkJRViewer(java.util.List jasperPrintList, String title, PrintInfo printInfo) { super(); this.setTitle(title); m_title = title; @@ -113,6 +120,7 @@ public class ZkJRViewer extends Window implements EventListener, ITabOnCl this.isList = true; m_WindowNo = SessionManager.getAppDesktop().registerWindow(this); setAttribute(IDesktop.WINDOWNO_ATTRIBUTE, m_WindowNo); + m_printInfo = printInfo; init(); } @@ -202,6 +210,16 @@ public class ZkJRViewer extends Window implements EventListener, ITabOnCl bSendMail.setTooltiptext(Util.cleanAmp(Msg.getMsg(Env.getCtx(), "SendMail"))); toolbar.appendChild(bSendMail); bSendMail.addEventListener(Events.ON_CLICK, this); + + toolbar.appendChild(new Separator("vertical")); + bArchive.setName("Archive"); + if (ThemeManager.isUseFontIconForImage()) + bArchive.setIconSclass("z-icon-Archive"); + else + bArchive.setImage(ThemeManager.getThemeResource("images/Archive24.png")); + bArchive.setTooltiptext(Util.cleanAmp(Msg.getMsg(Env.getCtx(), "Archive"))); + toolbar.appendChild(bArchive); + bArchive.addEventListener(Events.ON_CLICK, this); North north = new North(); layout.appendChild(north); @@ -251,6 +269,8 @@ public class ZkJRViewer extends Window implements EventListener, ITabOnCl cmd_render(); else if (e.getTarget() == bSendMail) // Added by Martin Augustine - Ntier software services 09/10/2013 cmd_sendMail(); + else if (e.getTarget() == bArchive) + cmd_archive(); } // actionPerformed private void cmd_render() { @@ -524,5 +544,62 @@ public class ZkJRViewer extends Window implements EventListener, ITabOnCl return media; } } + + /** + * Create archive for jasper report + */ + protected void cmd_archive() + { + boolean success = false; + try + { + byte[] data = getFileByteData(getPDF()); + if (data != null && m_printInfo != null) + { + MArchive archive = new MArchive(Env.getCtx(), m_printInfo, null); + archive.setBinaryData(data); + success = archive.save(); + } + + if (success) + FDialog.info(m_WindowNo, this, "Archived"); + else + FDialog.error(m_WindowNo, this, "ArchiveError"); + } + catch (IOException e) + { + log.log(Level.SEVERE, "Exception while reading file " + e); + } + catch (JRException e) + { + log.log(Level.SEVERE, "Error loading object from InputStream" + e); + } + } // cmd_archive + + /** + * convert File data into Byte Data + * @param tempFile + * @return file in ByteData + */ + private byte[] getFileByteData(File tempFile) + { + byte fileContent[] = new byte[(int) tempFile.length()]; + + try + { + FileInputStream fis = new FileInputStream(tempFile); + fis.read(fileContent); + fis.close(); + } + catch (FileNotFoundException e) + { + log.log(Level.SEVERE, "File not found " + e); + } + catch (IOException ioe) + { + log.log(Level.SEVERE, "Exception while reading file " + ioe); + } + return fileContent; + } // getFileByteData } diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/ZkJRViewerProvider.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/ZkJRViewerProvider.java index 61d66795e2..a11b6cba84 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/ZkJRViewerProvider.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/ZkJRViewerProvider.java @@ -8,19 +8,20 @@ import org.adempiere.webui.apps.AEnv; import org.adempiere.webui.component.Window; import org.adempiere.webui.part.WindowContainer; import org.adempiere.webui.session.SessionManager; +import org.compiere.model.PrintInfo; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperPrint; public class ZkJRViewerProvider implements JRViewerProvider, JRViewerProviderList { - public void openViewer(final JasperPrint jasperPrint, final String title) + public void openViewer(final JasperPrint jasperPrint, final String title, final PrintInfo printInfo) throws JRException { Runnable runnable = new Runnable() { @Override public void run() { - Window viewer = new ZkJRViewer(jasperPrint, title); + Window viewer = new ZkJRViewer(jasperPrint, title, printInfo); viewer.setAttribute(Window.MODE_KEY, Window.MODE_EMBEDDED); viewer.setAttribute(Window.INSERT_POSITION_KEY, Window.INSERT_NEXT); @@ -31,13 +32,13 @@ public class ZkJRViewerProvider implements JRViewerProvider, JRViewerProviderLis AEnv.executeAsyncDesktopTask(runnable); } - public void openViewer(final List jasperPrintList, final String title) + public void openViewer(final List jasperPrintList, final String title , final PrintInfo printInfo) throws JRException { Runnable runnable = new Runnable() { @Override public void run() { - Window viewer = new ZkJRViewer(jasperPrintList, title); + Window viewer = new ZkJRViewer(jasperPrintList, title, printInfo); viewer.setAttribute(Window.MODE_KEY, Window.MODE_EMBEDDED); viewer.setAttribute(Window.INSERT_POSITION_KEY, Window.INSERT_NEXT);