Skip to content

Instantly share code, notes, and snippets.

@mattyb149
Created September 18, 2012 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattyb149/3742850 to your computer and use it in GitHub Desktop.
Save mattyb149/3742850 to your computer and use it in GitHub Desktop.
Create Data Grid UDJC 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.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;
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);
// Create a new step with the original step name plus Data Grid (and rename if duplicate)
String dataGridPid = PluginRegistry.getInstance().getPluginId(StepPluginType.class, dgm);
PluginInterface dataGridPlugin = PluginRegistry.getInstance().findPluginWithName(StepPluginType.class, "Data Grid");
Spoon spoon = Spoon.getInstance();
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;
}
StepMeta dataGridStep = new StepMeta(dataGridPid, dataGridStepname, (StepMetaInterface)dgm);
// Locate new step a little underneath the original
Point p = getStepMeta().getLocation();
dataGridStep.setLocation(p.x+10, p.y+40);
dataGridStep.setDraw(true);
tm.addStep(dataGridStep);
try{
spoon.addUndoNew(tm, new StepMeta[] { dataGridStep }, new int[] { tm.indexOfStep(dataGridStep) });
}
catch(Exception e) { /* ignore thread access exception */ }
spoon.props.increasePluginHistory(dataGridPlugin.getIds()[0]);
// Redraw stuff
try {
spoon.refreshTree();
spoon.getActiveTransGraph().redraw();
}
catch(Exception e) {}
}
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] = "";
group[currIndex] = "";
decimal[currIndex] = "";
currency[currIndex] = "";
length[currIndex] = -1;
precision[currIndex] = -1;
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.getString(r[i]);
dataRow.add(v.getString(r[i]));
}
catch(Exception e){}
}
putRow(data.outputRowMeta, outputRow);
datalines.add(dataRow);
return true;
}
@KimBechHansen
Copy link

Heyyy

Great piece of coding :-)

I've tried to implement the code, but it seems that it has a little trouble reading the format of Numbers (in my original inputstep it has been set to #.##, but the Data Grid creator reads it as #.#). I'm not good enough at Java, to see where I can alter this... Is it possible to make a change somewhere, to correct this ?

Regards
Kim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment