KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > actions > ReportViewerAction


1 /*
2  * Copyright (C) 2004 Erik Swenson - erik@oreports.com
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.actions;
21
22 import java.awt.image.BufferedImage JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import javax.servlet.ServletOutputStream JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc submitType;
49     private Report report;
50     
51     public String JavaDoc 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 JavaDoc image = (BufferedImage JavaDoc) JasperPrintManager.printPageToImage(jasperPrint, pageIndex -1, zoom);
72                 imageData = new SunPNGEncoderAdapter().encode(image);
73             }
74             catch(Exception JavaDoc e)
75             {
76                 addActionError(e.getMessage());
77                 log.error(e.toString());
78             }
79         }
80         
81         if (imageData != null)
82         {
83
84             HttpServletResponse JavaDoc response = ServletActionContext.getResponse();
85
86             try
87             {
88                 response.setContentLength(imageData.length);
89                 ServletOutputStream JavaDoc ouputStream = response.getOutputStream();
90                 ouputStream.write(imageData, 0, imageData.length);
91                 ouputStream.flush();
92                 ouputStream.close();
93             }
94             catch (IOException JavaDoc 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 JavaDoc getSubmitType()
129     {
130         return submitType;
131     }
132
133     public void setSubmitType(String JavaDoc 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