Search in sources :

Example 1 with PipelineUnitTestFieldMapping

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();
}
Also used : ErrorDialog(org.apache.hop.ui.core.dialog.ErrorDialog) PipelineUnitTestFieldMapping(org.apache.hop.testing.PipelineUnitTestFieldMapping) HopException(org.apache.hop.core.exception.HopException)

Example 2 with PipelineUnitTestFieldMapping

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();
    }
}
Also used : RowProducer(org.apache.hop.pipeline.RowProducer) DataSet(org.apache.hop.testing.DataSet) HopException(org.apache.hop.core.exception.HopException) ILogChannel(org.apache.hop.core.logging.ILogChannel) RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) PipelineUnitTestFieldMapping(org.apache.hop.testing.PipelineUnitTestFieldMapping) ExtensionPoint(org.apache.hop.core.extension.ExtensionPoint) IExtensionPoint(org.apache.hop.core.extension.IExtensionPoint) HopPluginException(org.apache.hop.core.exception.HopPluginException) HopException(org.apache.hop.core.exception.HopException) HopTransformException(org.apache.hop.core.exception.HopTransformException) IValueMeta(org.apache.hop.core.row.IValueMeta) TransformMetaDataCombi(org.apache.hop.pipeline.transform.TransformMetaDataCombi)

Example 3 with PipelineUnitTestFieldMapping

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);
    }
}
Also used : PipelineUnitTestSetLocation(org.apache.hop.testing.PipelineUnitTestSetLocation) HopException(org.apache.hop.core.exception.HopException) DataSet(org.apache.hop.testing.DataSet) IRowMeta(org.apache.hop.core.row.IRowMeta) EnterMappingDialog(org.apache.hop.ui.core.dialog.EnterMappingDialog) ArrayList(java.util.ArrayList) ErrorDialog(org.apache.hop.ui.core.dialog.ErrorDialog) PipelineUnitTestFieldMapping(org.apache.hop.testing.PipelineUnitTestFieldMapping) HopException(org.apache.hop.core.exception.HopException) SourceToTargetMapping(org.apache.hop.core.SourceToTargetMapping)

Example 4 with PipelineUnitTestFieldMapping

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);
    }
}
Also used : PipelineUnitTestFieldMapping(org.apache.hop.testing.PipelineUnitTestFieldMapping)

Aggregations

PipelineUnitTestFieldMapping (org.apache.hop.testing.PipelineUnitTestFieldMapping)4 HopException (org.apache.hop.core.exception.HopException)3 IRowMeta (org.apache.hop.core.row.IRowMeta)2 DataSet (org.apache.hop.testing.DataSet)2 ErrorDialog (org.apache.hop.ui.core.dialog.ErrorDialog)2 ArrayList (java.util.ArrayList)1 SourceToTargetMapping (org.apache.hop.core.SourceToTargetMapping)1 HopPluginException (org.apache.hop.core.exception.HopPluginException)1 HopTransformException (org.apache.hop.core.exception.HopTransformException)1 ExtensionPoint (org.apache.hop.core.extension.ExtensionPoint)1 IExtensionPoint (org.apache.hop.core.extension.IExtensionPoint)1 ILogChannel (org.apache.hop.core.logging.ILogChannel)1 IValueMeta (org.apache.hop.core.row.IValueMeta)1 RowMeta (org.apache.hop.core.row.RowMeta)1 RowProducer (org.apache.hop.pipeline.RowProducer)1 TransformMetaDataCombi (org.apache.hop.pipeline.transform.TransformMetaDataCombi)1 PipelineUnitTestSetLocation (org.apache.hop.testing.PipelineUnitTestSetLocation)1 EnterMappingDialog (org.apache.hop.ui.core.dialog.EnterMappingDialog)1