01: /*
02: * ============================================================================
03: * GNU Lesser General Public License
04: * ============================================================================
05: *
06: * JasperReports - Free Java report-generating library.
07: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
08: *
09: * 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.util.HashMap;
31: import java.util.Map;
32:
33: import net.sf.jasperreports.engine.JRDataset;
34: import net.sf.jasperreports.engine.JRException;
35: import net.sf.jasperreports.engine.JRGroup;
36: import net.sf.jasperreports.engine.JRParameter;
37: import net.sf.jasperreports.engine.JasperReport;
38:
39: /**
40: * Utility class to be used to evaluate parameter default value expressions for a report
41: * without actually filling it.
42: *
43: * @author Lucian Chirita (lucianc@users.sourceforge.net)
44: * @version $Id: JRParameterDefaultValuesEvaluator.java 1759 2007-06-20 16:47:34Z lucianc $
45: */
46: public class JRParameterDefaultValuesEvaluator {
47:
48: /**
49: * Evaluates the default values for the parameters of a report.
50: *
51: * @param report the report
52: * @param initialParameters initial parameter value map
53: * @return a map containing parameter values indexed by parameter names
54: * @throws JRException
55: */
56: public static Map evaluateParameterDefaultValues(
57: JasperReport report, Map initialParameters)
58: throws JRException {
59: ObjectFactory factory = new ObjectFactory();
60: JRDataset reportDataset = report.getMainDataset();
61: JRFillDataset fillDataset = factory.getDataset(reportDataset);
62: fillDataset.createCalculator(report);
63: fillDataset.initCalculator();
64:
65: Map valuesMap = initialParameters == null ? new HashMap()
66: : new HashMap(initialParameters);
67: fillDataset.setParameterValues(valuesMap);
68:
69: Map parameterValues = new HashMap();
70: JRParameter[] parameters = reportDataset.getParameters();
71: for (int i = 0; i < parameters.length; i++) {
72: JRParameter param = parameters[i];
73: if (!param.isSystemDefined()) {
74: String name = param.getName();
75: Object value = fillDataset.getParameterValue(name);
76: parameterValues.put(name, value);
77: }
78: }
79:
80: return parameterValues;
81: }
82:
83: protected static class ObjectFactory extends JRFillObjectFactory {
84: protected ObjectFactory() {
85: super ((JRBaseFiller) null, null);
86: }
87:
88: protected JRFillGroup getGroup(JRGroup group) {
89: return super.getGroup(null);
90: }
91: }
92:
93: }
|