KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JRCustomDataSourceConnection.java
28  *
29  * Created on 4 giugno 2003, 18.15
30  *
31  */

32
33 package it.businesslogic.ireport.connection;
34 import it.businesslogic.ireport.*;
35 import it.businesslogic.ireport.gui.MainFrame;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 /**
39  *
40  * @author Administrator
41  */

42 public class JRCustomConnection extends it.businesslogic.ireport.IReportConnection {
43     
44     public static final String JavaDoc CLASSNAME_PROPERTY ="iReportConnectionClassName";
45     public static final String JavaDoc PROPERTY_PREFIX ="prop_";
46     
47     /**
48      * The map to store the static connection parameters
49      */

50     private java.util.HashMap JavaDoc innerConnectionProperties = new java.util.HashMap JavaDoc();
51     private IReportConnection innerConnection = null;
52     private String JavaDoc iReportConnectionClassName = null;
53     
54     /** Creates a new instance of JDBCConnection */
55     
56     
57     public JRCustomConnection() {
58     }
59     
60     /** This method return an instanced connection to the database.
61      * If isJDBCConnection() return false => getConnection() return null
62      *
63      */

64     public java.sql.Connection JavaDoc getConnection() {
65         if (getInnerConnection() == null) return super.getConnection();
66         return getInnerConnection().getConnection();
67     }
68     
69     public boolean isJDBCConnection() {
70         if (getInnerConnection() == null) return super.isJDBCConnection();
71         return getInnerConnection().isJDBCConnection();
72     }
73     
74     /*
75      * This method return all properties used by this connection
76      */

77     public java.util.HashMap JavaDoc getProperties()
78     {
79         HashMap JavaDoc map = new HashMap JavaDoc();
80         HashMap JavaDoc map2 = getInnerConnectionProperties();
81         Iterator JavaDoc iterator = map2.keySet().iterator();
82         while (iterator.hasNext())
83         {
84              Object JavaDoc key = iterator.next();
85              map.put(PROPERTY_PREFIX + key, map2.get(key));
86         }
87         
88         if (getIReportConnectionClassName() != null)
89         {
90             map.put(CLASSNAME_PROPERTY, getIReportConnectionClassName());
91         }
92         return map;
93     }
94     
95     public void loadProperties(java.util.HashMap JavaDoc map)
96     {
97         HashMap JavaDoc map2 = new HashMap JavaDoc();
98         Iterator JavaDoc iterator = map.keySet().iterator();
99         while (iterator.hasNext())
100         {
101             String JavaDoc key = ""+iterator.next();
102             Object JavaDoc value = map.get(key);
103             
104             if (key.startsWith(PROPERTY_PREFIX))
105             {
106                 map2.put(key.substring(PROPERTY_PREFIX.length()), value);
107             }
108             else if (key.equals(CLASSNAME_PROPERTY))
109             {
110                 this.setIReportConnectionClassName( ""+map.get( CLASSNAME_PROPERTY ) );
111             }
112         }
113         
114         setInnerConnectionProperties( map2 );
115         setInnerConnection(null);
116     }
117     
118     public String JavaDoc getDescription(){
119         return "Custom connection: " + getInnerConnection().getDescription();
120     }
121     
122     
123     /**
124      * This method return an instanced JRDataDource to the database.
125      * If isJDBCConnection() return true => getJRDataSource() return false
126      */

127     public net.sf.jasperreports.engine.JRDataSource getJRDataSource()
128     {
129         if (getInnerConnection() == null) return super.getJRDataSource();
130         return getInnerConnection().getJRDataSource();
131     }
132
133     public java.util.Map JavaDoc getSpecialParameters(java.util.Map JavaDoc map) throws net.sf.jasperreports.engine.JRException {
134
135         if (getInnerConnection() == null) return super.getSpecialParameters(map);
136         return getInnerConnection().getSpecialParameters(map);
137     }
138
139     public java.util.Map JavaDoc disposeSpecialParameters(java.util.Map JavaDoc map) {
140
141         if (getInnerConnection() == null) return super.disposeSpecialParameters(map);
142         return getInnerConnection().disposeSpecialParameters(map);
143     }
144
145     public boolean isJRDataSource() {
146
147         if (getInnerConnection() == null) return super.isJRDataSource();
148         return getInnerConnection().isJRDataSource();
149     }
150
151
152     public String JavaDoc getIReportConnectionClassName() {
153         return iReportConnectionClassName;
154     }
155
156     public void setIReportConnectionClassName(String JavaDoc iReportConnectionClassName) {
157         this.iReportConnectionClassName = iReportConnectionClassName;
158     }
159
160     public IReportConnection getInnerConnection() {
161         
162         if (innerConnection == null)
163         {
164             try {
165             
166                 innerConnection = (IReportConnection)Class.forName( getIReportConnectionClassName(), true, MainFrame.getMainInstance().getReportClassLoader() ).newInstance();
167                 innerConnection.loadProperties( getInnerConnectionProperties() );
168                 
169             } catch (Throwable JavaDoc ex)
170             {
171                 ex.printStackTrace();
172             }
173         }
174         
175         return innerConnection;
176     }
177
178     public void setInnerConnection(IReportConnection innerConnection) {
179         this.innerConnection = innerConnection;
180     }
181
182     public java.util.HashMap JavaDoc getInnerConnectionProperties() {
183         return innerConnectionProperties;
184     }
185
186     public void setInnerConnectionProperties(java.util.HashMap JavaDoc innerConnectionProperties) {
187         this.innerConnectionProperties = innerConnectionProperties;
188     }
189     
190     public void test() throws Exception JavaDoc
191     {
192         IReportConnection testConnection = (IReportConnection)Class.forName( getIReportConnectionClassName(), true, MainFrame.getMainInstance().getReportClassLoader() ).newInstance();
193         if (testConnection != null)
194         {
195             testConnection.loadProperties( getInnerConnectionProperties() );
196             testConnection.test();
197         }
198     }
199 }
200
Popular Tags