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: * MondrianConnection.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.MondrianConnectionEditor;
038: import it.businesslogic.ireport.gui.MainFrame;
039: import it.businesslogic.ireport.util.*;
040: import java.util.Vector;
041: import javax.swing.JOptionPane;
042: import javax.swing.SwingUtilities;
043: import mondrian.olap.DriverManager;
044:
045: /**
046: *
047: * @author Administrator
048: */
049: public class MondrianConnection extends
050: it.businesslogic.ireport.IReportConnection {
051:
052: public static final String CATALOG_URI = "CatalogUri";
053: public static final String CONNECTION_NAME = "ConnectionName";
054:
055: private String name;
056:
057: private java.util.HashMap map = null;
058: private String persistenceUnit;
059:
060: private mondrian.olap.Connection mondrianConnection = null;
061:
062: private int usedby = 0;
063:
064: /** Creates a new instance of JDBCConnection */
065:
066: public MondrianConnection() {
067: map = new java.util.HashMap();
068: }
069:
070: /** This method return an instanced connection to the database.
071: * If isJDBCConnection() return false => getConnection() return null
072: *
073: */
074: public java.sql.Connection getConnection() {
075: return null;
076: }
077:
078: public boolean isJDBCConnection() {
079: return false;
080: }
081:
082: public boolean isJRDataSource() {
083: return false;
084: }
085:
086: /*
087: * This method return all properties used by this connection
088: */
089: public java.util.HashMap getProperties() {
090: return map;
091: }
092:
093: public void loadProperties(java.util.HashMap map) {
094: this .map = map;
095: }
096:
097: /**
098: * This method return an instanced JRDataDource to the database.
099: * If isJDBCConnection() return true => getJRDataSource() return false
100: */
101: public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
102: return null;
103: }
104:
105: public void closeMondrianConnection() {
106: try {
107: if (getMondrianConnection() != null) {
108: usedby--;
109: if (usedby == 0) {
110: mondrianConnection.close();
111: mondrianConnection = null;
112: }
113: }
114: } catch (Exception ex) {
115: }
116: }
117:
118: public mondrian.olap.Connection getMondrianConnection()
119: throws Exception {
120:
121: if (mondrianConnection == null) {
122: JDBCConnection con = getJDBCConnection();
123:
124: mondrianConnection = DriverManager.getConnection(
125: "Provider=mondrian;" + "JdbcDrivers="
126: + escapeProperty(con.getJDBCDriver()) + ";"
127: + "Jdbc=" + escapeProperty(con.getUrl())
128: + ";" + "JdbcUser="
129: + escapeProperty(con.getUsername()) + ";"
130: + "JdbcPassword="
131: + escapeProperty(con.getPassword()) + ";"
132: + "Catalog="
133: + escapeProperty(getCatalogUri()) + ";",
134: null, false);
135: }
136: usedby++;
137: return mondrianConnection;
138: }
139:
140: public void setMondrianConnection(
141: mondrian.olap.Connection mondrianConnection) {
142: this .mondrianConnection = mondrianConnection;
143: }
144:
145: public String getCatalogUri() {
146: return (String) getProperties().get(CATALOG_URI);
147: }
148:
149: public void setCatalogUri(String catalogUri) {
150: getProperties().put(CATALOG_URI, catalogUri);
151: }
152:
153: public String getConnectionName() {
154: return (String) getProperties().get(CONNECTION_NAME);
155: }
156:
157: public void setConnectionName(String connectionName) {
158: getProperties().put(CONNECTION_NAME, connectionName);
159: }
160:
161: private JDBCConnection getJDBCConnection() {
162: String name = getConnectionName();
163: Vector conns = MainFrame.getMainInstance().getConnections();
164: for (int i = 0; i < conns.size(); ++i) {
165: IReportConnection con = (IReportConnection) conns
166: .elementAt(i);
167: if (con instanceof JDBCConnection
168: && con.getName().equals(name)) {
169: return (JDBCConnection) con;
170: }
171: }
172:
173: return null;
174: }
175:
176: public String escapeProperty(String s) {
177: if (s == null)
178: s = "";
179: s = Misc.string_replace("\"\"", "\"", s);
180:
181: return s;
182: }
183:
184: public String getDescription() {
185: return I18n.getString("connectionType.olap",
186: "Mondrian OLAP connection");
187: }
188:
189: public IReportConnectionEditor getIReportConnectionEditor() {
190: return new MondrianConnectionEditor();
191: }
192:
193: public void test() throws Exception {
194: try {
195: SwingUtilities.invokeLater(new Runnable() {
196: public void run() {
197:
198: Thread.currentThread().setContextClassLoader(
199: MainFrame.getMainInstance()
200: .getReportClassLoader());
201: try {
202:
203: getMondrianConnection();
204: closeMondrianConnection();
205: JOptionPane
206: .showMessageDialog(
207: MainFrame.getMainInstance(),
208: I18n
209: .getString(
210: "messages.connectionDialog.connectionTestSuccessful",
211: "Connection test successful!"),
212: "",
213: JOptionPane.INFORMATION_MESSAGE);
214:
215: } catch (Exception ex) {
216: ex.printStackTrace();
217: JOptionPane.showMessageDialog(MainFrame
218: .getMainInstance(), ex.getMessage(),
219: I18n.getString("message.title.error",
220: "Error"),
221: JOptionPane.ERROR_MESSAGE);
222: return;
223: } finally {
224:
225: }
226: }
227: });
228: } catch (Exception ex) {
229: }
230: }
231: }
|