001: /*
002: * Copyright 2006-2007 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.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.datasetplugin;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.jetbrains.annotations.Nullable;
018: import org.jfree.report.DataFactory;
019: import org.jfree.report.DataRow;
020: import org.jfree.report.ReportDataFactoryException;
021: import org.jfree.report.modules.misc.datafactory.sql.SQLReportDataFactory;
022:
023: import javax.swing.table.TableModel;
024: import java.util.ArrayList;
025:
026: /**
027: * User: Martin
028: * Date: 06.03.2007
029: * Time: 17:43:31
030: */
031: public class MultiDataFactory implements ExtendedDataFactory {
032: @NotNull
033: private ArrayList<ExtendedDataFactory> dataFactories;
034:
035: public MultiDataFactory() {
036: dataFactories = new ArrayList<ExtendedDataFactory>();
037: }
038:
039: public void close() {
040: for (DataFactory dataFactory : dataFactories) {
041: dataFactory.close();
042: }
043: }
044:
045: public void addDataFactory(@NotNull
046: final SQLReportDataFactory dataFactory) {
047: dataFactories.add(new ExtendedDataFactory() {
048: public boolean canExecuteQuery(@Nullable
049: String query) {
050: String[] queryNames = dataFactory.getQueryNames();
051: for (String queryName : queryNames) {
052: if (query != null && query.equals(queryName)) {
053: return true;
054: }
055: }
056: return false;
057: }
058:
059: @NotNull
060: public TableModel queryData(@Nullable
061: String query, @Nullable
062: DataRow parameters) throws ReportDataFactoryException {
063: return dataFactory.queryData(query, parameters);
064: }
065:
066: @NotNull
067: public DataFactory derive()
068: throws ReportDataFactoryException {
069: return dataFactory.derive();
070: }
071:
072: public void open() {
073: dataFactory.open();
074: }
075:
076: public void close() {
077: dataFactory.close();
078: }
079: });
080: }
081:
082: public void addDataFactory(@NotNull
083: final PreviewableSQLReportDataFactory dataFactory) {
084: dataFactories.add(new ExtendedDataFactory() {
085: public boolean canExecuteQuery(@Nullable
086: String query) {
087: String[] queryNames = dataFactory.getQueryNames();
088: for (String queryName : queryNames) {
089: if (query != null && query.equals(queryName)) {
090: return true;
091: }
092: }
093: return false;
094: }
095:
096: @NotNull
097: public TableModel queryData(@Nullable
098: String query, @Nullable
099: DataRow parameters) throws ReportDataFactoryException {
100: return dataFactory.queryData(query, parameters);
101: }
102:
103: @NotNull
104: public DataFactory derive()
105: throws ReportDataFactoryException {
106: return dataFactory.derive();
107: }
108:
109: public void open() {
110: dataFactory.open();
111: }
112:
113: public void close() {
114: dataFactory.close();
115: }
116: });
117: }
118:
119: public void addExtendedDataFactory(@NotNull
120: ExtendedDataFactory extendedDataFactory) {
121: dataFactories.add(extendedDataFactory);
122: }
123:
124: @NotNull
125: public DataFactory derive() throws ReportDataFactoryException {
126: return this ;
127: }
128:
129: public void open() {
130: }
131:
132: @Nullable
133: public TableModel queryData(@Nullable
134: final String query, @Nullable
135: final DataRow parameters) throws ReportDataFactoryException {
136: for (ExtendedDataFactory dataFactory : dataFactories) {
137: if (dataFactory.canExecuteQuery(query)) {
138: return dataFactory.queryData(query, parameters);
139: }
140: }
141: return null;
142: }
143:
144: public boolean canExecuteQuery(@Nullable
145: String query) {
146: for (ExtendedDataFactory dataFactory : dataFactories) {
147: if (dataFactory.canExecuteQuery(query)) {
148: return true;
149: }
150: }
151: return false;
152: }
153: }
|