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: * JavaBeanDataSourceConnection.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.*;
036: import it.businesslogic.ireport.IReportConnectionEditor;
037: import it.businesslogic.ireport.connection.gui.JavaBeanDataSourceConnectionEditor;
038: import it.businesslogic.ireport.gui.MainFrame;
039: import it.businesslogic.ireport.util.*;
040: import java.sql.*;
041: import javax.swing.*;
042: import net.sf.jasperreports.engine.data.JRAbstractBeanDataSource;
043:
044: /**
045: *
046: * @author Administrator
047: */
048: public class JavaBeanDataSourceConnection extends
049: it.businesslogic.ireport.IReportConnection {
050:
051: public static String BEAN_ARRAY = "BEAN_ARRAY";
052: public static String BEAN_COLLECTION = "BEAN_COLLECTION";
053:
054: private String name;
055:
056: private String factoryClass;
057:
058: private String methodToCall;
059:
060: private boolean useFieldDescription;
061:
062: private String type = "BEAN_COLLECTION";
063:
064: /** Creates a new instance of JDBCConnection */
065:
066: public JavaBeanDataSourceConnection() {
067: }
068:
069: /** This method return an instanced connection to the database.
070: * If isJDBCConnection() return false => getConnection() return null
071: *
072: */
073: public java.sql.Connection getConnection() {
074: return null;
075: }
076:
077: public boolean isJDBCConnection() {
078: return false;
079: }
080:
081: /*
082: * This method return all properties used by this connection
083: */
084: public java.util.HashMap getProperties() {
085: java.util.HashMap map = new java.util.HashMap();
086: map.put("FactoryClass", Misc.nvl(this .getFactoryClass(), ""));
087: map.put("MethodToCall", Misc.nvl(this .getMethodToCall(), ""));
088: map.put("Type", Misc.nvl(this .getType(), ""));
089: map.put("UseFieldDescription", ""
090: + this .isUseFieldDescription());
091:
092: return map;
093: }
094:
095: public void loadProperties(java.util.HashMap map) {
096: this .setFactoryClass((String) map.get("FactoryClass"));
097: this .setMethodToCall((String) map.get("MethodToCall"));
098: if (map.containsKey("UseFieldDescription")) {
099: this .setUseFieldDescription(((String) map
100: .get("UseFieldDescription")).equals("true"));
101: }
102: if (map.containsKey("Type")) {
103: this .setType((String) map.get("Type"));
104: }
105: }
106:
107: /** Getter for property methodToCall.
108: * @return Value of property methodToCall.
109: *
110: */
111: public java.lang.String getMethodToCall() {
112: return methodToCall;
113: }
114:
115: /** Setter for property methodToCall.
116: * @param methodToCall New value of property methodToCall.
117: *
118: */
119: public void setMethodToCall(java.lang.String methodToCall) {
120: this .methodToCall = methodToCall;
121: }
122:
123: /** Getter for property factoryClass.
124: * @return Value of property factoryClass.
125: *
126: */
127: public java.lang.String getFactoryClass() {
128: return factoryClass;
129: }
130:
131: /** Setter for property factoryClass.
132: * @param factoryClass New value of property factoryClass.
133: *
134: */
135: public void setFactoryClass(java.lang.String factoryClass) {
136: this .factoryClass = factoryClass;
137: }
138:
139: /**
140: * Getter for property type.
141: * @return Value of property type.
142: */
143: public java.lang.String getType() {
144: return type;
145: }
146:
147: /**
148: * Setter for property type.
149: * @param type New value of property type.
150: */
151: public void setType(java.lang.String type) {
152: this .type = type;
153: }
154:
155: /**
156: * This method return an instanced JRDataDource to the database.
157: * If isJDBCConnection() return true => getJRDataSource() return false
158: */
159: public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
160: try {
161:
162: Class clazz = Thread.currentThread()
163: .getContextClassLoader().loadClass(factoryClass);
164: Object obj = clazz.newInstance();
165: Object return_obj = obj.getClass().getMethod(methodToCall,
166: new Class[0]).invoke(null, new Object[0]);
167:
168: if (return_obj != null) {
169: if (Misc.nvl(this .getType(), "").equals(BEAN_ARRAY)) {
170: return new net.sf.jasperreports.engine.data.JRBeanArrayDataSource(
171: (Object[]) return_obj,
172: isUseFieldDescription());
173: } else if (Misc.nvl(this .getType(), "").equals(
174: BEAN_COLLECTION)) {
175: return new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(
176: (java.util.Collection) return_obj,
177: isUseFieldDescription());
178: }
179: }
180: return new net.sf.jasperreports.engine.JREmptyDataSource();
181:
182: } catch (Exception ex) {
183: ex.printStackTrace();
184: return super .getJRDataSource();
185: }
186: }
187:
188: public boolean isUseFieldDescription() {
189: return useFieldDescription;
190: }
191:
192: public void setUseFieldDescription(boolean useFieldDescription) {
193: this .useFieldDescription = useFieldDescription;
194: }
195:
196: public String getDescription() {
197: return I18n.getString("connectionType.javabeans",
198: "JavaBeans set datasource");
199: }
200:
201: public IReportConnectionEditor getIReportConnectionEditor() {
202: return new JavaBeanDataSourceConnectionEditor();
203: }
204:
205: public void test() throws Exception {
206: try {
207: Object obj = Class.forName(getFactoryClass(), true,
208: MainFrame.getMainInstance().getReportClassLoader())
209: .newInstance();
210: Object ret_obj = obj.getClass().getMethod(
211: getMethodToCall(), new Class[0]).invoke(null,
212: new Object[0]);
213:
214: if (ret_obj != null && getType() == BEAN_COLLECTION
215: && (ret_obj instanceof java.util.Collection)) {
216: JOptionPane
217: .showMessageDialog(
218: MainFrame.getMainInstance(),
219: I18n
220: .getString(
221: "messages.connectionDialog.connectionTestSuccessful",
222: "Connection test successful!"),
223: "", JOptionPane.INFORMATION_MESSAGE);
224: } else if (ret_obj != null && getType() == BEAN_ARRAY
225: && (ret_obj instanceof Object[])) {
226: JOptionPane
227: .showMessageDialog(
228: MainFrame.getMainInstance(),
229: I18n
230: .getString(
231: "messages.connectionDialog.connectionTestSuccessful",
232: "Connection test successful!"),
233: "", JOptionPane.INFORMATION_MESSAGE);
234: } else {
235: JOptionPane
236: .showMessageDialog(
237: MainFrame.getMainInstance(),
238: I18n
239: .getString(
240: "messages.connectionDialog.notValidValueReturned",
241: "The method don't return a valid Array or java.util.Collection!\n"),
242: I18n.getString("message.title.error",
243: "Error"),
244: JOptionPane.ERROR_MESSAGE);
245: }
246:
247: } catch (NoClassDefFoundError ex) {
248: JOptionPane
249: .showMessageDialog(
250: MainFrame.getMainInstance(),
251: I18n
252: .getFormattedString(
253: "messages.connection.noClassDefFoundError",
254: "{0}\nNoClassDefFoundError!!\nCheck your classpath!\n{1}",
255: new Object[] {
256: "",
257: ""
258: + ex
259: .getMessage() }),
260: I18n.getString("message.title.exception",
261: "Exception"),
262: JOptionPane.ERROR_MESSAGE);
263: throw ex;
264: } catch (ClassNotFoundException ex) {
265: JOptionPane
266: .showMessageDialog(
267: MainFrame.getMainInstance(),
268: I18n
269: .getFormattedString(
270: "messages.connection.classNotFoundError",
271: "{0}\nClassNotFoundError!\nMsg: {1}\nPossible not found class: {2}\nCheck your classpath!",
272: new Object[] {
273: "",
274: ""
275: + ex
276: .getMessage(),
277: ""
278: + getFactoryClass() }),
279: I18n.getString("message.title.exception",
280: "Exception"),
281: JOptionPane.ERROR_MESSAGE);
282: throw ex;
283: } catch (Exception ex) {
284: ex.printStackTrace();
285:
286: JOptionPane.showMessageDialog(MainFrame.getMainInstance(),
287: I18n.getFormattedString(
288: "messages.connection.generalError2",
289: "{0}\nGeneral problem:\n {1}",
290: new Object[] { "", "" + ex.getMessage() }),
291: I18n.getString("message.title.exception",
292: "Exception"), JOptionPane.ERROR_MESSAGE);
293: throw ex;
294: }
295: }
296: }
|