Search in sources :

Example 21 with MondrianException

use of mondrian.olap.MondrianException in project pentaho-platform by pentaho.

the class PentahoXmlaServlet method createConnectionFactory.

@Override
protected ConnectionFactory createConnectionFactory(final ServletConfig servletConfig) throws ServletException {
    final ConnectionFactory delegate = super.createConnectionFactory(servletConfig);
    /*
     * This wrapper for the connection factory allows us to
     * override the list of roles with the ones defined in
     * the IPentahoSession and filter it through the
     * IConnectionUserRoleMapper.
     */
    return new ConnectionFactory() {

        public Map<String, Object> getPreConfiguredDiscoverDatasourcesResponse() {
            return delegate.getPreConfiguredDiscoverDatasourcesResponse();
        }

        public OlapConnection getConnection(String databaseName, String catalogName, String roleName, Properties props) throws SQLException {
            // What we do here is to filter the role names with the mapper.
            // First, get a user role mapper, if one is configured.
            final IPentahoSession session = PentahoSessionHolder.getSession();
            final IConnectionUserRoleMapper mondrianUserRoleMapper = PentahoSystem.get(IConnectionUserRoleMapper.class, MDXConnection.MDX_CONNECTION_MAPPER_KEY, // Don't use the user session here yet.
            null);
            String[] effectiveRoles = new String[0];
            /*
         * If Catalog/Schema are null (this happens with high level metadata requests,
         * like DISCOVER_DATASOURCES) we can't use the role mapper, even if it
         * is present and configured.
         */
            if (mondrianUserRoleMapper != null && catalogName != null) {
                // Use the role mapper.
                try {
                    effectiveRoles = mondrianUserRoleMapper.mapConnectionRoles(session, catalogName);
                    if (effectiveRoles == null) {
                        effectiveRoles = new String[0];
                    }
                } catch (PentahoAccessControlException e) {
                    throw new SQLException(e);
                }
            }
            // Now we tokenize that list.
            boolean addComma = false;
            // $NON-NLS-1$
            roleName = "";
            for (String role : effectiveRoles) {
                if (addComma) {
                    // $NON-NLS-1$
                    roleName = roleName.concat(",");
                }
                roleName = roleName.concat(role);
                addComma = true;
            }
            // Now let the delegate connection factory do its magic.
            if (catalogName == null) {
                return delegate.getConnection(databaseName, catalogName, roleName.equals("") ? null : roleName, props);
            } else {
                // We create a connection differently so we can ensure that
                // the XMLA servlet shares the same MondrianServer instance as the rest
                // of the platform
                IMondrianCatalogService mcs = PentahoSystem.get(IMondrianCatalogService.class);
                MondrianCatalog mc = mcs.getCatalog(catalogName, PentahoSessionHolder.getSession());
                if (mc == null) {
                    throw new XmlaException(CLIENT_FAULT_FC, HSB_BAD_RESTRICTION_LIST_CODE, HSB_BAD_RESTRICTION_LIST_FAULT_FS, new MondrianException("No such catalog: " + catalogName));
                }
                Connection con = DriverManager.getConnection(mc.getDataSourceInfo() + ";Catalog=" + mc.getDefinition(), catalogLocator);
                try {
                    final MondrianServer server = MondrianServer.forConnection(con);
                    FileRepository fr = new FileRepository(makeContentFinder(makeDataSourcesUrl(servletConfig)), catalogLocator);
                    OlapConnection connection = fr.getConnection(server, databaseName, catalogName, roleName, props);
                    fr.shutdown();
                    return connection;
                } finally {
                    con.close();
                }
            }
        }
    };
}
Also used : FileRepository(mondrian.server.FileRepository) MondrianServer(mondrian.olap.MondrianServer) MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) SQLException(java.sql.SQLException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) IConnectionUserRoleMapper(org.pentaho.platform.api.engine.IConnectionUserRoleMapper) OlapConnection(org.olap4j.OlapConnection) OlapConnection(org.olap4j.OlapConnection) MDXConnection(org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection) Connection(mondrian.olap.Connection) Properties(java.util.Properties) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) ConnectionFactory(mondrian.xmla.XmlaHandler.ConnectionFactory) SolutionRepositoryVfsFileObject(org.pentaho.platform.repository.solution.filebased.SolutionRepositoryVfsFileObject) XmlaException(mondrian.xmla.XmlaException) MondrianException(mondrian.olap.MondrianException)

Example 22 with MondrianException

use of mondrian.olap.MondrianException in project pentaho-platform by pentaho.

the class MondrianCatalogHelper method loadCatalogsIntoCache.

protected void loadCatalogsIntoCache(final DataSourcesConfig.DataSources dataSources, final IPentahoSession pentahoSession) {
    // Create the cache region if necessary.
    ICacheManager cacheMgr = PentahoSystem.getCacheManager(pentahoSession);
    if (!cacheMgr.cacheEnabled(MONDRIAN_CATALOG_CACHE_REGION)) {
        // Create the region
        cacheMgr.addCacheRegion(MONDRIAN_CATALOG_CACHE_REGION);
    }
    Map<String, MondrianCatalog> catalogs = (Map<String, MondrianCatalog>) cacheMgr.getFromRegionCache(MONDRIAN_CATALOG_CACHE_REGION, getLocale().toString());
    if (catalogs == null) {
        catalogs = new HashMap<String, MondrianCatalog>();
    } else {
        return;
    }
    for (DataSourcesConfig.DataSource dataSource : dataSources.dataSources) {
        List<String> catalogNames = new ArrayList<String>();
        for (DataSourcesConfig.Catalog catalog : dataSource.catalogs.catalogs) {
            catalogNames.add(catalog.name);
        }
        for (DataSourcesConfig.Catalog catalog : dataSource.catalogs.catalogs) {
            if (catalog.definition.startsWith("mondrian:") || catalog.definition.startsWith("solution:")) {
                // $NON-NLS-1$
                // try catch here so the whole thing doesn't blow up if one datasource is configured incorrectly.
                MondrianSchema schema = null;
                try {
                    schema = makeSchema(getCatalogAsString(pentahoSession, catalog));
                } catch (Exception e) {
                    MondrianCatalogHelper.logger.error(Messages.getInstance().getErrorString("MondrianCatalogHelper.ERROR_0013_FAILED_TO_LOAD_SCHEMA", catalog.definition), // $NON-NLS-1$
                    e);
                    if (e instanceof MondrianException) {
                        throw (MondrianException) e;
                    }
                }
                MondrianCatalog mondrianCatalog = null;
                if (schema == null) {
                    mondrianCatalog = new MondrianCatalog(catalog.name, catalog.dataSourceInfo, catalog.definition, schema);
                } else {
                    mondrianCatalog = new MondrianCatalog(useSchemaNameAsCatalogName ? schema.getName() : catalog.name, catalog.dataSourceInfo, catalog.definition, schema);
                }
                catalogs.put(mondrianCatalog.getName(), mondrianCatalog);
                catalogs.put(mondrianCatalog.getDefinition(), mondrianCatalog);
            } else {
                MondrianCatalogHelper.logger.warn(Messages.getInstance().getString("MondrianCatalogHelper.WARN_SKIPPING_DATASOURCE_DEF", // $NON-NLS-1$
                catalog.definition));
            }
        }
    }
    cacheMgr.putInRegionCache(MONDRIAN_CATALOG_CACHE_REGION, getLocale().toString(), catalogs);
}
Also used : DataSourcesConfig(mondrian.xmla.DataSourcesConfig) ArrayList(java.util.ArrayList) XOMException(org.eigenbase.xom.XOMException) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileNotFoundException(java.io.FileNotFoundException) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) XmlParseException(org.pentaho.platform.api.util.XmlParseException) SAXException(org.xml.sax.SAXException) MondrianException(mondrian.olap.MondrianException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ICacheManager(org.pentaho.platform.api.engine.ICacheManager) MondrianException(mondrian.olap.MondrianException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 23 with MondrianException

use of mondrian.olap.MondrianException in project pentaho-platform by pentaho.

the class MondrianModelComponent method getInitialQuery.

public static String getInitialQuery(final Connection connection, final String cubeName) throws Throwable {
    String measuresMdx = null;
    String columnsMdx = null;
    // $NON-NLS-1$
    String whereMdx = "";
    StringBuffer rowsMdx = new StringBuffer();
    // Get catalog info, if exists
    String catalog = connection.getCatalogName();
    MondrianCatalogComplementInfo catalogComplementInfo = MondrianCatalogHelper.getInstance().getCatalogComplementInfoMap(catalog);
    try {
        Schema schema = connection.getSchema();
        if (schema == null) {
            Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0002_INVALID_SCHEMA", // $NON-NLS-1$ //$NON-NLS-2$
            connection.getConnectString()));
            return null;
        }
        Cube[] cubes = schema.getCubes();
        if ((cubes == null) || (cubes.length == 0)) {
            Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0003_NO_CUBES", // $NON-NLS-1$ //$NON-NLS-2$
            connection.getConnectString()));
            return null;
        }
        if ((cubes.length > 1) && (cubeName == null)) {
            Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0004_CUBE_NOT_SPECIFIED", // $NON-NLS-1$ //$NON-NLS-2$
            connection.getConnectString()));
            return null;
        }
        Cube cube = null;
        if (cubes.length == 1) {
            cube = cubes[0];
        } else {
            for (Cube element : cubes) {
                if (element.getName().equals(cubeName)) {
                    cube = element;
                    break;
                }
            }
        }
        if (cube == null) {
            Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0005_CUBE_NOT_FOUND", cubeName, // $NON-NLS-1$ //$NON-NLS-2$
            connection.getConnectString()));
            return null;
        }
        // If we have any whereConditions block, we need to find which hierarchies they are in
        // and not include them in the rows
        HashSet<Hierarchy> whereHierarchies = new HashSet<Hierarchy>();
        if (catalogComplementInfo != null && catalogComplementInfo.getWhereCondition(cube.getName()) != null && !catalogComplementInfo.getWhereCondition(cube.getName()).equals("")) {
            // $NON-NLS-1$
            final String rawString = catalogComplementInfo.getWhereCondition(cube.getName());
            try {
                // According to Julian, the better way to resolve the names is to build a query
                final String queryStr = // $NON-NLS-1$ //$NON-NLS-2$
                "select " + rawString + " on columns, {} on rows from " + cube.getName();
                final Query query = connection.parseQuery(queryStr);
                final Hierarchy[] hierarchies = query.getMdxHierarchiesOnAxis(AxisOrdinal.StandardAxisOrdinal.COLUMNS);
                boolean isWhereValid = true;
                for (int i = 0; i < hierarchies.length && isWhereValid; i++) {
                    final Hierarchy hierarchy = hierarchies[i];
                    if (connection.getRole().canAccess(hierarchy)) {
                        whereHierarchies.add(hierarchy);
                    } else {
                        isWhereValid = false;
                        whereHierarchies.clear();
                    }
                }
                if (isWhereValid) {
                    // $NON-NLS-1$
                    whereMdx = " WHERE " + rawString;
                }
            } catch (Exception e) {
                // We found an error in the where slicer, so we'll just act like it wasn't here
                whereHierarchies.clear();
            }
        }
        Dimension[] dimensions = cube.getDimensions();
        if ((dimensions == null) || (dimensions.length == 0)) {
            Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0006_NO_DIMENSIONS", cubeName, // $NON-NLS-1$ //$NON-NLS-2$
            connection.getConnectString()));
            return null;
        }
        for (Dimension element : dimensions) {
            final Hierarchy hierarchy = element.getHierarchy();
            if (hierarchy == null) {
                Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0007_NO_HIERARCHIES", element.getName(), cubeName, // $NON-NLS-1$ //$NON-NLS-2$
                connection.getConnectString()));
                return null;
            }
            if (!connection.getRole().canAccess(hierarchy)) {
                // We can't access this element
                continue;
            }
            if (whereHierarchies.contains(hierarchy)) {
                // We have it on the where condition - skip it
                continue;
            }
            Member member = Locus.execute((RolapConnection) connection, "Retrieving default members in plugin", new Locus.Action<Member>() {

                public Member execute() {
                    return connection.getSchemaReader().getHierarchyDefaultMember(hierarchy);
                }
            });
            if (member == null) {
                Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0008_NO_DEFAULT_MEMBER", element.getName(), cubeName, // $NON-NLS-1$ //$NON-NLS-2$
                connection.getConnectString()));
                return null;
            }
            if (element.isMeasures()) {
                // measuresMdx = "with member "+ member.getUniqueName();
                // //$NON-NLS-1$
                // $NON-NLS-1$
                measuresMdx = "";
                // $NON-NLS-1$ //$NON-NLS-2$
                columnsMdx = " select NON EMPTY {" + member.getUniqueName() + "} ON columns, ";
            } else {
                if (rowsMdx.length() > 0) {
                    // $NON-NLS-1$
                    rowsMdx.append(", ");
                }
                rowsMdx.append(member.getUniqueName());
            }
        }
        if ((measuresMdx != null) && (columnsMdx != null) && (rowsMdx.length() > 0)) {
            StringBuffer result = new StringBuffer(measuresMdx.length() + columnsMdx.length() + rowsMdx.length() + 50);
            // $NON-NLS-1$
            result.append(measuresMdx).append(columnsMdx).append("NON EMPTY {(").append(rowsMdx).append(// $NON-NLS-1$
            ")} ON rows ").append(// $NON-NLS-1$ //$NON-NLS-2$
            "from [" + cube.getName() + "]").append(whereMdx);
            return result.toString();
        }
        return null;
    } catch (Throwable t) {
        if (t instanceof MondrianException) {
            // pull the cause out, otherwise it never gets logged
            Throwable cause = ((MondrianException) t).getCause();
            if (cause != null) {
                throw cause;
            } else {
                throw t;
            }
        } else {
            throw t;
        }
    }
}
Also used : Query(mondrian.olap.Query) Schema(mondrian.olap.Schema) Dimension(mondrian.olap.Dimension) MondrianException(mondrian.olap.MondrianException) Hierarchy(mondrian.olap.Hierarchy) Cube(mondrian.olap.Cube) MondrianCatalogComplementInfo(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogComplementInfo) Locus(mondrian.server.Locus) MondrianException(mondrian.olap.MondrianException) Member(mondrian.olap.Member) HashSet(java.util.HashSet)

Aggregations

MondrianException (mondrian.olap.MondrianException)23 Util (mondrian.olap.Util)5 PostgreSqlDialect (mondrian.spi.impl.PostgreSqlDialect)5 Connection (mondrian.olap.Connection)4 RolapConnection (mondrian.rolap.RolapConnection)4 SQLException (java.sql.SQLException)3 Test (org.junit.Test)3 IMondrianCatalogService (org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Query (mondrian.olap.Query)2 AbortException (mondrian.rolap.agg.SegmentCacheManager.AbortException)2 SegmentCacheIndex (mondrian.rolap.cache.SegmentCacheIndex)2 Locus (mondrian.server.Locus)2 FileSystemException (org.apache.commons.vfs2.FileSystemException)2 XOMException (org.eigenbase.xom.XOMException)2