001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.fill;
029:
030: import java.sql.Connection;
031: import java.util.Map;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: import net.sf.jasperreports.engine.JRDataSource;
037: import net.sf.jasperreports.engine.JRDatasetParameter;
038: import net.sf.jasperreports.engine.JRDatasetRun;
039: import net.sf.jasperreports.engine.JRException;
040: import net.sf.jasperreports.engine.JRExpression;
041: import net.sf.jasperreports.engine.JRParameter;
042: import net.sf.jasperreports.engine.JRQuery;
043: import net.sf.jasperreports.engine.JRScriptletException;
044: import net.sf.jasperreports.engine.JRVariable;
045:
046: /**
047: * Class used to instantiate sub datasets.
048: *
049: * @author Lucian Chirita (lucianc@users.sourceforge.net)
050: * @version $Id: JRFillDatasetRun.java 1726 2007-05-17 16:31:21Z lucianc $
051: */
052: public class JRFillDatasetRun implements JRDatasetRun {
053:
054: private static final Log log = LogFactory
055: .getLog(JRFillDatasetRun.class);
056:
057: private final JRBaseFiller filler;
058:
059: private final JRFillDataset dataset;
060:
061: private JRExpression parametersMapExpression;
062:
063: private JRDatasetParameter[] parameters;
064:
065: private JRExpression connectionExpression;
066:
067: private JRExpression dataSourceExpression;
068:
069: /**
070: * Construct an instance for a dataset run.
071: *
072: * @param filler the filler
073: * @param datasetRun the dataset run
074: * @param factory the fill object factory
075: */
076: public JRFillDatasetRun(JRBaseFiller filler,
077: JRDatasetRun datasetRun, JRFillObjectFactory factory) {
078: factory.put(datasetRun, this );
079:
080: this .filler = filler;
081: this .dataset = (JRFillDataset) filler.datasetMap.get(datasetRun
082: .getDatasetName());
083:
084: parametersMapExpression = datasetRun
085: .getParametersMapExpression();
086: parameters = datasetRun.getParameters();
087: connectionExpression = datasetRun.getConnectionExpression();
088: dataSourceExpression = datasetRun.getDataSourceExpression();
089: }
090:
091: /**
092: * Instantiates and iterates the sub dataset for a chart dataset evaluation.
093: *
094: * @param elementDataset the chart dataset
095: * @param evaluation the evaluation type
096: * @throws JRException
097: */
098: public void evaluate(JRFillElementDataset elementDataset,
099: byte evaluation) throws JRException {
100: Map parameterValues = JRFillSubreport.getParameterValues(
101: filler, parametersMapExpression, parameters,
102: evaluation, false, dataset.getResourceBundle() != null,//hasResourceBundle
103: false//hasFormatFactory
104: );
105:
106: try {
107: if (dataSourceExpression != null) {
108: JRDataSource dataSource = (JRDataSource) filler
109: .evaluateExpression(dataSourceExpression,
110: evaluation);
111: dataset.setDatasourceParameterValue(parameterValues,
112: dataSource);
113: } else if (connectionExpression != null) {
114: Connection connection = (Connection) filler
115: .evaluateExpression(connectionExpression,
116: evaluation);
117: dataset.setConnectionParameterValue(parameterValues,
118: connection);
119: }
120:
121: copyConnectionParameter(parameterValues);
122:
123: dataset.setParameterValues(parameterValues);
124: dataset.initDatasource();
125:
126: dataset.filterElementDatasets(elementDataset);
127:
128: dataset.initCalculator();//FIXME too late for cascade param evaluations?
129:
130: iterate();
131: } finally {
132: dataset.closeDatasource();
133: dataset.restoreElementDatasets();
134: }
135: }
136:
137: protected void copyConnectionParameter(Map parameterValues) {
138: JRQuery query = dataset.getQuery();
139: if (query != null) {
140: String language = query.getLanguage();
141: if (connectionExpression == null
142: && (language.equals("sql") || language
143: .equals("SQL"))
144: && !parameterValues
145: .containsKey(JRParameter.REPORT_CONNECTION)) {
146: JRFillParameter connParam = (JRFillParameter) filler
147: .getParametersMap().get(
148: JRParameter.REPORT_CONNECTION);
149: Connection connection = (Connection) connParam
150: .getValue();
151: parameterValues.put(JRParameter.REPORT_CONNECTION,
152: connection);
153: }
154: }
155: }
156:
157: protected void iterate() throws JRException {
158: dataset.start();
159:
160: init();
161:
162: if (dataset.next()) {
163: detail();
164:
165: while (dataset.next()) {
166: checkInterrupted();
167:
168: group();
169:
170: detail();
171: }
172: }
173:
174: }
175:
176: protected void checkInterrupted() {
177: if (Thread.currentThread().isInterrupted()
178: || filler.isInterrupted()) {
179: if (log.isDebugEnabled()) {
180: log.debug("Fill " + filler.fillerId + ": interrupting");
181: }
182:
183: filler.setInterrupted(true);
184:
185: throw new JRFillInterruptedException();
186: }
187: }
188:
189: protected void group() throws JRException, JRScriptletException {
190: dataset.calculator.estimateGroupRuptures();
191:
192: dataset.scriptlet.callBeforeGroupInit();
193: dataset.calculator
194: .initializeVariables(JRVariable.RESET_TYPE_GROUP);
195: dataset.scriptlet.callAfterGroupInit();
196: }
197:
198: protected void init() throws JRScriptletException, JRException {
199: dataset.scriptlet.callBeforeReportInit();
200: dataset.calculator
201: .initializeVariables(JRVariable.RESET_TYPE_REPORT);
202: dataset.scriptlet.callAfterReportInit();
203: }
204:
205: protected void detail() throws JRScriptletException, JRException {
206: dataset.scriptlet.callBeforeDetailEval();
207: dataset.calculator.calculateVariables();
208: dataset.scriptlet.callAfterDetailEval();
209: }
210:
211: public String getDatasetName() {
212: return dataset.getName();
213: }
214:
215: public JRExpression getParametersMapExpression() {
216: return parametersMapExpression;
217: }
218:
219: public JRDatasetParameter[] getParameters() {
220: return parameters;
221: }
222:
223: public JRExpression getConnectionExpression() {
224: return connectionExpression;
225: }
226:
227: public JRExpression getDataSourceExpression() {
228: return dataSourceExpression;
229: }
230:
231: protected JRFillDataset getDataset() {
232: return dataset;
233: }
234: }
|