Search in sources :

Example 6 with MondrianCatalog

use of org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog in project pentaho-platform by pentaho.

the class MDXBaseComponent method getConnectionOrig.

protected IPentahoConnection getConnectionOrig() {
    IPentahoConnection localConnection = null;
    MdxConnectionAction connAction = (MdxConnectionAction) getActionDefinition();
    try {
        String mdxConnectionStr = connAction.getMdxConnectionString().getStringValue();
        Properties mdxConnectionProps = (Properties) connAction.getConnectionProps().getValue();
        String jdbcStr = connAction.getConnection().getStringValue();
        String jndiStr = connAction.getJndi().getStringValue();
        String location = connAction.getLocation().getStringValue();
        String role = connAction.getRole().getStringValue();
        String catalog = connAction.getCatalog().getStringValue();
        if ((catalog == null) && (connAction.getCatalogResource() != null)) {
            IActionSequenceResource resource = getResource(connAction.getCatalogResource().getName());
            catalog = resource.getAddress();
            if (resource.getSourceType() == IActionSequenceResource.URL_RESOURCE) {
                if (!catalog.startsWith("solution:") && !catalog.startsWith("http:")) {
                    // $NON-NLS-1$
                    if (fileExistsInRepository(catalog)) {
                        // About allowed "solution:"
                        // Extra step to make sure that remote mondrian models
                        // fully qualified aren't munged
                        // MB
                        // $NON-NLS-1$
                        catalog = "solution:" + catalog;
                    }
                }
            } else if ((resource.getSourceType() == IActionSequenceResource.SOLUTION_FILE_RESOURCE) || (resource.getSourceType() == IActionSequenceResource.FILE_RESOURCE)) {
                if (!catalog.startsWith("solution:")) {
                    // $NON-NLS-1$
                    catalog = "solution:" + catalog;
                }
            }
        }
        if (catalog == null) {
            // $NON-NLS-1$
            warn(Messages.getInstance().getString("MDXBaseComponent.ERROR_0007_CATALOG_NOT_DEFINED", getActionName()));
        } else {
            if (mdxConnectionProps != null) {
                mdxConnectionProps.put(MdxConnectionAction.CATALOG_ELEMENT, catalog);
            }
        }
        String userId = connAction.getUserId().getStringValue();
        String password = connAction.getPassword().getStringValue();
        if (mdxConnectionProps != null) {
            localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, mdxConnectionProps, getSession(), this);
        } else {
            if (mdxConnectionStr != null) {
                localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, mdxConnectionStr, getSession(), this);
            } else {
                String connectStr = null;
                if (jdbcStr != null) {
                    // $NON-NLS-1$
                    connectStr = jdbcStr + "; Catalog=" + catalog;
                } else if (jndiStr != null) {
                    IDBDatasourceService datasourceService = PentahoSystem.getObjectFactory().get(IDBDatasourceService.class, null);
                    if (datasourceService.getDataSource(jndiStr) == null) {
                        // $NON-NLS-1$
                        error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION"));
                        return null;
                    }
                    // $NON-NLS-1$ //$NON-NLS-2$
                    connectStr = "dataSource=" + jndiStr + "; Catalog=" + catalog;
                    // Add extra definitions from platform mondrian metadata
                    MondrianCatalog mc = getMondrianCatalog(catalog);
                    try {
                        connectStr += ";" + mc.getDataSourceInfo();
                    } catch (Exception e) {
                    // Just swallow the exception
                    }
                }
                if (role != null) {
                    // $NON-NLS-1$
                    connectStr += "; Role=" + role;
                }
                Properties props = new Properties();
                props.setProperty(IPentahoConnection.CONNECTION, connectStr);
                props.setProperty(IPentahoConnection.PROVIDER, location);
                if (userId != null) {
                    props.setProperty(IPentahoConnection.USERNAME_KEY, userId);
                }
                if (password != null) {
                    props.setProperty(IPentahoConnection.PASSWORD_KEY, password);
                }
                localConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, props, getSession(), this);
            }
            if (localConnection == null) {
                // $NON-NLS-1$
                error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0005_INVALID_CONNECTION"));
                return null;
            }
        }
        if (localConnection instanceof MDXConnection) {
            MDXConnection mdxConn = (MDXConnection) localConnection;
            if (connAction != null) {
                if ((connAction.getExtendedColumnNames() != ActionInputConstant.NULL_INPUT)) {
                    mdxConn.setUseExtendedColumnNames(connAction.getExtendedColumnNames().getBooleanValue());
                }
            }
        }
        return localConnection;
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e);
    }
    return null;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) MDXConnection(org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection) MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) MdxConnectionAction(org.pentaho.actionsequence.dom.actions.MdxConnectionAction) RolapConnectionProperties(mondrian.rolap.RolapConnectionProperties) Properties(java.util.Properties) IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 7 with MondrianCatalog

use of org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog in project pentaho-platform by pentaho.

the class MondrianModelComponent method getInitialQuery.

public static String getInitialQuery(final Properties properties, final String cubeName, IPentahoSession session) throws Throwable {
    // Apply any properties for this catalog specified in datasource.xml
    IMondrianCatalogService mondrianCatalogService = PentahoSystem.get(IMondrianCatalogService.class, "IMondrianCatalogService", PentahoSessionHolder.getSession());
    List<MondrianCatalog> catalogs = mondrianCatalogService.listCatalogs(PentahoSessionHolder.getSession(), true);
    String propCat = properties.getProperty(RolapConnectionProperties.Catalog.name());
    for (MondrianCatalog cat : catalogs) {
        if (cat.getDefinition().equalsIgnoreCase(propCat)) {
            Util.PropertyList connectProperties = Util.parseConnectString(cat.getDataSourceInfo());
            Iterator<Pair<String, String>> iter = connectProperties.iterator();
            while (iter.hasNext()) {
                Pair<String, String> pair = iter.next();
                if (// Only set if not set already
                !properties.containsKey(pair.getKey())) {
                    properties.put(pair.getKey(), pair.getValue());
                }
            }
            break;
        }
    }
    MDXConnection mdxConnection = (MDXConnection) PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, properties, session, null);
    // mdxConnection.setProperties( properties );
    Connection connection = mdxConnection.getConnection();
    if (connection == null) {
        Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0001_INVALID_CONNECTION", // $NON-NLS-1$ //$NON-NLS-2$
        properties.toString()));
        return null;
    }
    try {
        return MondrianModelComponent.getInitialQuery(connection, cubeName);
    } 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 : MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) SQLConnection(org.pentaho.platform.plugin.services.connections.sql.SQLConnection) MDXConnection(org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection) IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) Connection(mondrian.olap.Connection) RolapConnection(mondrian.rolap.RolapConnection) Util(mondrian.olap.Util) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) MDXConnection(org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection) MondrianException(mondrian.olap.MondrianException) Pair(mondrian.util.Pair)

Example 8 with MondrianCatalog

use of org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog 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)

Example 9 with MondrianCatalog

use of org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog in project data-access by pentaho.

the class AnalysisServiceTest method testGetAnalysisDatasourceAclNoAcl.

@Test
public void testGetAnalysisDatasourceAclNoAcl() throws Exception {
    allAccess();
    final String catalogName = "catalogName";
    final MondrianCatalog mondrianCatalog = mock(MondrianCatalog.class);
    when(catalogService.getCatalog(eq(catalogName), any(IPentahoSession.class))).thenReturn(mondrianCatalog);
    when(catalogService.getAclFor(catalogName)).thenReturn(null);
    final RepositoryFileAclDto aclDto = analysisService.getAnalysisDatasourceAcl(catalogName);
    assertNull(aclDto);
}
Also used : MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) RepositoryFileAclDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclDto) Test(org.junit.Test)

Example 10 with MondrianCatalog

use of org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog in project data-access by pentaho.

the class AnalysisServiceTest method testSetAnalysisDatasourceAclNoAcl.

@Test
public void testSetAnalysisDatasourceAclNoAcl() throws Exception {
    allAccess();
    String catalogName = "catalogName";
    final MondrianCatalog mondrianCatalog = mock(MondrianCatalog.class);
    when(catalogService.getCatalog(eq(catalogName), any(IPentahoSession.class))).thenReturn(mondrianCatalog);
    analysisService.setAnalysisDatasourceAcl(catalogName, null);
    verify(catalogService).setAclFor(eq(catalogName), (RepositoryFileAcl) isNull());
}
Also used : MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Test(org.junit.Test)

Aggregations

MondrianCatalog (org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog)28 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)13 IMondrianCatalogService (org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService)13 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)4 Properties (java.util.Properties)4 InputStream (java.io.InputStream)3 SQLException (java.sql.SQLException)3 MondrianException (mondrian.olap.MondrianException)3 IPentahoConnection (org.pentaho.commons.connection.IPentahoConnection)3 MondrianSchema (org.pentaho.platform.plugin.action.mondrian.catalog.MondrianSchema)3 MDXConnection (org.pentaho.platform.plugin.services.connections.mondrian.MDXConnection)3 IOException (java.io.IOException)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Connection (mondrian.olap.Connection)2 Util (mondrian.olap.Util)2 RolapConnection (mondrian.rolap.RolapConnection)2