Search in sources :

Example 1 with MondrianCatalogRepositoryHelper

use of org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper 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 2 with MondrianCatalogRepositoryHelper

use of org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper in project pentaho-platform by pentaho.

the class OlapServiceImpl method addHostedCatalog.

public void addHostedCatalog(String name, String dataSourceInfo, InputStream inputStream, boolean overwrite, IPentahoSession session) {
    // Access
    if (!hasAccess(name, EnumSet.of(RepositoryFilePermission.WRITE), session)) {
        // $NON-NLS-1$
        LOG.debug("user does not have access; throwing exception");
        throw new IOlapServiceException(Messages.getInstance().getErrorString(// $NON-NLS-1$
        "OlapServiceImpl.ERROR_0003_INSUFFICIENT_PERMISSION"), IOlapServiceException.Reason.ACCESS_DENIED);
    }
    // check for existing vs. the overwrite flag.
    if (getCatalogNames(session).contains(name) && !overwrite) {
        throw new IOlapServiceException(Messages.getInstance().getErrorString(// $NON-NLS-1$
        "OlapServiceImpl.ERROR_0004_ALREADY_EXISTS"), IOlapServiceException.Reason.ALREADY_EXISTS);
    }
    try {
        MondrianCatalogRepositoryHelper helper = new MondrianCatalogRepositoryHelper(getRepository());
        helper.addHostedCatalog(inputStream, name, dataSourceInfo);
    } catch (Exception e) {
        throw new IOlapServiceException(e, IOlapServiceException.Reason.convert(e));
    }
}
Also used : IOlapServiceException(org.pentaho.platform.plugin.action.olap.IOlapServiceException) MondrianCatalogRepositoryHelper(org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) FileSystemException(org.apache.commons.vfs2.FileSystemException) OlapException(org.olap4j.OlapException) SQLException(java.sql.SQLException) IOlapServiceException(org.pentaho.platform.plugin.action.olap.IOlapServiceException)

Example 3 with MondrianCatalogRepositoryHelper

use of org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper in project pentaho-platform by pentaho.

the class OlapServiceImpl method addOlap4jCatalog.

public void addOlap4jCatalog(String name, String className, String URL, String user, String password, Properties props, boolean overwrite, IPentahoSession session) {
    // Access
    if (!hasAccess(name, EnumSet.of(RepositoryFilePermission.WRITE), session)) {
        // $NON-NLS-1$
        LOG.debug("user does not have access; throwing exception");
        throw new IOlapServiceException(Messages.getInstance().getErrorString(// $NON-NLS-1$
        "OlapServiceImpl.ERROR_0003_INSUFFICIENT_PERMISSION"), IOlapServiceException.Reason.ACCESS_DENIED);
    }
    // check for existing vs. the overwrite flag.
    if (getCatalogNames(session).contains(name) && !overwrite) {
        throw new IOlapServiceException(Messages.getInstance().getErrorString(// $NON-NLS-1$
        "OlapServiceImpl.ERROR_0004_ALREADY_EXISTS"), IOlapServiceException.Reason.ALREADY_EXISTS);
    }
    MondrianCatalogRepositoryHelper helper = new MondrianCatalogRepositoryHelper(getRepository());
    helper.addOlap4jServer(name, className, URL, user, password, props);
}
Also used : IOlapServiceException(org.pentaho.platform.plugin.action.olap.IOlapServiceException) MondrianCatalogRepositoryHelper(org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper)

Example 4 with MondrianCatalogRepositoryHelper

use of org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper in project pentaho-platform by pentaho.

the class MondrianCatalogHelper method addCatalog.

/**
 * new method to pass the input stream directly from data access put and post schema
 *
 * @param schemaInputStream
 * @param catalog
 * @param overwrite
 * @param acl               catalog ACL
 * @param pentahoSession
 * @throws MondrianCatalogServiceException
 */
@Override
public synchronized void addCatalog(InputStream schemaInputStream, final MondrianCatalog catalog, final boolean overwrite, RepositoryFileAcl acl, final IPentahoSession pentahoSession) throws MondrianCatalogServiceException {
    if (MondrianCatalogHelper.logger.isDebugEnabled()) {
        // $NON-NLS-1$
        MondrianCatalogHelper.logger.debug("addCatalog");
    }
    init(pentahoSession);
    // check for existing dataSourceInfo+catalog
    final boolean catalogExistsWithSameDatasource = catalogExists(catalog, pentahoSession);
    if (catalogExistsWithSameDatasource && !overwrite) {
        throw new MondrianCatalogServiceException(Messages.getInstance().getErrorString("MondrianCatalogHelper.ERROR_0004_ALREADY_EXISTS"), // $NON-NLS-1$
        Reason.ALREADY_EXISTS);
    }
    // Checks if a catalog of the same name but with a different file
    // path exists.
    MondrianCatalog fileLocationCatalogTest = null;
    for (MondrianCatalog currentCatalogCheck : getCatalogs(pentahoSession)) {
        if (currentCatalogCheck.getName().equals(catalog.getName())) {
            fileLocationCatalogTest = currentCatalogCheck;
            break;
        }
    }
    // compare the catalog names and throw exception if same and NOT ovewrite
    final boolean catalogExistsWithDifferentDatasource;
    try {
        catalogExistsWithDifferentDatasource = fileLocationCatalogTest != null && definitionEquals(fileLocationCatalogTest.getDefinition(), "mondrian:/" + URLEncoder.encode(catalog.getName(), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new MondrianCatalogServiceException(e);
    }
    if (catalogExistsWithDifferentDatasource && !overwrite) {
        throw new MondrianCatalogServiceException(Messages.getInstance().getErrorString(// $NON-NLS-1$
        "MondrianCatalogHelper.ERROR_0004_ALREADY_EXISTS"), Reason.XMLA_SCHEMA_NAME_EXISTS);
    }
    MondrianCatalogRepositoryHelper helper = getMondrianCatalogRepositoryHelper();
    try {
        helper.addHostedCatalog(schemaInputStream, catalog.getName(), catalog.getDataSourceInfo());
    } catch (Exception e) {
        throw new MondrianCatalogServiceException(Messages.getInstance().getErrorString(// $NON-NLS-1$
        "MondrianCatalogHelper.ERROR_0008_ERROR_OCCURRED"), Reason.valueOf(e.getMessage()));
    }
    if (MondrianCatalogHelper.logger.isDebugEnabled()) {
        MondrianCatalogHelper.logger.debug(// $NON-NLS-1$ //$NON-NLS-2$
        "refreshing from dataSourcesConfig (" + dataSourcesConfig + ")");
    }
    try {
        reInit(pentahoSession);
        setAclFor(catalog.getName(), acl);
        if (catalogExistsWithSameDatasource || catalogExistsWithDifferentDatasource) {
            flushCacheForCatalog(catalog.getName(), pentahoSession);
        }
    } catch (MondrianException e) {
        helper.deleteHostedCatalog(catalog.getName());
        reInit(pentahoSession);
        throw e;
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) MondrianCatalogRepositoryHelper(org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper) MondrianException(mondrian.olap.MondrianException) 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)

Example 5 with MondrianCatalogRepositoryHelper

use of org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper in project pentaho-platform by pentaho.

the class MondrianCatalogHelperIT method testAddCatalogWithException.

@Test(expected = MondrianException.class)
public void testAddCatalogWithException() {
    initMondrianCatalogsCache();
    MondrianCatalogHelper helperSpy = spy(helper);
    doThrow(new MondrianException()).when(helperSpy).reInit(any(IPentahoSession.class));
    IPentahoSession session = mock(IPentahoSession.class);
    doNothing().when(helperSpy).init(session);
    MondrianCatalog cat = createTestCatalog();
    MondrianCatalogRepositoryHelper repositoryHelper = mock(MondrianCatalogRepositoryHelper.class);
    doReturn(repositoryHelper).when(helperSpy).getMondrianCatalogRepositoryHelper();
    try {
        helperSpy.addCatalog(new ByteArrayInputStream(new byte[0]), cat, true, null, session);
    } catch (MondrianException e) {
        // verifying the repository rolled back and the cache reinitialized
        verify(repositoryHelper, times(1)).deleteHostedCatalog(anyString());
        verify(helperSpy, times(2)).reInit(any(IPentahoSession.class));
    }
    helperSpy.addCatalog(new ByteArrayInputStream(new byte[0]), cat, true, null, session);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) MondrianCatalogRepositoryHelper(org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper) MondrianException(mondrian.olap.MondrianException) Test(org.junit.Test)

Aggregations

MondrianCatalogRepositoryHelper (org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper)13 Test (org.junit.Test)6 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 IAclNodeHelper (org.pentaho.platform.api.repository2.unified.IAclNodeHelper)4 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)4 EnumSet (java.util.EnumSet)3 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 ZipInputStream (java.util.zip.ZipInputStream)2 MondrianException (mondrian.olap.MondrianException)2 FileSystemException (org.apache.commons.vfs2.FileSystemException)2 Matchers.anyString (org.mockito.Matchers.anyString)2 OlapException (org.olap4j.OlapException)2 ModelerWorkspace (org.pentaho.agilebi.modeler.ModelerWorkspace)2 LogicalModel (org.pentaho.metadata.model.LogicalModel)2 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)2