1 19 20 package org.efs.openreports.actions; 21 22 import java.awt.image.BufferedImage ; 23 import java.io.IOException ; 24 25 import javax.servlet.ServletOutputStream ; 26 import javax.servlet.http.HttpServletResponse ; 27 28 import com.opensymphony.webwork.ServletActionContext; 29 import com.opensymphony.xwork.ActionContext; 30 import com.opensymphony.xwork.ActionSupport; 31 32 import org.apache.log4j.Logger; 33 import org.efs.openreports.ORStatics; 34 import org.efs.openreports.objects.Report; 35 import org.jfree.chart.encoders.SunPNGEncoderAdapter; 36 37 import net.sf.jasperreports.engine.JasperPrint; 38 import net.sf.jasperreports.engine.JasperPrintManager; 39 40 public class ReportViewerAction extends ActionSupport 41 { 42 protected static Logger log = Logger.getLogger(ReportViewerAction.class); 43 44 private int pageIndex = 1; 45 private int pageCount = 0; 46 private float zoom = 1.0f; 47 48 private String submitType; 49 private Report report; 50 51 public String execute() 52 { 53 JasperPrint jasperPrint = (JasperPrint) ActionContext.getContext().getSession().get( 54 ORStatics.JASPERPRINT); 55 56 report = (Report) ActionContext.getContext().getSession().get(ORStatics.REPORT); 57 58 if (jasperPrint != null && jasperPrint.getPages() != null) 59 { 60 pageCount = jasperPrint.getPages().size(); 61 } 62 63 if (!"image".equals(submitType)) return SUCCESS; 64 65 byte[] imageData = null; 66 67 if (jasperPrint != null) 68 { 69 try 70 { 71 BufferedImage image = (BufferedImage ) JasperPrintManager.printPageToImage(jasperPrint, pageIndex -1, zoom); 72 imageData = new SunPNGEncoderAdapter().encode(image); 73 } 74 catch(Exception e) 75 { 76 addActionError(e.getMessage()); 77 log.error(e.toString()); 78 } 79 } 80 81 if (imageData != null) 82 { 83 84 HttpServletResponse response = ServletActionContext.getResponse(); 85 86 try 87 { 88 response.setContentLength(imageData.length); 89 ServletOutputStream ouputStream = response.getOutputStream(); 90 ouputStream.write(imageData, 0, imageData.length); 91 ouputStream.flush(); 92 ouputStream.close(); 93 } 94 catch (IOException ioe) 95 { 96 log.warn(ioe.toString()); 97 } 98 } 99 100 return NONE; 101 } 102 103 public int getPageIndex() 104 { 105 return pageIndex; 106 } 107 108 public void setPageIndex(int pageIndex) 109 { 110 this.pageIndex = pageIndex; 111 } 112 113 public float getZoom() 114 { 115 return zoom; 116 } 117 118 public void setZoom(float zoom) 119 { 120 this.zoom = zoom; 121 } 122 123 public int getPageCount() 124 { 125 return pageCount; 126 } 127 128 public String getSubmitType() 129 { 130 return submitType; 131 } 132 133 public void setSubmitType(String submitType) 134 { 135 this.submitType = submitType; 136 } 137 138 public Report getReport() 139 { 140 return report; 141 } 142 143 public void setReport(Report report) 144 { 145 this.report = report; 146 } 147 }
| Popular Tags
|