Search in sources :

Example 1 with MappingInputMeta

use of org.pentaho.di.trans.steps.mappinginput.MappingInputMeta in project pentaho-kettle by pentaho.

the class SimpleMappingMeta method getFields.

public void getFields(RowMetaInterface row, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
    // First load some interesting data...
    // Then see which fields get added to the row.
    // 
    TransMeta mappingTransMeta = null;
    try {
        mappingTransMeta = loadMappingMeta(this, repository, metaStore, space, mappingParameters.isInheritingAllVariables());
    } catch (KettleException e) {
        throw new KettleStepException(BaseMessages.getString(PKG, "SimpleMappingMeta.Exception.UnableToLoadMappingTransformation"), e);
    }
    // for instance)
    if (mappingParameters != null && mappingTransMeta != null) {
        // Just set the variables in the transformation statically.
        // This just means: set a number of variables or parameter values:
        // 
        StepWithMappingMeta.activateParams(mappingTransMeta, mappingTransMeta, space, mappingTransMeta.listParameters(), mappingParameters.getVariable(), mappingParameters.getInputField());
    }
    // Keep track of all the fields that need renaming...
    // 
    List<MappingValueRename> inputRenameList = new ArrayList<MappingValueRename>();
    // 
    // Before we ask the mapping outputs anything, we should teach the mapping
    // input steps in the sub-transformation about the data coming in...
    // 
    RowMetaInterface inputRowMeta;
    // The row metadata, what we pass to the mapping input step
    // definition.getOutputStep(), is "row"
    // However, we do need to re-map some fields...
    // 
    inputRowMeta = row.clone();
    if (!inputRowMeta.isEmpty()) {
        for (MappingValueRename valueRename : inputMapping.getValueRenames()) {
            ValueMetaInterface valueMeta = inputRowMeta.searchValueMeta(valueRename.getSourceValueName());
            if (valueMeta == null) {
                throw new KettleStepException(BaseMessages.getString(PKG, "SimpleMappingMeta.Exception.UnableToFindField", valueRename.getSourceValueName()));
            }
            valueMeta.setName(valueRename.getTargetValueName());
        }
    }
    // What is this mapping input step?
    // 
    StepMeta mappingInputStep = mappingTransMeta.findMappingInputStep(null);
    // We're certain it's a MappingInput step...
    // 
    MappingInputMeta mappingInputMeta = (MappingInputMeta) mappingInputStep.getStepMetaInterface();
    // Inform the mapping input step about what it's going to receive...
    // 
    mappingInputMeta.setInputRowMeta(inputRowMeta);
    // What values are we changing names for: already done!
    // 
    mappingInputMeta.setValueRenames(null);
    // 
    if (inputMapping.isRenamingOnOutput()) {
        SimpleMapping.addInputRenames(inputRenameList, inputMapping.getValueRenames());
    }
    StepMeta mappingOutputStep = mappingTransMeta.findMappingOutputStep(null);
    // We know it's a mapping output step...
    MappingOutputMeta mappingOutputMeta = (MappingOutputMeta) mappingOutputStep.getStepMetaInterface();
    // Change a few columns.
    mappingOutputMeta.setOutputValueRenames(outputMapping.getValueRenames());
    // Perhaps we need to change a few input columns back to the original?
    // 
    mappingOutputMeta.setInputValueRenames(inputRenameList);
    // Now we know wat's going to come out of there...
    // This is going to be the full row, including all the remapping, etc.
    // 
    RowMetaInterface mappingOutputRowMeta = mappingTransMeta.getStepFields(mappingOutputStep);
    row.clear();
    row.addRowMeta(mappingOutputRowMeta);
}
Also used : MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) TransMeta(org.pentaho.di.trans.TransMeta) ArrayList(java.util.ArrayList) MappingInputMeta(org.pentaho.di.trans.steps.mappinginput.MappingInputMeta) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) StepMeta(org.pentaho.di.trans.step.StepMeta) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) MappingOutputMeta(org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta)

Example 2 with MappingInputMeta

use of org.pentaho.di.trans.steps.mappinginput.MappingInputMeta in project pentaho-kettle by pentaho.

the class MappingMeta method getFields.

public void getFields(RowMetaInterface row, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
    // First load some interesting data...
    // Then see which fields get added to the row.
    // 
    TransMeta mappingTransMeta = null;
    try {
        mappingTransMeta = loadMappingMeta(this, repository, metaStore, space);
    } catch (KettleException e) {
        throw new KettleStepException(BaseMessages.getString(PKG, "MappingMeta.Exception.UnableToLoadMappingTransformation"), e);
    }
    // for instance)
    if (mappingParameters != null) {
        // 
        if (mappingParameters.isInheritingAllVariables()) {
            mappingTransMeta.copyVariablesFrom(space);
        }
        // Just set the variables in the transformation statically.
        // This just means: set a number of variables or parameter values:
        // 
        List<String> subParams = Arrays.asList(mappingTransMeta.listParameters());
        for (int i = 0; i < mappingParameters.getVariable().length; i++) {
            String name = mappingParameters.getVariable()[i];
            String value = space.environmentSubstitute(mappingParameters.getInputField()[i]);
            if (!Utils.isEmpty(name) && !Utils.isEmpty(value)) {
                if (subParams.contains(name)) {
                    try {
                        mappingTransMeta.setParameterValue(name, value);
                    } catch (UnknownParamException e) {
                    // this is explicitly checked for up front
                    }
                }
                mappingTransMeta.setVariable(name, value);
            }
        }
    }
    // Keep track of all the fields that need renaming...
    // 
    List<MappingValueRename> inputRenameList = new ArrayList<MappingValueRename>();
    /*
     * Before we ask the mapping outputs anything, we should teach the mapping input steps in the sub-transformation
     * about the data coming in...
     */
    for (MappingIODefinition definition : inputMappings) {
        RowMetaInterface inputRowMeta;
        if (definition.isMainDataPath() || Utils.isEmpty(definition.getInputStepname())) {
            // The row metadata, what we pass to the mapping input step
            // definition.getOutputStep(), is "row"
            // However, we do need to re-map some fields...
            // 
            inputRowMeta = row.clone();
            if (!inputRowMeta.isEmpty()) {
                for (MappingValueRename valueRename : definition.getValueRenames()) {
                    ValueMetaInterface valueMeta = inputRowMeta.searchValueMeta(valueRename.getSourceValueName());
                    if (valueMeta == null) {
                        throw new KettleStepException(BaseMessages.getString(PKG, "MappingMeta.Exception.UnableToFindField", valueRename.getSourceValueName()));
                    }
                    valueMeta.setName(valueRename.getTargetValueName());
                }
            }
        } else {
            // The row metadata that goes to the info mapping input comes from the
            // specified step
            // In fact, it's one of the info steps that is going to contain this
            // information...
            // 
            String[] infoSteps = getInfoSteps();
            int infoStepIndex = Const.indexOfString(definition.getInputStepname(), infoSteps);
            if (infoStepIndex < 0) {
                throw new KettleStepException(BaseMessages.getString(PKG, "MappingMeta.Exception.UnableToFindMetadataInfo", definition.getInputStepname()));
            }
            if (info[infoStepIndex] != null) {
                inputRowMeta = info[infoStepIndex].clone();
            } else {
                inputRowMeta = null;
            }
        }
        // What is this mapping input step?
        // 
        StepMeta mappingInputStep = mappingTransMeta.findMappingInputStep(definition.getOutputStepname());
        // We're certain it's a MappingInput step...
        // 
        MappingInputMeta mappingInputMeta = (MappingInputMeta) mappingInputStep.getStepMetaInterface();
        // Inform the mapping input step about what it's going to receive...
        // 
        mappingInputMeta.setInputRowMeta(inputRowMeta);
        // What values are we changing names for?
        // 
        mappingInputMeta.setValueRenames(definition.getValueRenames());
        // 
        if (definition.isRenamingOnOutput()) {
            Mapping.addInputRenames(inputRenameList, definition.getValueRenames());
        }
    }
    // All the mapping steps now know what they will be receiving.
    // That also means that the sub-transformation / mapping has everything it
    // needs.
    // So that means that the MappingOutput steps know exactly what the output
    // is going to be.
    // That could basically be anything.
    // It also could have absolutely no resemblance to what came in on the
    // input.
    // The relative old approach is therefore no longer suited.
    // 
    // OK, but what we *can* do is have the MappingOutput step rename the
    // appropriate fields.
    // The mapping step will tell this step how it's done.
    // 
    // Let's look for the mapping output step that is relevant for this actual
    // call...
    // 
    MappingIODefinition mappingOutputDefinition = null;
    if (nextStep == null) {
        // 
        for (MappingIODefinition definition : outputMappings) {
            if (definition.isMainDataPath() || Utils.isEmpty(definition.getOutputStepname())) {
                // This is the definition to use...
                // 
                mappingOutputDefinition = definition;
            }
        }
    } else {
        for (MappingIODefinition definition : outputMappings) {
            if (nextStep.getName().equals(definition.getOutputStepname()) || definition.isMainDataPath() || Utils.isEmpty(definition.getOutputStepname())) {
                mappingOutputDefinition = definition;
            }
        }
    }
    if (mappingOutputDefinition == null) {
        throw new KettleStepException(BaseMessages.getString(PKG, "MappingMeta.Exception.UnableToFindMappingDefinition"));
    }
    // OK, now find the mapping output step in the mapping...
    // This method in TransMeta takes into account a number of things, such as
    // the step not specified, etc.
    // The method never returns null but throws an exception.
    // 
    StepMeta mappingOutputStep = mappingTransMeta.findMappingOutputStep(mappingOutputDefinition.getInputStepname());
    // We know it's a mapping output step...
    MappingOutputMeta mappingOutputMeta = (MappingOutputMeta) mappingOutputStep.getStepMetaInterface();
    // Change a few columns.
    mappingOutputMeta.setOutputValueRenames(mappingOutputDefinition.getValueRenames());
    // Perhaps we need to change a few input columns back to the original?
    // 
    mappingOutputMeta.setInputValueRenames(inputRenameList);
    // Now we know wat's going to come out of there...
    // This is going to be the full row, including all the remapping, etc.
    // 
    RowMetaInterface mappingOutputRowMeta = mappingTransMeta.getStepFields(mappingOutputStep);
    row.clear();
    row.addRowMeta(mappingOutputRowMeta);
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) TransMeta(org.pentaho.di.trans.TransMeta) ArrayList(java.util.ArrayList) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) StepMeta(org.pentaho.di.trans.step.StepMeta) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) MappingOutputMeta(org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta) MappingInputMeta(org.pentaho.di.trans.steps.mappinginput.MappingInputMeta) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException)

Aggregations

ArrayList (java.util.ArrayList)2 KettleException (org.pentaho.di.core.exception.KettleException)2 KettleStepException (org.pentaho.di.core.exception.KettleStepException)2 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)2 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)2 TransMeta (org.pentaho.di.trans.TransMeta)2 StepMeta (org.pentaho.di.trans.step.StepMeta)2 MappingInputMeta (org.pentaho.di.trans.steps.mappinginput.MappingInputMeta)2 MappingOutputMeta (org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta)2 UnknownParamException (org.pentaho.di.core.parameters.UnknownParamException)1 MappingValueRename (org.pentaho.di.trans.steps.mapping.MappingValueRename)1