001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * JRCSVDataSourceConnection.java
028: *
029: * Created on 4 giugno 2003, 18.15
030: *
031: */
032:
033: package it.businesslogic.ireport.connection;
034:
035: import it.businesslogic.ireport.IReportConnectionEditor;
036: import it.businesslogic.ireport.connection.gui.CSVDataSourceConnectionEditor;
037: import it.businesslogic.ireport.gui.MainFrame;
038: import it.businesslogic.ireport.util.*;
039: import java.io.File;
040: import java.text.SimpleDateFormat;
041: import java.util.Vector;
042: import javax.swing.JOptionPane;
043: import net.sf.jasperreports.engine.data.JRCsvDataSource;
044:
045: /**
046: *
047: * @author Administrator
048: */
049: public class JRCSVDataSourceConnection extends
050: it.businesslogic.ireport.IReportConnection {
051:
052: private String name;
053: private String recordDelimiter = "\n";
054: private String fieldDelimiter = ",";
055: private boolean useFirstRowAsHeader = false;
056: private String customDateFormat = "";
057:
058: private String filename;
059:
060: private Vector columnNames = new Vector();
061:
062: /** Creates a new instance of JDBCConnection */
063: public JRCSVDataSourceConnection() {
064: }
065:
066: /** This method return an instanced connection to the database.
067: * If isJDBCConnection() return false => getConnection() return null
068: *
069: */
070: public java.sql.Connection getConnection() {
071: return null;
072: }
073:
074: public boolean isJDBCConnection() {
075: return false;
076: }
077:
078: /*
079: * This method return all properties used by this connection
080: */
081: public java.util.HashMap getProperties() {
082: java.util.HashMap map = new java.util.HashMap();
083: map.put("Filename", Misc.nvl(this .getFilename(), ""));
084: map.put("recordDelimiter", Misc.nvl(this .getRecordDelimiter(),
085: "\n"));
086: map.put("fieldDelimiter", Misc
087: .nvl(this .getFieldDelimiter(), ""));
088: map.put("useFirstRowAsHeader", Misc.nvl(""
089: + this .isUseFirstRowAsHeader(), ""));
090: map.put("customDateFormat", Misc.nvl(
091: this .getCustomDateFormat(), ""));
092:
093: for (int i = 0; i < getColumnNames().size(); ++i) {
094: map.put("COLUMN_" + i, getColumnNames().elementAt(i));
095: }
096: return map;
097: }
098:
099: public void loadProperties(java.util.HashMap map) {
100: this .setFilename((String) map.get("Filename"));
101: this .setRecordDelimiter((String) map.get("recordDelimiter"));
102: this .setFieldDelimiter((String) map.get("fieldDelimiter"));
103: this .setUseFirstRowAsHeader(((String) map
104: .get("useFirstRowAsHeader")).equals("true"));
105: this .setCustomDateFormat((String) map.get("customDateFormat"));
106:
107: int i = 0;
108: while (map.containsKey("COLUMN_" + i)) {
109: getColumnNames().add(map.get("COLUMN_" + i));
110: i++;
111: }
112:
113: }
114:
115: /**
116: * Getter for property filename.
117: * @return Value of property filename.
118: */
119: public java.lang.String getFilename() {
120: return filename;
121: }
122:
123: /**
124: * Setter for property filename.
125: * @param filename New value of property filename.
126: */
127: public void setFilename(java.lang.String filename) {
128: this .filename = filename;
129: }
130:
131: /**
132: * Getter for property name.
133: * @return Value of property name.
134: */
135: public java.lang.String getName() {
136: return name;
137: }
138:
139: /**
140: * Setter for property name.
141: * @param name New value of property name.
142: */
143: public void setName(java.lang.String name) {
144: this .name = name;
145: }
146:
147: /**
148: * This method return an instanced JRDataDource to the database.
149: * If isJDBCConnection() return true => getJRDataSource() return false
150: */
151: public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
152:
153: try {
154: JRCsvDataSource ds = new JRCsvDataSource(new File(
155: getFilename()));
156: if (this .getCustomDateFormat() != null
157: && this .getCustomDateFormat().length() > 0) {
158: ds.setDateFormat(new SimpleDateFormat(this
159: .getCustomDateFormat()));
160: }
161:
162: ds.setFieldDelimiter(getFieldDelimiter().charAt(0));
163: ds.setRecordDelimiter(getRecordDelimiter());
164: ds.setUseFirstRowAsHeader(isUseFirstRowAsHeader());
165:
166: if (!isUseFirstRowAsHeader()) {
167: String[] names = new String[getColumnNames().size()];
168: for (int i = 0; i < names.length; ++i) {
169: names[i] = "" + getColumnNames().elementAt(i);
170: }
171: ds.setColumnNames(names);
172: }
173:
174: return ds;
175: } catch (Exception ex) {
176: ex.printStackTrace();
177: return super .getJRDataSource();
178: }
179: }
180:
181: public boolean isUseFirstRowAsHeader() {
182: return useFirstRowAsHeader;
183: }
184:
185: public void setUseFirstRowAsHeader(boolean useFirstRowAsHeader) {
186: this .useFirstRowAsHeader = useFirstRowAsHeader;
187: }
188:
189: public String getRecordDelimiter() {
190: return recordDelimiter;
191: }
192:
193: public void setRecordDelimiter(String recordDelimiter) {
194: this .recordDelimiter = recordDelimiter;
195: }
196:
197: public String getFieldDelimiter() {
198: return fieldDelimiter;
199: }
200:
201: public void setFieldDelimiter(String fieldDelimiter) {
202: this .fieldDelimiter = fieldDelimiter;
203: }
204:
205: public String getCustomDateFormat() {
206: return customDateFormat;
207: }
208:
209: public void setCustomDateFormat(String customDateFormat) {
210: this .customDateFormat = customDateFormat;
211: }
212:
213: public Vector getColumnNames() {
214: return columnNames;
215: }
216:
217: public void setColumnNames(Vector columnNames) {
218: this .columnNames = columnNames;
219: }
220:
221: public String getDescription() {
222: return I18n.getString("connectionType.csv",
223: "File CSV datasource");
224: }
225:
226: public IReportConnectionEditor getIReportConnectionEditor() {
227: return new CSVDataSourceConnectionEditor();
228: }
229:
230: public void test() throws Exception {
231: String csv_file = getFilename();
232:
233: try {
234:
235: JRCSVDataSourceConnection con = new JRCSVDataSourceConnection();
236: java.io.File f = new java.io.File(csv_file);
237: if (!f.exists()) {
238: JOptionPane
239: .showMessageDialog(
240: MainFrame.getMainInstance(),
241: I18n
242: .getFormattedString(
243: "messages.connectionDialog.fileNotFound",
244: "File {0} not found",
245: new Object[] { csv_file }),
246: I18n.getString("message.title.error",
247: "Error"),
248: JOptionPane.ERROR_MESSAGE);
249: return;
250: }
251:
252: con.setFilename(csv_file);
253: if (con.getJRDataSource() != null) {
254: JOptionPane
255: .showMessageDialog(
256: MainFrame.getMainInstance(),
257: I18n
258: .getString(
259: "messages.connectionDialog.connectionTestSuccessful",
260: "Connection test successful!"),
261: "", JOptionPane.INFORMATION_MESSAGE);
262: return;
263: }
264:
265: } catch (Exception ex) {
266: JOptionPane.showMessageDialog(MainFrame.getMainInstance(),
267: ex.getMessage(), I18n.getString(
268: "message.title.error", "Error"),
269: JOptionPane.ERROR_MESSAGE);
270: ex.printStackTrace();
271: return;
272: }
273: }
274:
275: }
|