From 08b1b1b2cff394884188273a0375d747c4d99681 Mon Sep 17 00:00:00 2001 From: Elaine Tan Date: Fri, 21 Jun 2013 12:27:01 +0800 Subject: [PATCH] IDEMPIERE-1083 Print shipping label applet is not working --- .../WEB-INF/src/PrintLabelApplet.java | 120 ++++++++++++++++++ .../adempiere/webui/LabelAppletWindow.java | 58 +++++---- org.adempiere.ui.zk/build-labelapplet.xml | 25 ++++ org.adempiere.ui.zk/labelappletkeystore | Bin 0 -> 1231 bytes org.adempiere.ui.zk/labeldata.jsp | 47 +++++-- 5 files changed, 212 insertions(+), 38 deletions(-) create mode 100644 org.adempiere.ui.zk/WEB-INF/src/PrintLabelApplet.java create mode 100644 org.adempiere.ui.zk/build-labelapplet.xml create mode 100644 org.adempiere.ui.zk/labelappletkeystore diff --git a/org.adempiere.ui.zk/WEB-INF/src/PrintLabelApplet.java b/org.adempiere.ui.zk/WEB-INF/src/PrintLabelApplet.java new file mode 100644 index 0000000000..2c76f431f7 --- /dev/null +++ b/org.adempiere.ui.zk/WEB-INF/src/PrintLabelApplet.java @@ -0,0 +1,120 @@ +import java.awt.print.PrinterJob; +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; + +import javax.print.Doc; +import javax.print.DocFlavor; +import javax.print.DocPrintJob; +import javax.print.PrintService; +import javax.print.SimpleDoc; +import javax.print.attribute.HashDocAttributeSet; +import javax.print.attribute.standard.DocumentName; +import javax.swing.JApplet; + +/** + * + * @author Low Heng Sin + * + */ +public class PrintLabelApplet extends JApplet { + + /** + * + */ + private static final long serialVersionUID = -3107445661561669767L; + private ArrayList filepaths = new ArrayList(); + private int listSize = 0; + + @Override + public void init() { + String s = getParameter("size"); + try { + listSize = Integer.parseInt(s); + } catch (Exception e) { + System.out.println("Invalid listSize param=" + s); + listSize = 0; + } + + for (int i = 0; i < listSize; i++) + filepaths.add(getParameter("file_" + i)); + + super.init(); + } + + @Override + public void start() { + System.out.println(this.getClass().getName() + " start()"); + PrinterJob pjob = null; + PrintService service = null; + DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; + + try { + int count = 0; + + while (true) { + URL url = new URL(getCodeBase(), "labeldata.jsp?filepath=" + filepaths.get(count) + "&count=" + (count + 1)); + System.out.println("Protocol="+url.getProtocol()); + System.out.println("Host="+url.getHost()); + System.out.println("Port="+url.getPort()); + System.out.println("Default Port="+url.getDefaultPort()); + System.out.println("Path"+url.getPath()); + System.out.println("URL="+url.toString()); + URLConnection conn = url.openConnection(); + int contentLength = conn.getContentLength(); + System.out.println("ContentLength="+contentLength); + InputStream is = conn.getInputStream(); + System.out.println("InputStream="+is.available()); + byte[] data = null; + BufferedInputStream bis = new BufferedInputStream(is); + System.out.println("BufferedInputStream="+bis.available()); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + while(bis.available() > 0) { + baos.write(bis.read()); + } + is.close(); + bis.close(); + data = baos.toByteArray(); + baos.close(); + System.out.println("ByteArrayOutputStream="+data.length); + if (data.length > 0) { + count ++; + if (pjob == null) { + // Create Print Job + pjob = PrinterJob.getPrinterJob(); + if (pjob.printDialog()) + service = pjob.getPrintService(); + else + break; + } + DocPrintJob job = service.createPrintJob(); + HashDocAttributeSet as = new HashDocAttributeSet(); + as.add(new DocumentName("shipping label", null)); + Doc doc = new SimpleDoc(data, flavor, as); + job.print(doc, null); + } else { + break; + } + if (count >= listSize) + break; + } + this.showStatus(count + " label printed."); + } catch (Exception e) { + e.printStackTrace(); + this.showStatus("Failed to print label - " + e.getLocalizedMessage()); + } + } + + @Override + public void destroy() { + System.out.println(this.getClass().getName() + " destroy()"); + } + + @Override + public void stop() { + System.out.println(this.getClass().getName() + " stop()"); + } +} diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/LabelAppletWindow.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/LabelAppletWindow.java index 29ec6acf07..0983da88e5 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/LabelAppletWindow.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/LabelAppletWindow.java @@ -13,20 +13,16 @@ *****************************************************************************/ package org.adempiere.webui; +import java.io.File; +import java.io.FileOutputStream; import java.util.List; -import java.util.UUID; - -import javax.servlet.http.HttpSession; import org.adempiere.webui.component.ToolBarButton; import org.adempiere.webui.component.Window; -import org.apache.commons.codec.binary.Base64; -import org.zkoss.zk.au.out.AuScript; -import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.Events; -import org.zkoss.zk.ui.util.Clients; +import org.zkoss.zul.Applet; import org.zkoss.zul.Div; /** @@ -40,37 +36,47 @@ public class LabelAppletWindow extends Window implements EventListener * */ private static final long serialVersionUID = -8980224912104404397L; - + public LabelAppletWindow(List list) { super(); - String uuid = UUID.randomUUID().toString(); - HttpSession session = (HttpSession) Executions.getCurrent().getDesktop().getSession().getNativeSession(); - session.setAttribute(uuid, list); + Div div = new Div(); appendChild(div); + Applet applet = new Applet(); + applet.setCode("PrintLabelApplet.class"); + applet.setArchive("labelapplet.jar"); + applet.setWidth("0"); + applet.setHeight("0"); + applet.setParam("size", list.size() + ""); + + File tempFile = null; + FileOutputStream fos = null; + for(int i = 0; i < list.size(); i++) + { + try + { + tempFile = File.createTempFile("lblapp", Long.toString(System.nanoTime())); + fos = new FileOutputStream(tempFile); + applet.setParam("file_" + i, tempFile.getAbsolutePath()); + fos.write(list.get(i)); + fos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + div.appendChild(applet); + ToolBarButton link = new ToolBarButton(); link.setLabel("Click here to close this popup after printing is completed."); link.addEventListener(Events.ON_CLICK, this); appendChild(link); - this.setBorder("normal"); - - StringBuffer appletTag = new StringBuffer(); - appletTag.append(""); - appletTag.append(""); - appletTag.append(""); - - for(int i = 0; i < list.size(); i++) - appletTag.append(""); - - appletTag.append(""); - - String script = "document.getElementById('" + div.getUuid() + "').innerHTML=\"" + appletTag.toString() + "\";"; - Clients.response(new AuScript(div, script)); + this.setBorder("normal"); } - + public void onEvent(Event event) throws Exception { if (Events.ON_CLICK.equals(event.getName())) diff --git a/org.adempiere.ui.zk/build-labelapplet.xml b/org.adempiere.ui.zk/build-labelapplet.xml new file mode 100644 index 0000000000..15b40e591d --- /dev/null +++ b/org.adempiere.ui.zk/build-labelapplet.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/org.adempiere.ui.zk/labelappletkeystore b/org.adempiere.ui.zk/labelappletkeystore new file mode 100644 index 0000000000000000000000000000000000000000..7c6b9645174ada0eda271753934b9fee8956e6a5 GIT binary patch literal 1231 zcmezO_TO6u1_mY|W&~sIoW!KmoWz2HoYWGan0<`%yKW#o!Jvt;$AFKGOPh_6g;9%1 zkdcvZ?(TJo)3ABL-zi-OHy@iFd1Ui0 z)|rzOvaYU)p2V@BvTr5B#x9GNOUomUt*$+`@U+y^*ST3ntU|r7qW>OltNQ- zL!oO)A9NHFMFdv#{k1%K?|xHG=OiD|4Kg3)?mu|c#N7BJXOgOR>IWYNyIM6yW5XDc z?GreindH*`m!`k>P#2u0;AjD`XdFY}n!{R;c{KM?*|_ z`*}{Io)gO++K5$kht%|HJXpL=PJ8pR(usMC>$!g^nkIexom9oBVtr=G$9*dTCv>i8 zzW*@l>3p_H-Dzj6T-?&U +<%@page import="java.io.ByteArrayOutputStream"%> +<%@page import="java.io.FileInputStream"%> +<%@page import="java.io.File"%> <%@page import="java.io.BufferedOutputStream"%> <%@page import="java.io.OutputStream"%> <%@ page language="java" contentType="application/octet-stream"%> <% - String key = request.getParameter("key"); - if (key == null || key.trim().length() == 0 ) + String filepath = request.getParameter("filepath"); + if (filepath == null || filepath.trim().length() == 0 ) return; - Object object = session.getAttribute(key); - if (object != null && object instanceof java.util.List) { - java.util.List list = (java.util.List)object; - System.out.println("size=" + list.size()); - if (!list.isEmpty()) { - byte[] data = (byte[])list.remove(0); - System.out.println("length=" + data.length); + File file = new File(filepath); + if (file.exists()) + { + FileInputStream fis = null; + ByteArrayOutputStream baos = null; + byte[] data = null; + try + { + fis = new FileInputStream (file); + baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024*8]; // 8kB + int length = -1; + while ((length = fis.read(buffer)) != -1) + baos.write(buffer, 0, length); + data = baos.toByteArray(); + fis.close(); + baos.close(); + response.setContentLength(data.length); OutputStream os = response.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(os); bos.write(data); bos.flush(); - } else { + bos.close(); + + file.delete(); + } + catch (Exception e) + { + e.printStackTrace(); response.setContentLength(0); } - } else { - System.out.println("key not found=" + key); + } + else + { + System.out.println("file not found=" + filepath); response.setContentLength(0); } %>