Search in sources :

Example 1 with SqlPhysicalTable

use of org.pentaho.metadata.model.SqlPhysicalTable in project pentaho-kettle by pentaho.

the class MetadataGenerator method generatePhysicalMetadataModel.

public Domain generatePhysicalMetadataModel() throws KettleException {
    // First do some checking and lookups...
    // 
    String targetDatabaseName = ConceptUtil.getString(logicalDomain, DefaultIDs.DOMAIN_TARGET_DATABASE);
    if (Utils.isEmpty(targetDatabaseName)) {
        throw new KettleException("Please specify a target database!");
    }
    DatabaseMeta targetDatabaseMeta = DatabaseMeta.findDatabase(databases, targetDatabaseName);
    if (targetDatabaseMeta == null) {
        throw new KettleException("Target database with name '" + targetDatabaseName + "' can't be found!");
    }
    // Now start creation of a new domain with physical underpinning.
    // 
    Domain domain = new Domain();
    // Copy the domain information...
    // 
    domain.setId(createId("DOMAIN", null, domain));
    domain.setName(logicalDomain.getName());
    domain.setDescription(logicalDomain.getDescription());
    // 
    for (LogicalModel logicalModel : logicalDomain.getLogicalModels()) {
        // Copy model information...
        // 
        LogicalModel model = new LogicalModel();
        model.setId(createId("MODEL", domain, model));
        model.setName(logicalModel.getName());
        model.setDescription(logicalModel.getDescription());
        // Create a physical model...
        // 
        SqlPhysicalModel sqlModel = new SqlPhysicalModel();
        sqlModel.setDatasource(createSqlDataSource(targetDatabaseMeta));
        model.setPhysicalModel(sqlModel);
        for (LogicalTable logicalTable : logicalModel.getLogicalTables()) {
            LogicalTable table = new LogicalTable();
            table.setId(createId("LOGICAL_TABLE", logicalModel, logicalTable));
            table.setName(logicalTable.getName());
            table.setDescription(logicalTable.getDescription());
            String targetTable = ConceptUtil.getString(logicalTable, DefaultIDs.LOGICAL_TABLE_PHYSICAL_TABLE_NAME);
            SqlPhysicalTable sqlTable = new SqlPhysicalTable(sqlModel);
            table.setPhysicalTable(sqlTable);
            // Copy name & description from physical level...
            // 
            sqlTable.setId(createId("PHYSICAL_TABLE", logicalModel, logicalTable));
            sqlTable.setName(logicalTable.getName());
            sqlTable.setDescription(logicalTable.getDescription());
            sqlTable.setTableType(ConceptUtil.getTableType(logicalTable));
            sqlTable.setTargetSchema(targetDatabaseMeta.getPreferredSchemaName());
            sqlTable.setTargetTable(targetTable);
        }
    }
    return domain;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) LogicalModel(org.pentaho.metadata.model.LogicalModel) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) LogicalTable(org.pentaho.metadata.model.LogicalTable) Domain(org.pentaho.metadata.model.Domain) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) SqlPhysicalTable(org.pentaho.metadata.model.SqlPhysicalTable)

Example 2 with SqlPhysicalTable

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

the class DataAccessServiceTestBase method getDomain.

/**
 * Provide a mock domain
 *
 * @return
 */
private Domain getDomain() {
    LogicalColumn logicalColumn1;
    LogicalColumn logicalColumn2;
    String locale = LocaleHelper.getLocale().toString();
    SqlPhysicalModel model = new SqlPhysicalModel();
    SqlDataSource dataSource = new SqlDataSource();
    dataSource.setDatabaseName("SampleData");
    model.setDatasource(dataSource);
    SqlPhysicalTable table = new SqlPhysicalTable(model);
    model.getPhysicalTables().add(table);
    table.setTargetTableType(TargetTableType.INLINE_SQL);
    table.setTargetTable("select * from customers");
    table.setId("customers");
    SqlPhysicalColumn column = new SqlPhysicalColumn(table);
    column.setTargetColumn("customername");
    column.setName(new LocalizedString(locale, "Customer Name"));
    column.setDescription(new LocalizedString(locale, "Customer Name Desc"));
    column.setDataType(DataType.STRING);
    column.setId("cutomer_customername");
    table.getPhysicalColumns().add(column);
    LogicalModel logicalModel = new LogicalModel();
    model.setId("MODEL");
    model.setName(new LocalizedString(locale, "My Model"));
    model.setDescription(new LocalizedString(locale, "A Description of the Model"));
    LogicalTable logicalTable = new LogicalTable();
    logicalTable.setId("BT_CUSTOMERS");
    logicalTable.setPhysicalTable(table);
    logicalModel.getLogicalTables().add(logicalTable);
    logicalModel.setName(new LocalizedString(locale, "My Model"));
    logicalColumn1 = new LogicalColumn();
    logicalColumn1.setId("LC_CUSTOMERNAME");
    logicalColumn1.setPhysicalColumn(column);
    logicalColumn1.setAggregationType(AggregationType.COUNT);
    logicalColumn1.setLogicalTable(logicalTable);
    logicalColumn1.setDataType(DataType.STRING);
    logicalColumn2 = new LogicalColumn();
    logicalColumn2.setId("LC_CUSTOMERNUMBER");
    logicalColumn2.setAggregationType(AggregationType.COUNT);
    logicalColumn2.setPhysicalColumn(column);
    logicalColumn2.setLogicalTable(logicalTable);
    logicalColumn2.setDataType(DataType.NUMERIC);
    logicalTable.addLogicalColumn(logicalColumn1);
    logicalTable.addLogicalColumn(logicalColumn2);
    Domain thisDomain = new Domain();
    thisDomain.addPhysicalModel(model);
    thisDomain.addLogicalModel(logicalModel);
    return thisDomain;
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) SqlDataSource(org.pentaho.metadata.model.SqlDataSource) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalColumn(org.pentaho.metadata.model.SqlPhysicalColumn) LogicalTable(org.pentaho.metadata.model.LogicalTable) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) Domain(org.pentaho.metadata.model.Domain) SqlPhysicalTable(org.pentaho.metadata.model.SqlPhysicalTable)

Example 3 with SqlPhysicalTable

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

the class DatasourceModelTest method contructRelationalModel.

private GuiStateModel contructRelationalModel(GuiStateModel guiStateModel) {
    IDatabaseConnection connection = new DatabaseConnection();
    connection.setAccessType(DatabaseAccessType.NATIVE);
    // connection.setDriverClass("org.hsqldb.jdbcDriver");
    connection.setName("SampleData");
    connection.setPassword("password");
    // connection.setUrl("jdbc:hsqldb:file:target/test-classes/solution1/system/data/sampledata");
    connection.setUsername("pentaho_user");
    List<IDatabaseConnection> connectionList = new ArrayList<IDatabaseConnection>();
    connectionList.add(connection);
    guiStateModel.setConnections(connectionList);
    guiStateModel.setPreviewLimit("10");
    guiStateModel.validateRelational();
    LogicalColumn logColumn = new LogicalColumn();
    logColumn.setDataType(DataType.NUMERIC);
    List<AggregationType> aggTypeList = new ArrayList<AggregationType>();
    aggTypeList.add(AggregationType.AVERAGE);
    logColumn.setAggregationList(aggTypeList);
    logColumn.setName(new LocalizedString("En", "Column1"));
    BusinessData businessData = new BusinessData();
    List<List<String>> dataSample = new ArrayList<List<String>>();
    List<String> rowData = new ArrayList<String>();
    rowData.add("Data1");
    rowData.add("Data2");
    rowData.add("Data3");
    rowData.add("Data4");
    dataSample.add(rowData);
    String locale = LocaleHelper.getLocale().toString();
    SqlPhysicalModel model = new SqlPhysicalModel();
    SqlDataSource dataSource = new SqlDataSource();
    dataSource.setDatabaseName("SampleData");
    model.setDatasource(dataSource);
    SqlPhysicalTable table = new SqlPhysicalTable(model);
    model.getPhysicalTables().add(table);
    table.setTargetTableType(TargetTableType.INLINE_SQL);
    table.setTargetTable("select * from customers");
    SqlPhysicalColumn column = new SqlPhysicalColumn(table);
    column.setTargetColumn("customername");
    column.setName(new LocalizedString(locale, "Customer Name"));
    column.setDescription(new LocalizedString(locale, "Customer Name Desc"));
    column.setDataType(DataType.STRING);
    table.getPhysicalColumns().add(column);
    LogicalModel logicalModel = new LogicalModel();
    model.setId("MODEL");
    model.setName(new LocalizedString(locale, "My Model"));
    model.setDescription(new LocalizedString(locale, "A Description of the Model"));
    LogicalTable logicalTable = new LogicalTable();
    logicalTable.setPhysicalTable(table);
    logicalModel.getLogicalTables().add(logicalTable);
    LogicalColumn logicalColumn = new LogicalColumn();
    logicalColumn.setId("LC_CUSTOMERNAME");
    logicalColumn.setPhysicalColumn(column);
    logicalTable.addLogicalColumn(logicalColumn);
    Category mainCategory = new Category();
    mainCategory.setId("CATEGORY");
    mainCategory.setName(new LocalizedString(locale, "Category"));
    mainCategory.addLogicalColumn(logicalColumn);
    logicalModel.getCategories().add(mainCategory);
    Domain domain = new Domain();
    domain.addPhysicalModel(model);
    domain.addLogicalModel(logicalModel);
    List<LocaleType> localeTypeList = new ArrayList<LocaleType>();
    localeTypeList.add(new LocaleType("Code", "Locale Description"));
    domain.setLocales(localeTypeList);
    businessData.setData(dataSample);
    businessData.setDomain(domain);
    guiStateModel.setLocaleCode("en");
    guiStateModel.setLogicalModels(domain.getLogicalModels());
    guiStateModel.validateRelational();
    return guiStateModel;
}
Also used : BusinessData(org.pentaho.platform.dataaccess.datasource.beans.BusinessData) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category) ArrayList(java.util.ArrayList) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalColumn(org.pentaho.metadata.model.SqlPhysicalColumn) LogicalTable(org.pentaho.metadata.model.LogicalTable) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) SqlPhysicalTable(org.pentaho.metadata.model.SqlPhysicalTable) LogicalModel(org.pentaho.metadata.model.LogicalModel) SqlDataSource(org.pentaho.metadata.model.SqlDataSource) LocaleType(org.pentaho.metadata.model.concept.types.LocaleType) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) ArrayList(java.util.ArrayList) List(java.util.List) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Domain(org.pentaho.metadata.model.Domain)

Example 4 with SqlPhysicalTable

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

the class GuiStateModelTest method test.

@Test
public void test() {
    GuiStateModel guiStateModel = new GuiStateModel();
    Assert.assertEquals(0, guiStateModel.getConnections().size());
    Assert.assertEquals(false, guiStateModel.isRelationalValidated());
    IDatabaseConnection connection = new DatabaseConnection();
    connection.setAccessType(DatabaseAccessType.NATIVE);
    // connection.setDriverClass("org.hsqldb.jdbcDriver");
    connection.setName("SampleData");
    connection.setPassword("password");
    // connection.setUrl("jdbc:hsqldb:file:target/test-classes/solution1/system/data/sampledata");
    connection.setUsername("pentaho_user");
    List<IDatabaseConnection> connectionList = new ArrayList<IDatabaseConnection>();
    connectionList.add(connection);
    guiStateModel.setConnections(connectionList);
    guiStateModel.setPreviewLimit("10");
    guiStateModel.validateRelational();
    Assert.assertEquals(true, guiStateModel.isRelationalPreviewValidated());
    // Assert.assertEquals(false, relationalModel.isValidated());
    LogicalColumn logColumn = new LogicalColumn();
    logColumn.setDataType(DataType.NUMERIC);
    List<AggregationType> aggTypeList = new ArrayList<AggregationType>();
    aggTypeList.add(AggregationType.AVERAGE);
    logColumn.setAggregationList(aggTypeList);
    logColumn.setName(new LocalizedString("En", "Column1"));
    BusinessData businessData = new BusinessData();
    List<List<String>> dataSample = new ArrayList<List<String>>();
    List<String> rowData = new ArrayList<String>();
    rowData.add("Data1");
    rowData.add("Data2");
    rowData.add("Data3");
    rowData.add("Data4");
    dataSample.add(rowData);
    String locale = LocaleHelper.getLocale().toString();
    SqlPhysicalModel model = new SqlPhysicalModel();
    SqlDataSource dataSource = new SqlDataSource();
    dataSource.setDatabaseName("SampleData");
    model.setDatasource(dataSource);
    SqlPhysicalTable table = new SqlPhysicalTable(model);
    model.getPhysicalTables().add(table);
    table.setTargetTableType(TargetTableType.INLINE_SQL);
    table.setTargetTable("select * from customers");
    SqlPhysicalColumn column = new SqlPhysicalColumn(table);
    column.setTargetColumn("customername");
    column.setName(new LocalizedString(locale, "Customer Name"));
    column.setDescription(new LocalizedString(locale, "Customer Name Desc"));
    column.setDataType(DataType.STRING);
    table.getPhysicalColumns().add(column);
    LogicalModel logicalModel = new LogicalModel();
    model.setId("MODEL");
    model.setName(new LocalizedString(locale, "My Model"));
    model.setDescription(new LocalizedString(locale, "A Description of the Model"));
    LogicalTable logicalTable = new LogicalTable();
    logicalTable.setPhysicalTable(table);
    logicalModel.getLogicalTables().add(logicalTable);
    LogicalColumn logicalColumn = new LogicalColumn();
    logicalColumn.setId("LC_CUSTOMERNAME");
    logicalColumn.setPhysicalColumn(column);
    logicalTable.addLogicalColumn(logicalColumn);
    Category mainCategory = new Category();
    mainCategory.setId("CATEGORY");
    mainCategory.setName(new LocalizedString(locale, "Category"));
    mainCategory.addLogicalColumn(logicalColumn);
    logicalModel.getCategories().add(mainCategory);
    Domain domain = new Domain();
    domain.addPhysicalModel(model);
    domain.addLogicalModel(logicalModel);
    List<LocaleType> localeTypeList = new ArrayList<LocaleType>();
    localeTypeList.add(new LocaleType("Code", "Locale Description"));
    domain.setLocales(localeTypeList);
    businessData.setData(dataSample);
    businessData.setDomain(domain);
    guiStateModel.setLogicalModels(domain.getLogicalModels());
    guiStateModel.setLocaleCode("en");
    Assert.assertEquals(true, guiStateModel.isRelationalValidated());
}
Also used : BusinessData(org.pentaho.platform.dataaccess.datasource.beans.BusinessData) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category) ArrayList(java.util.ArrayList) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalColumn(org.pentaho.metadata.model.SqlPhysicalColumn) LogicalTable(org.pentaho.metadata.model.LogicalTable) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) SqlPhysicalTable(org.pentaho.metadata.model.SqlPhysicalTable) LogicalModel(org.pentaho.metadata.model.LogicalModel) SqlDataSource(org.pentaho.metadata.model.SqlDataSource) LocaleType(org.pentaho.metadata.model.concept.types.LocaleType) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) ArrayList(java.util.ArrayList) List(java.util.List) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Domain(org.pentaho.metadata.model.Domain) Test(org.junit.Test)

Example 5 with SqlPhysicalTable

use of org.pentaho.metadata.model.SqlPhysicalTable 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)

Aggregations

SqlPhysicalModel (org.pentaho.metadata.model.SqlPhysicalModel)7 SqlPhysicalTable (org.pentaho.metadata.model.SqlPhysicalTable)7 Domain (org.pentaho.metadata.model.Domain)6 LogicalModel (org.pentaho.metadata.model.LogicalModel)6 LogicalTable (org.pentaho.metadata.model.LogicalTable)5 SqlDataSource (org.pentaho.metadata.model.SqlDataSource)5 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)5 LogicalColumn (org.pentaho.metadata.model.LogicalColumn)4 SqlPhysicalColumn (org.pentaho.metadata.model.SqlPhysicalColumn)4 Category (org.pentaho.metadata.model.Category)3 LocaleType (org.pentaho.metadata.model.concept.types.LocaleType)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DatabaseConnection (org.pentaho.database.model.DatabaseConnection)2 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)2 AggregationType (org.pentaho.metadata.model.concept.types.AggregationType)2 BusinessData (org.pentaho.platform.dataaccess.datasource.beans.BusinessData)2 SQLException (java.sql.SQLException)1 TreeSet (java.util.TreeSet)1 Before (org.junit.Before)1