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.*;
036: import it.businesslogic.ireport.gui.MainFrame;
037: import it.businesslogic.ireport.util.I18n;
038: import java.util.HashMap;
039: import java.util.Iterator;
040:
041: /**
042: *
043: * @author Administrator
044: */
045: public class JRCustomConnection extends
046: it.businesslogic.ireport.IReportConnection {
047:
048: public static final String CLASSNAME_PROPERTY = "iReportConnectionClassName";
049: public static final String PROPERTY_PREFIX = "prop_";
050:
051: /**
052: * The map to store the static connection parameters
053: */
054: private java.util.HashMap innerConnectionProperties = new java.util.HashMap();
055: private IReportConnection innerConnection = null;
056: private String iReportConnectionClassName = null;
057:
058: /** Creates a new instance of JDBCConnection */
059:
060: public JRCustomConnection() {
061: }
062:
063: /** This method return an instanced connection to the database.
064: * If isJDBCConnection() return false => getConnection() return null
065: *
066: */
067: public java.sql.Connection getConnection() {
068: if (getInnerConnection() == null)
069: return super .getConnection();
070: return getInnerConnection().getConnection();
071: }
072:
073: public boolean isJDBCConnection() {
074: if (getInnerConnection() == null)
075: return super .isJDBCConnection();
076: return getInnerConnection().isJDBCConnection();
077: }
078:
079: /*
080: * This method return all properties used by this connection
081: */
082: public java.util.HashMap getProperties() {
083: HashMap map = new HashMap();
084: HashMap map2 = getInnerConnectionProperties();
085: Iterator iterator = map2.keySet().iterator();
086: while (iterator.hasNext()) {
087: Object key = iterator.next();
088: map.put(PROPERTY_PREFIX + key, map2.get(key));
089: }
090:
091: if (getIReportConnectionClassName() != null) {
092: map
093: .put(CLASSNAME_PROPERTY,
094: getIReportConnectionClassName());
095: }
096: return map;
097: }
098:
099: public void loadProperties(java.util.HashMap map) {
100: HashMap map2 = new HashMap();
101: Iterator iterator = map.keySet().iterator();
102: while (iterator.hasNext()) {
103: String key = "" + iterator.next();
104: Object value = map.get(key);
105:
106: if (key.startsWith(PROPERTY_PREFIX)) {
107: map2
108: .put(key.substring(PROPERTY_PREFIX.length()),
109: value);
110: } else if (key.equals(CLASSNAME_PROPERTY)) {
111: this .setIReportConnectionClassName(""
112: + map.get(CLASSNAME_PROPERTY));
113: }
114: }
115:
116: setInnerConnectionProperties(map2);
117: setInnerConnection(null);
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: if (getInnerConnection() == null)
126: return super .getJRDataSource();
127: return getInnerConnection().getJRDataSource();
128: }
129:
130: public java.util.Map getSpecialParameters(java.util.Map map)
131: throws net.sf.jasperreports.engine.JRException {
132:
133: if (getInnerConnection() == null)
134: return super .getSpecialParameters(map);
135: return getInnerConnection().getSpecialParameters(map);
136: }
137:
138: public java.util.Map disposeSpecialParameters(java.util.Map map) {
139:
140: if (getInnerConnection() == null)
141: return super .disposeSpecialParameters(map);
142: return getInnerConnection().disposeSpecialParameters(map);
143: }
144:
145: public boolean isJRDataSource() {
146:
147: if (getInnerConnection() == null)
148: return super .isJRDataSource();
149: return getInnerConnection().isJRDataSource();
150: }
151:
152: public String getIReportConnectionClassName() {
153: return iReportConnectionClassName;
154: }
155:
156: public void setIReportConnectionClassName(
157: String iReportConnectionClassName) {
158: this .iReportConnectionClassName = iReportConnectionClassName;
159: }
160:
161: public IReportConnection getInnerConnection() {
162:
163: if (innerConnection == null) {
164: try {
165:
166: innerConnection = (IReportConnection) Class.forName(
167: getIReportConnectionClassName(),
168: true,
169: MainFrame.getMainInstance()
170: .getReportClassLoader()).newInstance();
171: innerConnection
172: .loadProperties(getInnerConnectionProperties());
173:
174: } catch (Throwable ex) {
175: ex.printStackTrace();
176: }
177: }
178:
179: return innerConnection;
180: }
181:
182: public void setInnerConnection(IReportConnection innerConnection) {
183: this .innerConnection = innerConnection;
184: }
185:
186: public java.util.HashMap getInnerConnectionProperties() {
187: return innerConnectionProperties;
188: }
189:
190: public void setInnerConnectionProperties(
191: java.util.HashMap innerConnectionProperties) {
192: this .innerConnectionProperties = innerConnectionProperties;
193: }
194:
195: public void test() throws Exception {
196: IReportConnection testConnection = (IReportConnection) Class
197: .forName(
198: getIReportConnectionClassName(),
199: true,
200: MainFrame.getMainInstance()
201: .getReportClassLoader()).newInstance();
202: if (testConnection != null) {
203: testConnection
204: .loadProperties(getInnerConnectionProperties());
205: testConnection.test();
206: }
207: }
208:
209: public String getDescription() {
210: return I18n.getString("connectionType.customIReportConnection",
211: "Custom iReport connection");
212: }
213: }
|