KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > olap > MondrianModelComponent


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 Apr 28, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.plugin.olap;
18
19 import java.util.Properties JavaDoc;
20
21 import mondrian.olap.Connection;
22 import mondrian.olap.Cube;
23 import mondrian.olap.Dimension;
24 import mondrian.olap.Hierarchy;
25 import mondrian.olap.Member;
26 import mondrian.olap.MondrianException;
27 import mondrian.olap.Schema;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.pentaho.core.system.PentahoSystem;
32 import org.pentaho.core.util.DatasourceHelper;
33 import org.pentaho.data.PentahoConnectionFactory;
34 import org.pentaho.data.connection.mdx.MDXConnection;
35 import org.pentaho.data.connection.sql.SQLConnection;
36 import org.pentaho.messages.Messages;
37 import org.pentaho.plugin.ComponentBase;
38 import org.pentaho.util.logging.Logger;
39
40 /**
41  * @author James Dixon
42  *
43  * TODO To change the template for this generated type comment go to Window -
44  * Preferences - Java - Code Style - Code Templates
45  */

46 public class MondrianModelComponent extends ComponentBase {
47
48     private static final long serialVersionUID = -718697500002076945L;
49
50     public Log getLogger() {
51         return LogFactory.getLog(MondrianModelComponent.class);
52     }
53
54     protected boolean validateSystemSettings() {
55         // This component does not have any system settings to validate
56
return true;
57     }
58
59     public boolean init() {
60         // get the settings from the system configuration file
61
return true;
62
63     }
64
65     public boolean validateAction() {
66
67         return true;
68
69     }
70
71     public boolean executeAction() {
72
73         return true;
74     }
75
76     public void done() {
77
78     }
79
80     public static String JavaDoc getInitialQuery(Properties JavaDoc properties, String JavaDoc cubeName) throws Throwable JavaDoc {
81         MDXConnection mdxConnection = new MDXConnection(properties, null);
82         Connection connection = mdxConnection.getConnection();
83         if (connection == null) {
84             Logger.error("MondrianModelComponent", Messages.getErrorString("MondrianModel.ERROR_0001_INVALID_CONNECTION", properties.toString())); //$NON-NLS-1$ //$NON-NLS-2$
85
return null;
86         }
87         
88         try {
89             return getInitialQuery(connection, cubeName);
90         } catch (Throwable JavaDoc t) {
91             if (t instanceof MondrianException) {
92                 // pull the cause out, otherwise it never gets logged
93
Throwable JavaDoc cause = ((MondrianException) t).getCause();
94                 if (cause != null) {
95                     throw cause;
96                 } else {
97                     throw t;
98                 }
99             } else {
100                 throw t;
101             }
102         }
103     }
104     
105     /**
106      * @param modelPath
107      * @param connectionString
108      * @param driver
109      * @param user
110      * @param password
111      * @param cubeName
112      * @return mdx string that represents the initial query
113      * @throws Throwable
114      * @deprecated
115      */

116     public static String JavaDoc getInitialQuery(String JavaDoc modelPath, String JavaDoc connectionString, String JavaDoc driver, String JavaDoc user, String JavaDoc password, String JavaDoc cubeName) throws Throwable JavaDoc {
117       return getInitialQuery(modelPath, connectionString, driver, user, password, cubeName, null);
118     }
119     
120     /**
121      * @param modelPath
122      * @param connectionString
123      * @param driver
124      * @param user
125      * @param password
126      * @param cubeName
127      * @param roleName
128      * @return mdx string that represents the initial query
129      * @throws Throwable
130      * @deprecated
131      */

132     public static String JavaDoc getInitialQuery(String JavaDoc modelPath, String JavaDoc connectionString, String JavaDoc driver, String JavaDoc user, String JavaDoc password, String JavaDoc cubeName, String JavaDoc roleName) throws Throwable JavaDoc {
133
134         Properties JavaDoc properties = new Properties JavaDoc();
135         
136         // TODO support driver manager connections
137
if (!PentahoSystem.ignored) {
138             if (driver != null) {
139                 properties.put("Driver", driver); //$NON-NLS-1$
140
}
141             if (user != null) {
142                 properties.put("User", user); //$NON-NLS-1$
143
}
144             if (password != null) {
145                 properties.put("Password", password); //$NON-NLS-1$
146
}
147         }
148
149         if (modelPath.indexOf("http") == 0) { //$NON-NLS-1$
150
properties.put("Catalog", modelPath); //$NON-NLS-1$
151
} else {
152             properties.put("Catalog", PentahoSystem.getApplicationContext().getBaseUrl() + "GetMondrianModel?model=" + modelPath); //$NON-NLS-1$ //$NON-NLS-2$
153
}
154         properties.put("Provider", "mondrian"); //$NON-NLS-1$ //$NON-NLS-2$
155
properties.put("PoolNeeded", "false"); //$NON-NLS-1$//$NON-NLS-2$
156
properties.put("dataSource", connectionString); //$NON-NLS-1$
157
if (roleName != null) {
158             properties.put("Role", roleName); //$NON-NLS-1$
159
}
160         
161         return getInitialQuery(properties, cubeName);
162     }
163
164     /**
165      * @param modelPath
166      * @param connectionString
167      * @param cubeName
168      * @return mdx string that represents the initial query
169      * @throws Throwable
170      * @deprecated
171      */

172     public static String JavaDoc getInitialQuery(String JavaDoc modelPath, String JavaDoc connectionString, String JavaDoc cubeName) throws Throwable JavaDoc {
173       return getInitialQuery(modelPath, connectionString, cubeName, null);
174     }
175     
176     /**
177      * @param modelPath
178      * @param jndi
179      * @param cubeName
180      * @param roleName
181      * @return mdx string that represents the initial query
182      * @throws Throwable
183      * @deprecated
184      */

185     public static String JavaDoc getInitialQuery(String JavaDoc modelPath, String JavaDoc jndi, String JavaDoc cubeName, String JavaDoc roleName) throws Throwable JavaDoc {
186
187         Properties JavaDoc properties = new Properties JavaDoc();
188         
189         
190         if (modelPath.indexOf("http") == 0) { //$NON-NLS-1$
191
properties.put("Catalog", modelPath); //$NON-NLS-1$
192
} else {
193             properties.put("Catalog", PentahoSystem.getApplicationContext().getBaseUrl() + "GetMondrianModel?model=" + modelPath); //$NON-NLS-1$ //$NON-NLS-2$
194
}
195
196         jndi = DatasourceHelper.getDSBoundName(jndi);
197
198         properties.put("Provider", "mondrian"); //$NON-NLS-1$ //$NON-NLS-2$
199
properties.put("PoolNeeded", "false"); //$NON-NLS-1$ //$NON-NLS-2$
200
properties.put("dataSource", jndi); //$NON-NLS-1$
201

202         if (roleName != null) {
203             properties.put("Role", roleName); //$NON-NLS-1$
204
}
205         return getInitialQuery(properties, cubeName);
206     }
207
208     public static String JavaDoc getInitialQuery(Connection connection, String JavaDoc cubeName) throws Throwable JavaDoc {
209
210         String JavaDoc measuresMdx = null;
211         String JavaDoc columnsMdx = null;
212         StringBuffer JavaDoc rowsMdx = new StringBuffer JavaDoc();
213
214         try {
215
216             Schema schema = connection.getSchema();
217             if (schema == null) {
218                 Logger.error("MondrianModelComponent", Messages.getErrorString("MondrianModel.ERROR_0002_INVALID_SCHEMA", connection.getConnectString())); //$NON-NLS-1$ //$NON-NLS-2$
219
return null;
220             }
221
222             Cube cubes[] = schema.getCubes();
223             if (cubes == null || cubes.length == 0) {
224                 Logger.error("MondrianModelComponent", Messages.getErrorString("MondrianModel.ERROR_0003_NO_CUBES", connection.getConnectString())); //$NON-NLS-1$ //$NON-NLS-2$
225
return null;
226             }
227
228             if (cubes.length > 1 && cubeName == null) {
229                 Logger.error("MondrianModelComponent", Messages.getErrorString("MondrianModel.ERROR_0004_CUBE_NOT_SPECIFIED", connection.getConnectString())); //$NON-NLS-1$ //$NON-NLS-2$
230
return null;
231             }
232
233             Cube cube = null;
234             if (cubes.length == 1) {
235                 cube = cubes[0];
236             } else {
237                 for (int n = 0; n < cubes.length; n++) {
238                     if (cubes[n].getName().equals(cubeName)) {
239                         cube = cubes[n];
240                         break;
241                     }
242                 }
243             }
244
245             if (cube == null) {
246                 Logger.error("MondrianModelComponent", Messages.getErrorString("MondrianModel.ERROR_0005_CUBE_NOT_FOUND", cubeName, connection.getConnectString())); //$NON-NLS-1$ //$NON-NLS-2$
247
return null;
248             }
249
250             Dimension dimensions[] = cube.getDimensions();
251             if (dimensions == null || dimensions.length == 0) {
252                 Logger.error("MondrianModelComponent", Messages.getErrorString("MondrianModel.ERROR_0006_NO_DIMENSIONS", cubeName, connection.getConnectString())); //$NON-NLS-1$ //$NON-NLS-2$
253
return null;
254             }
255
256             // now walk all the dimension.
257
for (int n = 0; n < dimensions.length; n++) {
258
259                 Hierarchy hierarchy = dimensions[n].getHierarchy();
260                 if (hierarchy == null) {
261                     Logger.error("MondrianModelComponent", Messages.getErrorString("MondrianModel.ERROR_0007_NO_HIERARCHIES", dimensions[n].getName(), cubeName, connection.getConnectString())); //$NON-NLS-1$ //$NON-NLS-2$
262
return null;
263                 }
264
265                 Member member = hierarchy.getDefaultMember();
266
267                 if (member == null) {
268                     Logger.error("MondrianModelComponent", Messages.getErrorString("MondrianModel.ERROR_0008_NO_DEFAULT_MEMBER", dimensions[n].getName(), cubeName, connection.getConnectString())); //$NON-NLS-1$ //$NON-NLS-2$
269
return null;
270                 }
271                 if (dimensions[n].isMeasures()) {
272                     // measuresMdx = "with member "+ member.getUniqueName();
273
// //$NON-NLS-1$
274
measuresMdx = ""; //$NON-NLS-1$
275
columnsMdx = " select NON EMPTY {" + member.getUniqueName() + "} ON columns, "; //$NON-NLS-1$ //$NON-NLS-2$
276
} else {
277                     if (rowsMdx.length() > 0) {
278                         rowsMdx.append(", "); //$NON-NLS-1$
279
}
280                     rowsMdx.append(member.getUniqueName());
281                 }
282             }
283             if (measuresMdx != null && columnsMdx != null && rowsMdx.length() > 0) {
284                 StringBuffer JavaDoc result = new StringBuffer JavaDoc(measuresMdx.length() + columnsMdx.length() + rowsMdx.length() + 50);
285                 result.append(measuresMdx).append(columnsMdx).append("NON EMPTY {(") //$NON-NLS-1$
286
.append(rowsMdx).append(")} ON rows ") //$NON-NLS-1$
287
.append("from [" + cube.getName() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
288

289                 return result.toString();
290
291             }
292             return null;
293         } catch (Throwable JavaDoc t) {
294             if (t instanceof MondrianException) {
295                 // pull the cause out, otherwise it never gets logged
296
Throwable JavaDoc cause = ((MondrianException) t).getCause();
297                 if (cause != null) {
298                     throw cause;
299                 } else {
300                     throw t;
301                 }
302             } else {
303                 throw t;
304             }
305         }
306     }
307
308     protected static SQLConnection getConnection(String JavaDoc jndiName, String JavaDoc driver, String JavaDoc userId, String JavaDoc password, String JavaDoc connectionInfo) {
309         SQLConnection connection = null;
310         try {
311             if (jndiName != null) {
312                 connection = (SQLConnection) PentahoConnectionFactory.getConnection(PentahoConnectionFactory.SQL_DATASOURCE, jndiName, null);
313             }
314             if (connection == null) {
315                 if (driver == null && connectionInfo == null) {
316                     // TODO raise an error
317
}
318                 connection = (SQLConnection) PentahoConnectionFactory.getConnection(PentahoConnectionFactory.SQL_DATASOURCE, driver, connectionInfo, userId, password, null);
319             }
320             if (connection == null) {
321                 Logger.error("MondrianModelComponent", Messages.getErrorString("SQLBaseComponent.ERROR_0005_INVALID_CONNECTION")); //$NON-NLS-1$ //$NON-NLS-2$
322
return null;
323             }
324             return connection;
325         } catch (Exception JavaDoc e) {
326             Logger.error("MondrianModelComponent", Messages.getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", ""), e); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
327
}
328         return null;
329     }
330
331 }
332
Popular Tags