Search in sources :

Example 1 with MondrianModelExporter

use of org.pentaho.metadata.util.MondrianModelExporter in project data-access by pentaho.

the class DataSourceWizardService method createMondrianDswBundle.

/**
 * Generate a mondrian schema from the model and create the appropriate import bundle
 * @param domain domain with olap model
 * @return import bundle
 * @throws DatasourceServiceException
 * @throws Exception If schema generation fails
 */
protected IPlatformImportBundle createMondrianDswBundle(Domain domain, RepositoryFileAclDto acl) throws DatasourceServiceException, DswPublishValidationException, IOException {
    final String analysisDomainId = toAnalysisDomainId(domain.getId());
    final String dataSource = ModelerService.getMondrianDatasource(domain);
    // get olap logical model
    final String locale = Locale.getDefault().toString();
    ModelerWorkspace workspace = new ModelerWorkspace(new ModelerWorkspaceHelper(locale), dswService.getGeoContext());
    workspace.setModelName(analysisDomainId);
    workspace.setDomain(domain);
    LogicalModel olapModel = workspace.getLogicalModel(ModelerPerspective.ANALYSIS);
    if (olapModel == null) {
        throw new IllegalArgumentException("No analysis model in xmi.");
    }
    // reference schema in xmi
    olapModel.setProperty(MONDRIAN_CATALOG_REF, analysisDomainId);
    // generate schema
    MondrianModelExporter exporter = new MondrianModelExporter(olapModel, locale);
    exporter.updateModelToNewDomainName(analysisDomainId);
    String mondrianSchema = null;
    try {
        mondrianSchema = exporter.createMondrianModelXML();
    } catch (Exception e) {
        throw new DswPublishValidationException(Type.INVALID_XMI, e.getMessage());
    }
    // create bundle
    final RepositoryFileImportBundle.Builder builder = new RepositoryFileImportBundle.Builder().input(IOUtils.toInputStream(mondrianSchema, ENCODING)).name(MONDRIAN_SCHEMA_NAME).charSet(ENCODING).overwriteFile(true).mime(MONDRIAN_MIME).withParam(IMPORT_DOMAIN_ID, analysisDomainId).withParam(MONDRIAN_CONNECTION_PARAM, "DataSource=" + dataSource);
    if (acl != null) {
        builder.acl(repositoryFileAclAdapter.unmarshal(acl)).applyAclSettings(true);
    }
    return builder.build();
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) ModelerWorkspaceHelper(org.pentaho.agilebi.modeler.util.ModelerWorkspaceHelper) GwtModelerWorkspaceHelper(org.pentaho.agilebi.modeler.gwt.GwtModelerWorkspaceHelper) MondrianModelExporter(org.pentaho.metadata.util.MondrianModelExporter) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) ModelerWorkspace(org.pentaho.agilebi.modeler.ModelerWorkspace) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) FileNotFoundException(java.io.FileNotFoundException) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) MondrianCatalogServiceException(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) IOException(java.io.IOException)

Example 2 with MondrianModelExporter

use of org.pentaho.metadata.util.MondrianModelExporter 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)

Aggregations

ModelerWorkspace (org.pentaho.agilebi.modeler.ModelerWorkspace)2 GwtModelerWorkspaceHelper (org.pentaho.agilebi.modeler.gwt.GwtModelerWorkspaceHelper)2 LogicalModel (org.pentaho.metadata.model.LogicalModel)2 MondrianModelExporter (org.pentaho.metadata.util.MondrianModelExporter)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ModelerWorkspaceHelper (org.pentaho.agilebi.modeler.util.ModelerWorkspaceHelper)1 KettleException (org.pentaho.di.core.exception.KettleException)1 IMetadataDomainRepository (org.pentaho.metadata.repository.IMetadataDomainRepository)1 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)1 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)1 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)1 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)1 IMondrianCatalogService (org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService)1 MondrianCatalog (org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalog)1 MondrianCatalogServiceException (org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException)1 RepositoryFileImportBundle (org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle)1