Search in sources :

Example 21 with LogicalColumn

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

the class JobGenerator method generateSqlJob.

public JobMeta generateSqlJob() throws KettleException {
    DatabaseMeta databaseMeta = findTargetDatabaseMeta();
    Database db = new Database(Spoon.loggingObject, databaseMeta);
    try {
        db.connect();
        JobMeta jobMeta = new JobMeta();
        jobMeta.setName("Create tables for '" + ConceptUtil.getName(domain, locale) + "'");
        jobMeta.setDescription(ConceptUtil.getDescription(domain, locale));
        // Let's not forget to add the database connection
        // 
        jobMeta.addDatabase(databaseMeta);
        Point location = new Point(GRAPH_LEFT, GRAPH_TOP);
        // Create a job entry
        // 
        JobEntryCopy startEntry = JobMeta.createStartEntry();
        startEntry.setLocation(location.x, location.y);
        startEntry.setDrawn();
        jobMeta.addJobEntry(startEntry);
        JobEntryCopy lastEntry = startEntry;
        nextLocation(location);
        // Create one SQL entry for all the physically unique dimensions and facts
        // We need to get a list of all known dimensions with physical table name.
        // 
        List<LogicalTable> tables = getUniqueLogicalTables();
        for (LogicalTable logicalTable : tables) {
            String phTable = ConceptUtil.getString(logicalTable, DefaultIDs.LOGICAL_TABLE_PHYSICAL_TABLE_NAME);
            String tableName = ConceptUtil.getName(logicalTable, locale);
            String tableDescription = ConceptUtil.getDescription(logicalTable, locale);
            TableType tableType = ConceptUtil.getTableType(logicalTable);
            DimensionType dimensionType = ConceptUtil.getDimensionType(logicalTable);
            boolean isFact = tableType == TableType.FACT;
            boolean isDimension = tableType == TableType.DIMENSION;
            boolean isJunk = isDimension && dimensionType == DimensionType.JUNK_DIMENSION;
            JobEntrySQL sqlEntry = new JobEntrySQL(phTable);
            sqlEntry.setDatabase(databaseMeta);
            // Get the SQL for this table...
            // 
            String schemaTable = databaseMeta.getQuotedSchemaTableCombination(null, phTable);
            String phKeyField = null;
            // The technical key is the first KEY field...
            // 
            LogicalColumn keyColumn = null;
            if (isDimension) {
                keyColumn = ConceptUtil.findLogicalColumn(logicalTable, AttributeType.TECHNICAL_KEY);
            }
            if (keyColumn != null) {
                phKeyField = ConceptUtil.getString(keyColumn, DefaultIDs.LOGICAL_COLUMN_PHYSICAL_COLUMN_NAME);
            }
            // Get all the fields for the logical table...
            // 
            RowMetaInterface fields = getRowForLogicalTable(databaseMeta, logicalTable);
            // Generate the required SQL to make this happen
            // 
            String sql = db.getCreateTableStatement(schemaTable, fields, phKeyField, databaseMeta.supportsAutoinc() && !isFact, null, true);
            // 
            if (keyColumn != null) {
                ValueMetaInterface keyValueMeta = getValueForLogicalColumn(databaseMeta, keyColumn);
                String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_" + phKeyField.toUpperCase());
                String indexSql = db.getCreateIndexStatement(schemaTable, indexName, new String[] { keyValueMeta.getName() }, true, false, true, true);
                sql += Const.CR + indexSql;
            }
            // 
            if (isFact) {
                List<LogicalColumn> fks = ConceptUtil.findLogicalColumns(logicalTable, AttributeType.TECHNICAL_KEY);
                for (LogicalColumn fk : fks) {
                    ValueMetaInterface keyValueMeta = getValueForLogicalColumn(databaseMeta, fk);
                    String phColumn = ConceptUtil.getString(fk, DefaultIDs.LOGICAL_COLUMN_PHYSICAL_COLUMN_NAME);
                    if (!Utils.isEmpty(phColumn)) {
                        String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_" + phColumn.toUpperCase());
                        String indexSql = db.getCreateIndexStatement(schemaTable, indexName, new String[] { keyValueMeta.getName() }, true, false, true, true);
                        sql += Const.CR + indexSql;
                    }
                }
            }
            // 
            if (isDimension) {
                List<LogicalColumn> naturalKeys = ConceptUtil.findLogicalColumns(logicalTable, AttributeType.NATURAL_KEY);
                if (!naturalKeys.isEmpty()) {
                    String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_LOOKUP");
                    String[] fieldNames = new String[naturalKeys.size()];
                    for (int i = 0; i < fieldNames.length; i++) {
                        ValueMetaInterface keyValueMeta = getValueForLogicalColumn(databaseMeta, naturalKeys.get(i));
                        fieldNames[i] = keyValueMeta.getName();
                    }
                    String indexSql = db.getCreateIndexStatement(schemaTable, indexName, fieldNames, false, false, false, true);
                    sql += Const.CR + indexSql;
                }
            }
            if (isJunk) {
                List<LogicalColumn> attributes = ConceptUtil.findLogicalColumns(logicalTable, AttributeType.ATTRIBUTE);
                if (!attributes.isEmpty()) {
                    String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_LOOKUP");
                    String[] fieldNames = new String[attributes.size()];
                    for (int i = 0; i < fieldNames.length; i++) {
                        ValueMetaInterface attrValueMeta = getValueForLogicalColumn(databaseMeta, attributes.get(i));
                        fieldNames[i] = attrValueMeta.getName();
                    }
                    String indexSql = db.getCreateIndexStatement(schemaTable, indexName, fieldNames, false, false, false, true);
                    sql += Const.CR + indexSql;
                }
            }
            // If it's
            sqlEntry.setSQL(sql);
            sqlEntry.setDescription("Generated based on logical table '" + tableName + "'" + Const.CR + Const.CR + Const.NVL(tableDescription, ""));
            JobEntryCopy sqlCopy = new JobEntryCopy(sqlEntry);
            sqlCopy.setLocation(location.x, location.y);
            sqlCopy.setDrawn();
            nextLocation(location);
            jobMeta.addJobEntry(sqlCopy);
            // Hook up with the previous job entry too...
            // 
            JobHopMeta jobHop = new JobHopMeta(lastEntry, sqlCopy);
            jobHop.setEnabled();
            jobHop.setConditional();
            jobHop.setEvaluation(true);
            if (lastEntry.isStart()) {
                jobHop.setUnconditional();
            }
            jobMeta.addJobHop(jobHop);
            lastEntry = sqlCopy;
        }
        return jobMeta;
    } catch (Exception e) {
        throw new KettleException("There was an error during the generation of the SQL job", e);
    } finally {
        if (db != null) {
            db.disconnect();
        }
    }
}
Also used : DimensionType(org.pentaho.di.starmodeler.DimensionType) KettleException(org.pentaho.di.core.exception.KettleException) JobMeta(org.pentaho.di.job.JobMeta) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) JobHopMeta(org.pentaho.di.job.JobHopMeta) TableType(org.pentaho.metadata.model.concept.types.TableType) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Point(org.pentaho.di.core.gui.Point) LogicalTable(org.pentaho.metadata.model.LogicalTable) JobEntrySQL(org.pentaho.di.job.entries.sql.JobEntrySQL) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) Point(org.pentaho.di.core.gui.Point) KettleException(org.pentaho.di.core.exception.KettleException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) JobEntryCopy(org.pentaho.di.job.entry.JobEntryCopy) Database(org.pentaho.di.core.database.Database)

Example 22 with LogicalColumn

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

the class SharedDimensionMetaStoreUtil method populateElementWithSharedDimension.

private static void populateElementWithSharedDimension(IMetaStore metaStore, LogicalTable sharedDimension, String locale, IMetaStoreElementType elementType, IMetaStoreElement element) throws MetaStoreException {
    element.setElementType(elementType);
    element.setName(sharedDimension.getName(locale));
    element.addChild(metaStore.newAttribute(Attribute.ID_SHARED_DIMENSION_DESCRIPTION.id, sharedDimension.getDescription(locale)));
    IMetaStoreAttribute columnsAttribute = metaStore.newAttribute(Attribute.ID_SHARED_DIMENSION_COLUMNS.id, null);
    element.addChild(columnsAttribute);
    for (LogicalColumn column : sharedDimension.getLogicalColumns()) {
        IMetaStoreAttribute columnAttribute = metaStore.newAttribute(Attribute.ID_SHARED_DIMENSION_COLUMN.id, null);
        columnsAttribute.addChild(columnAttribute);
        columnAttribute.addChild(metaStore.newAttribute(Attribute.ID_SHARED_DIMENSION_COLUMN_NAME.id, column.getName(locale)));
        columnAttribute.addChild(metaStore.newAttribute(Attribute.ID_SHARED_DIMENSION_COLUMN_DESCRIPTION.id, column.getDescription(locale)));
    }
}
Also used : LogicalColumn(org.pentaho.metadata.model.LogicalColumn) IMetaStoreAttribute(org.pentaho.metastore.api.IMetaStoreAttribute)

Example 23 with LogicalColumn

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

the class ConceptUtilTest method testFindLogicalColumn.

@Test
public void testFindLogicalColumn() throws Exception {
    final LogicalTable logicalTable = mock(LogicalTable.class);
    final AttributeType attribute = AttributeType.ATTRIBUTE;
    final LogicalColumn logicalColumn1 = mock(LogicalColumn.class);
    when(logicalColumn1.getProperty(DefaultIDs.LOGICAL_COLUMN_ATTRIBUTE_TYPE)).thenReturn(attribute.name());
    final LogicalColumn logicalColumn2 = mock(LogicalColumn.class);
    when(logicalColumn2.getProperty(DefaultIDs.LOGICAL_COLUMN_ATTRIBUTE_TYPE)).thenReturn(attribute.name());
    when(logicalTable.getLogicalColumns()).thenReturn(new LinkedList<LogicalColumn>() {

        {
            add(logicalColumn1);
            add(logicalColumn2);
        }
    });
    assertNull(ConceptUtil.findLogicalColumn(logicalTable, AttributeType.ATTRIBUTE_HISTORICAL));
    final LogicalColumn result = ConceptUtil.findLogicalColumn(logicalTable, attribute);
    assertEquals(logicalColumn1, result);
}
Also used : LogicalColumn(org.pentaho.metadata.model.LogicalColumn) LogicalTable(org.pentaho.metadata.model.LogicalTable) Test(org.junit.Test)

Example 24 with LogicalColumn

use of org.pentaho.metadata.model.LogicalColumn in project pentaho-platform by pentaho.

the class MetadataQueryComponentIT method getBasicDomain.

public Domain getBasicDomain() {
    SqlPhysicalModel model = new SqlPhysicalModel();
    SqlDataSource dataSource = new SqlDataSource();
    dataSource.setDatabaseName("SampleData");
    dataSource.setDialectType("HYPERSONIC");
    dataSource.setType(DataSourceType.JNDI);
    model.setDatasource(dataSource);
    SqlPhysicalTable table = new SqlPhysicalTable(model);
    table.setId("PT1");
    model.getPhysicalTables().add(table);
    table.setTargetTableType(TargetTableType.INLINE_SQL);
    table.setTargetTable("select distinct customername, salesrepemployeenumber from customers");
    SqlPhysicalColumn column = new SqlPhysicalColumn(table);
    column.setId("PC1");
    column.setTargetColumn("CUSTOMERNAME");
    column.setName(new LocalizedString("en_US", "Customer Name"));
    column.setDescription(new LocalizedString("en_US", "Customer Name Desc"));
    column.setDataType(DataType.STRING);
    table.getPhysicalColumns().add(column);
    SqlPhysicalColumn column2 = new SqlPhysicalColumn(table);
    column2.setId("PC2");
    column2.setTargetColumn("SALESREPEMPLOYEENUMBER");
    column2.setName(new LocalizedString("en_US", "Sales Rep"));
    column2.setDescription(new LocalizedString("en_US", "Sales Rep Employee Number"));
    column2.setDataType(DataType.NUMERIC);
    table.getPhysicalColumns().add(column2);
    LogicalModel logicalModel = new LogicalModel();
    logicalModel.setPhysicalModel(model);
    logicalModel.setId("MODEL");
    logicalModel.setName(new LocalizedString("en_US", "My Model"));
    logicalModel.setDescription(new LocalizedString("en_US", "A Description of the Model"));
    LogicalTable logicalTable = new LogicalTable();
    logicalTable.setId("LT");
    logicalTable.setPhysicalTable(table);
    logicalModel.getLogicalTables().add(logicalTable);
    LogicalColumn logicalColumn = new LogicalColumn();
    logicalColumn.setId("LC_CUSTOMERNAME");
    logicalColumn.setPhysicalColumn(column);
    logicalColumn.setLogicalTable(logicalTable);
    logicalTable.addLogicalColumn(logicalColumn);
    LogicalColumn logicalColumn2 = new LogicalColumn();
    logicalColumn2.setId("LC_SALESREP");
    logicalColumn2.setPhysicalColumn(column2);
    logicalColumn2.setLogicalTable(logicalTable);
    logicalTable.addLogicalColumn(logicalColumn2);
    Category mainCategory = new Category();
    mainCategory.setId("CATEGORY");
    mainCategory.setName(new LocalizedString("en_US", "Category"));
    mainCategory.addLogicalColumn(logicalColumn);
    mainCategory.addLogicalColumn(logicalColumn2);
    logicalModel.getCategories().add(mainCategory);
    Domain domain = new Domain();
    domain.setId("DOMAIN");
    domain.addPhysicalModel(model);
    domain.addLogicalModel(logicalModel);
    return domain;
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category) SqlDataSource(org.pentaho.metadata.model.SqlDataSource) 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 25 with LogicalColumn

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

Aggregations

LogicalColumn (org.pentaho.metadata.model.LogicalColumn)30 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)16 LogicalTable (org.pentaho.metadata.model.LogicalTable)12 ArrayList (java.util.ArrayList)10 LogicalModel (org.pentaho.metadata.model.LogicalModel)9 Domain (org.pentaho.metadata.model.Domain)8 Category (org.pentaho.metadata.model.Category)7 Test (org.junit.Test)5 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)4 SqlDataSource (org.pentaho.metadata.model.SqlDataSource)4 SqlPhysicalColumn (org.pentaho.metadata.model.SqlPhysicalColumn)4 SqlPhysicalModel (org.pentaho.metadata.model.SqlPhysicalModel)4 SqlPhysicalTable (org.pentaho.metadata.model.SqlPhysicalTable)4 AggregationType (org.pentaho.metadata.model.concept.types.AggregationType)4 Point (org.pentaho.di.core.gui.Point)3 StepMeta (org.pentaho.di.trans.step.StepMeta)3 List (java.util.List)2 Document (org.dom4j.Document)2 Element (org.dom4j.Element)2 TableItem (org.eclipse.swt.widgets.TableItem)2