Search in sources :

Example 21 with MondrianCatalog

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

the class DSWDatasourceServiceImplTest method setUp.

@Before
public void setUp() throws Exception {
    SqlDataSource dataSource = new SqlDataSource();
    dataSource.setDatabaseName(CONNECTION_NAME);
    SqlPhysicalTable sqlTable = new SqlPhysicalTable();
    sqlTable.setTargetTable(VALID_QUERY);
    SqlPhysicalModel sqlModel = new SqlPhysicalModel();
    sqlModel.addPhysicalTable(sqlTable);
    sqlModel.setDatasource(dataSource);
    analysisModel = new LogicalModel();
    analysisModel.setId(LOGICAL_MODEL_ID_ANALYSIS);
    analysisModel.setProperty(DSWDatasourceServiceImpl.LM_PROP_VISIBLE, LOGICAL_MODEL_CONTEXTNAME);
    reportingModel = new LogicalModel();
    reportingModel.setId(LOGICAL_MODEL_ID_REPORTING);
    domain2Models = new Domain();
    domain2Models.setId(DOMAIN_ID_2MODELS);
    domain2Models.addLogicalModel(analysisModel);
    domain2Models.addLogicalModel(reportingModel);
    domain2Models.setLocales(Arrays.asList(new LocaleType("en_US", "Test locale")));
    domain2Models.addPhysicalModel(sqlModel);
    Set<String> domains = new TreeSet<String>();
    domains.add(DOMAIN_ID_2MODELS);
    doReturn(domain2Models).when(domainRepository).getDomain(DOMAIN_ID_2MODELS);
    doReturn(domains).when(domainRepository).getDomainIds();
    doAnswer(new Answer<Object>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            final String modelId = (String) invocation.getArguments()[1];
            final LogicalModel modelToRemove = domain2Models.findLogicalModel(modelId);
            domain2Models.getLogicalModels().remove(modelToRemove);
            return null;
        }
    }).when(domainRepository).removeModel(anyString(), anyString());
    workspace2Models = mock(ModelerWorkspace.class);
    when(workspace2Models.getLogicalModel(ModelerPerspective.ANALYSIS)).thenReturn(analysisModel);
    when(workspace2Models.getLogicalModel(ModelerPerspective.REPORTING)).thenReturn(reportingModel);
    dswService = spy(new DSWDatasourceServiceImpl(mock(ConnectionServiceImpl.class)));
    doNothing().when(dswService).checkSqlQueriesSupported(anyString());
    dswService.setMetadataDomainRepository(domainRepository);
    Object[][] coumnHeaders = new Object[][] { columns };
    SQLMetaData metadata = mock(SQLMetaData.class);
    when(metadata.getColumnHeaders()).thenReturn(coumnHeaders);
    when(metadata.getJDBCColumnTypes()).thenReturn(columnTypes);
    IPentahoResultSet resultSet = mock(IPentahoResultSet.class);
    when(resultSet.getMetaData()).thenReturn(metadata);
    doReturn(resultSet).when(sqlConnection).executeQuery(matches("(.*" + VALID_QUERY + ".*)"));
    when(sqlConnection.executeQuery(matches("(.*" + QUERY_COLUMN_ALREADY_EXIST + ".*)"))).thenThrow(new SQLException("Reason", "S0021", 21));
    doReturn(nativeConnection).when(sqlConnection).getNativeConnection();
    MondrianCatalog catalog = mock(MondrianCatalog.class);
    doReturn(catalog).when(mondrianService).getCatalog(anyString(), any(IPentahoSession.class));
    pentahoObjectFactory = mock(IPentahoObjectFactory.class);
    when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
    when(pentahoObjectFactory.get(this.anyClass(), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            if (invocation.getArguments()[0].equals(IMondrianCatalogService.class)) {
                return mondrianService;
            }
            if (invocation.getArguments()[0].equals(IPentahoConnection.class)) {
                return sqlConnection;
            }
            if (invocation.getArguments()[0].equals(IMetadataDomainRepository.class)) {
                return domainRepository;
            }
            return null;
        }
    });
    PentahoSystem.registerObjectFactory(pentahoObjectFactory);
    IPentahoSession pentahoSessionMock = mock(IPentahoSession.class);
    when(pentahoSessionMock.getName()).thenReturn("sessionName");
    PentahoSessionHolder.setSession(pentahoSessionMock);
}
Also used : MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) SQLException(java.sql.SQLException) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) Mockito.anyString(org.mockito.Mockito.anyString) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) SqlPhysicalTable(org.pentaho.metadata.model.SqlPhysicalTable) IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) LogicalModel(org.pentaho.metadata.model.LogicalModel) TreeSet(java.util.TreeSet) LocaleType(org.pentaho.metadata.model.concept.types.LocaleType) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) SQLMetaData(org.pentaho.platform.plugin.services.connections.sql.SQLMetaData) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SqlDataSource(org.pentaho.metadata.model.SqlDataSource) Domain(org.pentaho.metadata.model.Domain) ModelerWorkspace(org.pentaho.agilebi.modeler.ModelerWorkspace) Before(org.junit.Before)

Example 22 with MondrianCatalog

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

the class DatasourceResource method getAnalysisDatasourceInfo.

/**
 * Get the data source wizard info (parameters) for a specific data source wizard id
 *
 * @param dswId
 *          String id for a data source wizard
 *
 * @return Response containing the parameter list
 */
@GET
@Path("/{catalogId : .+}/getAnalysisDatasourceInfo")
@Produces(WILDCARD)
@Facet(name = "Unsupported")
public Response getAnalysisDatasourceInfo(@PathParam("catalogId") String catalogId) {
    IMondrianCatalogService mondrianCatalogService = PentahoSystem.get(IMondrianCatalogService.class, PentahoSessionHolder.getSession());
    MondrianCatalog catalog = mondrianCatalogService.getCatalog(catalogId, PentahoSessionHolder.getSession());
    if (catalog == null) {
        logger.warn("Catalog " + catalogId + " doesn't exist");
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    // dataSourceInfo can contain XML-escaped characters
    String parameters = prepareDataSourceInfo(catalog.getDataSourceInfo());
    // after preparation, parameters have escaped only quotes
    return Response.ok().entity(parameters).build();
}
Also used : MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Facet(org.codehaus.enunciate.Facet)

Example 23 with MondrianCatalog

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

the class ModelerService method serializeModels.

public String serializeModels(final Domain domain, final String name, final boolean doOlap) throws Exception {
    String domainId = null;
    initKettle();
    if (dataAccessPermHandler.hasDataAccessPermission(PentahoSessionHolder.getSession())) {
        SecurityHelper.getInstance().runAsSystem(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                try {
                    if (datasourceService == null) {
                        datasourceService = new DSWDatasourceServiceImpl();
                    }
                    ModelerWorkspace model = new ModelerWorkspace(new GwtModelerWorkspaceHelper(), datasourceService.getGeoContext());
                    model.setModelName(name);
                    model.setDomain(domain);
                    if (name.endsWith(".xmi")) {
                        domain.setId(name);
                    } else {
                        domain.setId(name + ".xmi");
                    }
                    LogicalModel lModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
                    if (lModel == null) {
                        lModel = model.getLogicalModel(ModelerPerspective.REPORTING);
                    }
                    lModel.setProperty("AGILE_BI_GENERATED_SCHEMA", "TRUE");
                    lModel.setProperty("WIZARD_GENERATED_SCHEMA", "TRUE");
                    String catName = lModel.getName(Locale.getDefault().toString());
                    // strip off the _olap suffix for the catalog ref
                    catName = catName.replace(BaseModelerWorkspaceHelper.OLAP_SUFFIX, "");
                    if (doOlap) {
                        // $NON-NLS-1$
                        lModel.setProperty("MondrianCatalogRef", catName);
                    }
                    // Stores metadata into JCR.
                    IMetadataDomainRepository metadataDomainRep = PentahoSystem.get(IMetadataDomainRepository.class);
                    if (metadataDomainRep != null) {
                        metadataDomainRep.storeDomain(model.getDomain(), true);
                    }
                    // Serialize domain to olap schema.
                    if (doOlap) {
                        MondrianModelExporter exporter = new MondrianModelExporter(lModel, Locale.getDefault().toString());
                        String mondrianSchema = exporter.createMondrianModelXML();
                        IPentahoSession session = PentahoSessionHolder.getSession();
                        if (session != null) {
                            // first remove the existing schema, including any
                            // model annotations which may have been previously applied
                            IMondrianCatalogService mondrianCatalogService = // $NON-NLS-1$
                            PentahoSystem.get(IMondrianCatalogService.class, "IMondrianCatalogService", session);
                            // try to get the current catalog
                            MondrianCatalog currentCatalog = mondrianCatalogService.getCatalog(catName, session);
                            // if current catalog exists, remove it
                            if (currentCatalog != null) {
                                mondrianCatalogService.removeCatalog(catName, session);
                            }
                            session.setAttribute("MONDRIAN_SCHEMA_XML_CONTENT", mondrianSchema);
                            // $NON-NLS-1$
                            String catConnectStr = "Provider=mondrian;DataSource=\"" + getMondrianDatasource(domain) + "\"";
                            addCatalog(catName, catConnectStr, session);
                        }
                    }
                } catch (Exception e) {
                    logger.error(e);
                    throw e;
                }
                return null;
            }
        });
    }
    return domainId;
}
Also used : MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) KettleException(org.pentaho.di.core.exception.KettleException) GwtModelerWorkspaceHelper(org.pentaho.agilebi.modeler.gwt.GwtModelerWorkspaceHelper) LogicalModel(org.pentaho.metadata.model.LogicalModel) MondrianModelExporter(org.pentaho.metadata.util.MondrianModelExporter) ModelerWorkspace(org.pentaho.agilebi.modeler.ModelerWorkspace)

Example 24 with MondrianCatalog

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

the class AnalysisServiceTest method testGetAnalysisDatasourceIds.

@Test
public void testGetAnalysisDatasourceIds() throws Exception {
    final MondrianCatalog foodmartCatalog = new MondrianCatalog("foodmart", "info", "file:///place", new MondrianSchema("foodmart", Collections.emptyList()));
    final MondrianCatalog foodmartCatalog2 = new MondrianCatalog("foodmart2", "info", "file:///place", new MondrianSchema("foodmart2", Collections.emptyList()));
    final List<MondrianCatalog> catalogs = Arrays.asList(foodmartCatalog, foodmartCatalog2);
    doReturn(catalogs).when(catalogService).listCatalogs(any(IPentahoSession.class), eq(false));
    final HashSet<String> domainIds = Sets.newHashSet("foodmart.xmi", "sample.xmi");
    doReturn(domainIds).when(metadataRepository).getDomainIds();
    final List<String> response = analysisService.getAnalysisDatasourceIds();
    assertEquals(Collections.singletonList("foodmart2"), response);
}
Also used : MondrianCatalog(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog) MondrianSchema(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianSchema) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Test(org.junit.Test)

Example 25 with MondrianCatalog

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

the class AnalysisServiceTest method testImportingSchemaRemovesExistingAnnotationsByDefault.

@Test
public void testImportingSchemaRemovesExistingAnnotationsByDefault() throws Exception {
    MondrianCatalog otherCatalog = new MondrianCatalog("other", "", "", null);
    MondrianCatalog salesCatalog = new MondrianCatalog("sales", "", "", null);
    when(catalogService.listCatalogs(any(IPentahoSession.class), eq(false))).thenReturn(Arrays.asList(otherCatalog, salesCatalog));
    putMondrianSchemaWithSchemaFileName("stubFileName", "overwrite=true");
    verify(importer).importFile(argThat(matchBundle(true, acl)));
    verify(catalogService).removeCatalog(eq("sales"), any(IPentahoSession.class));
}
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