use of org.apache.hop.testing.PipelineUnitTestFieldMapping in project hop by apache.
the class PipelineUnitTestSetLocationDialog method getData.
public void getData() {
wTransformName.setText(Const.NVL(location.getTransformName(), ""));
try {
wDataset.fillItems();
} catch (Exception e) {
new ErrorDialog(shell, "Error", "Error getting data sets from the metadata", e);
}
wDataset.setText(Const.NVL(location.getDataSetName(), ""));
for (int i = 0; i < location.getFieldMappings().size(); i++) {
PipelineUnitTestFieldMapping fieldMapping = location.getFieldMappings().get(i);
int colnr = 1;
wFieldMappings.setText(Const.NVL(fieldMapping.getTransformFieldName(), ""), colnr++, i);
wFieldMappings.setText(Const.NVL(fieldMapping.getDataSetFieldName(), ""), colnr++, i);
}
wFieldMappings.removeEmptyRows();
wFieldMappings.setRowNums();
wFieldMappings.optWidth(true);
for (int i = 0; i < location.getFieldOrder().size(); i++) {
String field = location.getFieldOrder().get(i);
int colnr = 1;
wFieldOrder.setText(Const.NVL(field, ""), colnr++, i);
}
wFieldOrder.removeEmptyRows();
wFieldOrder.setRowNums();
wFieldOrder.optWidth(true);
wTransformName.setFocus();
}
use of org.apache.hop.testing.PipelineUnitTestFieldMapping in project hop by apache.
the class InjectDataSetIntoTransformExtensionPoint method injectDataSetIntoTransform.
private void injectDataSetIntoTransform(final LocalPipelineEngine pipeline, final String dataSetName, final IHopMetadataProvider metadataProvider, final TransformMeta transformMeta, PipelineUnitTestSetLocation inputLocation) throws HopException, HopException {
final DataSet dataSet = metadataProvider.getSerializer(DataSet.class).load(dataSetName);
if (dataSet == null) {
throw new HopException("Unable to find data set '" + dataSetName + "'");
}
final ILogChannel log = pipeline.getLogChannel();
final RowProducer rowProducer = pipeline.addRowProducer(transformMeta.getName(), 0);
// Look for the transform into which we'll inject rows...
//
TransformMetaDataCombi combi = null;
for (TransformMetaDataCombi transform : pipeline.getTransforms()) {
if (transform.transformName.equals(transformMeta.getName())) {
combi = transform;
break;
}
}
if (combi != null) {
// Get the rows of the mapped values in the mapped order sorted as asked
//
final List<Object[]> dataSetRows = dataSet.getAllRows(pipeline, log, inputLocation);
IRowMeta dataSetRowMeta = dataSet.getMappedDataSetFieldsRowMeta(inputLocation);
// The rows to inject are always driven by the dataset, NOT the transform it replaces (!) for
// simplicity
//
IRowMeta injectRowMeta = new RowMeta();
// Figure out which fields to pass
// Only inject those mentioned in the field mappings...
//
int[] fieldIndexes = new int[inputLocation.getFieldMappings().size()];
for (int i = 0; i < inputLocation.getFieldMappings().size(); i++) {
PipelineUnitTestFieldMapping fieldMapping = inputLocation.getFieldMappings().get(i);
fieldIndexes[i] = dataSetRowMeta.indexOfValue(fieldMapping.getDataSetFieldName());
if (fieldIndexes[i] < 0) {
throw new HopException("Unable to find mapped field '" + fieldMapping.getDataSetFieldName() + "' in data set '" + dataSet.getName() + "'");
}
IValueMeta injectValueMeta = dataSetRowMeta.getValueMeta(fieldIndexes[i]).clone();
// Rename to the transform output names though...
//
injectValueMeta.setName(fieldMapping.getTransformFieldName());
injectRowMeta.addValueMeta(injectValueMeta);
}
log.logDetailed("Injecting data set '" + dataSetName + "' into transform '" + transformMeta.getName() + "', fields: " + Arrays.toString(injectRowMeta.getFieldNames()));
// Pass rows
//
Runnable runnable = () -> {
try {
for (Object[] dataSetRow : dataSetRows) {
// pass the row with the external names, in the right order and with the selected
// columns from the data set
//
Object[] row = RowDataUtil.allocateRowData(injectRowMeta.size());
for (int i = 0; i < fieldIndexes.length; i++) {
row[i] = dataSetRow[fieldIndexes[i]];
}
rowProducer.putRow(injectRowMeta, row);
}
rowProducer.finished();
} catch (Exception e) {
throw new RuntimeException("Problem injecting data set '" + dataSetName + "' row into transform '" + transformMeta.getName() + "'", e);
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
use of org.apache.hop.testing.PipelineUnitTestFieldMapping in project hop by apache.
the class PipelineUnitTestSetLocationDialog method getFieldMappings.
protected void getFieldMappings() {
try {
PipelineUnitTestSetLocation loc = new PipelineUnitTestSetLocation();
getInfo(loc);
String transformName = wTransformName.getText();
String datasetName = wDataset.getText();
if (StringUtils.isEmpty(transformName) || StringUtils.isEmpty(datasetName)) {
throw new HopException("Please select a transform and a data set to map fields between");
}
IRowMeta transformRowMeta = transformFieldsMap.get(transformName);
if (transformRowMeta == null) {
throw new HopException("Unable to find fields for transform " + transformName);
}
String[] transformFieldNames = transformRowMeta.getFieldNames();
DataSet dataSet = findDataSet(datasetName);
IRowMeta setRowMeta = dataSet.getSetRowMeta();
String[] setFieldNames = setRowMeta.getFieldNames();
// Get the current mappings...
//
List<SourceToTargetMapping> currentMappings = new ArrayList<>();
for (PipelineUnitTestFieldMapping mapping : loc.getFieldMappings()) {
int transformFieldIndex = transformRowMeta.indexOfValue(mapping.getTransformFieldName());
int setFieldIndex = transformRowMeta.indexOfValue(mapping.getDataSetFieldName());
if (transformFieldIndex >= 0 && setFieldIndex >= 0) {
currentMappings.add(new SourceToTargetMapping(transformFieldIndex, setFieldIndex));
}
}
// Edit them
//
EnterMappingDialog mappingDialog = new EnterMappingDialog(shell, transformFieldNames, setFieldNames, currentMappings);
List<SourceToTargetMapping> newMappings = mappingDialog.open();
if (newMappings != null) {
// Simply clean everything and add the new mappings
//
wFieldMappings.clearAll();
for (SourceToTargetMapping sourceToTargetMapping : newMappings) {
TableItem item = new TableItem(wFieldMappings.table, SWT.NONE);
item.setText(1, transformFieldNames[sourceToTargetMapping.getSourcePosition()]);
item.setText(2, setFieldNames[sourceToTargetMapping.getTargetPosition()]);
}
wFieldMappings.removeEmptyRows();
wFieldMappings.setRowNums();
wFieldMappings.optWidth(true);
}
} catch (Exception e) {
new ErrorDialog(shell, "Error", "Error mapping fields from transform to dataset", e);
}
}
use of org.apache.hop.testing.PipelineUnitTestFieldMapping in project hop by apache.
the class PipelineUnitTestSetLocationDialog method getInfo.
/**
* @param loc The data set to load the dialog information into
*/
public void getInfo(PipelineUnitTestSetLocation loc) {
loc.setTransformName(wTransformName.getText());
loc.setDataSetName(wDataset.getText());
loc.getFieldMappings().clear();
int nrMappings = wFieldMappings.nrNonEmpty();
for (int i = 0; i < nrMappings; i++) {
TableItem item = wFieldMappings.getNonEmpty(i);
int colnr = 1;
String transformFieldName = item.getText(colnr++);
String dataSetFieldName = item.getText(colnr++);
loc.getFieldMappings().add(new PipelineUnitTestFieldMapping(transformFieldName, dataSetFieldName));
}
loc.getFieldOrder().clear();
int nrFields = wFieldOrder.nrNonEmpty();
for (int i = 0; i < nrFields; i++) {
TableItem item = wFieldOrder.getNonEmpty(i);
int colnr = 1;
String fieldname = item.getText(colnr++);
loc.getFieldOrder().add(fieldname);
}
}
Aggregations