Search in sources :

Example 1 with MappingValueRename

use of org.pentaho.di.trans.steps.mapping.MappingValueRename in project pentaho-metaverse by pentaho.

the class BaseMappingAnalyzer method processOutputMappings.

/**
 * Processes output mappings and returns a map of mapped fields ( parent source > sub-trans target ).
 */
private Map<String, String> processOutputMappings(final TransMeta subTransMeta, final Vertex stepVertex, final List<MappingIODefinition> mappings, final List<String> verboseProps) {
    final Map<String, String> mappedOutputFields = new HashMap();
    int mappingIdx = 1;
    for (final MappingIODefinition mapping : mappings) {
        final String mappingKey = "output [" + mappingIdx + "]";
        verboseProps.add(mappingKey);
        String sourceStep = getSourceStepName(subTransMeta, mapping, true, false);
        String targetStep = getTargetStepName(subTransMeta, mapping, true, false);
        final StringBuilder mappingStr = new StringBuilder();
        if (sourceStep != null && targetStep != null) {
            mappingStr.append(sourceStep).append(" > ").append(targetStep);
        }
        // main path?
        if (!mapping.isMainDataPath()) {
            final String descriptionKey = mappingKey + " description";
            verboseProps.add(descriptionKey);
            setPropertySafely(stepVertex, descriptionKey, mapping.getDescription());
        }
        setCommonProps(stepVertex, mappingKey, mapping, mappingStr, verboseProps);
        for (final MappingValueRename rename : mapping.getValueRenames()) {
            mappedOutputFields.put(rename.getSourceValueName(), rename.getTargetValueName());
        }
        mappingIdx++;
    }
    return mappedOutputFields;
}
Also used : MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) HashMap(java.util.HashMap) MappingIODefinition(org.pentaho.di.trans.steps.mapping.MappingIODefinition)

Example 2 with MappingValueRename

use of org.pentaho.di.trans.steps.mapping.MappingValueRename in project pentaho-metaverse by pentaho.

the class BaseMappingAnalyzer method setCommonProps.

public void setCommonProps(final Vertex stepVertex, final String mappingKey, final MappingIODefinition mapping, final StringBuilder mappingStr, final List<String> verboseProps) {
    setPropertySafely(stepVertex, mappingKey, mappingStr.toString());
    final String updateFieldnamesKey = mappingKey + " update field names";
    verboseProps.add(updateFieldnamesKey);
    setPropertySafely(stepVertex, updateFieldnamesKey, Boolean.toString(mapping.isRenamingOnOutput()));
    int renameIdx = 1;
    for (final MappingValueRename valueRename : mapping.getValueRenames()) {
        final String renameKey = mappingKey + " rename [" + renameIdx++ + "]";
        verboseProps.add(renameKey);
        setPropertySafely(stepVertex, renameKey, valueRename.getSourceValueName() + " > " + valueRename.getTargetValueName());
    }
}
Also used : MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename)

Example 3 with MappingValueRename

use of org.pentaho.di.trans.steps.mapping.MappingValueRename in project pentaho-metaverse by pentaho.

the class BaseMappingAnalyzer method processInputMappings.

/**
 * Processes input mappings and returns a map of mapped fields ( parent source > sub-trans target ).
 */
private Map<String, String> processInputMappings(final TransMeta subTransMeta, final Vertex stepVertex, final List<MappingIODefinition> inputMappings, final List<String> verboseProps) {
    final Map<String, String> mappedInputFields = new HashMap();
    int mappingIdx = 1;
    for (final MappingIODefinition mapping : inputMappings) {
        final String mappingKey = "input [" + mappingIdx + "]";
        verboseProps.add(mappingKey);
        String sourceStep = getSourceStepName(subTransMeta, mapping, true, true);
        String targetStep = getTargetStepName(subTransMeta, mapping, true, true);
        final StringBuilder mappingStr = new StringBuilder();
        if (sourceStep != null && targetStep != null) {
            mappingStr.append(sourceStep).append(" > ").append(targetStep);
        }
        // main path?
        if (!mapping.isMainDataPath()) {
            final String descriptionKey = mappingKey + " description";
            verboseProps.add(descriptionKey);
            setPropertySafely(stepVertex, descriptionKey, mapping.getDescription());
        }
        setCommonProps(stepVertex, mappingKey, mapping, mappingStr, verboseProps);
        for (final MappingValueRename rename : mapping.getValueRenames()) {
            mappedInputFields.put(rename.getSourceValueName(), rename.getTargetValueName());
        }
        mappingIdx++;
    }
    return mappedInputFields;
}
Also used : MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) HashMap(java.util.HashMap) MappingIODefinition(org.pentaho.di.trans.steps.mapping.MappingIODefinition)

Example 4 with MappingValueRename

use of org.pentaho.di.trans.steps.mapping.MappingValueRename in project pentaho-kettle by pentaho.

the class MappingInputMeta method getFields.

public void getFields(RowMetaInterface row, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
    // 
    if (inputRowMeta != null && !inputRowMeta.isEmpty()) {
        // First rename any fields...
        if (valueRenames != null) {
            for (MappingValueRename valueRename : valueRenames) {
                ValueMetaInterface valueMeta = inputRowMeta.searchValueMeta(valueRename.getSourceValueName());
                if (valueMeta == null) {
                    // ok, let's search once again, now using target name
                    valueMeta = inputRowMeta.searchValueMeta(valueRename.getTargetValueName());
                    if (valueMeta == null) {
                        throw new KettleStepException(BaseMessages.getString(PKG, "MappingInput.Exception.UnableToFindMappedValue", valueRename.getSourceValueName()));
                    }
                } else {
                    valueMeta.setName(valueRename.getTargetValueName());
                }
            }
        }
        if (selectingAndSortingUnspecifiedFields) {
            // Select the specified fields from the input, re-order everything and put the other fields at the back,
            // sorted...
            // 
            RowMetaInterface newRow = new RowMeta();
            for (int i = 0; i < fieldName.length; i++) {
                int index = inputRowMeta.indexOfValue(fieldName[i]);
                if (index < 0) {
                    throw new KettleStepException(BaseMessages.getString(PKG, "MappingInputMeta.Exception.UnknownField", fieldName[i]));
                }
                newRow.addValueMeta(inputRowMeta.getValueMeta(index));
            }
            // Now get the unspecified fields.
            // Sort the fields
            // Add them after the specified fields...
            // 
            List<String> extra = new ArrayList<String>();
            for (int i = 0; i < inputRowMeta.size(); i++) {
                String fieldName = inputRowMeta.getValueMeta(i).getName();
                if (newRow.indexOfValue(fieldName) < 0) {
                    extra.add(fieldName);
                }
            }
            Collections.sort(extra);
            for (String fieldName : extra) {
                ValueMetaInterface extraValue = inputRowMeta.searchValueMeta(fieldName);
                newRow.addValueMeta(extraValue);
            }
            // now merge the new row...
            // This is basically the input row meta data with the fields re-ordered.
            // 
            row.mergeRowMeta(newRow);
        } else {
            row.mergeRowMeta(inputRowMeta);
            // 
            if (!row.isEmpty()) {
                for (int i = 0; i < fieldName.length; i++) {
                    if (row.indexOfValue(fieldName[i]) < 0) {
                        throw new KettleStepException(BaseMessages.getString(PKG, "MappingInputMeta.Exception.UnknownField", fieldName[i]));
                    }
                }
            }
        }
    } else {
        if (row.isEmpty()) {
            // We'll have to work with the statically provided information
            for (int i = 0; i < fieldName.length; i++) {
                if (!Utils.isEmpty(fieldName[i])) {
                    int valueType = fieldType[i];
                    if (valueType == ValueMetaInterface.TYPE_NONE) {
                        valueType = ValueMetaInterface.TYPE_STRING;
                    }
                    ValueMetaInterface v;
                    try {
                        v = ValueMetaFactory.createValueMeta(fieldName[i], valueType);
                        v.setLength(fieldLength[i]);
                        v.setPrecision(fieldPrecision[i]);
                        v.setOrigin(origin);
                        row.addValueMeta(v);
                    } catch (KettlePluginException e) {
                        throw new KettleStepException(e);
                    }
                }
            }
        }
    // else: row is OK, keep it as it is.
    }
}
Also used : MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) KettleStepException(org.pentaho.di.core.exception.KettleStepException) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) RowMeta(org.pentaho.di.core.row.RowMeta) ArrayList(java.util.ArrayList) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 5 with MappingValueRename

use of org.pentaho.di.trans.steps.mapping.MappingValueRename in project pentaho-kettle by pentaho.

the class MappingInputFieldsTest method testOnlySpecifiedFields.

/*
   * verifies: If SelectingAndSortingUnspecifiedFields checkbox is not checked, then 1)all fields throw to the next step;
   * 2)fields are not resorted;
   */
@Test
public void testOnlySpecifiedFields() throws Exception {
    meta.setSelectingAndSortingUnspecifiedFields(false);
    MappingInputData sdi = new MappingInputData();
    sdi.linked = true;
    sdi.valueRenames = new ArrayList<MappingValueRename>();
    sdi.valueRenames.add(new MappingValueRename("number2", "n2"));
    sdi.valueRenames.add(new MappingValueRename("number4", "n4"));
    BlockingRowSet in = new BlockingRowSet(10);
    BlockingRowSet out = new BlockingRowSet(10);
    RowMeta rm = new RowMeta();
    rm.addValueMeta(new ValueMetaString("string"));
    rm.addValueMeta(new ValueMetaInteger("number1"));
    rm.addValueMeta(new ValueMetaInteger("number2"));
    rm.addValueMeta(new ValueMetaInteger("number3"));
    rm.addValueMeta(new ValueMetaInteger("number"));
    rm.addValueMeta(new ValueMetaInteger("number4"));
    rm.addValueMeta(new ValueMetaInteger("number5"));
    in.putRow(rm, new Object[] { "str", new Integer(100501), new Integer(100502), new Integer(100503), new Integer(100500), new Integer(100504), new Integer(100505) });
    in.putRow(rm, new Object[] { "str_1", new Integer(200501), new Integer(200502), new Integer(200503), new Integer(200500), new Integer(200504), new Integer(200505) });
    step.addRowSetToInputRowSets(in);
    step.addRowSetToOutputRowSets(out);
    assertTrue(step.init(meta, sdi));
    assertTrue(step.processRow(meta, sdi));
    Object[] outRowData = out.getRow();
    RowMetaInterface outMeta = out.getRowMeta();
    assertEquals("All fields are expected.", 7, outMeta.size());
    int i = 0;
    // Check if row-meta is formed according to the step specification
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_STRING, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "string", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "number1", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "n2", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "number3", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "number", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "n4", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "number5", outMeta.getValueMeta(i++).getName());
    // Check if row-data corresponds to the row-meta
    assertEquals("the field value mismatch.", "str", outRowData[0]);
    assertEquals("the field value mismatch.", new Integer(100501), outRowData[1]);
    assertEquals("the field value mismatch.", new Integer(100502), outRowData[2]);
    assertEquals("the field value mismatch.", new Integer(100503), outRowData[3]);
    assertEquals("the field value mismatch.", new Integer(100500), outRowData[4]);
    assertEquals("the field value mismatch.", new Integer(100504), outRowData[5]);
    assertEquals("the field value mismatch.", new Integer(100505), outRowData[6]);
    assertTrue(step.processRow(meta, sdi));
    outRowData = out.getRow();
    outMeta = out.getRowMeta();
    assertEquals("All fields are expected.", 7, outMeta.size());
    i = 0;
    // Check if row-meta is formed according to the step specification
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_STRING, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "string", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "number1", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "n2", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "number3", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "number", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "n4", outMeta.getValueMeta(i++).getName());
    assertEquals("the field type-meta mismatch.", ValueMetaInterface.TYPE_INTEGER, outMeta.getValueMeta(i).getType());
    assertEquals("the field name-meta mismatch.", "number5", outMeta.getValueMeta(i++).getName());
    // Check if row-data corresponds to the row-meta
    assertEquals("the field value mismatch.", "str_1", outRowData[0]);
    assertEquals("the field value mismatch.", new Integer(200501), outRowData[1]);
    assertEquals("the field value mismatch.", new Integer(200502), outRowData[2]);
    assertEquals("the field value mismatch.", new Integer(200503), outRowData[3]);
    assertEquals("the field value mismatch.", new Integer(200500), outRowData[4]);
    assertEquals("the field value mismatch.", new Integer(200504), outRowData[5]);
    assertEquals("the field value mismatch.", new Integer(200505), outRowData[6]);
}
Also used : MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) RowMeta(org.pentaho.di.core.row.RowMeta) BlockingRowSet(org.pentaho.di.core.BlockingRowSet) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) SimpleLoggingObject(org.pentaho.di.core.logging.SimpleLoggingObject) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Test(org.junit.Test)

Aggregations

MappingValueRename (org.pentaho.di.trans.steps.mapping.MappingValueRename)23 RowMeta (org.pentaho.di.core.row.RowMeta)7 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)7 ArrayList (java.util.ArrayList)6 KettleException (org.pentaho.di.core.exception.KettleException)6 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)6 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)5 MappingIODefinition (org.pentaho.di.trans.steps.mapping.MappingIODefinition)5 KettleStepException (org.pentaho.di.core.exception.KettleStepException)4 HashMap (java.util.HashMap)3 TableItem (org.eclipse.swt.widgets.TableItem)3 SourceToTargetMapping (org.pentaho.di.core.SourceToTargetMapping)3 List (java.util.List)2 CTabItem (org.eclipse.swt.custom.CTabItem)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 FormAttachment (org.eclipse.swt.layout.FormAttachment)2 FormData (org.eclipse.swt.layout.FormData)2 FormLayout (org.eclipse.swt.layout.FormLayout)2 Button (org.eclipse.swt.widgets.Button)2