KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > connection > JRCSVDataSourceConnection


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * JRCSVDataSourceConnection.java
28  *
29  * Created on 4 giugno 2003, 18.15
30  *
31  */

32
33 package it.businesslogic.ireport.connection;
34 import it.businesslogic.ireport.util.*;
35 import java.io.File JavaDoc;
36 import java.text.SimpleDateFormat JavaDoc;
37 import java.util.Vector JavaDoc;
38 import net.sf.jasperreports.engine.data.JRCsvDataSource;
39
40 /**
41  *
42  * @author Administrator
43  */

44 public class JRCSVDataSourceConnection extends it.businesslogic.ireport.IReportConnection {
45     
46     private String JavaDoc name;
47     private String JavaDoc recordDelimiter = "\n";
48     private String JavaDoc fieldDelimiter = ",";
49     private boolean useFirstRowAsHeader = false;
50     private String JavaDoc customDateFormat = "";
51         
52     private String JavaDoc filename;
53     
54     private Vector JavaDoc columnNames = new Vector JavaDoc();
55     
56     /** Creates a new instance of JDBCConnection */
57     public JRCSVDataSourceConnection() {
58     }
59     
60     /** This method return an instanced connection to the database.
61      * If isJDBCConnection() return false => getConnection() return null
62      *
63      */

64     public java.sql.Connection JavaDoc getConnection() {
65             return null;
66     }
67     
68     public boolean isJDBCConnection() {
69         return false;
70     }
71     
72     /*
73      * This method return all properties used by this connection
74      */

75     public java.util.HashMap JavaDoc getProperties()
76     {
77         java.util.HashMap JavaDoc map = new java.util.HashMap JavaDoc();
78         map.put("Filename", Misc.nvl(this.getFilename() ,"") );
79         map.put("recordDelimiter", Misc.nvl(this.getRecordDelimiter(),"\n") );
80         map.put("fieldDelimiter", Misc.nvl(this.getFieldDelimiter() ,"") );
81         map.put("useFirstRowAsHeader", Misc.nvl(""+this.isUseFirstRowAsHeader() ,"") );
82         map.put("customDateFormat", Misc.nvl(this.getCustomDateFormat() ,"") );
83         
84         for (int i=0; i< getColumnNames().size(); ++i)
85         {
86             map.put("COLUMN_" + i,getColumnNames().elementAt(i) );
87         }
88         return map;
89     }
90     
91     public void loadProperties(java.util.HashMap JavaDoc map)
92     {
93         this.setFilename( (String JavaDoc)map.get("Filename"));
94         this.setRecordDelimiter( (String JavaDoc)map.get("recordDelimiter"));
95         this.setFieldDelimiter( (String JavaDoc)map.get("fieldDelimiter"));
96         this.setUseFirstRowAsHeader( ((String JavaDoc)map.get("useFirstRowAsHeader")).equals("true"));
97         this.setCustomDateFormat( (String JavaDoc)map.get("customDateFormat"));
98         
99         int i = 0;
100         while (map.containsKey("COLUMN_" + i))
101         {
102            getColumnNames().add( map.get("COLUMN_" + i));
103            i++;
104         }
105         
106     }
107     
108     public String JavaDoc getDescription(){ return "File CSV Datasource"; }
109     
110     /**
111      * Getter for property filename.
112      * @return Value of property filename.
113      */

114     public java.lang.String JavaDoc getFilename() {
115         return filename;
116     }
117    
118     /**
119      * Setter for property filename.
120      * @param filename New value of property filename.
121      */

122     public void setFilename(java.lang.String JavaDoc filename) {
123         this.filename = filename;
124     }
125     
126     /**
127      * Getter for property name.
128      * @return Value of property name.
129      */

130     public java.lang.String JavaDoc getName() {
131         return name;
132     }
133     
134     /**
135      * Setter for property name.
136      * @param name New value of property name.
137      */

138     public void setName(java.lang.String JavaDoc name) {
139         this.name = name;
140     }
141     
142     /**
143      * This method return an instanced JRDataDource to the database.
144      * If isJDBCConnection() return true => getJRDataSource() return false
145      */

146     public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
147         
148         try {
149         JRCsvDataSource ds = new JRCsvDataSource(new File JavaDoc(getFilename()));
150         if (this.getCustomDateFormat() != null && this.getCustomDateFormat().length() > 0)
151         {
152             ds.setDateFormat(new SimpleDateFormat JavaDoc(this.getCustomDateFormat()));
153         }
154         
155         ds.setFieldDelimiter( getFieldDelimiter().charAt(0) );
156         ds.setRecordDelimiter( getRecordDelimiter());
157         ds.setUseFirstRowAsHeader( isUseFirstRowAsHeader());
158         
159         if (!isUseFirstRowAsHeader())
160         {
161             String JavaDoc[] names = new String JavaDoc[getColumnNames().size()];
162             for (int i=0; i<names.length; ++i )
163             {
164                 names[i] = ""+getColumnNames().elementAt(i);
165             }
166             ds.setColumnNames( names );
167         }
168         
169         return ds;
170         } catch (Exception JavaDoc ex)
171         {
172             ex.printStackTrace();
173             return super.getJRDataSource();
174         }
175     }
176
177     public boolean isUseFirstRowAsHeader() {
178         return useFirstRowAsHeader;
179     }
180
181     public void setUseFirstRowAsHeader(boolean useFirstRowAsHeader) {
182         this.useFirstRowAsHeader = useFirstRowAsHeader;
183     }
184
185     public String JavaDoc getRecordDelimiter() {
186         return recordDelimiter;
187     }
188
189     public void setRecordDelimiter(String JavaDoc recordDelimiter) {
190         this.recordDelimiter = recordDelimiter;
191     }
192
193     public String JavaDoc getFieldDelimiter() {
194         return fieldDelimiter;
195     }
196
197     public void setFieldDelimiter(String JavaDoc fieldDelimiter) {
198         this.fieldDelimiter = fieldDelimiter;
199     }
200
201     public String JavaDoc getCustomDateFormat() {
202         return customDateFormat;
203     }
204
205     public void setCustomDateFormat(String JavaDoc customDateFormat) {
206         this.customDateFormat = customDateFormat;
207     }
208
209     public Vector JavaDoc getColumnNames() {
210         return columnNames;
211     }
212
213     public void setColumnNames(Vector JavaDoc columnNames) {
214         this.columnNames = columnNames;
215     }
216 }
217
218
Popular Tags