1 16 17 package org.pentaho.plugin.olap; 18 19 import java.util.Properties ; 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 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 return true; 57 } 58 59 public boolean init() { 60 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 getInitialQuery(Properties properties, String cubeName) throws Throwable { 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())); return null; 86 } 87 88 try { 89 return getInitialQuery(connection, cubeName); 90 } catch (Throwable t) { 91 if (t instanceof MondrianException) { 92 Throwable 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 116 public static String getInitialQuery(String modelPath, String connectionString, String driver, String user, String password, String cubeName) throws Throwable { 117 return getInitialQuery(modelPath, connectionString, driver, user, password, cubeName, null); 118 } 119 120 132 public static String getInitialQuery(String modelPath, String connectionString, String driver, String user, String password, String cubeName, String roleName) throws Throwable { 133 134 Properties properties = new Properties (); 135 136 if (!PentahoSystem.ignored) { 138 if (driver != null) { 139 properties.put("Driver", driver); } 141 if (user != null) { 142 properties.put("User", user); } 144 if (password != null) { 145 properties.put("Password", password); } 147 } 148 149 if (modelPath.indexOf("http") == 0) { properties.put("Catalog", modelPath); } else { 152 properties.put("Catalog", PentahoSystem.getApplicationContext().getBaseUrl() + "GetMondrianModel?model=" + modelPath); } 154 properties.put("Provider", "mondrian"); properties.put("PoolNeeded", "false"); properties.put("dataSource", connectionString); if (roleName != null) { 158 properties.put("Role", roleName); } 160 161 return getInitialQuery(properties, cubeName); 162 } 163 164 172 public static String getInitialQuery(String modelPath, String connectionString, String cubeName) throws Throwable { 173 return getInitialQuery(modelPath, connectionString, cubeName, null); 174 } 175 176 185 public static String getInitialQuery(String modelPath, String jndi, String cubeName, String roleName) throws Throwable { 186 187 Properties properties = new Properties (); 188 189 190 if (modelPath.indexOf("http") == 0) { properties.put("Catalog", modelPath); } else { 193 properties.put("Catalog", PentahoSystem.getApplicationContext().getBaseUrl() + "GetMondrianModel?model=" + modelPath); } 195 196 jndi = DatasourceHelper.getDSBoundName(jndi); 197 198 properties.put("Provider", "mondrian"); properties.put("PoolNeeded", "false"); properties.put("dataSource", jndi); 202 if (roleName != null) { 203 properties.put("Role", roleName); } 205 return getInitialQuery(properties, cubeName); 206 } 207 208 public static String getInitialQuery(Connection connection, String cubeName) throws Throwable { 209 210 String measuresMdx = null; 211 String columnsMdx = null; 212 StringBuffer rowsMdx = new StringBuffer (); 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())); 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())); 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())); 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())); 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())); return null; 254 } 255 256 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())); 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())); return null; 270 } 271 if (dimensions[n].isMeasures()) { 272 measuresMdx = ""; columnsMdx = " select NON EMPTY {" + member.getUniqueName() + "} ON columns, "; } else { 277 if (rowsMdx.length() > 0) { 278 rowsMdx.append(", "); } 280 rowsMdx.append(member.getUniqueName()); 281 } 282 } 283 if (measuresMdx != null && columnsMdx != null && rowsMdx.length() > 0) { 284 StringBuffer result = new StringBuffer (measuresMdx.length() + columnsMdx.length() + rowsMdx.length() + 50); 285 result.append(measuresMdx).append(columnsMdx).append("NON EMPTY {(") .append(rowsMdx).append(")} ON rows ") .append("from [" + cube.getName() + "]"); 289 return result.toString(); 290 291 } 292 return null; 293 } catch (Throwable t) { 294 if (t instanceof MondrianException) { 295 Throwable 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 jndiName, String driver, String userId, String password, String 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 } 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")); return null; 323 } 324 return connection; 325 } catch (Exception e) { 326 Logger.error("MondrianModelComponent", Messages.getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", ""), e); } 328 return null; 329 } 330 331 } 332 | Popular Tags |