Search in sources :

Example 1 with OlapException

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");
    }
}
Also used : OlapStatement(org.olap4j.OlapStatement) OlapWrapper(org.olap4j.OlapWrapper) SQLException(java.sql.SQLException) OlapConnection(org.olap4j.OlapConnection) OlapException(org.olap4j.OlapException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) OlapException(org.olap4j.OlapException) CellSet(org.olap4j.CellSet)

Example 2 with OlapException

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);
    }
}
Also used : OlapException(org.olap4j.OlapException) Dimension(org.olap4j.metadata.Dimension) mondrian.olap(mondrian.olap) Member(org.olap4j.metadata.Member)

Example 3 with OlapException

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);
}
Also used : MondrianOlap4jDriver(mondrian.olap4j.MondrianOlap4jDriver) OlapWrapper(org.olap4j.OlapWrapper) LockBox(mondrian.util.LockBox) OlapException(org.olap4j.OlapException) RolapConnectionProperties(mondrian.rolap.RolapConnectionProperties) MondrianProperties(mondrian.olap.MondrianProperties) Properties(java.util.Properties)

Example 4 with OlapException

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");
}
Also used : PropertyList(mondrian.olap.Util.PropertyList) OlapException(org.olap4j.OlapException) OlapException(org.olap4j.OlapException) SQLException(java.sql.SQLException)

Example 5 with OlapException

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;
}
Also used : MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) IOlapService(org.pentaho.platform.plugin.action.olap.IOlapService) SQLException(java.sql.SQLException) MondrianSchema(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianSchema) OlapConnection(org.olap4j.OlapConnection) OlapException(org.olap4j.OlapException) MondrianCatalogRepositoryHelper(org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) PropertyList(mondrian.olap.Util.PropertyList) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository)

Aggregations

OlapException (org.olap4j.OlapException)6 SQLException (java.sql.SQLException)4 OlapConnection (org.olap4j.OlapConnection)3 PropertyList (mondrian.olap.Util.PropertyList)2 OlapWrapper (org.olap4j.OlapWrapper)2 IOlapService (org.pentaho.platform.plugin.action.olap.IOlapService)2 Properties (java.util.Properties)1 mondrian.olap (mondrian.olap)1 MondrianProperties (mondrian.olap.MondrianProperties)1 MondrianOlap4jDriver (mondrian.olap4j.MondrianOlap4jDriver)1 RolapConnectionProperties (mondrian.rolap.RolapConnectionProperties)1 LockBox (mondrian.util.LockBox)1 CellSet (org.olap4j.CellSet)1 OlapStatement (org.olap4j.OlapStatement)1 Dimension (org.olap4j.metadata.Dimension)1 Member (org.olap4j.metadata.Member)1 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)1 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)1 IMondrianCatalogService (org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService)1 MondrianCatalog (org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog)1