use of org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta in project pentaho-kettle by pentaho.
the class MappingOutputMetaIT method testGetFields_OutputValueRenames_WillRenameOutputIfValueMetaExist.
@Test
public void testGetFields_OutputValueRenames_WillRenameOutputIfValueMetaExist() throws KettleStepException {
ValueMetaInterface valueMeta1 = new ValueMetaBase("valueMeta1");
ValueMetaInterface valueMeta2 = new ValueMetaBase("valueMeta2");
RowMetaInterface rowMeta = new RowMeta();
rowMeta.addValueMeta(valueMeta1);
rowMeta.addValueMeta(valueMeta2);
List<MappingValueRename> outputValueRenames = new ArrayList<MappingValueRename>();
outputValueRenames.add(new MappingValueRename("valueMeta2", "valueMeta1"));
MappingOutputMeta meta = new MappingOutputMeta();
meta.setOutputValueRenames(outputValueRenames);
meta.getFields(rowMeta, null, info, nextStep, space, repository, metaStore);
// we must not add additional field
assertEquals(2, rowMeta.getValueMetaList().size());
// we must not keep the first value meta since we want to rename second
assertEquals(valueMeta1, rowMeta.getValueMeta(0));
// the second value meta must be other than we want to rename since we already have value meta with such name
assertFalse("valueMeta1".equals(rowMeta.getValueMeta(1).getName()));
// the second value meta must be other than we want to rename since we already have value meta with such name.
// It must be renamed according the rules from the #RowMeta
assertTrue("valueMeta1_1".equals(rowMeta.getValueMeta(1).getName()));
}
use of org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta 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);
}
use of org.pentaho.di.trans.steps.mappingoutput.MappingOutputMeta 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(), mappingParameters.isInheritingAllVariables());
}
// 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);
}
Aggregations