use of org.olap4j.OlapException in project pentaho-kettle by pentaho.
the class OlapHelper method openQuery.
public void openQuery() throws Exception {
Class.forName(olap4jDriver);
OlapConnection connection = null;
if (Utils.isEmpty(username) && Utils.isEmpty(password)) {
connection = (OlapConnection) DriverManager.getConnection(olap4jUrl);
} else {
connection = (OlapConnection) DriverManager.getConnection(olap4jUrl, username, password);
}
OlapWrapper wrapper = connection;
olapConnection = wrapper.unwrap(OlapConnection.class);
try {
if (!Utils.isEmpty(catalogName)) {
olapConnection.setCatalog(catalogName);
}
} catch (SQLException e) {
throw new OlapException("Error setting catalog for MDX statement: '" + catalogName + "'");
}
OlapStatement stmt = olapConnection.createStatement();
if (!Utils.isEmpty(mdx)) {
CellSet tmp = stmt.executeOlapQuery(mdx);
result = tmp;
} else {
throw new Exception("Error executing empty MDX query");
}
}
use of org.olap4j.OlapException in project mondrian by pentaho.
the class MondrianOlap4jCube method getMeasures.
public List<Measure> getMeasures() {
final Dimension dimension = getDimensions().get("Measures");
if (dimension == null) {
return Collections.emptyList();
}
final MondrianOlap4jConnection olap4jConnection = olap4jSchema.olap4jCatalog.olap4jDatabaseMetaData.olap4jConnection;
try {
final mondrian.olap.SchemaReader schemaReader = olap4jConnection.getMondrianConnection().getSchemaReader().withLocus();
final MondrianOlap4jLevel measuresLevel = (MondrianOlap4jLevel) dimension.getDefaultHierarchy().getLevels().get(0);
final List<Measure> measures = new ArrayList<Measure>();
List<mondrian.olap.Member> levelMembers = schemaReader.getLevelMembers(measuresLevel.level, true);
for (mondrian.olap.Member member : levelMembers) {
// This corrects MONDRIAN-1123, a ClassCastException (see below)
// that occurs when you create a calculated member on a
// dimension other than Measures:
// java.lang.ClassCastException:
// mondrian.olap4j.MondrianOlap4jMember cannot be cast to
// org.olap4j.metadata.Measure
MondrianOlap4jMember olap4jMember = olap4jConnection.toOlap4j(member);
if (olap4jMember instanceof Measure) {
measures.add((Measure) olap4jMember);
}
}
return measures;
} catch (OlapException e) {
// Demote from checked to unchecked exception.
throw new RuntimeException(e);
}
}
use of org.olap4j.OlapException in project mondrian by pentaho.
the class FileRepository method getConnection.
OlapConnection getConnection(CatalogInfo catalogInfo, MondrianServer server, String roleName, Properties props) throws SQLException {
String connectString = catalogInfo.olap4jConnectString;
// Save the server for the duration of the call to 'getConnection'.
final LockBox.Entry entry = MondrianServerRegistry.INSTANCE.lockBox.register(server);
final Properties properties = new Properties();
properties.setProperty(RolapConnectionProperties.Instance.name(), entry.getMoniker());
if (roleName != null) {
properties.setProperty(RolapConnectionProperties.Role.name(), roleName);
}
properties.putAll(props);
// the ClassLoader.
try {
ClassResolver.INSTANCE.forName(MondrianOlap4jDriver.class.getName(), true);
} catch (ClassNotFoundException e) {
throw new OlapException("Cannot find mondrian olap4j driver.");
}
final java.sql.Connection connection = java.sql.DriverManager.getConnection(connectString, properties);
return ((OlapWrapper) connection).unwrap(OlapConnection.class);
}
use of org.olap4j.OlapException in project mondrian by pentaho.
the class FileRepository method getConnection.
public OlapConnection getConnection(MondrianServer server, String databaseName, String catalogName, String roleName, Properties props) throws SQLException {
final ServerInfo serverInfo = getServerInfo();
DatabaseInfo datasourceInfo;
if (databaseName == null) {
if (serverInfo.datasourceMap.size() == 0) {
throw new OlapException("No databases configured on this server");
}
datasourceInfo = serverInfo.datasourceMap.values().iterator().next();
} else {
datasourceInfo = serverInfo.datasourceMap.get(databaseName);
// that here as well.
if (datasourceInfo == null) {
for (DatabaseInfo infos : serverInfo.datasourceMap.values()) {
PropertyList pl = Util.parseConnectString((String) infos.properties.get("DataSourceInfo"));
pl.remove(RolapConnectionProperties.Jdbc.name());
pl.remove(RolapConnectionProperties.JdbcUser.name());
pl.remove(RolapConnectionProperties.JdbcPassword.name());
if (pl.toString().equals(databaseName)) {
datasourceInfo = infos;
}
}
}
}
if (datasourceInfo == null) {
throw Util.newError("Unknown database '" + databaseName + "'");
}
if (catalogName == null) {
if (datasourceInfo.catalogMap.size() == 0) {
throw new OlapException("No catalogs in the database named " + datasourceInfo.name);
}
for (CatalogInfo catalogInfo : datasourceInfo.catalogMap.values()) {
try {
return getConnection(catalogInfo, server, roleName, props);
} catch (Exception e) {
LOGGER.warn("Failed getting connection. Skipping", e);
}
}
} else {
CatalogInfo namedCatalogInfo = datasourceInfo.catalogMap.get(catalogName);
if (namedCatalogInfo == null) {
throw Util.newError("Unknown catalog '" + catalogName + "'");
}
return getConnection(namedCatalogInfo, server, roleName, props);
}
throw Util.newError("No suitable connection found");
}
use of org.olap4j.OlapException in project pentaho-platform by pentaho.
the class MondrianAbstractPlatformUserRoleMapper method getMondrianRolesFromCatalog.
/**
* This method returns the role names as found in the Mondrian schema. The returned names must be ordered (sorted) or
* code down-stream will not work.
*
* @param userSession
* Users' session
* @param catalogName
* The name of the catalog
* @return Array of role names from the schema file
*/
protected String[] getMondrianRolesFromCatalog(IPentahoSession userSession, String context) {
String[] rtn = null;
// Get the catalog service
IMondrianCatalogService catalogService = PentahoSystem.get(IMondrianCatalogService.class);
if (catalogService != null) {
// Get the catalog by name
MondrianCatalog catalog = catalogService.getCatalog(context, userSession);
if (catalog != null) {
// The roles are in the schema object
MondrianSchema schema = catalog.getSchema();
if (schema != null) {
// Ask the schema for the role names array
String[] roleNames = schema.getRoleNames();
if ((roleNames != null) && (roleNames.length > 0)) {
// Return the roles from the schema
Arrays.sort(roleNames);
return roleNames;
}
}
}
}
// Check with the IOlapService and try to get a list of roles there.
IOlapService olapService = PentahoSystem.get(IOlapService.class);
if (olapService != null) {
MondrianCatalogRepositoryHelper helper = new MondrianCatalogRepositoryHelper(PentahoSystem.get(IUnifiedRepository.class));
String serverName = null;
for (String name : helper.getOlap4jServers()) {
PropertyList props = Util.parseConnectString(helper.getOlap4jServerInfo(name).URL);
if (props.get(RolapConnectionProperties.Catalog.name(), "").equals(context)) {
serverName = name;
}
}
if (serverName != null) {
OlapConnection conn = null;
try {
// Use a null session for root access.
conn = olapService.getConnection(serverName, null);
List<String> roleList = conn.getAvailableRoleNames();
String[] roleArray = roleList.toArray(new String[roleList.size()]);
Arrays.sort(roleArray);
return roleArray;
} catch (OlapException e) {
log.error("Failed to get a list of roles from olap connection " + context, e);
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// OK to squash this one.
log.error("Failed to get a list of roles from olap connection " + context, e);
}
}
}
}
}
// Sort the returned list of roles.
return rtn;
}
Aggregations