KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MondrianConnection.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 it.businesslogic.ireport.util.*;
37 import java.net.URLClassLoader JavaDoc;
38 import java.sql.*;
39 import java.util.HashMap JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Vector JavaDoc;
42 import javax.swing.*;
43
44 import mondrian.olap.Connection;
45 import mondrian.olap.DriverManager;
46
47 /**
48  *
49  * @author Administrator
50  */

51 public class MondrianConnection extends it.businesslogic.ireport.IReportConnection {
52     
53     public static final String JavaDoc CATALOG_URI = "CatalogUri";
54     public static final String JavaDoc CONNECTION_NAME = "ConnectionName";
55     
56     private String JavaDoc name;
57     
58
59     
60     private java.util.HashMap JavaDoc map = null;
61     private String JavaDoc persistenceUnit;
62     
63     private mondrian.olap.Connection mondrianConnection = null;
64     
65     private int usedby = 0;
66     
67     /** Creates a new instance of JDBCConnection */
68     
69     
70     public MondrianConnection() {
71         map = new java.util.HashMap JavaDoc();
72     }
73     
74     /** This method return an instanced connection to the database.
75      * If isJDBCConnection() return false => getConnection() return null
76      *
77      */

78     public java.sql.Connection JavaDoc getConnection() {
79             return null;
80     }
81     
82     public boolean isJDBCConnection() {
83         return false;
84     }
85     
86     public boolean isJRDataSource() {
87         return false;
88     }
89     
90     /*
91      * This method return all properties used by this connection
92      */

93     public java.util.HashMap JavaDoc getProperties()
94     {
95         return map;
96     }
97     
98     public void loadProperties(java.util.HashMap JavaDoc map)
99     {
100         this.map = map;
101     }
102     
103     public String JavaDoc getDescription(){ return "Mondrian OLAP connection"; }
104     
105     
106     /**
107      * This method return an instanced JRDataDource to the database.
108      * If isJDBCConnection() return true => getJRDataSource() return false
109      */

110     public net.sf.jasperreports.engine.JRDataSource getJRDataSource()
111     {
112         return null;
113     }
114     
115     
116     public void closeMondrianConnection()
117     {
118         try {
119             if (getMondrianConnection() != null)
120             {
121                 usedby--;
122                 if (usedby == 0)
123                 {
124                     mondrianConnection.close();
125                     mondrianConnection = null;
126                 }
127             }
128         } catch (Exception JavaDoc ex)
129         {
130         }
131     }
132
133     public mondrian.olap.Connection getMondrianConnection() throws Exception JavaDoc {
134         
135         if (mondrianConnection == null)
136         {
137             JDBCConnection con = getJDBCConnection();
138             
139             mondrianConnection =
140             DriverManager.getConnection(
141                     "Provider=mondrian;" +
142                     "JdbcDrivers=" + escapeProperty( con.getJDBCDriver() ) + ";" +
143                     "Jdbc=" + escapeProperty( con.getUrl() ) + ";" +
144                     "JdbcUser=" + escapeProperty( con.getUsername() ) + ";" +
145                     "JdbcPassword=" + escapeProperty( con.getPassword() ) + ";" +
146                     "Catalog=" + escapeProperty( getCatalogUri() ) + ";",
147                     null, false);
148         }
149         usedby++;
150         return mondrianConnection;
151     }
152
153     public void setMondrianConnection(mondrian.olap.Connection mondrianConnection) {
154         this.mondrianConnection = mondrianConnection;
155     }
156
157     public String JavaDoc getCatalogUri() {
158         return (String JavaDoc)getProperties().get( CATALOG_URI );
159     }
160
161     public void setCatalogUri(String JavaDoc catalogUri) {
162         getProperties().put( CATALOG_URI, catalogUri);
163     }
164
165     public String JavaDoc getConnectionName() {
166         return (String JavaDoc)getProperties().get( CONNECTION_NAME );
167     }
168
169     public void setConnectionName(String JavaDoc connectionName) {
170         getProperties().put( CONNECTION_NAME, connectionName);
171     }
172     
173     private JDBCConnection getJDBCConnection()
174     {
175             String JavaDoc name = getConnectionName();
176             Vector JavaDoc conns = MainFrame.getMainInstance().getConnections();
177             for (int i=0; i<conns.size(); ++i)
178             {
179                 IReportConnection con = (IReportConnection)conns.elementAt(i);
180                 if (con instanceof JDBCConnection &&
181                     con.getName().equals(name))
182                 {
183                     return (JDBCConnection)con;
184                 }
185             }
186             
187             return null;
188     }
189     
190     public String JavaDoc escapeProperty( String JavaDoc s)
191     {
192         if (s == null) s = "";
193         s = Misc.string_replace("\"\"","\"",s);
194     
195         return s;
196     }
197 }
198
Popular Tags