Search in sources :

Example 46 with LogicalModel

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

the class MetadataServiceTest method initialize.

@Before
public void initialize() {
    List<Category> categoryList = new ArrayList();
    Category category = mock(Category.class);
    when(category.getId()).thenReturn(CATEGORY_ID);
    categoryList.add(category);
    LogicalColumn logicalColumn = mock(LogicalColumn.class);
    when(logicalColumn.getId()).thenReturn(COLUMN_ID);
    when(logicalColumn.getDataType()).thenReturn(DataType.STRING);
    when(category.findLogicalColumn(anyString())).thenReturn(logicalColumn);
    logicalModel = mock(LogicalModel.class);
    when(logicalModel.getId()).thenReturn(LOGICAL_MODEL_ID);
    when(logicalModel.getName(anyString())).thenReturn(LOGICAL_MODEL_NAME);
    when(logicalModel.getCategories()).thenReturn(categoryList);
    when(logicalModel.findLogicalColumn(anyString())).thenReturn(logicalColumn);
    when(logicalModel.getProperty(anyString())).thenReturn(null);
    logicalModel2 = mock(LogicalModel.class);
    when(logicalModel2.getId()).thenReturn(LOGICAL_MODEL_2_ID);
    when(logicalModel2.getName(anyString())).thenReturn(LOGICAL_MODEL_2_NAME);
    when(logicalModel2.getProperty(anyString())).thenReturn(null);
    Domain domainOnlyReportingModel = new Domain();
    domainOnlyReportingModel.setId(DOMAIN_ID);
    domainOnlyReportingModel.setLogicalModels(new ArrayList<LogicalModel>() {

        {
            add(logicalModel2);
        }

        {
            add(logicalModel);
        }
    });
    iMetadataDomainRepository = mock(IMetadataDomainRepository.class);
    when(iMetadataDomainRepository.getDomain(DOMAIN_ID)).thenReturn(domainOnlyReportingModel);
    Set<String> domains = new HashSet<String>();
    domains.add(DOMAIN_ID);
    when(iMetadataDomainRepository.getDomainIds()).thenReturn(domains);
    metadataService = mock(MetadataService.class);
    when(metadataService.getMetadataRepository()).thenReturn(iMetadataDomainRepository);
    metadataServiceUtil = mock(MetadataServiceUtil.class);
    when(metadataServiceUtil.getMetadataRepository()).thenReturn(iMetadataDomainRepository);
    when(metadataServiceUtil.createThinModel(any(LogicalModel.class), anyString())).thenCallRealMethod();
    when(metadataService.getMetadataServiceUtil()).thenReturn(metadataServiceUtil);
}
Also used : LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category) ArrayList(java.util.ArrayList) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) Matchers.anyString(org.mockito.Matchers.anyString) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) LogicalModel(org.pentaho.metadata.model.LogicalModel) Domain(org.pentaho.metadata.model.Domain) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 47 with LogicalModel

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

the class ModelerDialog method onAccept.

@Bindable
public void onAccept() {
    enableWaitCursor(true);
    try {
        model.getWorkspaceHelper().populateDomain(model);
    } catch (ModelerException e) {
        e.printStackTrace();
        showErrorDialog(messages.getString("ModelEditor.ERROR"), messages.getString("ModelEditor.ERROR_0001_SAVING_MODELS"));
    }
    boolean doOlap = true;
    LogicalModel lModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
    if (lModel == null) {
        lModel = model.getLogicalModel(ModelerPerspective.REPORTING);
    }
    if (lModel.getProperty("MondrianCatalogRef") == null && (lModel.getProperty("DUAL_MODELING_SCHEMA") == null || "false".equals(lModel.getProperty("DUAL_MODELING_SCHEMA")))) {
        doOlap = false;
    }
    service.serializeModels(model.getDomain(), model.getModelName(), doOlap, new XulServiceCallback<String>() {

        public void success(String retVal) {
            enableWaitCursor(false);
            hideDialog();
            model.getDomain().setId(retVal);
            ModelerDialog.this.listener.onDialogAccept(model.getDomain());
        }

        public void error(String message, Throwable error) {
            enableWaitCursor(false);
            showErrorDialog(messages.getString("ModelEditor.ERROR"), messages.getString("ModelEditor.ERROR_0001_SAVING_MODELS"));
        }
    });
}
Also used : ModelerException(org.pentaho.agilebi.modeler.ModelerException) LogicalModel(org.pentaho.metadata.model.LogicalModel) Bindable(org.pentaho.ui.xul.stereotype.Bindable)

Example 48 with LogicalModel

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

the class DSWDatasourceServiceImpl method deleteLogicalModel.

public boolean deleteLogicalModel(String domainId, String modelName) throws DatasourceServiceException {
    if (!hasDataAccessPermission()) {
        // $NON-NLS-1$
        logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0001_PERMISSION_DENIED"));
        return false;
    }
    String catalogRef = null;
    String targetTable = null;
    try {
        // first load the model
        Domain domain = getMetadataDomainRepository().getDomain(domainId);
        ModelerWorkspace model = createModelerWorkspace();
        model.setDomain(domain);
        LogicalModel logicalModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
        if (logicalModel == null) {
            logicalModel = model.getLogicalModel(ModelerPerspective.REPORTING);
        }
        LogicalModel logicalModelRep = model.getLogicalModel(ModelerPerspective.REPORTING);
        // CSV related data is bounded to reporting model so need to perform some additional clean up here
        if (logicalModelRep != null) {
            String modelState = (String) logicalModelRep.getProperty(LM_PROP_DATASOURCE_MODEL);
            // TODO: use the edit story's stored info to do this
            if ("CSV".equals(logicalModelRep.getProperty(LM_PROP_DATASOURCE_TYPE)) || "true".equalsIgnoreCase((String) logicalModelRep.getProperty(LogicalModel.PROPERTY_TARGET_TABLE_STAGED))) {
                targetTable = ((SqlPhysicalTable) domain.getPhysicalModels().get(0).getPhysicalTables().get(0)).getTargetTable();
                DatasourceDTO datasource = null;
                if (modelState != null) {
                    datasource = deSerializeModelState(modelState);
                }
                if (datasource != null) {
                    CsvTransformGenerator csvTransformGenerator = new CsvTransformGenerator(datasource.getCsvModelInfo(), AgileHelper.getDatabaseMeta());
                    try {
                        csvTransformGenerator.dropTable(targetTable);
                    } catch (CsvTransformGeneratorException e) {
                        // table might not be there, it's OK that is what we were trying to do anyway
                        logger.warn(Messages.getErrorString("DatasourceServiceImpl.ERROR_0019_UNABLE_TO_DROP_TABLE", targetTable, domainId, e.getLocalizedMessage()), // $NON-NLS-1$
                        e);
                    }
                    String fileName = datasource.getCsvModelInfo().getFileInfo().getFilename();
                    FileUtils fileService = new FileUtils();
                    if (fileName != null) {
                        fileService.deleteFile(fileName);
                    }
                }
            }
        }
        // if associated mondrian file, delete
        if (logicalModel.getProperty(LM_PROP_MONDRIAN_CATALOG_REF) != null) {
            // remove Mondrian schema
            IMondrianCatalogService service = PentahoSystem.get(IMondrianCatalogService.class, null);
            catalogRef = (String) logicalModel.getProperty(LM_PROP_MONDRIAN_CATALOG_REF);
            // check if the model is not already removed
            if (service.getCatalog(catalogRef, PentahoSessionHolder.getSession()) != null) {
                service.removeCatalog(catalogRef, PentahoSessionHolder.getSession());
            }
        }
        getMetadataDomainRepository().removeModel(domainId, logicalModel.getId());
        if (logicalModelRep != null && !logicalModelRep.getId().equals(logicalModel.getId())) {
            getMetadataDomainRepository().removeModel(domainId, logicalModelRep.getId());
        }
        // get updated domain
        domain = getMetadataDomainRepository().getDomain(domainId);
        if (domain == null) {
            // already deleted
            return true;
        }
        if (domain.getLogicalModels() == null || domain.getLogicalModels().isEmpty()) {
            getMetadataDomainRepository().removeDomain(domainId);
        }
    } catch (MondrianCatalogServiceException me) {
        logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0020_UNABLE_TO_DELETE_CATALOG", catalogRef, domainId, me.getLocalizedMessage()), // $NON-NLS-1$
        me);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceServiceImpl.ERROR_0020_UNABLE_TO_DELETE_CATALOG", catalogRef, domainId, me.getLocalizedMessage()), // $NON-NLS-1$
        me);
    } catch (DomainStorageException dse) {
        logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0017_UNABLE_TO_STORE_DOMAIN", domainId, dse.getLocalizedMessage()), // $NON-NLS-1$
        dse);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceServiceImpl.ERROR_0016_UNABLE_TO_STORE_DOMAIN", domainId, dse.getLocalizedMessage()), // $NON-NLS-1$
        dse);
    } catch (DomainIdNullException dne) {
        logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0019_DOMAIN_IS_NULL", dne.getLocalizedMessage()), // $NON-NLS-1$
        dne);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceServiceImpl.ERROR_0019_DOMAIN_IS_NULL", dne.getLocalizedMessage()), // $NON-NLS-1$
        dne);
    }
    return true;
}
Also used : CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) FileUtils(org.pentaho.platform.dataaccess.datasource.wizard.csv.FileUtils) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DatasourceDTO(org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) LogicalModel(org.pentaho.metadata.model.LogicalModel) MondrianCatalogServiceException(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) CsvTransformGenerator(org.pentaho.platform.dataaccess.datasource.wizard.service.agile.CsvTransformGenerator) Domain(org.pentaho.metadata.model.Domain) ModelerWorkspace(org.pentaho.agilebi.modeler.ModelerWorkspace) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)

Example 49 with LogicalModel

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

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

the class DatasourceModel method copyOverMetadata.

/**
 * This is a utility method that looks into an old domain for the same column ids, and then copies over the old
 * metadata into the new.
 *
 * @param oldDomain
 * @param newDomain
 */
public void copyOverMetadata(Domain oldDomain, Domain newDomain) {
    Category category = newDomain.getLogicalModels().get(0).getCategories().get(0);
    LogicalModel oldModel = oldDomain.getLogicalModels().get(0);
    for (LogicalColumn column : category.getLogicalColumns()) {
        LogicalColumn oldColumn = oldModel.findLogicalColumn(column.getId());
        if (oldColumn != null) {
            column.setDataType(oldColumn.getDataType());
            column.setName(oldColumn.getName());
            column.setAggregationList(oldColumn.getAggregationList());
            column.setAggregationType(oldColumn.getAggregationType());
        }
    }
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category)

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