001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either 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, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * SubReport.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report;
030:
031: import java.util.HashMap;
032: import java.util.Map;
033:
034: /**
035: * A subreport element. A subreport can be attached to a root-level band and will be printed afterwards. Subreports have
036: * their own tablemodel (queried with the sub-reports's defined query and the master report's data-factory).
037: * <p/>
038: * A sub-report that has been added to a root-level band will always be printed below the root-level band.
039: * <p/>
040: * Sub-reports can have import and export parameters. The parameter mapping can be defined freely, so a subreport is not
041: * required to use the same column names as the parent report.
042: * <p/>
043: * If a global import or export is defined (by adding the parameter mapping "*" => "*") the other defined parameter
044: * mappings will be ignored.
045: *
046: * @author Thomas Morgner
047: */
048: public class SubReport extends AbstractReportDefinition {
049: /**
050: * A mapping of export parameters.
051: */
052: private HashMap exportParameters;
053: /**
054: * A mapping of import parameters.
055: */
056: private HashMap inputParameters;
057:
058: /**
059: * Creates a new subreport instance.
060: */
061: public SubReport() {
062: exportParameters = new HashMap();
063: inputParameters = new HashMap();
064: }
065:
066: /**
067: * Returns the page definition assigned to the report definition. The page definition defines the report area and how
068: * the report is subdivided by the child pages.
069: *
070: * @return null, as subreports have no page-definition at all.
071: */
072: public PageDefinition getPageDefinition() {
073: return null;
074: }
075:
076: /**
077: * Clones the report.
078: *
079: * @return the clone.
080: * @throws CloneNotSupportedException this should never happen.
081: */
082: public Object clone() throws CloneNotSupportedException {
083: final SubReport o = (SubReport) super .clone();
084: o.exportParameters = (HashMap) exportParameters.clone();
085: o.inputParameters = (HashMap) inputParameters.clone();
086: return o;
087: }
088:
089: /**
090: * Adds an export-parameter mapping to the subreport. The parameter specified by 'sourceColumn' will be made available
091: * with the name 'outerName' in the parent report.
092: *
093: * @param outerName the name the parameter will get in the master report.
094: * @param sourceColumn the source-column in the sub-report.
095: */
096: public void addExportParameter(final String outerName,
097: final String sourceColumn) {
098: if (outerName == null) {
099: throw new NullPointerException();
100: }
101: if (sourceColumn == null) {
102: throw new NullPointerException();
103: }
104: exportParameters.put(outerName, sourceColumn);
105: }
106:
107: /**
108: * Removes the export parameter from the mapping.
109: *
110: * @param outerName the name of the parameter as it is known in the master report.
111: */
112: public void removeExportParameter(final String outerName) {
113: if (outerName == null) {
114: throw new NullPointerException();
115: }
116: exportParameters.remove(outerName);
117: }
118:
119: /**
120: * Returns the parameter mappings for the subreport. The parameter mappings define how columns of the sub-report get
121: * mapped into the master report.
122: *
123: * @return the parameter mappings array.
124: */
125: public ParameterMapping[] getExportMappings() {
126: final int length = exportParameters.size();
127: final Map.Entry[] inputEntries = (Map.Entry[]) exportParameters
128: .entrySet().toArray(new Map.Entry[length]);
129: final ParameterMapping[] mapping = new ParameterMapping[length];
130:
131: for (int i = 0; i < length; i++) {
132: final Map.Entry entry = inputEntries[i];
133: final String name = (String) entry.getKey();
134: final String alias = (String) entry.getValue();
135: mapping[i] = new ParameterMapping(name, alias);
136: }
137: return mapping;
138: }
139:
140: /**
141: * Adds an input-parameter mapping to the subreport. Input parameters define how columns that exist in the parent
142: * report get mapped into the subreport.
143: * <p/>
144: * Input parameter mapping happens only once, so after the report has been started, changes to the parameters will not
145: * pass through to the subreport.
146: *
147: * @param outerName the name of the parent report's column that provides the data.
148: * @param sourceColumn the name under which the parameter will be available in the subreport.
149: */
150: public void addInputParameter(final String outerName,
151: final String sourceColumn) {
152: inputParameters.put(sourceColumn, outerName);
153: }
154:
155: /**
156: * Removes the input parameter from the parameter mapping.
157: *
158: * @param sourceColumn the name of the column of the subreport report that acts as source for the input parameter.
159: */
160: public void removeInputParameter(final String sourceColumn) {
161: inputParameters.remove(sourceColumn);
162: }
163:
164: /**
165: * Returns the input mappings defined for this subreport.
166: *
167: * @return the input mappings, never null.
168: */
169: public ParameterMapping[] getInputMappings() {
170: final int length = inputParameters.size();
171: final Map.Entry[] inputEntries = (Map.Entry[]) inputParameters
172: .entrySet().toArray(new Map.Entry[length]);
173: final ParameterMapping[] mapping = new ParameterMapping[length];
174:
175: for (int i = 0; i < length; i++) {
176: final Map.Entry entry = inputEntries[i];
177: final String alias = (String) entry.getKey();
178: final String name = (String) entry.getValue();
179: mapping[i] = new ParameterMapping(name, alias);
180: }
181: return mapping;
182: }
183:
184: /**
185: * Checks, whether a global import is defined. A global import effectly overrides all other imports.
186: *
187: * @return true, if there is a global import defined, false otherwise.
188: */
189: public boolean isGlobalImport() {
190: return "*".equals(inputParameters.get("*"));
191: }
192:
193: /**
194: * Checks, whether a global export is defined. A global export effectly overrides all other export mappings.
195: *
196: * @return true, if there is a global export defined, false otherwise.
197: */
198: public boolean isGlobalExport() {
199: return "*".equals(exportParameters.get("*"));
200: }
201: }
|