001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * Created Sep 12, 2005
014: * @author wseyler
015: */
016:
017: package org.pentaho.data.connection.mdx;
018:
019: import java.util.Enumeration;
020: import java.util.List;
021: import java.util.Properties;
022:
023: import mondrian.olap.Connection;
024: import mondrian.olap.DriverManager;
025: import mondrian.olap.Query;
026: import mondrian.olap.Result;
027: import mondrian.olap.Util;
028:
029: import org.pentaho.commons.connection.IPentahoConnection;
030: import org.pentaho.commons.connection.IPentahoResultSet;
031: import org.pentaho.messages.Messages;
032: import org.pentaho.util.logging.ILogger;
033: import org.pentaho.util.logging.Logger;
034:
035: /**
036: * @author wseyler
037: *
038: * TODO To change the template for this generated type comment go to Window -
039: * Preferences - Java - Code Style - Code Templates
040: */
041: public class MDXConnection implements IPentahoConnection {
042: /**
043: * Defines the XML element in the component-definition that holds the
044: * mondrian-specific MDX Connection string.
045: */
046: public static final String CONNECTION_STRING_KEY = "mdx-connection-string"; //$NON-NLS-1$
047:
048: Connection nativeConnection = null;
049:
050: String lastQuery = null;
051:
052: IPentahoResultSet resultSet = null;
053:
054: ILogger logger = null;
055:
056: public MDXConnection(Properties props, ILogger logger) {
057: super ();
058: this .logger = logger;
059: init(props);
060: }
061:
062: /**
063: * @param driver - The name of the driver or the connection string
064: * @param provider - the provider for MDX usally "mondrian"
065: * @param userName - User to connect to the datasource with
066: * @param password - Password for the user
067: *
068: * @deprecated
069: * @see MDXConnection(Properties props, ILogger logger)
070: */
071: public MDXConnection(String driver, String provider,
072: String userName, String password) {
073: super ();
074: init(driver, provider, userName, password);
075: }
076:
077: public MDXConnection(String connectStr, ILogger logger) {
078: super ();
079: this .logger = logger;
080: init(connectStr);
081: }
082:
083: private void init(String connectStr) {
084: try {
085: if (nativeConnection != null) { // Assume we're open
086: close();
087: }
088: // System.out.println("connectStr = " + connectStr);
089: nativeConnection = DriverManager.getConnection(connectStr,
090: null, false);
091: if (nativeConnection == null) {
092: logger
093: .error(Messages
094: .getErrorString(
095: "MDXConnection.ERROR_0002_INVALID_CONNECTION", connectStr != null ? connectStr : "null")); //$NON-NLS-1$ //$NON-NLS-2$
096: }
097: } catch (Throwable t) {
098: if (logger != null) {
099: logger
100: .error(
101: Messages
102: .getErrorString(
103: "MDXConnection.ERROR_0002_INVALID_CONNECTION", connectStr != null ? connectStr : "null"), t); //$NON-NLS-1$ //$NON-NLS-2$
104: } else {
105: Logger
106: .error(
107: this .getClass().getName(),
108: Messages
109: .getErrorString(
110: "MDXConnection.ERROR_0002_INVALID_CONNECTION", connectStr != null ? connectStr : "null"), t); //$NON-NLS-1$ //$NON-NLS-2$
111: }
112: }
113: }
114:
115: private void init(Properties properties) {
116: try {
117: if (nativeConnection != null) { // Assume we're open
118: close();
119: }
120:
121: Util.PropertyList pl = new Util.PropertyList();
122: Enumeration enum1 = properties.keys();
123: while (enum1.hasMoreElements()) {
124: Object key = enum1.nextElement();
125: Object value = properties.get(key);
126: pl.put(key.toString(), value.toString());
127: }
128: nativeConnection = DriverManager.getConnection(pl, null,
129: false);
130: } catch (Throwable t) {
131: if (logger != null) {
132: logger
133: .error(Messages
134: .getErrorString(
135: "MDXConnection.ERROR_0002_INVALID_CONNECTION", t.getMessage())); //$NON-NLS-1$
136: } else {
137: Logger
138: .error(
139: this .getClass().getName(),
140: Messages
141: .getErrorString(
142: "MDXConnection.ERROR_0002_INVALID_CONNECTION", t.getMessage()), t); //$NON-NLS-1$
143: }
144: }
145: }
146:
147: private void init(String driver, String provider, String userName,
148: String password) {
149: StringBuffer buffer = new StringBuffer();
150: buffer.append("provider=" + provider); //$NON-NLS-1$
151: //
152: // MB - This is a hack. Should instead have either a flag or a
153: // different method for specifying a datasource instead of this.
154: //
155: // TODO: Fix for post 1.2 RC 2
156: //
157:
158: //
159: // WES - This hack was fixed up to maintain backward capability.
160: // In addition methods were added so that connection info can be passed
161: // in via a properties map.
162:
163: if (driver.indexOf("dataSource=") >= 0) { //$NON-NLS-1$
164: buffer.append("; ").append(driver); //$NON-NLS-1$
165: } else {
166: buffer.append("; Jdbc=" + driver); //$NON-NLS-1$
167: }
168: if (userName != null) {
169: buffer.append("; JdbcUser=" + userName); //$NON-NLS-1$
170: }
171: if (password != null) {
172: buffer.append("; JdbcPassword=" + password); //$NON-NLS-1$
173: }
174: init(buffer.toString());
175: }
176:
177: public boolean initialized() {
178: return nativeConnection != null;
179: }
180:
181: public IPentahoResultSet prepareAndExecuteQuery(String query,
182: List parameters) throws Exception {
183: throw new UnsupportedOperationException();
184: }
185:
186: public boolean preparedQueriesSupported() {
187: return false;
188: }
189:
190: /*
191: * (non-Javadoc)
192: *
193: * @see org.pentaho.connection.IPentahoConnection#close()
194: */
195: public void close() {
196: if (nativeConnection != null) {
197: nativeConnection.close();
198: }
199: }
200:
201: /*
202: * (non-Javadoc)
203: *
204: * @see org.pentaho.connection.IPentahoConnection#getLastQuery()
205: */
206: public String getLastQuery() {
207: return lastQuery;
208: }
209:
210: /*
211: * (non-Javadoc)
212: *
213: * @see org.pentaho.connection.IPentahoConnection#executeQuery(java.lang.String)
214: */
215: public IPentahoResultSet executeQuery(String query) {
216: Query mdxQuery = nativeConnection.parseQuery(query);
217: Result result = nativeConnection.execute(mdxQuery);
218: resultSet = new MDXResultSet(result, nativeConnection);
219: return resultSet;
220: }
221:
222: /*
223: * (non-Javadoc)
224: *
225: * @see org.pentaho.connection.IPentahoConnection#isClosed()
226: */
227: public boolean isClosed() {
228: return false;
229: }
230:
231: /*
232: * (non-Javadoc)
233: *
234: * @see org.pentaho.connection.IPentahoConnection#isReadOnly()
235: */
236: public boolean isReadOnly() {
237: return true;
238: }
239:
240: /*
241: * (non-Javadoc)
242: *
243: * @see org.pentaho.connection.IPentahoConnection#clearWarnings()
244: */
245: public void clearWarnings() {
246: // TODO Auto-generated method stub
247:
248: }
249:
250: public IPentahoResultSet getResultSet() {
251: return resultSet;
252: }
253:
254: public boolean connect(Properties props) {
255: if (nativeConnection != null) { // Assume we're open
256: close();
257: }
258: init(props);
259: String query = props.getProperty(IPentahoConnection.QUERY_KEY);
260: if (query != null && query.length() > 0
261: && nativeConnection != null) {
262: executeQuery(query);
263: }
264: return nativeConnection != null;
265: }
266:
267: /*
268: * (non-Javadoc)
269: *
270: * @see org.pentaho.connection.IPentahoConnection#setMaxRows(int)
271: */
272: public void setMaxRows(int maxRows) {
273: // TODO Auto-generated method stub
274: throw new UnsupportedOperationException();
275: }
276:
277: /*
278: * (non-Javadoc)
279: *
280: * @see org.pentaho.connection.IPentahoConnection#setFetchSize(int)
281: */
282: public void setFetchSize(int fetchSize) {
283: // TODO Auto-generated method stub
284: throw new UnsupportedOperationException();
285: }
286:
287: public Connection getConnection() {
288: return nativeConnection;
289: }
290:
291: /**
292: * return datasource type MDX
293: * @return datasource type
294: */
295: public int getDatasourceType() {
296: return MDX_DATASOURCE;
297: }
298: }
|