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: * JRCustomDataSourceConnection.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.JRCustomDataSourceConnectionEditor;
037: import it.businesslogic.ireport.gui.MainFrame;
038: import it.businesslogic.ireport.util.*;
039: import java.awt.Component;
040: import javax.swing.JOptionPane;
041:
042: /**
043: *
044: * @author Administrator
045: */
046: public class JRCustomDataSourceConnection extends
047: it.businesslogic.ireport.IReportConnection {
048:
049: private String name;
050:
051: private String factoryClass;
052:
053: private String methodToCall;
054:
055: /** Creates a new instance of JDBCConnection */
056:
057: public JRCustomDataSourceConnection() {
058: }
059:
060: /** This method return an instanced connection to the database.
061: * If isJDBCConnection() return false => getConnection() return null
062: *
063: */
064: public java.sql.Connection getConnection() {
065: return null;
066: }
067:
068: public boolean isJDBCConnection() {
069: return false;
070: }
071:
072: /*
073: * This method return all properties used by this connection
074: */
075: public java.util.HashMap getProperties() {
076: java.util.HashMap map = new java.util.HashMap();
077: map.put("FactoryClass", Misc.nvl(this .getFactoryClass(), ""));
078: map.put("MethodToCall", Misc.nvl(this .getMethodToCall(), ""));
079:
080: return map;
081: }
082:
083: public void loadProperties(java.util.HashMap map) {
084: this .setFactoryClass((String) map.get("FactoryClass"));
085: this .setMethodToCall((String) map.get("MethodToCall"));
086: }
087:
088: /** Getter for property methodToCall.
089: * @return Value of property methodToCall.
090: *
091: */
092: public java.lang.String getMethodToCall() {
093: return methodToCall;
094: }
095:
096: /** Setter for property methodToCall.
097: * @param methodToCall New value of property methodToCall.
098: *
099: */
100: public void setMethodToCall(java.lang.String methodToCall) {
101: this .methodToCall = methodToCall;
102: }
103:
104: /** Getter for property factoryClass.
105: * @return Value of property factoryClass.
106: *
107: */
108: public java.lang.String getFactoryClass() {
109: return factoryClass;
110: }
111:
112: /** Setter for property factoryClass.
113: * @param factoryClass New value of property factoryClass.
114: *
115: */
116: public void setFactoryClass(java.lang.String factoryClass) {
117: this .factoryClass = factoryClass;
118: }
119:
120: /**
121: * This method return an instanced JRDataDource to the database.
122: * If isJDBCConnection() return true => getJRDataSource() return false
123: */
124: public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
125: try {
126: Object obj = Class.forName(factoryClass, true,
127: Thread.currentThread().getContextClassLoader())
128: .newInstance();
129: return (net.sf.jasperreports.engine.JRDataSource) obj
130: .getClass().getMethod(methodToCall, new Class[0])
131: .invoke(obj, new Object[0]);
132: } catch (Exception ex) {
133: ex.printStackTrace();
134: return super .getJRDataSource();
135: }
136: }
137:
138: public String getDescription() {
139: return I18n.getString("connectionType.customDataSource",
140: "Custom JRDataSource");
141: }
142:
143: public IReportConnectionEditor getIReportConnectionEditor() {
144: return new JRCustomDataSourceConnectionEditor();
145: }
146:
147: public void test() throws Exception {
148: try {
149: Object obj = Class.forName(getFactoryClass(), true,
150: MainFrame.getMainInstance().getReportClassLoader())
151: .newInstance();
152: obj.getClass().getMethod(getMethodToCall(), new Class[0])
153: .invoke(obj, new Object[0]);
154: } catch (NoClassDefFoundError ex) {
155: JOptionPane
156: .showMessageDialog(
157: MainFrame.getMainInstance(),
158: I18n
159: .getFormattedString(
160: "messages.connection.noClassDefFoundError",
161: "{0}\nNoClassDefFoundError!!\nCheck your classpath!\n{1}",
162: new Object[] {
163: "",
164: ""
165: + ex
166: .getMessage() }),
167: I18n.getString("message.title.exception",
168: "Exception"),
169: JOptionPane.ERROR_MESSAGE);
170: throw new Exception();
171: } catch (ClassNotFoundException ex) {
172: JOptionPane
173: .showMessageDialog(
174: MainFrame.getMainInstance(),
175: I18n
176: .getFormattedString(
177: "messages.connection.classNotFoundError",
178: "{0}\nClassNotFoundError!\nMsg: {1}\nPossible not found class: {2}\nCheck your classpath!",
179: new Object[] {
180: "",
181: ""
182: + ex
183: .getMessage(),
184: ""
185: + getFactoryClass() }),
186: I18n.getString("message.title.exception",
187: "Exception"),
188: JOptionPane.ERROR_MESSAGE);
189: throw new Exception();
190: } catch (Exception ex) {
191:
192: JOptionPane.showMessageDialog(MainFrame.getMainInstance(),
193: I18n.getFormattedString(
194: "messages.connection.generalError2",
195: "{0}\nGeneral problem:\n {1}",
196: new Object[] { "", "" + ex.getMessage() }),
197: I18n.getString("message.title.exception",
198: "Exception"), JOptionPane.ERROR_MESSAGE);
199: return;
200: }
201: JOptionPane
202: .showMessageDialog(
203: MainFrame.getMainInstance(),
204: I18n
205: .getString(
206: "messages.connectionDialog.connectionTestSuccessful",
207: "Connection test successful!"),
208: "", JOptionPane.INFORMATION_MESSAGE);
209: return;
210: }
211: }
|