Search in sources :

Example 26 with LogicalModel

use of org.pentaho.metadata.model.LogicalModel 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 27 with LogicalModel

use of org.pentaho.metadata.model.LogicalModel in project data-access by pentaho.

the class DatasourceService method isMetadataDatasource.

public static boolean isMetadataDatasource(Domain domain) {
    if (domain == null) {
        return false;
    }
    List<LogicalModel> logicalModelList = domain.getLogicalModels();
    if (logicalModelList != null && logicalModelList.size() >= 1) {
        for (LogicalModel logicalModel : logicalModelList) {
            // keep this check for backwards compatibility for now
            // $NON-NLS-1$
            Object property = logicalModel.getProperty("AGILE_BI_GENERATED_SCHEMA");
            if (property != null) {
                return false;
            }
            // moving forward any non metadata generated datasource should have this property
            // $NON-NLS-1$
            property = logicalModel.getProperty("WIZARD_GENERATED_SCHEMA");
            if (property != null) {
                return false;
            }
        }
        return true;
    } else {
        return true;
    }
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel)

Example 28 with LogicalModel

use of org.pentaho.metadata.model.LogicalModel in project data-access by pentaho.

the class CsvDatasourceServiceImpl method prepareForSerialization.

protected void prepareForSerialization(Domain domain) throws IOException {
    /*
     * This method is responsible for cleaning up legacy information when
     * changing datasource types and also manages CSV files for CSV based
     * datasources.
     */
    String relativePath = PentahoSystem.getSystemSetting("file-upload-defaults/relative-path", // $NON-NLS-1$
    String.valueOf(FileUtils.DEFAULT_RELATIVE_UPLOAD_FILE_PATH));
    String path = PentahoSystem.getApplicationContext().getSolutionPath(relativePath);
    String TMP_FILE_PATH = File.separatorChar + "system" + File.separatorChar + File.separatorChar + "tmp" + File.separatorChar;
    String sysTmpDir = PentahoSystem.getApplicationContext().getSolutionPath(TMP_FILE_PATH);
    LogicalModel logicalModel = domain.getLogicalModels().get(0);
    // $NON-NLS-1$
    String modelState = (String) logicalModel.getProperty("datasourceModel");
    if (modelState != null) {
        XStream xs = new XStream();
        DatasourceDTO datasource = (DatasourceDTO) xs.fromXML(modelState);
        CsvFileInfo csvFileInfo = datasource.getCsvModelInfo().getFileInfo();
        String tmpFileName = csvFileInfo.getTmpFilename();
        String csvFileName = csvFileInfo.getFilename();
        File tmpFile = new File(sysTmpDir + File.separatorChar + tmpFileName);
        // Move CSV temporary file to final destination.
        if (tmpFile.exists()) {
            File csvFile = new File(path + File.separatorChar + csvFileName);
            org.apache.commons.io.FileUtils.copyFile(tmpFile, csvFile);
        }
        // Cleanup logic when updating from SQL datasource to CSV
        // datasource.
        datasource.setQuery(null);
        // Update datasourceModel with the new modelState
        modelState = xs.toXML(datasource);
        logicalModel.setProperty("datasourceModel", modelState);
    }
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) XStream(com.thoughtworks.xstream.XStream) DatasourceDTO(org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO) File(java.io.File)

Example 29 with LogicalModel

use of org.pentaho.metadata.model.LogicalModel in project data-access by pentaho.

the class DSWDatasourceServiceImpl method getLogicalModels.

public List<LogicalModelSummary> getLogicalModels(String context) throws DatasourceServiceException {
    if (!hasDataAccessViewPermission()) {
        // $NON-NLS-1$
        logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0001_PERMISSION_DENIED"));
        throw new DatasourceServiceException(Messages.getErrorString(// $NON-NLS-1$
        "DatasourceServiceImpl.ERROR_0001_PERMISSION_DENIED"));
    }
    List<LogicalModelSummary> logicalModelSummaries = new ArrayList<LogicalModelSummary>();
    for (String domainId : getMetadataDomainRepository().getDomainIds()) {
        Domain domain;
        try {
            domain = getMetadataDomainRepository().getDomain(domainId);
        } catch (Exception e) {
            logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0022_UNABLE_TO_PROCESS_LOGICAL_MODEL", domainId), e);
            continue;
        }
        String locale = LocaleHelper.getLocale().toString();
        String[] locales = new String[domain.getLocales().size()];
        for (int i = 0; i < domain.getLocales().size(); i++) {
            locales[i] = domain.getLocales().get(i).getCode();
        }
        locale = LocaleHelper.getClosestLocale(locale, locales);
        for (LogicalModel model : domain.getLogicalModels()) {
            String vis = (String) model.getProperty(LM_PROP_VISIBLE);
            if (vis != null) {
                String[] visibleContexts = vis.split(",");
                boolean visibleToContext = false;
                for (String c : visibleContexts) {
                    if (StringUtils.isNotEmpty(c.trim()) && c.trim().equals(context)) {
                        visibleToContext = true;
                        break;
                    }
                }
                if (!visibleToContext) {
                    continue;
                }
            }
            logicalModelSummaries.add(new LogicalModelSummary(domainId, model.getId(), model.getName(locale)));
        }
    }
    return logicalModelSummaries;
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) LogicalModelSummary(org.pentaho.platform.dataaccess.datasource.beans.LogicalModelSummary) ArrayList(java.util.ArrayList) Domain(org.pentaho.metadata.model.Domain) ModelerException(org.pentaho.agilebi.modeler.ModelerException) QueryValidationException(org.pentaho.platform.dataaccess.datasource.wizard.service.QueryValidationException) SQLModelGeneratorException(org.pentaho.metadata.util.SQLModelGeneratorException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) SQLException(java.sql.SQLException) SqlQueriesNotSupportedException(org.pentaho.platform.dataaccess.datasource.wizard.service.SqlQueriesNotSupportedException) MondrianCatalogServiceException(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)

Example 30 with LogicalModel

use of org.pentaho.metadata.model.LogicalModel in project data-access by pentaho.

the class DebugModelerService method serializeModels.

public String serializeModels(Domain domain, String name) throws Exception {
    String domainId;
    PentahoSystemHelper.init();
    initKettle();
    try {
        DSWDatasourceServiceImpl datasourceService = new DSWDatasourceServiceImpl();
        ModelerWorkspace model = new ModelerWorkspace(new GwtModelerWorkspaceHelper(), datasourceService.getGeoContext());
        model.setModelName(name);
        model.setDomain(domain);
        String solutionStorage = AgileHelper.getDatasourceSolutionStorage();
        // $NON-NLS-1$  //$NON-NLS-2$
        String metadataLocation = "resources" + RepositoryFile.SEPARATOR + "metadata";
        String path = solutionStorage + RepositoryFile.SEPARATOR + metadataLocation + RepositoryFile.SEPARATOR;
        // $NON-NLS-1$
        domainId = path + name + ".xmi";
        IApplicationContext appContext = PentahoSystem.getApplicationContext();
        if (appContext != null) {
            path = PentahoSystem.getApplicationContext().getSolutionPath(path);
        }
        File pathDir = new File(path);
        if (!pathDir.exists()) {
            pathDir.mkdirs();
        }
        IPentahoSession session = getSession();
        // Keep a reference to the mondrian catalog
        model.getWorkspaceHelper().populateDomain(model);
        LogicalModel lModel = domain.getLogicalModels().get(0);
        String catName = lModel.getName(LocalizedString.DEFAULT_LOCALE);
        // $NON-NLS-1$
        lModel.setProperty("MondrianCatalogRef", catName);
        // Serialize domain to xmi.
        /*
        DISABLED DUE TO USE OF OLD API
      String base = PentahoSystem.getApplicationContext().getSolutionRootPath();
      String parentPath = ActionInfo.buildSolutionPath(solutionStorage, metadataLocation, ""); //$NON-NLS-1$
      int status = repository.publish(base, '/' + parentPath, name + ".xmi", reportXML.getBytes("UTF-8"),
      true); //$NON-NLS-1$ //$NON-NLS-2$
      if (status != ISolutionRepository.FILE_ADD_SUCCESSFUL) {
        throw new RuntimeException("Unable to save to repository. Status: " + status); //$NON-NLS-1$
      }

      // Serialize domain to olap schema.
      lModel = domain.getLogicalModels().get(1);
      MondrianModelExporter exporter = new MondrianModelExporter(lModel, LocalizedString.DEFAULT_LOCALE);
      String mondrianSchema = exporter.createMondrianModelXML();
      Document schemaDoc = DocumentHelper.parseText(mondrianSchema);
      byte[] schemaBytes = schemaDoc.asXML().getBytes("UTF-8"); //$NON-NLS-1$

      status = repository.publish(base, '/' + parentPath, name + ".mondrian.xml", schemaBytes, true); //$NON-NLS-1$  
      if (status != ISolutionRepository.FILE_ADD_SUCCESSFUL) {
        throw new RuntimeException("Unable to save to repository. Status: " + status); //$NON-NLS-1$  
      }

      // Refresh Metadata
      PentahoSystem.publish(session, MetadataPublisher.class.getName());
      */
        // Write this catalog to the default Pentaho DataSource and refresh the cache.
        // $NON-NLS-1$
        File file = new File(path + name + ".mondrian.xml");
        // Need to find a better way to get the connection name instead of using the Id.
        String catConnectStr = "Provider=mondrian;DataSource=\"" + ((SqlPhysicalModel) domain.getPhysicalModels().get(0)).getId() + // $NON-NLS-1$
        "\"";
        String catDef = // $NON-NLS-1$
        "solution:" + solutionStorage + RepositoryFile.SEPARATOR + "resources" + RepositoryFile.SEPARATOR + "metadata" + RepositoryFile.SEPARATOR + file.getName();
        addCatalog(catName, catConnectStr, catDef, session);
    } catch (Exception e) {
        getLogger().error(e);
        throw e;
    }
    return domainId;
}
Also used : GwtModelerWorkspaceHelper(org.pentaho.agilebi.modeler.gwt.GwtModelerWorkspaceHelper) LogicalModel(org.pentaho.metadata.model.LogicalModel) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) IApplicationContext(org.pentaho.platform.api.engine.IApplicationContext) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) ModelerWorkspace(org.pentaho.agilebi.modeler.ModelerWorkspace) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException)

Aggregations

LogicalModel (org.pentaho.metadata.model.LogicalModel)53 Domain (org.pentaho.metadata.model.Domain)29 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)14 Test (org.junit.Test)13 ModelerWorkspace (org.pentaho.agilebi.modeler.ModelerWorkspace)11 LogicalTable (org.pentaho.metadata.model.LogicalTable)11 ArrayList (java.util.ArrayList)10 LogicalColumn (org.pentaho.metadata.model.LogicalColumn)9 Category (org.pentaho.metadata.model.Category)8 Matchers.anyString (org.mockito.Matchers.anyString)7 SqlPhysicalModel (org.pentaho.metadata.model.SqlPhysicalModel)7 SqlPhysicalTable (org.pentaho.metadata.model.SqlPhysicalTable)6 IMetadataDomainRepository (org.pentaho.metadata.repository.IMetadataDomainRepository)6 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)6 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)5 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)5 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)5 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)5 IOException (java.io.IOException)4 SqlDataSource (org.pentaho.metadata.model.SqlDataSource)4