001: /*
002: * Copyright (C) 2004 Erik Swenson - erik@oreports.com
003: *
004: * This program is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU General Public License as published by the Free Software
006: * Foundation; either version 2 of the License, or (at your option) any later
007: * version.
008: *
009: * This program is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU General Public License along with
015: * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
016: * Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package org.efs.openreports.actions;
021:
022: import java.awt.image.BufferedImage;
023: import java.io.IOException;
024:
025: import javax.servlet.ServletOutputStream;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.apache.struts2.ServletActionContext;
029: import com.opensymphony.xwork2.ActionContext;
030: import com.opensymphony.xwork2.ActionSupport;
031:
032: import org.apache.log4j.Logger;
033: import org.efs.openreports.ORStatics;
034: import org.efs.openreports.objects.Report;
035: import org.jfree.chart.encoders.SunPNGEncoderAdapter;
036:
037: import net.sf.jasperreports.engine.JasperPrint;
038: import net.sf.jasperreports.engine.JasperPrintManager;
039:
040: public class ReportViewerAction extends ActionSupport {
041: private static final long serialVersionUID = 6910790405397971123L;
042:
043: protected static Logger log = Logger
044: .getLogger(ReportViewerAction.class);
045:
046: private int pageIndex = 1;
047: private int pageCount = 0;
048: private float zoom = 1.0f;
049:
050: private String submitType;
051: private Report report;
052:
053: public String execute() {
054: JasperPrint jasperPrint = (JasperPrint) ActionContext
055: .getContext().getSession().get(ORStatics.JASPERPRINT);
056:
057: report = (Report) ActionContext.getContext().getSession().get(
058: ORStatics.REPORT);
059:
060: if (jasperPrint != null && jasperPrint.getPages() != null) {
061: pageCount = jasperPrint.getPages().size();
062: }
063:
064: if (!"image".equals(submitType))
065: return SUCCESS;
066:
067: byte[] imageData = null;
068:
069: if (jasperPrint != null) {
070: try {
071: BufferedImage image = (BufferedImage) JasperPrintManager
072: .printPageToImage(jasperPrint, pageIndex - 1,
073: zoom);
074: imageData = new SunPNGEncoderAdapter().encode(image);
075: } catch (Exception e) {
076: addActionError(e.getMessage());
077: log.error(e.toString());
078: }
079: }
080:
081: if (imageData != null) {
082:
083: HttpServletResponse response = ServletActionContext
084: .getResponse();
085:
086: try {
087: response.setContentLength(imageData.length);
088: ServletOutputStream ouputStream = response
089: .getOutputStream();
090: ouputStream.write(imageData, 0, imageData.length);
091: ouputStream.flush();
092: ouputStream.close();
093: } catch (IOException ioe) {
094: log.warn(ioe.toString());
095: }
096: }
097:
098: return NONE;
099: }
100:
101: public int getPageIndex() {
102: return pageIndex;
103: }
104:
105: public void setPageIndex(int pageIndex) {
106: this .pageIndex = pageIndex;
107: }
108:
109: public float getZoom() {
110: return zoom;
111: }
112:
113: public void setZoom(float zoom) {
114: this .zoom = zoom;
115: }
116:
117: public int getPageCount() {
118: return pageCount;
119: }
120:
121: public String getSubmitType() {
122: return submitType;
123: }
124:
125: public void setSubmitType(String submitType) {
126: this .submitType = submitType;
127: }
128:
129: public Report getReport() {
130: return report;
131: }
132:
133: public void setReport(Report report) {
134: this.report = report;
135: }
136: }
|