KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > action > ViewReportAction


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.action;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import net.sf.jasperreports.engine.JasperPrint;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.springframework.context.i18n.LocaleContextHolder;
34 import org.springframework.webflow.AttributeMap;
35 import org.springframework.webflow.Event;
36 import org.springframework.webflow.RequestContext;
37 import org.springframework.webflow.ExternalContext.SharedAttributeMap;
38
39 import com.jaspersoft.jasperserver.api.engine.jasperreports.domain.impl.ReportUnitRequest;
40 import com.jaspersoft.jasperserver.api.engine.jasperreports.domain.impl.ReportUnitResult;
41 import com.jaspersoft.jasperserver.war.action.hyperlinks.HyperlinkProducerFactoryFlowFactory;
42 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
43
44 /**
45  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
46  * @version $Id: ViewReportAction.java 4428 2006-09-06 16:54:08Z lucian $
47  */

48 public class ViewReportAction extends ReportParametersAction
49 {
50     protected final static Log log = LogFactory.getLog(ViewReportAction.class);
51     
52     public static final String JavaDoc REPORTUNIT_URI = "reportUnit";
53
54     private String JavaDoc transactionAttributeRequestParsed;
55     private String JavaDoc flowAttributeInhibitRequestParsing;
56     private String JavaDoc requestParameterPageIndex;
57     private String JavaDoc flowAttributePageIndex;
58     private String JavaDoc requestAttributeHtmlLinkHandlerFactory;
59     private String JavaDoc flowAttributeDepth;
60     private String JavaDoc sessionAttributePrefixJasperPrint;
61     private String JavaDoc flowAttributeJasperPrintSessionName;
62     private HyperlinkProducerFactoryFlowFactory hyperlinkProducerFactory;
63     private String JavaDoc flowAttributeIsSubflow;
64     private String JavaDoc requestParameterReportOutput;
65     private String JavaDoc flowAttributeReportOutput;
66     private String JavaDoc flowAttributeUseClientTimezone;
67     
68     public Event checkForParams(RequestContext context) throws Exception JavaDoc
69     {
70         AttributeMap flowScope = context.getFlowScope();
71         
72         Integer JavaDoc depth = flowScope.getInteger(getFlowAttributeDepth());
73         if (depth == null) {
74             depth = new Integer JavaDoc(0);
75             flowScope.put(getFlowAttributeDepth(), depth);
76         }
77         
78         boolean isSubflow = !context.getFlowExecutionContext().getActiveSession().isRoot();
79         flowScope.put(getFlowAttributeIsSubflow(), Boolean.valueOf(isSubflow));
80         
81         String JavaDoc reportOutput = context.getRequestParameters().get(getRequestParameterReportOutput());
82         if (reportOutput != null) {
83             flowScope.put(getFlowAttributeReportOutput(), reportOutput);
84         }
85         
86         Boolean JavaDoc inhibitRequestParsingAttr = flowScope.getBoolean(getFlowAttributeInhibitRequestParsing());
87         boolean parseRequest = inhibitRequestParsingAttr == null || !inhibitRequestParsingAttr.booleanValue();
88         
89         flowScope.put(getFlowAttributeUseClientTimezone(), Boolean.valueOf(!parseRequest));
90         
91         return createWrappers(context, parseRequest);
92     }
93
94     protected void addCustomParameters(RequestContext context, Map JavaDoc parameterValues) {
95     }
96
97     public Event verifyData(RequestContext context)
98     {
99         AttributeMap transactionAttributes = context.getLastTransition().getAttributeMap();
100         boolean requestParsed = transactionAttributes.getRequiredBoolean(getTransactionAttributeRequestParsed()).booleanValue();
101         Map JavaDoc parameterValues = getParameterValues(context, requestParsed);
102         if (parameterValues == null) {
103             return error();
104         }
105
106         String JavaDoc reportUnitUri = (String JavaDoc) context.getFlowScope().get(REPORTUNIT_URI);
107         if (reportUnitUri != null) {
108             ReportUnitResult result =
109                 (ReportUnitResult)getEngine().execute(
110                     JasperServerUtil.getExecutionContext(LocaleContextHolder.getLocale()),
111                     new ReportUnitRequest(reportUnitUri, parameterValues)
112                     );
113
114             JasperPrint jasperPrint = result.getJasperPrint();
115             setJasperPrint(context, jasperPrint);
116             
117             context.getFlowScope().remove(getFlowAttributePageIndex());
118         }
119         return success();
120     }
121
122     protected void setJasperPrint(RequestContext context, JasperPrint jasperPrint) {
123         SharedAttributeMap sessionMap = context.getExternalContext().getSessionMap();
124         int depth = context.getFlowScope().getRequiredInteger(getFlowAttributeDepth()).intValue();
125         cleanJasperPrintObjects(sessionMap, depth);
126
127         String JavaDoc sessionName = getSessionAttributePrefixJasperPrint() + depth;
128         sessionMap.put(sessionName, jasperPrint);
129         context.getFlowScope().put(getFlowAttributeJasperPrintSessionName(), sessionName);
130     }
131
132     protected void cleanJasperPrintObjects(SharedAttributeMap sessionMap, int depth) {
133         String JavaDoc prefix = getSessionAttributePrefixJasperPrint();
134         List JavaDoc toRemove = new ArrayList JavaDoc();
135         for (Iterator JavaDoc it = sessionMap.getMap().keySet().iterator(); it.hasNext();) {
136             String JavaDoc key = (String JavaDoc) it.next();
137             if (key.startsWith(prefix)) {
138                 try {
139                     int keyDepth = Integer.parseInt(key.substring(prefix.length()));
140                     if (keyDepth > depth) {
141                         toRemove.add(key);
142                     }
143                 } catch (NumberFormatException JavaDoc e) {
144                     //ignore
145
}
146             }
147         }
148         
149         for (Iterator JavaDoc it = toRemove.iterator(); it.hasNext();) {
150             String JavaDoc key = (String JavaDoc) it.next();
151             sessionMap.remove(key);
152         }
153     }
154
155     public Event navigate(RequestContext context) {
156         Integer JavaDoc pageIndex = (Integer JavaDoc) context.getRequestParameters().getNumber(getRequestParameterPageIndex(), Integer JavaDoc.class);
157         if (pageIndex == null) {
158             context.getFlowScope().remove(getFlowAttributePageIndex());
159         } else {
160             context.getFlowScope().put(getFlowAttributePageIndex(), pageIndex);
161         }
162         return success();
163     }
164     
165     
166     public Event prepareReportView(RequestContext context) {
167         if (getHyperlinkProducerFactory() != null) {
168             context.getRequestScope().put(getRequestAttributeHtmlLinkHandlerFactory(), getHyperlinkProducerFactory());
169         }
170         
171         return success();
172     }
173     
174     
175     public Event cleanSession(RequestContext context) {
176         String JavaDoc jasperPrintSessionName = context.getFlowScope().getString(getFlowAttributeJasperPrintSessionName());
177         if (jasperPrintSessionName != null) {
178             context.getExternalContext().getSessionMap().remove(jasperPrintSessionName);
179         }
180         
181         return success();
182     }
183
184     public String JavaDoc getTransactionAttributeRequestParsed() {
185         return transactionAttributeRequestParsed;
186     }
187
188     public void setTransactionAttributeRequestParsed(String JavaDoc transactionAttributeRequestParsed) {
189         this.transactionAttributeRequestParsed = transactionAttributeRequestParsed;
190     }
191
192     public String JavaDoc getFlowAttributeInhibitRequestParsing() {
193         return flowAttributeInhibitRequestParsing;
194     }
195
196     public void setFlowAttributeInhibitRequestParsing(
197             String JavaDoc flowAttributeInhibitRequestParsing) {
198         this.flowAttributeInhibitRequestParsing = flowAttributeInhibitRequestParsing;
199     }
200
201     public String JavaDoc getFlowAttributePageIndex() {
202         return flowAttributePageIndex;
203     }
204
205     public void setFlowAttributePageIndex(String JavaDoc flowAttributePageIndex) {
206         this.flowAttributePageIndex = flowAttributePageIndex;
207     }
208
209     public String JavaDoc getRequestParameterPageIndex() {
210         return requestParameterPageIndex;
211     }
212
213     public void setRequestParameterPageIndex(String JavaDoc requestParameterPageIndex) {
214         this.requestParameterPageIndex = requestParameterPageIndex;
215     }
216
217     public String JavaDoc getRequestAttributeHtmlLinkHandlerFactory() {
218         return requestAttributeHtmlLinkHandlerFactory;
219     }
220
221     public void setRequestAttributeHtmlLinkHandlerFactory(
222             String JavaDoc requestAttributeHtmlLinkHandlerFactory) {
223         this.requestAttributeHtmlLinkHandlerFactory = requestAttributeHtmlLinkHandlerFactory;
224     }
225
226     public String JavaDoc getSessionAttributePrefixJasperPrint() {
227         return sessionAttributePrefixJasperPrint;
228     }
229
230     public void setSessionAttributePrefixJasperPrint(
231             String JavaDoc sessionAttributeJasperPrintStack) {
232         this.sessionAttributePrefixJasperPrint = sessionAttributeJasperPrintStack;
233     }
234
235     public String JavaDoc getFlowAttributeDepth() {
236         return flowAttributeDepth;
237     }
238
239     public void setFlowAttributeDepth(String JavaDoc flowAttributeDepth) {
240         this.flowAttributeDepth = flowAttributeDepth;
241     }
242
243     public String JavaDoc getFlowAttributeJasperPrintSessionName() {
244         return flowAttributeJasperPrintSessionName;
245     }
246
247     public void setFlowAttributeJasperPrintSessionName(
248             String JavaDoc flowAttributeJasperPrintSessionName) {
249         this.flowAttributeJasperPrintSessionName = flowAttributeJasperPrintSessionName;
250     }
251
252     public HyperlinkProducerFactoryFlowFactory getHyperlinkProducerFactory() {
253         return hyperlinkProducerFactory;
254     }
255
256     public void setHyperlinkProducerFactory(
257             HyperlinkProducerFactoryFlowFactory hyperlinkProducerFactory) {
258         this.hyperlinkProducerFactory = hyperlinkProducerFactory;
259     }
260
261     public String JavaDoc getFlowAttributeIsSubflow() {
262         return flowAttributeIsSubflow;
263     }
264
265     public void setFlowAttributeIsSubflow(String JavaDoc requestAttributeIsSubflow) {
266         this.flowAttributeIsSubflow = requestAttributeIsSubflow;
267     }
268
269     public String JavaDoc getFlowAttributeReportOutput() {
270         return flowAttributeReportOutput;
271     }
272
273     public void setFlowAttributeReportOutput(String JavaDoc flowAttributeReportOutput) {
274         this.flowAttributeReportOutput = flowAttributeReportOutput;
275     }
276
277     public String JavaDoc getRequestParameterReportOutput() {
278         return requestParameterReportOutput;
279     }
280
281     public void setRequestParameterReportOutput(String JavaDoc requestParameterReportOutput) {
282         this.requestParameterReportOutput = requestParameterReportOutput;
283     }
284
285     public String JavaDoc getFlowAttributeUseClientTimezone() {
286         return flowAttributeUseClientTimezone;
287     }
288
289     public void setFlowAttributeUseClientTimezone(
290             String JavaDoc flowAttributeUseClientTimezone) {
291         this.flowAttributeUseClientTimezone = flowAttributeUseClientTimezone;
292     }
293
294 }
295
Popular Tags