Search in sources :

Example 6 with DataType

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

the class JobGenerator method getValueForLogicalColumn.

private ValueMetaInterface getValueForLogicalColumn(DatabaseMeta databaseMeta, LogicalColumn column) {
    String columnName = ConceptUtil.getName(column, locale);
    String phColumnName = ConceptUtil.getString(column, DefaultIDs.LOGICAL_COLUMN_PHYSICAL_COLUMN_NAME);
    DataType columnType = column.getDataType();
    String lengthString = ConceptUtil.getString(column, DefaultIDs.LOGICAL_COLUMN_LENGTH);
    int length = Const.toInt(lengthString, -1);
    String precisionString = ConceptUtil.getString(column, DefaultIDs.LOGICAL_COLUMN_PRECISION);
    int precision = Const.toInt(precisionString, -1);
    int type = ValueMetaInterface.TYPE_STRING;
    switch(columnType) {
        case UNKNOWN:
        case URL:
        case STRING:
            precision = -1;
            break;
        case IMAGE:
        case BINARY:
            type = ValueMetaInterface.TYPE_BINARY;
            precision = -1;
            break;
        case BOOLEAN:
            type = ValueMetaInterface.TYPE_BOOLEAN;
            length = -1;
            precision = -1;
            break;
        case DATE:
            type = ValueMetaInterface.TYPE_DATE;
            length = -1;
            precision = -1;
            break;
        case NUMERIC:
            if (precision <= 0 && length < 15) {
                type = ValueMetaInterface.TYPE_INTEGER;
            } else {
                if (length >= 15) {
                    type = ValueMetaInterface.TYPE_BIGNUMBER;
                } else {
                    type = ValueMetaInterface.TYPE_NUMBER;
                }
            }
            break;
        default:
            break;
    }
    ValueMetaInterface value = new ValueMeta(databaseMeta.quoteField(Const.NVL(phColumnName, columnName)), type);
    value.setLength(length, precision);
    return value;
}
Also used : DataType(org.pentaho.metadata.model.concept.types.DataType) ValueMeta(org.pentaho.di.core.row.ValueMeta) Point(org.pentaho.di.core.gui.Point) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 7 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 8 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)

Example 9 with DataType

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

Example 10 with DataType

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

the class ConditionTest method testGetConditionWithoutParametersForAllDataTypes.

@Test
public void testGetConditionWithoutParametersForAllDataTypes() {
    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())) {
                // Exception for DataType STRING
                if (operator.equals(Operator.EQUAL)) {
                    Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("[" + categoryName + "." + columnName + "] = \"" + values[0] + "\""));
                } else {
                    Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("[" + categoryName + "." + columnName + "] " + operator.toString() + "\"" + values[0] + "\""));
                }
            } else if (dataType.getName().equals(DataType.DATE.getName())) {
                Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("[" + categoryName + "." + columnName + "] " + operator.toString() + "DATEVALUE(\"" + values[0] + "\")"));
            } else {
                Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("[" + categoryName + "." + columnName + "] " + operator.toString() + values[0]));
            }
        }
    }
    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(), null), is("[" + categoryName + "." + columnName + "] = \"" + values[0] + "\""));
        }
        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(), null), is("CONTAINS([" + categoryName + "." + columnName + "];\"" + values[0] + "\")"));
        }
        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(), null), is("NOT(CONTAINS([" + categoryName + "." + columnName + "];\"" + values[0] + "\"))"));
        }
        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(), null), is("BEGINSWITH([" + categoryName + "." + columnName + "];\"" + values[0] + "\")"));
        }
        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(), null), is("ENDSWITH([" + categoryName + "." + columnName + "];\"" + values[0] + "\")"));
        }
        when(icondition.getOperator()).thenReturn(Operator.IS_NULL.toString());
        Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("ISNA([" + categoryName + "." + columnName + "])"));
        when(icondition.getOperator()).thenReturn(Operator.IS_NOT_NULL.toString());
        Assert.assertThat(icondition.getCondition(dataType.getName(), null), 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)

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