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: * JRDataSourceProviderConnection.java
028: *
029: * Created on 17 febbraio 2005, 7.26
030: *
031: */
032:
033: package it.businesslogic.ireport.connection;
034:
035: import it.businesslogic.ireport.IReportConnectionEditor;
036: import it.businesslogic.ireport.connection.gui.JRDataSourceProviderConnectionEditor;
037: import it.businesslogic.ireport.util.I18n;
038: import java.lang.reflect.InvocationTargetException;
039: import net.sf.jasperreports.engine.*;
040: import javax.swing.*;
041: import it.businesslogic.ireport.gui.MainFrame;
042:
043: /**
044: *
045: * @author Administrator
046: */
047: public class JRDataSourceProviderConnection extends
048: it.businesslogic.ireport.IReportConnection {
049:
050: private net.sf.jasperreports.engine.JRDataSourceProvider dsp;
051: private net.sf.jasperreports.engine.JRDataSource ds;
052: private java.util.HashMap properties = new java.util.HashMap();
053:
054: public net.sf.jasperreports.engine.JRDataSourceProvider getDataSourceProvider() {
055:
056: if (dsp == null
057: && this .getProperties().get("JRDataSourceProvider") != null) {
058: try {
059: dsp = (JRDataSourceProvider) (Class.forName(
060: (String) this .getProperties().get(
061: "JRDataSourceProvider"), true,
062: it.businesslogic.ireport.gui.MainFrame
063: .getMainInstance()
064: .getReportClassLoader()).newInstance());
065: } catch (NoClassDefFoundError ex) {
066: showErrorMessage(
067: I18n
068: .getString(
069: "messages.JRDataSourceProviderConnection.noClassDefFoundError",
070: "No class definition found error!!\nCheck your classpath!"),
071: I18n.getString("message.title.exception",
072: "Exception"));
073: } catch (ClassNotFoundException ex) {
074: showErrorMessage(
075: I18n
076: .getString(
077: "messages.JRDataSourceProviderConnection.classNotFoundError",
078: "Class not found error!!\nCheck your classpath!"),
079: I18n.getString("message.title.exception",
080: "Exception"));
081: } catch (Exception ex) {
082: showErrorMessage("" + ex.getMessage(), I18n.getString(
083: "message.title.exception", "Exception"));
084: }
085: }
086:
087: return dsp;
088: }
089:
090: private void showErrorMessage(final String errorMsg,
091: final String title) {
092: Runnable r = new Runnable() {
093: public void run() {
094: JOptionPane.showMessageDialog(MainFrame
095: .getMainInstance(), errorMsg, title,
096: JOptionPane.ERROR_MESSAGE);
097: }
098: };
099:
100: if (!SwingUtilities.isEventDispatchThread()) {
101: try {
102: SwingUtilities.invokeAndWait(r);
103: } catch (InvocationTargetException ex) {
104: ex.printStackTrace();
105: } catch (InterruptedException ex) {
106: ex.printStackTrace();
107: }
108: } else {
109: r.run();
110: }
111: }
112:
113: /** Creates a new instance of JRDataSourceProviderConnection */
114: public JRDataSourceProviderConnection() {
115: }
116:
117: /**
118: * This method return an instanced JRDataDource to the database.
119: * If isJDBCConnection() return true => getJRDataSource() return false
120: */
121: public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
122:
123: return getJRDataSource(null);
124: }
125:
126: public net.sf.jasperreports.engine.JRDataSource getJRDataSource(
127: JasperReport jasper) {
128:
129: if (ds != null) {
130: JOptionPane
131: .showMessageDialog(
132: MainFrame.getMainInstance(),
133: I18n
134: .getString(
135: "messages.JRDataSourceProviderConnection.datasourceInUse",
136: "This datasource is already in use by another filling process!!"),
137: I18n.getString("message.title.error",
138: "Error"), JOptionPane.ERROR_MESSAGE);
139: return null;
140: }
141:
142: try {
143: ds = getDataSourceProvider().create(jasper);
144: } catch (Exception ex) {
145: JOptionPane
146: .showMessageDialog(
147: MainFrame.getMainInstance(),
148: I18n
149: .getFormattedString(
150: "messages.JRDataSourceProviderConnection.problemsCreatingDatasource",
151: "Problems occurred creating the new datasource!!\n{0}",
152: new Object[] { ""
153: + ex.getMessage() }),
154: I18n.getString("message.title.error",
155: "Error"), JOptionPane.ERROR_MESSAGE);
156: }
157:
158: return ds;
159: }
160:
161: public void disposeDataSource() {
162: if (dsp != null) {
163: try {
164: dsp.dispose(ds);
165: } catch (Exception ex) {
166: }
167: ds = null;
168: }
169: }
170:
171: public java.util.HashMap getProperties() {
172: return properties;
173: }
174:
175: /** All properties of a IReportConnection are stored in a XML file as Pair key/value
176: * This HashMap contains alla properties found for this IReportConnection in the
177: * XML. You must use this hashMap to initialize all attributes of your IReprotConnection
178: */
179: public void loadProperties(java.util.HashMap map) {
180: properties = map;
181: }
182:
183: /** Redefine this method is not useful (and not raccomended)
184: * It just write a portion of XML for save properties a IReportConnection name
185: */
186: public void save(java.io.PrintWriter pw) {
187: java.util.HashMap hm = this .getProperties();
188: pw.println("\t<iReportConnection name=\"" + this .getName()
189: + "\" connectionClass=\"" + this .getClass().getName()
190: + "\">");
191: java.util.Iterator iterator = hm.keySet().iterator();
192:
193: while (iterator.hasNext()) {
194: String key = (String) iterator.next();
195: pw.println("\t\t<connectionParameter name=\"" + key
196: + "\"><![CDATA[" + hm.get(key)
197: + "]]></connectionParameter>");
198: }
199: pw.println("\t</iReportConnection>");
200: }
201:
202: public String getDescription() {
203: return I18n.getString("connectionType.datasourceProvider",
204: "JRDataSourceProvider");
205: }
206:
207: public IReportConnectionEditor getIReportConnectionEditor() {
208: return new JRDataSourceProviderConnectionEditor();
209: }
210:
211: public void test() throws Exception {
212: try {
213:
214: Class c = Class.forName((String) this .getProperties().get(
215: "JRDataSourceProvider"), true, MainFrame
216: .getMainInstance().getReportClassLoader());
217:
218: if (!(net.sf.jasperreports.engine.JRDataSourceProvider.class
219: .isAssignableFrom(c))) {
220: JOptionPane
221: .showMessageDialog(
222: MainFrame.getMainInstance(),
223: I18n
224: .getFormattedString(
225: "messages.connectionDialog.invalidJRDataSourceProvider",
226: "\"{0}\" is not a subclass of\nnet.sf.jasperreports.engine.JRDataSourceProvider.",
227: new Object[] { this
228: .getProperties()
229: .get(
230: "JRDataSourceProvider") }),
231: I18n.getString("message.title.error",
232: "Error"),
233: JOptionPane.ERROR_MESSAGE);
234: return;
235: } else {
236: JOptionPane
237: .showMessageDialog(
238: MainFrame.getMainInstance(),
239: I18n
240: .getString(
241: "messages.connectionDialog.connectionTestSuccessful",
242: "Connection test successful!"),
243: "", JOptionPane.INFORMATION_MESSAGE);
244: return;
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: return;
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: + this
279: .getProperties()
280: .get(
281: "JRDataSourceProvider") }),
282: I18n.getString("message.title.exception",
283: "Exception"),
284: JOptionPane.ERROR_MESSAGE);
285: return;
286: } catch (Exception ex) {
287: JOptionPane.showMessageDialog(MainFrame.getMainInstance(),
288: I18n.getFormattedString(
289: "messages.connection.generalError2",
290: "{0}\nGeneral problem:\n {1}",
291: new Object[] { "", "" + ex.getMessage() }),
292: I18n.getString("message.title.exception",
293: "Exception"), JOptionPane.ERROR_MESSAGE);
294: return;
295: }
296: }
297: }
|