KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > data > connection > mdx > MDXConnection


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Sep 12, 2005
14  * @author wseyler
15  */

16
17 package org.pentaho.data.connection.mdx;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import mondrian.olap.Connection;
24 import mondrian.olap.DriverManager;
25 import mondrian.olap.Query;
26 import mondrian.olap.Result;
27 import mondrian.olap.Util;
28
29 import org.pentaho.core.connection.IPentahoConnection;
30 import org.pentaho.core.connection.IPentahoResultSet;
31 import org.pentaho.messages.Messages;
32 import org.pentaho.util.logging.ILogger;
33
34 /**
35  * @author wseyler
36  *
37  * TODO To change the template for this generated type comment go to Window -
38  * Preferences - Java - Code Style - Code Templates
39  */

40 public class MDXConnection implements IPentahoConnection {
41     /**
42      * Defines the XML element in the component-definition that holds the
43      * mondrian-specific MDX Connection string.
44      */

45     public static final String JavaDoc CONNECTION_STRING_KEY = "mdx-connection-string"; //$NON-NLS-1$
46

47     Connection nativeConnection = null;
48
49     String JavaDoc lastQuery = null;
50
51     IPentahoResultSet resultSet = null;
52
53     ILogger logger = null;
54
55     public MDXConnection(Properties JavaDoc props, ILogger logger) {
56         super();
57         
58         init(props);
59         this.logger = logger;
60     }
61
62     /**
63      * @param driver - The name of the driver or the connection string
64      * @param provider - the provider for MDX usally "mondrian"
65      * @param userName - User to connect to the datasource with
66      * @param password - Password for the user
67      *
68      * @deprecated
69      * @see MDXConnection(Properties props, ILogger logger)
70      */

71     public MDXConnection(String JavaDoc driver, String JavaDoc provider, String JavaDoc userName, String JavaDoc password) {
72         super();
73         
74         init(driver, provider, userName, password);
75     }
76
77     public MDXConnection(String JavaDoc connectStr, ILogger logger) {
78         super();
79         init(connectStr);
80         this.logger = logger;
81     }
82
83     private void init(String JavaDoc connectStr) {
84         try {
85             if (nativeConnection != null) { // Assume we're open
86
close();
87             }
88             nativeConnection = DriverManager.getConnection(connectStr, null, false);
89             if (nativeConnection == null) {
90                 logger.error(Messages.getErrorString("MDXConnection.ERROR_0002_INVALID_CONNECTION", connectStr)); //$NON-NLS-1$
91
}
92         } catch (Throwable JavaDoc t) {
93             logger.error(Messages.getErrorString("MDXConnection.ERROR_0002_INVALID_CONNECTION", t.getMessage())); //$NON-NLS-1$
94
}
95     }
96     
97     private void init(Properties JavaDoc properties) {
98         try {
99             if (nativeConnection != null) { // Assume we're open
100
close();
101             }
102             
103             Util.PropertyList pl = new Util.PropertyList();
104             Enumeration JavaDoc enum1 = properties.keys();
105             while(enum1.hasMoreElements()) {
106                 Object JavaDoc key = enum1.nextElement();
107                 Object JavaDoc value = properties.get(key);
108                 pl.put(key.toString(), value.toString());
109             }
110             nativeConnection = DriverManager.getConnection(pl, null, false);
111         } catch (Throwable JavaDoc t) {
112             logger.error(Messages.getErrorString("MDXConnection.ERROR_0002_INVALID_CONNECTION", t.getMessage())); //$NON-NLS-1$
113
}
114     }
115
116     private void init(String JavaDoc driver, String JavaDoc provider, String JavaDoc userName, String JavaDoc password) {
117         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
118         buffer.append("provider=" + provider); //$NON-NLS-1$
119
//
120
// MB - This is a hack. Should instead have either a flag or a
121
// different method for specifying a datasource instead of this.
122
//
123
// TODO: Fix for post 1.2 RC 2
124
//
125

126         //
127
// WES - This hack was fixed up to maintain backward capability.
128
// In addition methods were added so that connection info can be passed
129
// in via a properties map.
130

131         if (driver.indexOf("dataSource=") >=0 ) { //$NON-NLS-1$
132
buffer.append("; ").append(driver); //$NON-NLS-1$
133
} else {
134           buffer.append("; Jdbc=" + driver); //$NON-NLS-1$
135
}
136         if (userName != null) {
137             buffer.append("; user=" + userName); //$NON-NLS-1$
138
}
139         if (password != null) {
140             buffer.append("; password=" + password); //$NON-NLS-1$
141
}
142         init(buffer.toString());
143     }
144
145     public boolean initialized() {
146         return nativeConnection != null;
147     }
148
149     public IPentahoResultSet prepareAndExecuteQuery(String JavaDoc query, List JavaDoc parameters) throws Exception JavaDoc {
150       throw new UnsupportedOperationException JavaDoc();
151     }
152     
153     public boolean preparedQueriesSupported() {
154       return false;
155     }
156     
157     /*
158      * (non-Javadoc)
159      *
160      * @see org.pentaho.connection.IPentahoConnection#close()
161      */

162     public void close() {
163         nativeConnection.close();
164     }
165
166     /*
167      * (non-Javadoc)
168      *
169      * @see org.pentaho.connection.IPentahoConnection#getLastQuery()
170      */

171     public String JavaDoc getLastQuery() {
172         return lastQuery;
173     }
174
175     /*
176      * (non-Javadoc)
177      *
178      * @see org.pentaho.connection.IPentahoConnection#executeQuery(java.lang.String)
179      */

180     public IPentahoResultSet executeQuery(String JavaDoc query) {
181         Query mdxQuery = nativeConnection.parseQuery(query);
182         Result result = nativeConnection.execute(mdxQuery);
183         resultSet = new MDXResultSet(result, nativeConnection);
184         return resultSet;
185     }
186
187     /*
188      * (non-Javadoc)
189      *
190      * @see org.pentaho.connection.IPentahoConnection#isClosed()
191      */

192     public boolean isClosed() {
193         return false;
194     }
195
196     /*
197      * (non-Javadoc)
198      *
199      * @see org.pentaho.connection.IPentahoConnection#isReadOnly()
200      */

201     public boolean isReadOnly() {
202          return true;
203     }
204
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.pentaho.connection.IPentahoConnection#clearWarnings()
209      */

210     public void clearWarnings() {
211         // TODO Auto-generated method stub
212

213     }
214
215     public IPentahoResultSet getResultSet() {
216         return resultSet;
217     }
218
219     public boolean connect(Properties JavaDoc props) {
220         if (nativeConnection != null) { // Assume we're open
221
close();
222         }
223         init(props);
224         String JavaDoc query = props.getProperty(IPentahoConnection.QUERY_KEY);
225         if (query != null && query.length() > 0 && nativeConnection != null) {
226             executeQuery(query);
227         }
228         return nativeConnection != null;
229     }
230
231     /*
232      * (non-Javadoc)
233      *
234      * @see org.pentaho.connection.IPentahoConnection#setMaxRows(int)
235      */

236     public void setMaxRows(int maxRows) {
237         // TODO Auto-generated method stub
238
throw new UnsupportedOperationException JavaDoc();
239     }
240
241     /*
242      * (non-Javadoc)
243      *
244      * @see org.pentaho.connection.IPentahoConnection#setFetchSize(int)
245      */

246     public void setFetchSize(int fetchSize) {
247         // TODO Auto-generated method stub
248
throw new UnsupportedOperationException JavaDoc();
249     }
250
251     public Connection getConnection() {
252         return nativeConnection;
253     }
254 }
Popular Tags