KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRFillDatasetRun


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.fill;
29
30 import java.sql.Connection JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import net.sf.jasperreports.engine.JRDataSource;
34 import net.sf.jasperreports.engine.JRDatasetParameter;
35 import net.sf.jasperreports.engine.JRDatasetRun;
36 import net.sf.jasperreports.engine.JRException;
37 import net.sf.jasperreports.engine.JRExpression;
38 import net.sf.jasperreports.engine.JRParameter;
39 import net.sf.jasperreports.engine.JRQuery;
40 import net.sf.jasperreports.engine.JRScriptletException;
41 import net.sf.jasperreports.engine.JRVariable;
42
43 /**
44  * Class used to instantiate sub datasets.
45  *
46  * @author Lucian Chirita (lucianc@users.sourceforge.net)
47  * @version $Id: JRFillDatasetRun.java 1485 2006-11-14 20:23:17 +0200 (Tue, 14 Nov 2006) teodord $
48  */

49 public class JRFillDatasetRun implements JRDatasetRun
50 {
51     private final JRBaseFiller filler;
52
53     private final JRFillDataset dataset;
54
55     private JRExpression parametersMapExpression;
56
57     private JRDatasetParameter[] parameters;
58
59     private JRExpression connectionExpression;
60
61     private JRExpression dataSourceExpression;
62
63     
64     /**
65      * Construct an instance for a dataset run.
66      *
67      * @param filler the filler
68      * @param datasetRun the dataset run
69      * @param factory the fill object factory
70      */

71     public JRFillDatasetRun(JRBaseFiller filler, JRDatasetRun datasetRun, JRFillObjectFactory factory)
72     {
73         factory.put(datasetRun, this);
74
75         this.filler = filler;
76         this.dataset = (JRFillDataset) filler.datasetMap.get(datasetRun.getDatasetName());
77
78         parametersMapExpression = datasetRun.getParametersMapExpression();
79         parameters = datasetRun.getParameters();
80         connectionExpression = datasetRun.getConnectionExpression();
81         dataSourceExpression = datasetRun.getDataSourceExpression();
82     }
83
84     
85     /**
86      * Instantiates and iterates the sub dataset for a chart dataset evaluation.
87      *
88      * @param elementDataset the chart dataset
89      * @param evaluation the evaluation type
90      * @throws JRException
91      */

92     public void evaluate(JRFillElementDataset elementDataset, byte evaluation) throws JRException
93     {
94         Map JavaDoc parameterValues =
95             JRFillSubreport.getParameterValues(
96                 filler,
97                 parametersMapExpression,
98                 parameters,
99                 evaluation,
100                 false,
101                 dataset.getResourceBundle() != null,//hasResourceBundle
102
false//hasFormatFactory
103
);
104
105         try
106         {
107             if (dataSourceExpression != null)
108             {
109                 JRDataSource dataSource = (JRDataSource) filler.evaluateExpression(dataSourceExpression, evaluation);
110                 dataset.setDatasourceParameterValue(parameterValues, dataSource);
111             }
112             else if (connectionExpression != null)
113             {
114                 Connection JavaDoc connection = (Connection JavaDoc) filler.evaluateExpression(connectionExpression, evaluation);
115                 dataset.setConnectionParameterValue(parameterValues, connection);
116             }
117
118             copyConnectionParameter(parameterValues);
119             
120             dataset.setParameterValues(parameterValues);
121             
122             dataset.filterElementDatasets(elementDataset);
123             
124             dataset.initCalculator();
125
126             iterate();
127         }
128         finally
129         {
130             dataset.closeDatasource();
131             dataset.restoreElementDatasets();
132         }
133     }
134
135     protected void copyConnectionParameter(Map JavaDoc parameterValues)
136     {
137         JRQuery query = dataset.getQuery();
138         if (query != null)
139         {
140             String JavaDoc language = query.getLanguage();
141             if (connectionExpression == null &&
142                     (language.equals("sql") || language.equals("SQL")) &&
143                     !parameterValues.containsKey(JRParameter.REPORT_CONNECTION))
144             {
145                 JRFillParameter connParam = (JRFillParameter) filler.getParametersMap().get(JRParameter.REPORT_CONNECTION);
146                 Connection JavaDoc connection = (Connection JavaDoc) connParam.getValue();
147                 parameterValues.put(JRParameter.REPORT_CONNECTION, connection);
148             }
149         }
150     }
151
152     protected void iterate() throws JRException
153     {
154         dataset.start();
155
156         init();
157
158         if (dataset.next())
159         {
160             detail();
161
162             while (dataset.next())
163             {
164                 checkInterrupted();
165
166                 group();
167
168                 detail();
169             }
170         }
171
172     }
173
174     
175     protected void checkInterrupted()
176     {
177         if (Thread.currentThread().isInterrupted() || filler.isInterrupted())
178         {
179             filler.setInterrupted(true);
180
181             throw new JRFillInterruptedException();
182         }
183     }
184
185     
186     protected void group() throws JRException, JRScriptletException
187     {
188         dataset.calculator.estimateGroupRuptures();
189
190         dataset.scriptlet.callBeforeGroupInit();
191         dataset.calculator.initializeVariables(JRVariable.RESET_TYPE_GROUP);
192         dataset.scriptlet.callAfterGroupInit();
193     }
194
195     protected void init() throws JRScriptletException, JRException
196     {
197         dataset.scriptlet.callBeforeReportInit();
198         dataset.calculator.initializeVariables(JRVariable.RESET_TYPE_REPORT);
199         dataset.scriptlet.callAfterReportInit();
200     }
201
202     protected void detail() throws JRScriptletException, JRException
203     {
204         dataset.scriptlet.callBeforeDetailEval();
205         dataset.calculator.calculateVariables();
206         dataset.scriptlet.callAfterDetailEval();
207     }
208
209     public String JavaDoc getDatasetName()
210     {
211         return dataset.getName();
212     }
213
214     public JRExpression getParametersMapExpression()
215     {
216         return parametersMapExpression;
217     }
218
219     public JRDatasetParameter[] getParameters()
220     {
221         return parameters;
222     }
223
224     public JRExpression getConnectionExpression()
225     {
226         return connectionExpression;
227     }
228
229     public JRExpression getDataSourceExpression()
230     {
231         return dataSourceExpression;
232     }
233     
234     protected JRFillDataset getDataset()
235     {
236         return dataset;
237     }
238 }
239
Popular Tags