Search in sources :

Example 1 with AggregationType

use of org.pentaho.metadata.model.concept.types.AggregationType in project data-access by pentaho.

the class MetadataServiceUtil method convertQuery.

/**
 * Converts a thin query model into a full query
 *
 * @param src
 * @return
 */
public org.pentaho.metadata.query.model.Query convertQuery(Query src) {
    IMetadataDomainRepository domainRepository = getMetadataRepository();
    Domain fullDomain = domainRepository.getDomain(src.getDomainName());
    LogicalModel logicalModel = fullDomain.findLogicalModel(src.getModelId());
    // create a new full query object
    org.pentaho.metadata.query.model.Query dest = new org.pentaho.metadata.query.model.Query(fullDomain, logicalModel);
    // now add the selections
    List<Selection> selections = dest.getSelections();
    for (Column column : src.getColumns()) {
        // get the objects needed for the selection
        LogicalColumn logicalColumn = logicalModel.findLogicalColumn(column.getId());
        org.pentaho.metadata.model.Category category = getCategory(column.getId(), logicalModel);
        AggregationType aggregationType = AggregationType.valueOf(column.getSelectedAggType());
        // create a selection and add it to the list
        Selection selection = new Selection(category, logicalColumn, aggregationType);
        selections.add(selection);
    }
    // now add the filters
    List<Constraint> constraints = dest.getConstraints();
    for (Condition condition : src.getConditions()) {
        org.pentaho.metadata.query.model.CombinationType combinationType = CombinationType.valueOf(condition.getCombinationType());
        LogicalColumn logicalColumn = logicalModel.findLogicalColumn(condition.getColumn());
        String paramName = null;
        for (Parameter parameter : src.getParameters()) {
            if (parameter.getColumn().equals(condition.getColumn())) {
                paramName = parameter.getName() == null ? parameter.getColumn() : parameter.getName();
            }
        }
        // condition.setParameterized(parameterized);
        String formula = condition.getCondition(logicalColumn.getDataType().name(), paramName);
        Constraint constraint = new Constraint(combinationType, formula);
        constraints.add(constraint);
    }
    // now set the disable distinct option
    if (src.getDisableDistinct() != null) {
        dest.setDisableDistinct(src.getDisableDistinct());
    }
    // now add the sorting information
    List<org.pentaho.metadata.query.model.Order> orders = dest.getOrders();
    for (Order order : src.getOrders()) {
        // find the selection
        for (Selection selection : selections) {
            if (selection.getLogicalColumn().getId().equals(order.getColumn())) {
                Type type = Type.valueOf(order.getOrderType());
                org.pentaho.metadata.query.model.Order fullOrder = new org.pentaho.metadata.query.model.Order(selection, type);
                orders.add(fullOrder);
            }
        }
    }
    // now add the parameter information
    List<org.pentaho.metadata.query.model.Parameter> parameters = dest.getParameters();
    for (Parameter parameter : src.getParameters()) {
        // find the column for this parameter
        LogicalColumn logicalColumn = logicalModel.findLogicalColumn(parameter.getColumn());
        DataType type = logicalColumn.getDataType();
        String[] value = parameter.getValue();
        org.pentaho.metadata.query.model.Parameter fullParam = new org.pentaho.metadata.query.model.Parameter(parameter.getColumn(), type, value[0]);
        parameters.add(fullParam);
    }
    return dest;
}
Also used : LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Query(org.pentaho.platform.dataaccess.metadata.model.impl.Query) Constraint(org.pentaho.metadata.query.model.Constraint) Selection(org.pentaho.metadata.query.model.Selection) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) LogicalModel(org.pentaho.metadata.model.LogicalModel) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) IPhysicalColumn(org.pentaho.metadata.model.IPhysicalColumn) Column(org.pentaho.platform.dataaccess.metadata.model.impl.Column) DataType(org.pentaho.metadata.model.concept.types.DataType) Condition(org.pentaho.platform.dataaccess.metadata.model.impl.Condition) Order(org.pentaho.platform.dataaccess.metadata.model.impl.Order) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) DataType(org.pentaho.metadata.model.concept.types.DataType) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) FieldType(org.pentaho.metadata.model.concept.types.FieldType) Type(org.pentaho.metadata.query.model.Order.Type) CombinationType(org.pentaho.metadata.query.model.CombinationType) Parameter(org.pentaho.platform.dataaccess.metadata.model.impl.Parameter) Domain(org.pentaho.metadata.model.Domain) CombinationType(org.pentaho.metadata.query.model.CombinationType)

Example 2 with AggregationType

use of org.pentaho.metadata.model.concept.types.AggregationType in project data-access by pentaho.

the class MetadataServiceTest method buildQuery.

private Query buildQuery() {
    // Build the query Columns
    AggregationType defaultAggType = AggregationType.NONE;
    Column[] columns = new Column[1];
    Column column = mock(Column.class);
    when(column.getId()).thenReturn(COLUMN_ID);
    when(column.getName()).thenReturn(COLUMN_NAME);
    when(column.getSelectedAggType()).thenReturn(defaultAggType.name());
    columns[0] = column;
    // Build the query Conditions
    CombinationType combinationType = CombinationType.AND;
    Condition[] conditions = new Condition[1];
    Condition condition = mock(Condition.class);
    when(condition.getCombinationType()).thenReturn(combinationType.name());
    when(condition.getColumn()).thenReturn(COLUMN_NAME);
    when(condition.getCondition(anyString(), anyString())).thenReturn("[" + CATEGORY_NAME + "." + COLUMN_NAME + "] = " + VALUE);
    conditions[0] = condition;
    // Build the query Parameters
    String[] values = { VALUE };
    Parameter[] parameters = new Parameter[1];
    Parameter parameter = mock(Parameter.class);
    when(parameter.getColumn()).thenReturn(COLUMN_NAME);
    when(parameter.getValue()).thenReturn(values);
    parameters[0] = parameter;
    // Build the query Order
    Order[] orders = new Order[1];
    Order order = mock(Order.class);
    orders[0] = order;
    // Build the Query
    Query query = mock(Query.class);
    when(query.getDomainName()).thenReturn(DOMAIN_ID);
    when(query.getModelId()).thenReturn(LOGICAL_MODEL_ID);
    when(query.getColumns()).thenReturn(columns);
    when(query.getConditions()).thenReturn(conditions);
    when(query.getParameters()).thenReturn(parameters);
    when(query.getOrders()).thenReturn(orders);
    return query;
}
Also used : Condition(org.pentaho.platform.dataaccess.metadata.model.impl.Condition) Order(org.pentaho.platform.dataaccess.metadata.model.impl.Order) Query(org.pentaho.platform.dataaccess.metadata.model.impl.Query) CombinationType(org.pentaho.metadata.query.model.CombinationType) Matchers.anyString(org.mockito.Matchers.anyString) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) IPhysicalColumn(org.pentaho.metadata.model.IPhysicalColumn) Column(org.pentaho.platform.dataaccess.metadata.model.impl.Column) Parameter(org.pentaho.platform.dataaccess.metadata.model.impl.Parameter)

Example 3 with AggregationType

use of org.pentaho.metadata.model.concept.types.AggregationType 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 AggregationType

use of org.pentaho.metadata.model.concept.types.AggregationType 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 AggregationType

use of org.pentaho.metadata.model.concept.types.AggregationType in project data-access by pentaho.

the class MetadataServiceUtil method createColumn.

/**
 * Creates a lightweight, serializable Column object from a logical model column
 *
 * @param m
 * @param c
 * @return
 */
private Column createColumn(LogicalModel m, LogicalColumn c, org.pentaho.metadata.model.Category category) {
    Column col = new Column();
    col.setName(c.getName(getLocale()));
    col.setId(c.getId());
    if (c.getFieldType() != null) {
        col.setFieldType(c.getFieldType().name());
    } else {
        // $NON-NLS-1$
        col.setFieldType("UNKNOWN");
    }
    col.setType(c.getDataType().getName().toUpperCase());
    col.setCategory(category.getId());
    // set the aggregation fields for the column
    List<AggregationType> possibleAggs = c.getAggregationList();
    List<String> aggTypes = new ArrayList<String>();
    if (possibleAggs != null) {
        for (AggregationType agg : possibleAggs) {
            aggTypes.add(agg.name());
        }
    }
    // There might be a default agg, but no agg list. If so, add it to the list.
    AggregationType defaultAggType = AggregationType.NONE;
    if (c.getAggregationType() != null) {
        defaultAggType = c.getAggregationType();
    }
    if (!aggTypes.contains(defaultAggType)) {
        aggTypes.add(defaultAggType.name());
    }
    col.setAggTypes(aggTypes);
    col.setDefaultAggType(defaultAggType.name());
    col.setSelectedAggType(defaultAggType.name());
    // set the alignment
    DataType dataType = c.getDataType();
    FieldType fieldType = c.getFieldType();
    // $NON-NLS-1$
    Object obj = c.getProperty("alignment");
    if (obj instanceof Alignment) {
        if (obj == Alignment.LEFT) {
            col.setHorizontalAlignment(Alignment.LEFT.toString());
        } else if (obj == Alignment.RIGHT) {
            col.setHorizontalAlignment(Alignment.RIGHT.toString());
        } else if (obj == Alignment.CENTERED) {
            col.setHorizontalAlignment(Alignment.CENTERED.toString());
        }
    } else if (fieldType == FieldType.FACT) {
        col.setHorizontalAlignment(Alignment.RIGHT.toString());
    } else if (fieldType == FieldType.OTHER && dataType == DataType.NUMERIC) {
        col.setHorizontalAlignment(Alignment.RIGHT.toString());
    } else {
        col.setHorizontalAlignment(Alignment.LEFT.toString());
    }
    // set the format mask
    // $NON-NLS-1$
    obj = c.getProperty("mask");
    if (obj != null) {
        col.setFormatMask((String) obj);
    }
    return col;
}
Also used : Alignment(org.pentaho.metadata.model.concept.types.Alignment) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) IPhysicalColumn(org.pentaho.metadata.model.IPhysicalColumn) Column(org.pentaho.platform.dataaccess.metadata.model.impl.Column) ArrayList(java.util.ArrayList) DataType(org.pentaho.metadata.model.concept.types.DataType) JSONObject(org.json.JSONObject) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) FieldType(org.pentaho.metadata.model.concept.types.FieldType)

Aggregations

LogicalColumn (org.pentaho.metadata.model.LogicalColumn)5 AggregationType (org.pentaho.metadata.model.concept.types.AggregationType)5 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)5 ArrayList (java.util.ArrayList)3 Domain (org.pentaho.metadata.model.Domain)3 IPhysicalColumn (org.pentaho.metadata.model.IPhysicalColumn)3 LogicalModel (org.pentaho.metadata.model.LogicalModel)3 Column (org.pentaho.platform.dataaccess.metadata.model.impl.Column)3 List (java.util.List)2 DatabaseConnection (org.pentaho.database.model.DatabaseConnection)2 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)2 Category (org.pentaho.metadata.model.Category)2 LogicalTable (org.pentaho.metadata.model.LogicalTable)2 SqlDataSource (org.pentaho.metadata.model.SqlDataSource)2 SqlPhysicalColumn (org.pentaho.metadata.model.SqlPhysicalColumn)2 SqlPhysicalModel (org.pentaho.metadata.model.SqlPhysicalModel)2 SqlPhysicalTable (org.pentaho.metadata.model.SqlPhysicalTable)2 DataType (org.pentaho.metadata.model.concept.types.DataType)2 FieldType (org.pentaho.metadata.model.concept.types.FieldType)2 LocaleType (org.pentaho.metadata.model.concept.types.LocaleType)2