Search in sources :

Example 41 with LogicalModel

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

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

the class DSWDatasourceServiceImplTest method testDeleteLogicalModel_keepDomainIfLogicalModelExist.

@Test
public void testDeleteLogicalModel_keepDomainIfLogicalModelExist() throws Exception {
    doReturn(true).when(dswService).hasDataAccessPermission();
    doReturn(workspace2Models).when(dswService).createModelerWorkspace();
    LogicalModel logicalModel = new LogicalModel();
    logicalModel.setId(LOGICAL_MODEL_ID_DEFAULT);
    domain2Models.getLogicalModels().add(logicalModel);
    assertTrue(dswService.deleteLogicalModel(DOMAIN_ID_2MODELS, MODEL_NAME));
    List<LogicalModel> logicalModels = domain2Models.getLogicalModels();
    assertNotNull(logicalModels);
    assertEquals(1, logicalModels.size());
    verify(domainRepository, never()).removeDomain(domain2Models.getId());
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) Test(org.junit.Test)

Example 43 with LogicalModel

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

the class DSWDatasourceServiceImpl method prepareForSerializaton.

public void prepareForSerializaton(Domain domain) {
    /*
     * 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);
    LogicalModel logicalModel = domain.getLogicalModels().get(0);
    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 csvFileName = csvFileInfo.getFilename();
        if (csvFileName != null) {
            // Cleanup logic when updating from CSV datasource to SQL
            // datasource.
            csvFileInfo.setFilename(null);
            csvFileInfo.setTmpFilename(null);
            csvFileInfo.setFriendlyFilename(null);
            csvFileInfo.setContents(null);
            csvFileInfo.setEncoding(null);
            // Delete CSV file.
            File csvFile = new File(path + File.separatorChar + csvFileName);
            if (csvFile.exists()) {
                csvFile.delete();
            }
            // Delete STAGING database table.
            CsvTransformGenerator csvTransformGenerator = new CsvTransformGenerator(datasource.getCsvModelInfo(), AgileHelper.getDatabaseMeta());
            try {
                csvTransformGenerator.dropTable(datasource.getCsvModelInfo().getStageTableName());
            } catch (CsvTransformGeneratorException e) {
                logger.error(e);
            }
        }
        // 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) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) XStream(com.thoughtworks.xstream.XStream) CsvTransformGenerator(org.pentaho.platform.dataaccess.datasource.wizard.service.agile.CsvTransformGenerator) DatasourceDTO(org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO) File(java.io.File)

Example 44 with LogicalModel

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

the class MetadataService method getModelInfos.

/**
 * Returns a list of ModelInfo objects for the specified domain. These objects are small and this list is intended to
 * allow a client to provide a list of models to a user so the user can pick which one they want to work with.
 *
 * @param domain
 * @param models
 */
private void getModelInfos(final String domain, List<ModelInfo> models) {
    IMetadataDomainRepository repo = getMetadataRepository();
    Domain domainObject = repo.getDomain(domain);
    // find the best locale
    String locale = LocaleHelper.getClosestLocale(LocaleHelper.getLocale().toString(), domainObject.getLocaleCodes());
    // iterate over all of the models in this domain
    for (LogicalModel model : domainObject.getLogicalModels()) {
        // create a new ModelInfo object and give it the envelope information about the model
        ModelInfo modelInfo = new ModelInfo();
        modelInfo.setDomainId(domain);
        modelInfo.setModelId(model.getId());
        modelInfo.setModelName(model.getName(locale));
        if (model.getDescription() != null) {
            String modelDescription = model.getDescription(locale);
            modelInfo.setModelDescription(modelDescription);
        }
        models.add(modelInfo);
    }
    return;
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) ModelInfo(org.pentaho.platform.dataaccess.metadata.model.impl.ModelInfo) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) Domain(org.pentaho.metadata.model.Domain)

Example 45 with LogicalModel

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

the class MetadataService method loadModel.

/**
 * Returns a Model object for the requested model. The model will include the basic metadata - categories and
 * columns.
 *
 * @param domainId
 * @param modelId
 * @return
 */
public Model loadModel(String domainId, String modelId) {
    if (domainId == null) {
        // we can't do this without a model
        // $NON-NLS-1$
        error(Messages.getErrorString("MetadataService.ERROR_0003_NULL_DOMAIN"));
        return null;
    }
    if (modelId == null) {
        // we can't do this without a model
        // $NON-NLS-1$
        error(Messages.getErrorString("MetadataService.ERROR_0004_NULL_Model"));
        return null;
    }
    // because it's lighter weight, check the thin model
    Domain domain = getMetadataRepository().getDomain(domainId);
    if (domain == null) {
        // $NON-NLS-1$
        error(Messages.getErrorString("MetadataService.ERROR_0005_DOMAIN_NOT_FOUND", domainId));
        return null;
    }
    LogicalModel model = domain.findLogicalModel(modelId);
    if (model == null) {
        // the model cannot be found or cannot be loaded
        // $NON-NLS-1$
        error(Messages.getErrorString("MetadataService.ERROR_0006_MODEL_NOT_FOUND", modelId));
        return null;
    }
    // create the thin metadata model and return it
    MetadataServiceUtil util = getMetadataServiceUtil();
    util.setDomain(domain);
    Model thinModel = util.createThinModel(model, domainId);
    return thinModel;
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) LogicalModel(org.pentaho.metadata.model.LogicalModel) Model(org.pentaho.platform.dataaccess.metadata.model.impl.Model) Domain(org.pentaho.metadata.model.Domain)

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