001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.plugin.jfreereport.helper;
014:
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import javax.swing.table.TableModel;
018:
019: import org.jfree.report.DataFactory;
020: import org.jfree.report.DataRow;
021: import org.jfree.report.ReportDataFactoryException;
022: import org.jfree.report.util.CloseableTableModel;
023: import org.pentaho.core.component.IPreparedComponent;
024: import org.pentaho.commons.connection.IPentahoResultSet;
025: import org.pentaho.messages.Messages;
026:
027: /**
028: * The PentahoTableDataFactory class implements JFreeReport's data factory
029: * and manages the TableModels provided to JFreeReport. The primary
030: * difference between this class and JFreeReport's standard TableDataFactory
031: * is the "getTableIterator" method, which allows the Platform to clean up and
032: * table model resources after their use. Also, we support Pentaho's
033: * IPreparedComponent interface which allows a prepared component to generate
034: * a result set when requested.
035: *
036: * @author Will Gorman
037: */
038: public class PentahoTableDataFactory implements DataFactory, Cloneable {
039:
040: /** map of tables to keep track of */
041: private HashMap tables;
042:
043: private HashMap components;
044:
045: /**
046: * default constructor
047: *
048: */
049: public PentahoTableDataFactory() {
050: this .tables = new HashMap();
051: this .components = new HashMap();
052: }
053:
054: /**
055: * constructor with one time call to addTable for convenience.
056: *
057: * @param name table name
058: * @param tableModel instance of table model
059: */
060: public PentahoTableDataFactory(final String name,
061: final TableModel tableModel) {
062: this ();
063: addTable(name, tableModel);
064: }
065:
066: /**
067: * add a table to the map
068: *
069: * @param name table name
070: * @param tableModel instance of table model
071: */
072: public void addTable(final String name, final TableModel tableModel) {
073: tables.put(name, tableModel);
074: }
075:
076: /**
077: * add a prepared component to the map
078: *
079: * @param name prepared component name
080: * @param component instance of prepared component
081: */
082: public void addPreparedComponent(final String name,
083: final IPreparedComponent component) {
084: components.put(name, component);
085: }
086:
087: /**
088: * remove a table from the map
089: *
090: * @param name table name
091: */
092: public void removeTable(final String name) {
093: tables.remove(name);
094: }
095:
096: /**
097: * Queries a datasource. The string 'query' defines the name of the query. The
098: * Parameterset given here may contain more data than actually needed.
099: * <p/>
100: * The dataset may change between two calls, do not assume anything!
101: *
102: * @param query the name of the table.
103: * @param parameters are ignored for this factory.
104: * @return the report data or null.
105: */
106: public TableModel queryData(final String query,
107: final DataRow parameters) {
108: TableModel model = (TableModel) tables.get(query);
109: if (model == null) {
110: final IPreparedComponent component = (IPreparedComponent) components
111: .get(query);
112: if (component != null) {
113: final HashMap map = new HashMap();
114: if (parameters != null) {
115: for (int i = 0; i < parameters.getColumnCount(); i++) {
116: map.put(parameters.getColumnName(i), parameters
117: .get(i));
118: }
119: }
120: final IPentahoResultSet rs = component
121: .executePrepared(map);
122: model = new PentahoTableModel(rs);
123: }
124: }
125: return model;
126: }
127:
128: public void open() {
129:
130: }
131:
132: public void close() {
133: final Iterator iter = tables.values().iterator();
134: while (iter.hasNext()) {
135: final TableModel model = (TableModel) iter.next();
136: if (model instanceof CloseableTableModel) {
137: final CloseableTableModel closeableTableModel = (CloseableTableModel) model;
138: closeableTableModel.close();
139: }
140: }
141: tables.clear();
142: }
143:
144: /**
145: * Derives a freshly initialized report data factory, which is independend of
146: * the original data factory. Opening or Closing one data factory must not
147: * affect the other factories.
148: *
149: * @return
150: */
151: public DataFactory derive() throws ReportDataFactoryException {
152: try {
153: return (DataFactory) clone();
154: } catch (CloneNotSupportedException e) {
155: throw new ReportDataFactoryException(
156: Messages
157: .getErrorString("PentahoTableDataFactory.ERROR_0001_CLONE_SHOULD_NOT_FAIL")); //$NON-NLS-1$
158: }
159: }
160:
161: public Object clone() throws CloneNotSupportedException {
162: final PentahoTableDataFactory dataFactory = (PentahoTableDataFactory) super
163: .clone();
164: dataFactory.tables = (HashMap) tables.clone();
165: return dataFactory;
166: }
167:
168: }
|