use of org.pentaho.di.trans.TransStoppedListener in project pentaho-kettle by pentaho.
the class MetaInject method processRow.
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
meta = (MetaInjectMeta) smi;
data = (MetaInjectData) sdi;
// Read the data from all input steps and keep it in memory...
// Skip the step from which we stream data. Keep that available for runtime action.
//
data.rowMap = new HashMap<String, List<RowMetaAndData>>();
for (String prevStepName : getTransMeta().getPrevStepNames(getStepMeta())) {
//
if (!data.streaming || !prevStepName.equalsIgnoreCase(data.streamingSourceStepname)) {
List<RowMetaAndData> list = new ArrayList<RowMetaAndData>();
RowSet rowSet = findInputRowSet(prevStepName);
Object[] row = getRowFrom(rowSet);
while (row != null) {
RowMetaAndData rd = new RowMetaAndData();
rd.setRowMeta(rowSet.getRowMeta());
rd.setData(row);
list.add(rd);
row = getRowFrom(rowSet);
}
if (!list.isEmpty()) {
data.rowMap.put(prevStepName, list);
}
}
}
List<StepMeta> steps = data.transMeta.getSteps();
for (Map.Entry<String, StepMetaInterface> en : data.stepInjectionMetasMap.entrySet()) {
newInjection(en.getKey(), en.getValue());
}
/*
* constants injection should be executed after steps, because if constant should be inserted into target with array
* in path, constants should be inserted into all arrays items
*/
for (Map.Entry<String, StepMetaInterface> en : data.stepInjectionMetasMap.entrySet()) {
newInjectionConstants(en.getKey(), en.getValue());
}
for (Map.Entry<String, StepMetaInterface> en : data.stepInjectionMetasMap.entrySet()) {
en.getValue().searchInfoAndTargetSteps(steps);
}
for (String targetStepName : data.stepInjectionMap.keySet()) {
if (!data.stepInjectionMetasMap.containsKey(targetStepName)) {
oldInjection(targetStepName);
StepMeta targetStep = StepMeta.findStep(steps, targetStepName);
if (targetStep != null) {
targetStep.getStepMetaInterface().searchInfoAndTargetSteps(steps);
}
}
}
if (!meta.isNoExecution()) {
// Now we can execute this modified transformation metadata.
//
final Trans injectTrans = createInjectTrans();
injectTrans.setMetaStore(getMetaStore());
if (getTrans().getParentJob() != null) {
// See PDI-13224
injectTrans.setParentJob(getTrans().getParentJob());
}
getTrans().addTransStoppedListener(new TransStoppedListener() {
public void transStopped(Trans parentTrans) {
injectTrans.stopAll();
}
});
injectTrans.prepareExecution(null);
// See if we need to stream some data over...
//
RowProducer rowProducer = null;
if (data.streaming) {
rowProducer = injectTrans.addRowProducer(data.streamingTargetStepname, 0);
}
// Finally, add the mapping transformation to the active sub-transformations
// map in the parent transformation
//
getTrans().addActiveSubTransformation(getStepname(), injectTrans);
if (!Utils.isEmpty(meta.getSourceStepName())) {
StepInterface stepInterface = injectTrans.getStepInterface(meta.getSourceStepName(), 0);
if (stepInterface == null) {
throw new KettleException("Unable to find step '" + meta.getSourceStepName() + "' to read from.");
}
stepInterface.addRowListener(new RowAdapter() {
@Override
public void rowWrittenEvent(RowMetaInterface rowMeta, Object[] row) throws KettleStepException {
// Just pass along the data as output of this step...
//
MetaInject.this.putRow(rowMeta, row);
}
});
}
injectTrans.startThreads();
if (data.streaming) {
// Deplete all the rows from the parent transformation into the modified transformation
//
RowSet rowSet = findInputRowSet(data.streamingSourceStepname);
if (rowSet == null) {
throw new KettleException("Unable to find step '" + data.streamingSourceStepname + "' to stream data from");
}
Object[] row = getRowFrom(rowSet);
while (row != null && !isStopped()) {
rowProducer.putRow(rowSet.getRowMeta(), row);
row = getRowFrom(rowSet);
}
rowProducer.finished();
}
//
while (!injectTrans.isFinished() && !injectTrans.isStopped() && !isStopped()) {
copyResult(injectTrans);
// Wait a little bit.
try {
Thread.sleep(50);
} catch (Exception e) {
// Ignore errors
}
}
copyResult(injectTrans);
waitUntilFinished(injectTrans);
}
// let the transformation complete it's execution to allow for any customizations to MDI to happen in the init methods of steps
if (log.isDetailed()) {
logDetailed("XML of transformation after injection: " + data.transMeta.getXML());
}
String targetFile = environmentSubstitute(meta.getTargetFile());
if (!Utils.isEmpty(targetFile)) {
writeInjectedKtr(targetFile);
}
// All done!
setOutputDone();
return false;
}
Aggregations