Search in sources :

Example 1 with DataType

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

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

the class ConditionTest method testGetConditionWithParametersForAllDataTypes.

@Test
public void testGetConditionWithParametersForAllDataTypes() {
    Operator[] operatorsType1Array = { Operator.GREATER_THAN, Operator.LESS_THAN, Operator.EQUAL, Operator.GREATOR_OR_EQUAL, Operator.LESS_OR_EQUAL };
    for (Operator operator : operatorsType1Array) {
        when(icondition.getOperator()).thenReturn(operator.toString());
        for (DataType dataType : DataType.values()) {
            if (dataType.getName().equals(DataType.STRING.getName()) && operator.equals(Operator.EQUAL)) {
                // Exception for DataType STRING
                Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] = [param:" + paramName + "]"));
            } else if (dataType.getName().equals(DataType.DATE.getName())) {
                Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] " + operator.toString() + "DATEVALUE([param:" + values[0] + "])"));
            } else {
                Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] " + operator.toString() + "[param:" + paramName + "]"));
            }
        }
    }
    for (DataType dataType : DataType.values()) {
        when(icondition.getOperator()).thenReturn(Operator.EXACTLY_MATCHES.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] = \"DATEVALUE([param:" + values[0] + "])\""));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] = " + "[param:" + paramName + "]"));
        }
        when(icondition.getOperator()).thenReturn(Operator.CONTAINS.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("CONTAINS([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\")"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("CONTAINS([" + categoryName + "." + columnName + "];" + "[param:" + paramName + "])"));
        }
        when(icondition.getOperator()).thenReturn(Operator.DOES_NOT_CONTAIN.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("NOT(CONTAINS([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\"))"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("NOT(CONTAINS([" + categoryName + "." + columnName + "];" + "[param:" + paramName + "]))"));
        }
        when(icondition.getOperator()).thenReturn(Operator.BEGINS_WITH.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("BEGINSWITH([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\")"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("BEGINSWITH([" + categoryName + "." + columnName + "];" + "[param:" + paramName + "])"));
        }
        when(icondition.getOperator()).thenReturn(Operator.ENDS_WITH.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("ENDSWITH([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\")"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("ENDSWITH([" + categoryName + "." + columnName + "];" + "[param:" + paramName + "])"));
        }
        when(icondition.getOperator()).thenReturn(Operator.IS_NULL.toString());
        Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("ISNA([" + categoryName + "." + columnName + "])"));
        when(icondition.getOperator()).thenReturn(Operator.IS_NOT_NULL.toString());
        Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("NOT(ISNA([" + categoryName + "." + columnName + "]))"));
    }
}
Also used : Operator(org.pentaho.platform.dataaccess.metadata.model.Operator) DataType(org.pentaho.metadata.model.concept.types.DataType) Test(org.junit.Test)

Example 3 with DataType

use of org.pentaho.metadata.model.concept.types.DataType in project pentaho-kettle by pentaho.

the class DimensionTableDialog method addLogicalColumnToAttributesList.

private void addLogicalColumnToAttributesList(LogicalColumn column) {
    TableItem item = new TableItem(wAttributes.table, SWT.NONE);
    // name, description, physical column name, data type, length, precision, source db, source table, source column, conversion remarks
    // 
    int col = 1;
    item.setText(col++, Const.NVL(ConceptUtil.getName(column, locale), ""));
    item.setText(col++, Const.NVL(ConceptUtil.getDescription(column, locale), ""));
    item.setText(col++, ConceptUtil.getAttributeType(column).name());
    item.setText(col++, Const.NVL((String) column.getProperty(DefaultIDs.LOGICAL_COLUMN_PHYSICAL_COLUMN_NAME), ""));
    DataType dataType = (DataType) column.getProperty(DefaultPropertyID.DATA_TYPE.getId());
    item.setText(col++, dataType == null ? "" : dataType.name());
    item.setText(col++, Const.NVL(ConceptUtil.getString(column, DefaultIDs.LOGICAL_COLUMN_LENGTH), ""));
    item.setText(col++, Const.NVL(ConceptUtil.getString(column, DefaultIDs.LOGICAL_COLUMN_PRECISION), ""));
    item.setText(col++, Const.NVL(ConceptUtil.getString(column, DefaultIDs.LOGICAL_COLUMN_SOURCE_DB), ""));
    item.setText(col++, Const.NVL(ConceptUtil.getString(column, DefaultIDs.LOGICAL_COLUMN_SOURCE_TABLE), ""));
    item.setText(col++, Const.NVL(ConceptUtil.getString(column, DefaultIDs.LOGICAL_COLUMN_SOURCE_COLUMN), ""));
    item.setText(col++, Const.NVL(ConceptUtil.getString(column, DefaultIDs.LOGICAL_COLUMN_CONVERSION_REMARKS), ""));
}
Also used : TableItem(org.eclipse.swt.widgets.TableItem) DataType(org.pentaho.metadata.model.concept.types.DataType) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString)

Example 4 with DataType

use of org.pentaho.metadata.model.concept.types.DataType in project pentaho-platform by pentaho.

the class PMDUIComponent method addColumn.

@SuppressWarnings("deprecation")
public void addColumn(final LogicalColumn column, final Element tableNode, final String locale) {
    // $NON-NLS-1$
    Element columnNode = tableNode.addElement("column");
    if (column.getId() != null) {
        // $NON-NLS-1$
        columnNode.addElement("column_id").setText(column.getId());
    }
    if (column.getName(locale) != null) {
        // $NON-NLS-1$
        columnNode.addElement("column_name").setText(column.getName(locale));
    }
    if (column.getDescription(locale) != null) {
        // $NON-NLS-1$
        columnNode.addElement("column_description").setText(column.getDescription(locale));
    }
    if (column.getFieldType() != null) {
        // TODO this should take a locale
        String desc = column.getFieldType().getDescription();
        desc = org.pentaho.pms.messages.Messages.getString(desc);
        // $NON-NLS-1$
        columnNode.addElement("column_field_type").setText(desc);
    }
    DataType dataType = column.getDataType();
    if (dataType != null) {
        // $NON-NLS-1$
        columnNode.addElement("column_type").setText(dataType.getName());
    }
    if (column.getProperty("lookup") != null) {
        // $NON-NLS-1$
        // $NON-NLS-1$ //$NON-NLS-2$
        columnNode.addElement("column_lookup").setText("true");
    }
}
Also used : Element(org.dom4j.Element) DataType(org.pentaho.metadata.model.concept.types.DataType) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString)

Example 5 with DataType

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

the class MetadataServiceUtil method createCdaJson.

/**
 * Returns a CDA JSON representation of a query result set
 *
 * @param resultSet
 * @return
 * @throws JSONException
 */
public String createCdaJson(final IPentahoResultSet resultSet, String locale) throws JSONException {
    if (resultSet == null) {
        return null;
    }
    JSONObject json = new JSONObject();
    // Generate the metadata
    final JSONArray metadataArray = new JSONArray();
    final int columnCount = resultSet.getColumnCount();
    final int rowCount = resultSet.getRowCount();
    for (int i = 0; i < columnCount; i++) {
        JSONObject info = new JSONObject();
        // $NON-NLS-1$
        info.put("colIndex", i);
        // $NON-NLS-1$
        info.put("colName", resultSet.getMetaData().getColumnHeaders()[0][i]);
        DataType type = (DataType) resultSet.getMetaData().getAttribute(0, i, IPhysicalColumn.DATATYPE_PROPERTY);
        // $NON-NLS-1$
        info.put("colType", type.getName().toUpperCase());
        LocalizedString name = (LocalizedString) resultSet.getMetaData().getAttribute(0, i, Concept.NAME_PROPERTY);
        if (name != null && locale != null) {
            // $NON-NLS-1$
            info.put("colLabel", name.getString(locale));
        }
        metadataArray.put(info);
    }
    // $NON-NLS-1$
    json.put("metadata", metadataArray);
    // add the rows of data
    final JSONArray valuesArray = new JSONArray();
    for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) {
        final JSONArray rowArray = new JSONArray();
        valuesArray.put(rowArray);
        for (int colIdx = 0; colIdx < columnCount; colIdx++) {
            rowArray.put(resultSet.getValueAt(rowIdx, colIdx));
        }
    }
    // $NON-NLS-1$
    json.put("resultset", valuesArray);
    return json.toString();
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) DataType(org.pentaho.metadata.model.concept.types.DataType) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) Constraint(org.pentaho.metadata.query.model.Constraint)

Aggregations

DataType (org.pentaho.metadata.model.concept.types.DataType)12 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)7 ArrayList (java.util.ArrayList)3 TableItem (org.eclipse.swt.widgets.TableItem)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 IPhysicalColumn (org.pentaho.metadata.model.IPhysicalColumn)2 LogicalColumn (org.pentaho.metadata.model.LogicalColumn)2 AggregationType (org.pentaho.metadata.model.concept.types.AggregationType)2 FieldType (org.pentaho.metadata.model.concept.types.FieldType)2 Constraint (org.pentaho.metadata.query.model.Constraint)2 Operator (org.pentaho.platform.dataaccess.metadata.model.Operator)2 Column (org.pentaho.platform.dataaccess.metadata.model.impl.Column)2 InputStream (java.io.InputStream)1 List (java.util.List)1 Vector (java.util.Vector)1 Element (org.dom4j.Element)1 JSONArray (org.json.JSONArray)1 Point (org.pentaho.di.core.gui.Point)1 ValueMeta (org.pentaho.di.core.row.ValueMeta)1