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: * ParameterDataRow.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report;
030:
031: import org.jfree.report.states.datarow.StaticDataRow;
032: import org.jfree.report.util.ReportProperties;
033:
034: /**
035: * A static data row that reads its values from a report properties collection. Changes to the report property
036: * collection do not affect the parameter-data-row.
037: *
038: * @author Thomas Morgner
039: */
040: public class ParameterDataRow extends StaticDataRow {
041: /**
042: * Create a parameter row from a master report's report properties collection.
043: *
044: * @param parameters the report parameter set.
045: */
046: public ParameterDataRow(final ReportProperties parameters) {
047: final String[] names = parameters.keyArray();
048: final int length = names.length;
049: final Object[] values = new Object[length];
050:
051: for (int i = 0; i < length; i++) {
052: final String key = names[i];
053: values[i] = parameters.get(key);
054: }
055: setData(names, values);
056: }
057:
058: /**
059: * Create a parameter data row from a master report's data row and a set
060: * of parameter mappings. The incoming parameters can be aliased through
061: * the parameter mapping definitions.
062: *
063: * @param parameters the parameter mappings
064: * @param dataRow the data row.
065: */
066: public ParameterDataRow(final ParameterMapping[] parameters,
067: final DataRow dataRow) {
068: final int length = parameters.length;
069: final String[] innerNames = new String[length];
070: final Object[] values = new Object[length];
071: for (int i = 0; i < length; i++) {
072: final ParameterMapping parameter = parameters[i];
073: final String name = parameter.getName();
074: innerNames[i] = parameter.getAlias();
075: values[i] = dataRow.get(name);
076: }
077: setData(innerNames, values);
078: }
079:
080: /**
081: * Create a parameter data row from a master report's data row and a set
082: * of parameter mappings.
083: *
084: * @param dataRow the data row.
085: */
086: public ParameterDataRow(final DataRow dataRow) {
087: final int columnCount = dataRow.getColumnCount();
088: final String[] innerNames = new String[columnCount];
089: int nameCount = 0;
090: for (int i = 0; i < columnCount; i++) {
091: final String innerName = dataRow.getColumnName(i);
092: if (innerName == null) {
093: continue;
094: }
095: if (contains(innerName, innerNames, nameCount - 1) == false) {
096: innerNames[nameCount] = innerName;
097: nameCount += 1;
098: }
099: }
100:
101: final Object[] values = new Object[nameCount];
102: for (int i = 0; i < nameCount; i++) {
103: values[i] = dataRow.get(innerNames[i]);
104: }
105: setData(innerNames, values);
106: }
107:
108: /**
109: * A helper function that searches the given name if the provided array.
110: *
111: * @param name the name that is searched.
112: * @param array the array containing all known names.
113: * @param length the maximum number of elements in the given array that are valid.
114: * @return true, if the name has been found, false otherwise.
115: */
116: private boolean contains(final String name, final String[] array,
117: final int length) {
118: for (int i = 0; i < length; i++) {
119: if (name.equals(array[i])) {
120: return true;
121: }
122: }
123: return false;
124: }
125: }
|