KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JRDataSourceProviderConnection.java
28  *
29  * Created on 17 febbraio 2005, 7.26
30  *
31  */

32
33 package it.businesslogic.ireport.connection;
34
35
36 import it.businesslogic.ireport.util.I18n;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38 import net.sf.jasperreports.engine.*;
39 import javax.swing.*;
40 import it.businesslogic.ireport.gui.MainFrame;
41 /**
42  *
43  * @author Administrator
44  */

45 public class JRDataSourceProviderConnection extends it.businesslogic.ireport.IReportConnection {
46     
47     
48     private net.sf.jasperreports.engine.JRDataSourceProvider dsp;
49     private net.sf.jasperreports.engine.JRDataSource ds;
50     private java.util.HashMap JavaDoc properties = new java.util.HashMap JavaDoc();
51     
52     public net.sf.jasperreports.engine.JRDataSourceProvider getDataSourceProvider() {
53         
54         if (dsp == null && this.getProperties().get("JRDataSourceProvider") != null)
55         {
56             try {
57                 dsp = (JRDataSourceProvider)(Class.forName( (String JavaDoc)this.getProperties().get("JRDataSourceProvider"), true, it.businesslogic.ireport.gui.MainFrame.getMainInstance().getReportClassLoader()).newInstance());
58             } catch (NoClassDefFoundError JavaDoc ex)
59         {
60                 showErrorMessage(
61                                 I18n.getString("messages.JRDataSourceProviderConnection.noClassDefFoundError",
62                                 "No class definition found error!!\nCheck your classpath!"),
63                                 I18n.getString("message.title.exception","Exception"));
64             }
65         catch (ClassNotFoundException JavaDoc ex)
66         {
67         showErrorMessage(
68                         I18n.getString("messages.JRDataSourceProviderConnection.classNotFoundError",
69                                 "Class not found error!!\nCheck your classpath!"),
70                         I18n.getString("message.title.exception","Exception"));
71             }
72             catch (Exception JavaDoc ex)
73             {
74                 showErrorMessage("" + ex.getMessage(),
75                         I18n.getString("message.title.exception","Exception"));
76             }
77         }
78         
79         return dsp;
80     }
81     
82     private void showErrorMessage(final String JavaDoc errorMsg, final String JavaDoc title)
83     {
84         Runnable JavaDoc r = new Runnable JavaDoc() {
85                 public void run() {
86                     JOptionPane.showMessageDialog(MainFrame.getMainInstance(),errorMsg,title,JOptionPane.ERROR_MESSAGE);
87                 }
88             };
89
90         if (!SwingUtilities.isEventDispatchThread())
91         {
92             try {
93                 SwingUtilities.invokeAndWait( r );
94             } catch (InvocationTargetException JavaDoc ex) {
95                 ex.printStackTrace();
96             } catch (InterruptedException JavaDoc ex) {
97                 ex.printStackTrace();
98             }
99         }
100         else
101         {
102                 r.run();
103         }
104     }
105      
106     public String JavaDoc getDescription(){ return "JasperReports DataSource Provider"; }
107     
108     /** Creates a new instance of JRDataSourceProviderConnection */
109     public JRDataSourceProviderConnection() {
110     }
111     
112         /**
113      * This method return an instanced JRDataDource to the database.
114      * If isJDBCConnection() return true => getJRDataSource() return false
115      */

116     public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
117         
118         return getJRDataSource(null);
119     }
120     
121     public net.sf.jasperreports.engine.JRDataSource getJRDataSource(JasperReport jasper) {
122         
123         if (ds != null)
124         {
125             JOptionPane.showMessageDialog(MainFrame.getMainInstance(),I18n.getString("messages.JRDataSourceProviderConnection.datasourceInUse","This datasource is already in use by another filling process!!"),I18n.getString("message.title.error","Error"),JOptionPane.ERROR_MESSAGE);
126             return null;
127         }
128         
129         try {
130             ds = getDataSourceProvider().create(jasper);
131         } catch (Exception JavaDoc ex)
132         {
133             JOptionPane.showMessageDialog(MainFrame.getMainInstance(),I18n.getFormattedString("messages.JRDataSourceProviderConnection.problemsCreatingDatasource","Problems occurred creating the new datasource!!\n{0}", new Object JavaDoc[]{""+ex.getMessage()}),I18n.getString("message.title.error","Error"),JOptionPane.ERROR_MESSAGE);
134         }
135         
136         return ds;
137     }
138     
139     public void disposeDataSource()
140     {
141             if (dsp != null)
142             {
143                 try {
144                     dsp.dispose(ds);
145                 } catch (Exception JavaDoc ex) {}
146                 ds = null;
147             }
148     }
149     
150     public java.util.HashMap JavaDoc getProperties()
151     {
152         return properties;
153     }
154     
155     /** All properties of a IReportConnection are stored in a XML file as Pair key/value
156      * This HashMap contains alla properties found for this IReportConnection in the
157      * XML. You must use this hashMap to initialize all attributes of your IReprotConnection
158      */

159     public void loadProperties(java.util.HashMap JavaDoc map)
160     {
161         properties = map;
162     }
163     
164     /** Redefine this method is not useful (and not raccomended)
165      * It just write a portion of XML for save properties a IReportConnection name
166      */

167     public void save(java.io.PrintWriter JavaDoc pw)
168     {
169         java.util.HashMap JavaDoc hm = this.getProperties();
170         pw.println("\t<iReportConnection name=\""+ this.getName() +"\" connectionClass=\"" + this.getClass().getName() +"\">");
171         java.util.Iterator JavaDoc iterator = hm.keySet().iterator();
172         
173         while (iterator.hasNext())
174         {
175             String JavaDoc key = (String JavaDoc)iterator.next();
176             pw.println("\t\t<connectionParameter name=\"" + key + "\"><![CDATA[" + hm.get(key) + "]]></connectionParameter>");
177         }
178         pw.println("\t</iReportConnection>");
179     }
180     
181 }
182
Popular Tags