KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > examples > queryexecuter > CSVFieldsProvider


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  *
21  *
22  * CSVFieldsProvider.java
23  *
24  * Created on December 6, 2006, 11:56 PM
25  *
26  * To change this template, choose Tools | Template Manager
27  * and open the template in the editor.
28  */

29
30 package it.businesslogic.ireport.examples.queryexecuter;
31
32 import it.businesslogic.ireport.FieldsProvider;
33 import it.businesslogic.ireport.FieldsProviderEditor;
34 import it.businesslogic.ireport.IReportConnection;
35 import it.businesslogic.ireport.data.TestDesigner;
36 import it.businesslogic.ireport.gui.ReportQueryDialog;
37 import java.util.Map JavaDoc;
38 import javax.swing.JOptionPane JavaDoc;
39 import net.sf.jasperreports.engine.JRDataSource;
40 import net.sf.jasperreports.engine.JRDataset;
41 import net.sf.jasperreports.engine.JRException;
42 import net.sf.jasperreports.engine.JRField;
43 import net.sf.jasperreports.engine.design.JRDesignField;
44 import net.sf.jasperreports.engine.query.JRQueryExecuter;
45
46 /**
47  *
48  * @author gtoffoli
49  */

50 public class CSVFieldsProvider implements FieldsProvider {
51     
52     /** Creates a new instance of CSVFieldsProvider */
53     public CSVFieldsProvider() {
54     }
55
56     public JRField[] getFields(IReportConnection con, JRDataset report, Map JavaDoc parameters) throws JRException, UnsupportedOperationException JavaDoc {
57     
58         java.util.List JavaDoc list = new java.util.ArrayList JavaDoc();
59         
60         // 1. Instance the query executer....
61
JRQueryExecuter executer = new CSVQueryExecuterFactory().createQueryExecuter(report, parameters);
62         JRDataSource ds = executer.createDatasource();
63         if (ds.next() != false)
64         {
65             for (int i=0; ; ++i)
66             {
67                 JRDesignField f = new JRDesignField();
68                 f.setName("COLUMN_" + i);
69                 try {
70                     ds.getFieldValue(f);
71                     list.add(f);
72                 } catch (Exception JavaDoc ex)
73                 {
74                     //ex.printStackTrace();
75
break;
76                 }
77             }
78         }
79         
80         JRField[] fields = new JRField[list.size()];
81         for (int i=0; i<fields.length; ++i)
82         {
83             fields[i] = (JRField)list.get(i);
84         }
85         
86         return fields;
87     
88     }
89
90     public boolean supportsAutomaticQueryExecution() {
91         return true;
92     }
93
94     public boolean hasQueryDesigner() {
95         return true;
96     }
97
98     public String JavaDoc designQuery(IReportConnection con, String JavaDoc query, ReportQueryDialog reportQueryDialog) throws JRException, UnsupportedOperationException JavaDoc {
99         // Start FREE QUERY BUILDER....
100
TestDesigner td = new TestDesigner(reportQueryDialog, true);
101         td.setQuery(query);
102         td.setVisible(true);
103         if (td.getDialogResult() == JOptionPane.OK_OPTION)
104         {
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(ReportQueryDialog reportQueryDialog) {
119         return null;
120     }
121     
122 }
123
Popular Tags