001: /*
002: * Copyright (C) 2005-2007 JasperSoft http://www.jaspersoft.com
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed WITHOUT ANY WARRANTY; and without the
010: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: * See the GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public License
014: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
015: * or write to:
016: *
017: * Free Software Foundation, Inc.,
018: * 59 Temple Place - Suite 330,
019: * Boston, MA USA 02111-1307
020: *
021: *
022: * CSVFieldsProvider.java
023: *
024: * Created on December 6, 2006, 11:56 PM
025: *
026: * To change this template, choose Tools | Template Manager
027: * and open the template in the editor.
028: */
029:
030: package it.businesslogic.ireport.examples.queryexecuter;
031:
032: import it.businesslogic.ireport.FieldsProvider;
033: import it.businesslogic.ireport.FieldsProviderEditor;
034: import it.businesslogic.ireport.IReportConnection;
035: import it.businesslogic.ireport.data.TestDesigner;
036: import it.businesslogic.ireport.gui.ReportQueryDialog;
037: import java.util.Map;
038: import javax.swing.JOptionPane;
039: import net.sf.jasperreports.engine.JRDataSource;
040: import net.sf.jasperreports.engine.JRDataset;
041: import net.sf.jasperreports.engine.JRException;
042: import net.sf.jasperreports.engine.JRField;
043: import net.sf.jasperreports.engine.design.JRDesignField;
044: import net.sf.jasperreports.engine.query.JRQueryExecuter;
045:
046: /**
047: *
048: * @author gtoffoli
049: */
050: public class CSVFieldsProvider implements FieldsProvider {
051:
052: /** Creates a new instance of CSVFieldsProvider */
053: public CSVFieldsProvider() {
054: }
055:
056: public JRField[] getFields(IReportConnection con, JRDataset report,
057: Map parameters) throws JRException,
058: UnsupportedOperationException {
059:
060: java.util.List list = new java.util.ArrayList();
061:
062: // 1. Instance the query executer....
063: JRQueryExecuter executer = new CSVQueryExecuterFactory()
064: .createQueryExecuter(report, parameters);
065: JRDataSource ds = executer.createDatasource();
066: if (ds.next() != false) {
067: for (int i = 0;; ++i) {
068: JRDesignField f = new JRDesignField();
069: f.setName("COLUMN_" + i);
070: try {
071: ds.getFieldValue(f);
072: list.add(f);
073: } catch (Exception ex) {
074: //ex.printStackTrace();
075: break;
076: }
077: }
078: }
079:
080: JRField[] fields = new JRField[list.size()];
081: for (int i = 0; i < fields.length; ++i) {
082: fields[i] = (JRField) list.get(i);
083: }
084:
085: return fields;
086:
087: }
088:
089: public boolean supportsAutomaticQueryExecution() {
090: return true;
091: }
092:
093: public boolean hasQueryDesigner() {
094: return true;
095: }
096:
097: public String designQuery(IReportConnection con, String query,
098: ReportQueryDialog reportQueryDialog) throws JRException,
099: UnsupportedOperationException {
100: // Start FREE QUERY BUILDER....
101: TestDesigner td = new TestDesigner(reportQueryDialog, true);
102: td.setQuery(query);
103: td.setVisible(true);
104: if (td.getDialogResult() == JOptionPane.OK_OPTION) {
105: return td.getQuery();
106: }
107: return null;
108: }
109:
110: public boolean supportsGetFieldsOperation() {
111: return true;
112: }
113:
114: public boolean hasEditorComponent() {
115: return false;
116: }
117:
118: public FieldsProviderEditor getEditorComponent(
119: ReportQueryDialog reportQueryDialog) {
120: return null;
121: }
122:
123: }
|