Skip to content

Instantly share code, notes, and snippets.

@mattyb149
Created September 20, 2012 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattyb149/3757784 to your computer and use it in GitHub Desktop.
Save mattyb149/3757784 to your computer and use it in GitHub Desktop.
Create Reproduction Transformation step for Pentaho Data Integration
import org.pentaho.di.core.gui.Point;
import org.pentaho.di.core.plugins.*;
import org.pentaho.di.core.row.*;
import org.pentaho.di.trans.TransHopMeta;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.pentaho.di.trans.steps.datagrid.DataGridMeta;
import org.pentaho.di.ui.spoon.Spoon;
import java.util.ArrayList;
import java.util.List;
private DataGridMeta dgm;
private List datalines;
private boolean hasRows = false;
private Spoon spoon;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
// First, get a row from the default input hop
//
Object[] r = getRow();
// If the row object is null, we are done processing.
//
if (r == null) {
if(hasRows) {
dgm.setDataLines(datalines);
spoon = Spoon.getInstance();
// Create a new step with the original step name plus Data Grid (and rename if duplicate)
String dataGridPid = PluginRegistry.getInstance().getPluginId(StepPluginType.class, dgm);
final PluginInterface dataGridPlugin = PluginRegistry.getInstance().findPluginWithName(StepPluginType.class, "Data Grid");
final TransMeta tm = spoon.getActiveTransformation();
String dataGridStepname = "Data Grid";
if (tm.findStep(dataGridStepname) != null) {
int i = 2;
String newname = dataGridStepname + " " + i;
while (tm.findStep(newname) != null) {
i++;
newname = dataGridStepname + " " + i;
}
dataGridStepname = newname;
}
final StepMeta dataGridStep = new StepMeta(dataGridPid, dataGridStepname, (StepMetaInterface)dgm);
// Create a copy of the current trans, and remove the previous steps
final TransMeta newTM = (TransMeta)tm.clone();
String newTransNameBase = tm.getName()+" repro";
String newTransName = newTransNameBase;
int i = 1;
while(spoon.findTransformation(newTransName) != null) {
i++;
newTransName = newTransNameBase + " " + i;
}
newTM.setName(newTransName);
newTM.setFilename(tm.getFilename()+newTransName.substring(tm.getName().length()));
// Remove previous steps and hops
removeStepsAndHops(newTM, getStepMeta());
TransHopMeta prevHop = newTM.findTransHopTo(getStepMeta());
newTM.removeTransHop(newTM.indexOfTransHop(prevHop));
logBasic("Removed hop: "+prevHop.toString());
// Replace this step with the data grid step, update the hop
dataGridStep.setLocation(getStepMeta().getLocation());
dataGridStep.setDraw(true);
newTM.findTransHopFrom(getStepMeta()).setFromStep(dataGridStep);
newTM.getSteps().remove(getStepMeta());
logBasic("Removed step: "+getStepMeta().getName());
newTM.addStep(dataGridStep);
spoon.getDisplay().syncExec(
new Runnable() {
public void run(){
// Redraw stuff
//spoon.addUndoNew(tm, new StepMeta[] { dataGridStep }, new int[] { tm.indexOfStep(dataGridStep) });
//spoon.props.increasePluginHistory(dataGridPlugin.getIds()[0]);
spoon.addTransGraph(newTM);
spoon.refreshTree();
spoon.getActiveTransGraph().redraw();
}
});
}
setOutputDone();
return false;
}
RowMetaInterface rmi = getInputRowMeta();
int numCols = rmi.size();
if(first) {
// Create a new Data Grid meta, fill it in with default values and values from the original step meta
dgm = new DataGridMeta();
dgm.setDefault();
List vmiList = rmi.getValueMetaList();
//int numCols = vmiList.size();
int currIndex = 0;
String fieldName[] = new String[numCols];
String type[] = new String[numCols];
String fieldFormat[] = new String[numCols];
String group[] = new String[numCols];
String decimal[] = new String[numCols];
String currency[] = new String[numCols];
int length[] = new int[numCols];
int precision[] = new int[numCols];
for(int i=0;i<vmiList.size();i++) {
ValueMetaInterface vmi = (ValueMetaInterface)vmiList.get(i);
fieldName[currIndex] = vmi.getName();
type[currIndex] = vmi.getTypeDesc();
fieldFormat[currIndex] = vmi.getConversionMask();
group[currIndex] = vmi.getGroupingSymbol();
decimal[currIndex] = vmi.getDecimalSymbol();
currency[currIndex] = vmi.getCurrencySymbol();
length[currIndex] = vmi.getLength();
precision[currIndex] = vmi.getPrecision();
currIndex++;
}
dgm.setFieldName(fieldName);
dgm.setFieldType(type);
dgm.setFieldFormat(fieldFormat);
dgm.setGroup(group);
dgm.setDecimal(decimal);
dgm.setCurrency(currency);
dgm.setFieldLength(length);
dgm.setFieldPrecision(precision);
// Create list of data lines for Data Grid
datalines = new ArrayList();
first=false;
}
Object[] outputRow = createOutputRow(r, numCols);
hasRows = true;
List dataRow = new ArrayList(numCols);
for(int i=0;i<numCols;i++) {
ValueMetaInterface v = rmi.getValueMeta(i);
try {
outputRow[i] = v.convertData(v,r[i]);
// Gotta trim spaces from numeric fields so they parse correctly
if(v.isNumeric())
dataRow.add(v.getString(r[i]).trim());
else
dataRow.add(v.getString(r[i]));
}
catch(Exception e){}
}
putRow(data.outputRowMeta, outputRow);
datalines.add(dataRow);
return true;
}
private void removeStepsAndHops(TransMeta tm, StepMeta startStep) {
List previousSteps = tm.findPreviousSteps(startStep);
TransHopMeta prevHop;
for(int s=0;s<previousSteps.size();s++) {
StepMeta prevStep = (StepMeta)previousSteps.get(s);
// Recursively remove hops and previous steps
removeStepsAndHops(tm, prevStep);
tm.getSteps().remove(prevStep);
logBasic("Removed step: "+prevStep.getName());
prevHop = tm.findTransHopTo(prevStep);
while(prevHop != null) {
tm.removeTransHop(tm.indexOfTransHop(prevHop));
logBasic("Removed hop: "+prevHop.toString());
prevHop = tm.findTransHopTo(prevStep);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment