01: /*
02: * Copyright (c) 2008, Your Corporation. All Rights Reserved.
03: */
04:
05: package org.pentaho.reportdesigner.crm.report.datasetplugin;
06:
07: import org.jfree.report.DataRow;
08: import org.jfree.report.ReportDataFactoryException;
09: import org.jfree.report.modules.misc.datafactory.sql.ConnectionProvider;
10:
11: import javax.swing.table.TableModel;
12: import java.util.HashMap;
13:
14: /**
15: * User: Martin
16: * Date: Jan 16, 2008
17: * Time: 5:10:58 PM
18: */
19: @SuppressWarnings({"ALL"})
20: public class PreviewableSQLReportDataFactory extends
21: SimplePreviewableSQLReportDataFactory {
22: private HashMap querymappings;
23:
24: public PreviewableSQLReportDataFactory(
25: final ConnectionProvider connectionProvider,
26: boolean limitRows, int maxRows) {
27: super (connectionProvider, limitRows, maxRows);
28: querymappings = new HashMap();
29: }
30:
31: public void setQuery(final String name, final String queryString) {
32: if (queryString == null) {
33: querymappings.remove(name);
34: } else {
35: querymappings.put(name, queryString);
36: }
37: }
38:
39: /**
40: * Queries a datasource. The string 'query' defines the name of the query. The
41: * Parameterset given here may contain more data than actually needed.
42: * <p/>
43: * The dataset may change between two calls, do not assume anything!
44: *
45: * @param query
46: * @param parameters
47: * @return
48: */
49: public synchronized TableModel queryData(final String query,
50: final DataRow parameters) throws ReportDataFactoryException {
51: if (query == null) {
52: throw new NullPointerException("Query is null."); //$NON-NLS-1$
53: }
54: final String realQuery = getQuery(query);
55: if (realQuery == null) {
56: throw new ReportDataFactoryException(
57: "Query '" + query + "' is not recognized."); //$NON-NLS-1$ //$NON-NLS-2$
58: }
59: return super .queryData(realQuery, parameters);
60: }
61:
62: public String getQuery(final String name) {
63: return (String) querymappings.get(name);
64: }
65:
66: public String[] getQueryNames() {
67: return (String[]) querymappings.keySet().toArray(
68: new String[querymappings.size()]);
69: }
70:
71: public Object clone() throws CloneNotSupportedException {
72: final PreviewableSQLReportDataFactory dataFactory = (PreviewableSQLReportDataFactory) super
73: .clone();
74: dataFactory.querymappings = (HashMap) querymappings.clone();
75: return dataFactory;
76: }
77: }
|