Search in sources :

Example 11 with MappingValueRename

use of org.pentaho.di.trans.steps.mapping.MappingValueRename in project pentaho-kettle by pentaho.

the class SimpleMapping method prepareMappingExecution.

public void prepareMappingExecution() throws KettleException {
    SimpleMappingData simpleMappingData = getData();
    // Create the transformation from meta-data...
    simpleMappingData.mappingTrans = new Trans(simpleMappingData.mappingTransMeta, this);
    // Set the parameters values in the mapping.
    // 
    StepWithMappingMeta.activateParams(simpleMappingData.mappingTrans, simpleMappingData.mappingTrans, this, simpleMappingData.mappingTransMeta.listParameters(), meta.getMappingParameters().getVariable(), meta.getMappingParameters().getInputField(), meta.getMappingParameters().isInheritingAllVariables());
    if (simpleMappingData.mappingTransMeta.getTransformationType() != TransformationType.Normal) {
        simpleMappingData.mappingTrans.getTransMeta().setUsingThreadPriorityManagment(false);
    }
    // Leave a path up so that we can set variables in sub-transformations...
    // 
    simpleMappingData.mappingTrans.setParentTrans(getTrans());
    // Pass down the safe mode flag to the mapping...
    // 
    simpleMappingData.mappingTrans.setSafeModeEnabled(getTrans().isSafeModeEnabled());
    // Pass down the metrics gathering flag:
    // 
    simpleMappingData.mappingTrans.setGatheringMetrics(getTrans().isGatheringMetrics());
    // Also set the name of this step in the mapping transformation for logging
    // purposes
    // 
    simpleMappingData.mappingTrans.setMappingStepName(getStepname());
    initServletConfig();
    // 
    try {
        simpleMappingData.mappingTrans.prepareExecution(getTrans().getArguments());
    } catch (KettleException e) {
        throw new KettleException(BaseMessages.getString(PKG, "SimpleMapping.Exception.UnableToPrepareExecutionOfMapping"), e);
    }
    // If there is no read/write logging step set, we can insert the data from
    // the first mapping input/output step...
    // 
    MappingInput[] mappingInputs = simpleMappingData.mappingTrans.findMappingInput();
    if (mappingInputs.length == 0) {
        throw new KettleException("The simple mapping step needs one Mapping Input step to write to in the sub-transformation");
    }
    if (mappingInputs.length > 1) {
        throw new KettleException("The simple mapping step does not support multiple Mapping Input steps to write to in the sub-transformation");
    }
    simpleMappingData.mappingInput = mappingInputs[0];
    simpleMappingData.mappingInput.setConnectorSteps(new StepInterface[0], new ArrayList<MappingValueRename>(), null);
    // LogTableField readField = data.mappingTransMeta.getTransLogTable().findField(TransLogTable.ID.LINES_READ);
    // if (readField.getSubject() == null) {
    // readField.setSubject(data.mappingInput.getStepMeta());
    // }
    MappingOutput[] mappingOutputs = simpleMappingData.mappingTrans.findMappingOutput();
    if (mappingOutputs.length == 0) {
        throw new KettleException("The simple mapping step needs one Mapping Output step to read from in the sub-transformation");
    }
    if (mappingOutputs.length > 1) {
        throw new KettleException("The simple mapping step does not support " + "multiple Mapping Output steps to read from in the sub-transformation");
    }
    simpleMappingData.mappingOutput = mappingOutputs[0];
    // LogTableField writeField = data.mappingTransMeta.getTransLogTable().findField(TransLogTable.ID.LINES_WRITTEN);
    // if (writeField.getSubject() == null && data.mappingOutputs != null && data.mappingOutputs.length >= 1) {
    // writeField.setSubject(data.mappingOutputs[0].getStepMeta());
    // }
    // Finally, add the mapping transformation to the active sub-transformations
    // map in the parent transformation
    // 
    getTrans().addActiveSubTransformation(getStepname(), simpleMappingData.mappingTrans);
}
Also used : MappingInput(org.pentaho.di.trans.steps.mappinginput.MappingInput) MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) KettleException(org.pentaho.di.core.exception.KettleException) Trans(org.pentaho.di.trans.Trans) MappingOutput(org.pentaho.di.trans.steps.mappingoutput.MappingOutput)

Example 12 with MappingValueRename

use of org.pentaho.di.trans.steps.mapping.MappingValueRename in project pentaho-kettle by pentaho.

the class MappingDialog method addMappingDefinitionTab.

private void addMappingDefinitionTab(List<MappingIODefinition> definitions, final String tabTitle, String listLabel, String addToolTip, String removeToolTip, String inputStepLabel, String outputStepLabel, String descriptionLabel, String sourceColumnLabel, String targetColumnLabel, String noItemsLabel, final boolean input) {
    final CTabItem wTab = new CTabItem(wTabFolder, SWT.NONE);
    wTab.setText(tabTitle);
    Composite wInputComposite = new Composite(wTabFolder, SWT.NONE);
    props.setLook(wInputComposite);
    FormLayout tabLayout = new FormLayout();
    tabLayout.marginWidth = 15;
    tabLayout.marginHeight = 15;
    wInputComposite.setLayout(tabLayout);
    Label wAvailableInputs = new Label(wInputComposite, SWT.LEFT);
    props.setLook(wAvailableInputs);
    wAvailableInputs.setText(listLabel);
    FormData fdwAvailableInputs = new FormData();
    fdwAvailableInputs.left = new FormAttachment(0);
    fdwAvailableInputs.top = new FormAttachment(0);
    Label wRemoveButton = new Label(wInputComposite, SWT.NONE);
    wRemoveButton.setImage(GUIResource.getInstance().getImage("ui/images/generic-delete.svg"));
    wRemoveButton.setToolTipText(removeToolTip);
    props.setLook(wRemoveButton);
    FormData fdwAddInputButton = new FormData();
    fdwAddInputButton.top = new FormAttachment(0);
    fdwAddInputButton.right = new FormAttachment(30);
    wRemoveButton.setLayoutData(fdwAddInputButton);
    Label wAddButton = new Label(wInputComposite, SWT.NONE);
    wAddButton.setImage(GUIResource.getInstance().getImage("ui/images/Add.svg"));
    wAddButton.setToolTipText(addToolTip);
    props.setLook(wAddButton);
    FormData fdwAddButton = new FormData();
    fdwAddButton.top = new FormAttachment(0);
    fdwAddButton.right = new FormAttachment(wRemoveButton, -5);
    wAddButton.setLayoutData(fdwAddButton);
    org.eclipse.swt.widgets.List wInputList = new org.eclipse.swt.widgets.List(wInputComposite, SWT.BORDER);
    FormData fdwInputList = new FormData();
    fdwInputList.left = new FormAttachment(0);
    fdwInputList.top = new FormAttachment(wAvailableInputs, 5);
    fdwInputList.bottom = new FormAttachment(100);
    fdwInputList.right = new FormAttachment(30);
    wInputList.setLayoutData(fdwInputList);
    for (int i = 0; i < definitions.size(); i++) {
        String label = !Utils.isEmpty(definitions.get(i).getInputStepname()) ? definitions.get(i).getInputStepname() : tabTitle + (i > 0 ? String.valueOf(i + 1) : "");
        wInputList.add(label);
    }
    final Label wlNoItems = new Label(wInputComposite, SWT.CENTER);
    wlNoItems.setText(noItemsLabel);
    props.setLook(wlNoItems);
    FormData fdlNoItems = new FormData();
    fdlNoItems.left = new FormAttachment(wInputList, 30);
    fdlNoItems.right = new FormAttachment(100);
    fdlNoItems.top = new FormAttachment(50);
    wlNoItems.setLayoutData(fdlNoItems);
    wlNoItems.setVisible(false);
    Composite wFieldsComposite = new Composite(wInputComposite, SWT.NONE);
    props.setLook(wFieldsComposite);
    FormLayout fieldLayout = new FormLayout();
    fieldLayout.marginWidth = 0;
    fieldLayout.marginHeight = 0;
    wFieldsComposite.setLayout(fieldLayout);
    final Button wMainPath = new Button(wFieldsComposite, SWT.CHECK);
    wMainPath.setText(BaseMessages.getString(PKG, "MappingDialog.input.MainDataPath"));
    props.setLook(wMainPath);
    FormData fdMainPath = new FormData();
    fdMainPath.top = new FormAttachment(0);
    fdMainPath.left = new FormAttachment(0);
    wMainPath.setLayoutData(fdMainPath);
    wMainPath.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            definitions.get(wInputList.getSelectionIndex()).setMainDataPath(!definitions.get(wInputList.getSelectionIndex()).isMainDataPath());
        }
    });
    final Label wlInputStep = new Label(wFieldsComposite, SWT.RIGHT);
    props.setLook(wlInputStep);
    wlInputStep.setText(inputStepLabel);
    FormData fdlInputStep = new FormData();
    fdlInputStep.top = new FormAttachment(wMainPath, 10);
    fdlInputStep.left = new FormAttachment(0);
    wlInputStep.setLayoutData(fdlInputStep);
    // What's the stepname to read from? (empty is OK too)
    // 
    final Button wbInputStep = new Button(wFieldsComposite, SWT.PUSH);
    props.setLook(wbInputStep);
    wbInputStep.setText(BaseMessages.getString(PKG, "MappingDialog.button.SourceStepName"));
    FormData fdbInputStep = new FormData();
    fdbInputStep.top = new FormAttachment(wlInputStep, 5);
    // First one in the
    fdbInputStep.right = new FormAttachment(100);
    // left top corner
    wbInputStep.setLayoutData(fdbInputStep);
    final Text wInputStep = new Text(wFieldsComposite, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
    props.setLook(wInputStep);
    wInputStep.addModifyListener(lsMod);
    FormData fdInputStep = new FormData();
    fdInputStep.top = new FormAttachment(wlInputStep, 5);
    // To the right of
    fdInputStep.left = new FormAttachment(0);
    // the label
    fdInputStep.right = new FormAttachment(wbInputStep, -5);
    wInputStep.setLayoutData(fdInputStep);
    wInputStep.addFocusListener(new FocusAdapter() {

        @Override
        public void focusLost(FocusEvent event) {
            definitions.get(wInputList.getSelectionIndex()).setInputStepname(wInputStep.getText());
            String label = !Utils.isEmpty(wInputStep.getText()) ? wInputStep.getText() : tabTitle + (wInputList.getSelectionIndex() > 0 ? String.valueOf(wInputList.getSelectionIndex() + 1) : "");
            wInputList.setItem(wInputList.getSelectionIndex(), label);
        }
    });
    wbInputStep.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            String stepName = selectTransformationStepname(input, input);
            if (stepName != null) {
                wInputStep.setText(stepName);
                definitions.get(wInputList.getSelectionIndex()).setInputStepname(stepName);
            }
        }
    });
    // What's the step name to read from? (empty is OK too)
    // 
    final Label wlOutputStep = new Label(wFieldsComposite, SWT.RIGHT);
    props.setLook(wlOutputStep);
    wlOutputStep.setText(outputStepLabel);
    FormData fdlOutputStep = new FormData();
    fdlOutputStep.top = new FormAttachment(wInputStep, 10);
    fdlOutputStep.left = new FormAttachment(0);
    wlOutputStep.setLayoutData(fdlOutputStep);
    final Button wbOutputStep = new Button(wFieldsComposite, SWT.PUSH);
    props.setLook(wbOutputStep);
    wbOutputStep.setText(BaseMessages.getString(PKG, "MappingDialog.button.SourceStepName"));
    FormData fdbOutputStep = new FormData();
    fdbOutputStep.top = new FormAttachment(wlOutputStep, 5);
    fdbOutputStep.right = new FormAttachment(100);
    wbOutputStep.setLayoutData(fdbOutputStep);
    final Text wOutputStep = new Text(wFieldsComposite, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
    props.setLook(wOutputStep);
    wOutputStep.addModifyListener(lsMod);
    FormData fdOutputStep = new FormData();
    fdOutputStep.top = new FormAttachment(wlOutputStep, 5);
    // To the right of
    fdOutputStep.left = new FormAttachment(0);
    // the label
    fdOutputStep.right = new FormAttachment(wbOutputStep, -5);
    wOutputStep.setLayoutData(fdOutputStep);
    // Allow for a small description
    // 
    Label wlDescription = new Label(wFieldsComposite, SWT.RIGHT);
    props.setLook(wlDescription);
    wlDescription.setText(descriptionLabel);
    FormData fdlDescription = new FormData();
    fdlDescription.top = new FormAttachment(wOutputStep, 5);
    // First one in the left
    fdlDescription.left = new FormAttachment(0);
    wlDescription.setLayoutData(fdlDescription);
    final Text wDescription = new Text(wFieldsComposite, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
    props.setLook(wDescription);
    wDescription.addModifyListener(lsMod);
    FormData fdDescription = new FormData();
    fdDescription.top = new FormAttachment(wlDescription, 5);
    // To the right of
    fdDescription.left = new FormAttachment(0);
    // the label
    fdDescription.right = new FormAttachment(100);
    wDescription.setLayoutData(fdDescription);
    wDescription.addFocusListener(new FocusAdapter() {

        @Override
        public void focusLost(FocusEvent event) {
            definitions.get(wInputList.getSelectionIndex()).setDescription(wDescription.getText());
        }
    });
    final Button wbEnterMapping = new Button(wFieldsComposite, SWT.PUSH);
    props.setLook(wbEnterMapping);
    wbEnterMapping.setText(BaseMessages.getString(PKG, "MappingDialog.button.EnterMapping"));
    FormData fdbEnterMapping = new FormData();
    fdbEnterMapping.bottom = new FormAttachment(100);
    fdbEnterMapping.right = new FormAttachment(100);
    wbEnterMapping.setLayoutData(fdbEnterMapping);
    wbEnterMapping.setEnabled(input);
    ColumnInfo[] colinfo = new ColumnInfo[] { new ColumnInfo(sourceColumnLabel, ColumnInfo.COLUMN_TYPE_TEXT, false, false), new ColumnInfo(targetColumnLabel, ColumnInfo.COLUMN_TYPE_TEXT, false, false) };
    final TableView wFieldMappings = new TableView(transMeta, wFieldsComposite, SWT.FULL_SELECTION | SWT.SINGLE | SWT.BORDER, colinfo, 1, false, lsMod, props, false);
    props.setLook(wFieldMappings);
    FormData fdMappings = new FormData();
    fdMappings.top = new FormAttachment(wDescription, 20);
    fdMappings.bottom = new FormAttachment(wbEnterMapping, -5);
    fdMappings.left = new FormAttachment(0);
    fdMappings.right = new FormAttachment(100);
    wFieldMappings.setLayoutData(fdMappings);
    wFieldMappings.getTable().addListener(SWT.Resize, new ColumnsResizer(0, 50, 50));
    wbEnterMapping.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent arg0) {
            try {
                RowMetaInterface sourceRowMeta = getFieldsFromStep(wInputStep.getText(), true, input);
                RowMetaInterface targetRowMeta = getFieldsFromStep(wOutputStep.getText(), false, input);
                String[] sourceFields = sourceRowMeta.getFieldNames();
                String[] targetFields = targetRowMeta.getFieldNames();
                // Refresh mappings
                int nrLines = wFieldMappings.nrNonEmpty();
                definitions.get(wInputList.getSelectionIndex()).getValueRenames().clear();
                for (int i = 0; i < nrLines; i++) {
                    TableItem item = wFieldMappings.getNonEmpty(i);
                    definitions.get(wInputList.getSelectionIndex()).getValueRenames().add(new MappingValueRename(item.getText(1), item.getText(2)));
                }
                List<MappingValueRename> mappingValue = definitions.get(wInputList.getSelectionIndex()).getValueRenames();
                List<SourceToTargetMapping> currentMappings = MappingUtil.getCurrentMappings(Arrays.asList(sourceFields), Arrays.asList(targetFields), mappingValue);
                EnterMappingDialog dialog = new EnterMappingDialog(shell, sourceFields, targetFields, currentMappings);
                List<SourceToTargetMapping> mappings = dialog.open();
                if (mappings != null) {
                    // first clear the dialog...
                    wFieldMappings.clearAll(false);
                    // 
                    mappingValue.clear();
                    // Now add the new values...
                    for (SourceToTargetMapping mapping : mappings) {
                        TableItem item = new TableItem(wFieldMappings.table, SWT.NONE);
                        item.setText(1, mapping.getSourceString(sourceFields));
                        item.setText(2, mapping.getTargetString(targetFields));
                        String source = input ? item.getText(1) : item.getText(2);
                        String target = input ? item.getText(2) : item.getText(1);
                        mappingValue.add(new MappingValueRename(source, target));
                    }
                    wFieldMappings.removeEmptyRows();
                    wFieldMappings.setRowNums();
                    wFieldMappings.optWidth(true);
                }
            } catch (KettleException e) {
                Listener ok = new Listener() {

                    @Override
                    public void handleEvent(final Event event) {
                    /* do nothing for now */
                    }
                };
                Map<String, Listener> listenerMap = new LinkedHashMap<>();
                listenerMap.put(BaseMessages.getString("System.Button.OK"), ok);
                new WarningDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"), e.getMessage(), listenerMap);
            // new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.Error.Title" ), BaseMessages.getString(
            // PKG, "MappingDialog.Exception.ErrorGettingMappingSourceAndTargetFields", e.toString() ), e );
            }
        }
    });
    wOutputStep.addFocusListener(new FocusAdapter() {

        @Override
        public void focusLost(FocusEvent event) {
            definitions.get(wInputList.getSelectionIndex()).setOutputStepname(wOutputStep.getText());
            try {
                enableMappingButton(wbEnterMapping, input, wInputStep.getText(), wOutputStep.getText());
            } catch (KettleException e) {
                // Show the missing/wrong step name error
                // 
                Listener ok = new Listener() {

                    @Override
                    public void handleEvent(final Event event) {
                    /* do nothing for now */
                    }
                };
                Map<String, Listener> listenerMap = new LinkedHashMap<>();
                listenerMap.put(BaseMessages.getString("System.Button.OK"), ok);
                new WarningDialog(shell, "Error", e.getMessage(), listenerMap).open();
            // 
            // new ErrorDialog( shell, "Error", "Unexpected error", e );
            }
        }
    });
    wbOutputStep.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            String stepName = selectTransformationStepname(!input, input);
            if (stepName != null) {
                wOutputStep.setText(stepName);
                definitions.get(wInputList.getSelectionIndex()).setOutputStepname(stepName);
                try {
                    enableMappingButton(wbEnterMapping, input, wInputStep.getText(), wOutputStep.getText());
                } catch (KettleException e) {
                    // Show the missing/wrong stepname error
                    new ErrorDialog(shell, "Error", "Unexpected error", e);
                }
            }
        }
    });
    final Button wRenameOutput;
    if (input) {
        // Add a checkbox to indicate that all output mappings need to rename
        // the values back...
        // 
        wRenameOutput = new Button(wFieldsComposite, SWT.CHECK);
        wRenameOutput.setText(BaseMessages.getString(PKG, "MappingDialog.input.RenamingOnOutput"));
        props.setLook(wRenameOutput);
        FormData fdRenameOutput = new FormData();
        fdRenameOutput.top = new FormAttachment(wFieldMappings, 5);
        fdRenameOutput.left = new FormAttachment(0);
        wRenameOutput.setLayoutData(fdRenameOutput);
        wRenameOutput.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent event) {
                definitions.get(wInputList.getSelectionIndex()).setRenamingOnOutput(!definitions.get(wInputList.getSelectionIndex()).isRenamingOnOutput());
            }
        });
    } else {
        wRenameOutput = null;
    }
    FormData fdInputComposite = new FormData();
    fdInputComposite.left = new FormAttachment(0);
    fdInputComposite.top = new FormAttachment(0);
    fdInputComposite.right = new FormAttachment(100);
    fdInputComposite.bottom = new FormAttachment(100);
    wInputComposite.setLayoutData(fdInputComposite);
    FormData fdFieldsComposite = new FormData();
    fdFieldsComposite.left = new FormAttachment(wInputList, 30);
    fdFieldsComposite.right = new FormAttachment(100);
    fdFieldsComposite.bottom = new FormAttachment(100);
    fdFieldsComposite.top = new FormAttachment(0);
    wFieldsComposite.setLayoutData(fdFieldsComposite);
    wInputComposite.layout();
    wTab.setControl(wInputComposite);
    wMainPath.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent arg0) {
            setTabFlags(wMainPath, wlInputStep, wInputStep, wbInputStep, wlOutputStep, wOutputStep, wbOutputStep, wlDescription, wDescription);
        }
    });
    wInputList.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent selectionEvent) {
            updateFields(definitions.get(wInputList.getSelectionIndex()), input, wMainPath, wlInputStep, wInputStep, wbInputStep, wlOutputStep, wOutputStep, wbOutputStep, wlDescription, wDescription, wFieldMappings, wRenameOutput);
        }
    });
    wAddButton.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseUp(MouseEvent mouseEvent) {
            MappingIODefinition definition = new MappingIODefinition();
            definition.setMainDataPath(true);
            definitions.add(definition);
            wInputList.add(tabTitle + (definitions.size() > 1 ? String.valueOf(definitions.size()) : ""));
            wInputList.select(definitions.size() - 1);
            updateFields(definitions.get(wInputList.getSelectionIndex()), input, wMainPath, wlInputStep, wInputStep, wbInputStep, wlOutputStep, wOutputStep, wbOutputStep, wlDescription, wDescription, wFieldMappings, wRenameOutput);
            wlNoItems.setVisible(false);
            wFieldsComposite.setVisible(true);
            wRemoveButton.setEnabled(true);
        }
    });
    wRemoveButton.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseUp(MouseEvent mouseEvent) {
            MessageBox box = new MessageBox(shell, SWT.YES | SWT.NO);
            box.setText(BaseMessages.getString(PKG, "MappingDialog.CloseDefinitionTabAreYouSure.Title"));
            box.setMessage(BaseMessages.getString(PKG, "MappingDialog.CloseDefinitionTabAreYouSure.Message"));
            int answer = box.open();
            if (answer != SWT.YES) {
                return;
            }
            int index = wInputList.getSelectionIndex();
            definitions.remove(index);
            wInputList.removeAll();
            for (int i = 0; i < definitions.size(); i++) {
                String label = !Utils.isEmpty(definitions.get(i).getInputStepname()) ? definitions.get(i).getInputStepname() : tabTitle + (i > 0 ? String.valueOf(i + 1) : "");
                wInputList.add(label);
            }
            if (index > 0) {
                wInputList.select(index - 1);
            } else if (definitions.size() > 0) {
                wInputList.select(index);
            } else {
                index = -1;
            }
            if (index != -1) {
                updateFields(definitions.get(wInputList.getSelectionIndex()), input, wMainPath, wlInputStep, wInputStep, wbInputStep, wlOutputStep, wOutputStep, wbOutputStep, wlDescription, wDescription, wFieldMappings, wRenameOutput);
            }
            if (definitions.size() == 0) {
                wlNoItems.setVisible(true);
                wFieldsComposite.setVisible(false);
                wRemoveButton.setEnabled(false);
            }
        }
    });
    if (definitions.size() > 0) {
        wInputList.select(0);
        updateFields(definitions.get(0), input, wMainPath, wlInputStep, wInputStep, wbInputStep, wlOutputStep, wOutputStep, wbOutputStep, wlDescription, wDescription, wFieldMappings, wRenameOutput);
    } else {
        wlNoItems.setVisible(true);
        wFieldsComposite.setVisible(false);
        wRemoveButton.setEnabled(false);
    }
    setTabFlags(wMainPath, wlInputStep, wInputStep, wbInputStep, wlOutputStep, wOutputStep, wbOutputStep, wlDescription, wDescription);
    wTabFolder.setSelection(wTab);
}
Also used : MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) FocusAdapter(org.eclipse.swt.events.FocusAdapter) KettleException(org.pentaho.di.core.exception.KettleException) Listener(org.eclipse.swt.widgets.Listener) ModifyListener(org.eclipse.swt.events.ModifyListener) ColumnsResizer(org.pentaho.di.ui.core.widget.ColumnsResizer) EnterMappingDialog(org.pentaho.di.ui.core.dialog.EnterMappingDialog) MappingIODefinition(org.pentaho.di.trans.steps.mapping.MappingIODefinition) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) ColumnInfo(org.pentaho.di.ui.core.widget.ColumnInfo) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) CTabItem(org.eclipse.swt.custom.CTabItem) FocusEvent(org.eclipse.swt.events.FocusEvent) LinkedHashMap(java.util.LinkedHashMap) WarningDialog(org.pentaho.di.ui.core.dialog.WarningDialog) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) SourceToTargetMapping(org.pentaho.di.core.SourceToTargetMapping) List(java.util.List) ArrayList(java.util.ArrayList) FormAttachment(org.eclipse.swt.layout.FormAttachment) TableView(org.pentaho.di.ui.core.widget.TableView) FormLayout(org.eclipse.swt.layout.FormLayout) FormData(org.eclipse.swt.layout.FormData) MouseEvent(org.eclipse.swt.events.MouseEvent) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MouseAdapter(org.eclipse.swt.events.MouseAdapter) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) Text(org.eclipse.swt.widgets.Text) MessageBox(org.eclipse.swt.widgets.MessageBox) FocusEvent(org.eclipse.swt.events.FocusEvent) ModifyEvent(org.eclipse.swt.events.ModifyEvent) MouseEvent(org.eclipse.swt.events.MouseEvent) Event(org.eclipse.swt.widgets.Event) ShellEvent(org.eclipse.swt.events.ShellEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Example 13 with MappingValueRename

use of org.pentaho.di.trans.steps.mapping.MappingValueRename in project pentaho-kettle by pentaho.

the class MappingDialog method updateFields.

private void updateFields(MappingIODefinition definition, boolean input, Button wMainPath, Label wlInputStep, Text wInputStep, Button wbInputStep, Label wlOutputStep, Text wOutputStep, Button wbOutputStep, Label wlDescription, Text wDescription, TableView wFieldMappings, Button wRenameOutput) {
    if (tabChanges != null) {
        tabChanges.applyChanges();
    }
    wMainPath.setSelection(definition.isMainDataPath());
    wInputStep.setText(Const.NVL(definition.getInputStepname(), ""));
    wOutputStep.setText(Const.NVL(definition.getOutputStepname(), ""));
    wDescription.setText(Const.NVL(definition.getDescription(), ""));
    setTabFlags(wMainPath, wlInputStep, wInputStep, wbInputStep, wlOutputStep, wOutputStep, wbOutputStep, wlDescription, wDescription);
    wFieldMappings.removeAll();
    for (MappingValueRename valueRename : definition.getValueRenames()) {
        TableItem tableItem = new TableItem(wFieldMappings.table, SWT.NONE);
        tableItem.setText(1, Const.NVL(valueRename.getSourceValueName(), ""));
        tableItem.setText(2, Const.NVL(valueRename.getTargetValueName(), ""));
    }
    wFieldMappings.removeEmptyRows();
    wFieldMappings.setRowNums();
    wFieldMappings.optWidth(true);
    if (input) {
        wRenameOutput.setSelection(definition.isRenamingOnOutput());
    }
    tabChanges = new MappingDefinitionTab(definition, wInputStep, wOutputStep, wMainPath, wDescription, wFieldMappings);
}
Also used : MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) TableItem(org.eclipse.swt.widgets.TableItem)

Example 14 with MappingValueRename

use of org.pentaho.di.trans.steps.mapping.MappingValueRename in project pentaho-kettle by pentaho.

the class SimpleMappingDialog method addMappingDefinitionTab.

private void addMappingDefinitionTab(final MappingIODefinition definition, int index, final String tabTitle, final String tabTooltip, String sourceColumnLabel, String targetColumnLabel, final boolean input) {
    final CTabItem wTab;
    if (index >= wTabFolder.getItemCount()) {
        wTab = new CTabItem(wTabFolder, SWT.CLOSE);
    } else {
        wTab = new CTabItem(wTabFolder, SWT.CLOSE, index);
    }
    setMappingDefinitionTabNameAndToolTip(wTab, tabTitle, tabTooltip, definition, input);
    Composite wInputComposite = new Composite(wTabFolder, SWT.NONE);
    props.setLook(wInputComposite);
    FormLayout tabLayout = new FormLayout();
    tabLayout.marginWidth = 15;
    tabLayout.marginHeight = 15;
    wInputComposite.setLayout(tabLayout);
    // Now add a table view with the 2 columns to specify: input and output
    // fields for the source and target steps.
    // 
    final Button wbEnterMapping = new Button(wInputComposite, SWT.PUSH);
    props.setLook(wbEnterMapping);
    if (input) {
        wbEnterMapping.setText(BaseMessages.getString(PKG, "SimpleMappingDialog.button.EnterMapping"));
    } else {
        wbEnterMapping.setText(BaseMessages.getString(PKG, "SimpleMappingDialog.button.GetFields"));
    }
    FormData fdbEnterMapping = new FormData();
    fdbEnterMapping.bottom = new FormAttachment(100);
    fdbEnterMapping.right = new FormAttachment(100);
    wbEnterMapping.setLayoutData(fdbEnterMapping);
    ColumnInfo[] colinfo = new ColumnInfo[] { new ColumnInfo(sourceColumnLabel, ColumnInfo.COLUMN_TYPE_TEXT, false, false), new ColumnInfo(targetColumnLabel, ColumnInfo.COLUMN_TYPE_TEXT, false, false) };
    final TableView wFieldMappings = new TableView(transMeta, wInputComposite, SWT.FULL_SELECTION | SWT.SINGLE | SWT.BORDER, colinfo, 1, false, lsMod, props, false);
    props.setLook(wFieldMappings);
    FormData fdMappings = new FormData();
    fdMappings.left = new FormAttachment(0);
    fdMappings.right = new FormAttachment(100);
    fdMappings.top = new FormAttachment(0);
    fdMappings.bottom = new FormAttachment(wbEnterMapping, -10);
    wFieldMappings.setLayoutData(fdMappings);
    wFieldMappings.getTable().addListener(SWT.Resize, new ColumnsResizer(0, 50, 50));
    for (MappingValueRename valueRename : definition.getValueRenames()) {
        TableItem tableItem = new TableItem(wFieldMappings.table, SWT.NONE);
        tableItem.setText(1, Const.NVL(valueRename.getSourceValueName(), ""));
        tableItem.setText(2, Const.NVL(valueRename.getTargetValueName(), ""));
    }
    wFieldMappings.removeEmptyRows();
    wFieldMappings.setRowNums();
    wFieldMappings.optWidth(true);
    wbEnterMapping.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent arg0) {
            try {
                if (input) {
                    // INPUT
                    // 
                    RowMetaInterface sourceRowMeta = getFieldsFromStep(true, input);
                    RowMetaInterface targetRowMeta = getFieldsFromStep(false, input);
                    String[] sourceFields = sourceRowMeta.getFieldNames();
                    String[] targetFields = targetRowMeta.getFieldNames();
                    // refresh mappings
                    int nrLines = wFieldMappings.nrNonEmpty();
                    definition.getValueRenames().clear();
                    for (int i = 0; i < nrLines; i++) {
                        TableItem item = wFieldMappings.getNonEmpty(i);
                        definition.getValueRenames().add(new MappingValueRename(item.getText(1), item.getText(2)));
                    }
                    List<MappingValueRename> mappingValue = definition.getValueRenames();
                    List<SourceToTargetMapping> currentMappings = MappingUtil.getCurrentMappings(Arrays.asList(sourceFields), Arrays.asList(targetFields), mappingValue);
                    EnterMappingDialog dialog = new EnterMappingDialog(shell, sourceFields, targetFields, currentMappings);
                    List<SourceToTargetMapping> mappings = dialog.open();
                    if (mappings != null) {
                        // first clear the dialog...
                        wFieldMappings.clearAll(false);
                        // 
                        definition.getValueRenames().clear();
                        // Now add the new values...
                        for (int i = 0; i < mappings.size(); i++) {
                            SourceToTargetMapping mapping = mappings.get(i);
                            TableItem item = new TableItem(wFieldMappings.table, SWT.NONE);
                            item.setText(1, mapping.getSourceString(sourceFields));
                            item.setText(2, mapping.getTargetString(targetFields));
                            String source = input ? item.getText(1) : item.getText(2);
                            String target = input ? item.getText(2) : item.getText(1);
                            definition.getValueRenames().add(new MappingValueRename(source, target));
                        }
                        wFieldMappings.removeEmptyRows();
                        wFieldMappings.setRowNums();
                        wFieldMappings.optWidth(true);
                    }
                } else {
                    // OUTPUT
                    // 
                    RowMetaInterface sourceRowMeta = getFieldsFromStep(true, input);
                    BaseStepDialog.getFieldsFromPrevious(sourceRowMeta, wFieldMappings, 1, new int[] { 1 }, new int[] {}, -1, -1, null);
                }
            } catch (KettleException e) {
                new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"), BaseMessages.getString(PKG, "SimpleMappingDialog.Exception.ErrorGettingMappingSourceAndTargetFields", e.toString()), e);
            }
        }
    });
    if (input) {
        Button wRenameOutput = new Button(wInputComposite, SWT.CHECK);
        props.setLook(wRenameOutput);
        wRenameOutput.setText(BaseMessages.getString(PKG, "SimpleMappingDialog.input.RenamingOnOutput"));
        FormData fdRenameOutput = new FormData();
        fdRenameOutput.top = new FormAttachment(wFieldMappings, 10);
        fdRenameOutput.left = new FormAttachment(0, 0);
        wRenameOutput.setLayoutData(fdRenameOutput);
        wRenameOutput.setSelection(definition.isRenamingOnOutput());
        wRenameOutput.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent event) {
                // flip the switch
                definition.setRenamingOnOutput(!definition.isRenamingOnOutput());
            }
        });
    }
    FormData fdParametersComposite = new FormData();
    fdParametersComposite.left = new FormAttachment(0, 0);
    fdParametersComposite.top = new FormAttachment(0, 0);
    fdParametersComposite.right = new FormAttachment(100, 0);
    fdParametersComposite.bottom = new FormAttachment(100, 0);
    wInputComposite.setLayoutData(fdParametersComposite);
    wInputComposite.layout();
    wTab.setControl(wInputComposite);
    final ApplyChanges applyChanges = new MappingDefinitionTab(definition, wFieldMappings);
    changeList.add(applyChanges);
    wTabFolder.setSelection(wTab);
}
Also used : FormLayout(org.eclipse.swt.layout.FormLayout) FormData(org.eclipse.swt.layout.FormData) MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) KettleException(org.pentaho.di.core.exception.KettleException) Composite(org.eclipse.swt.widgets.Composite) ColumnsResizer(org.pentaho.di.ui.core.widget.ColumnsResizer) EnterMappingDialog(org.pentaho.di.ui.core.dialog.EnterMappingDialog) TableItem(org.eclipse.swt.widgets.TableItem) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ColumnInfo(org.pentaho.di.ui.core.widget.ColumnInfo) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) CTabItem(org.eclipse.swt.custom.CTabItem) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) SourceToTargetMapping(org.pentaho.di.core.SourceToTargetMapping) List(java.util.List) ArrayList(java.util.ArrayList) FormAttachment(org.eclipse.swt.layout.FormAttachment) TableView(org.pentaho.di.ui.core.widget.TableView)

Example 15 with MappingValueRename

use of org.pentaho.di.trans.steps.mapping.MappingValueRename in project pentaho-metaverse by pentaho.

the class BaseMappingAnalyzer method createLinks.

private void createLinks(final TransMeta subTransMeta, final StepWithMappingMeta meta) {
    boolean renameFields = shouldRenameFields(meta);
    final Map<String, String> fieldRenames = new HashMap();
    // process input mappings
    for (final MappingIODefinition inputMapping : meta.getInputMappings()) {
        final Vertex inputSourceVertex = getInputSourceStepVertex(parentTransMeta, inputMapping);
        final Vertex inputTargetVertex = getInputTargetStepVertex(subTransMeta, inputMapping);
        final String inputTargetName = inputTargetVertex.getProperty(DictionaryConst.PROPERTY_NAME);
        final List<MappingValueRename> renames = inputMapping.getValueRenames();
        for (final MappingValueRename rename : renames) {
            // This is deliberately target > source, since we are going to be looking up the target to rename back to the
            // source
            fieldRenames.put(rename.getTargetValueName(), rename.getSourceValueName());
        }
        // traverse output fields of the input source step
        // for each field, if a rename exists, create a "derives" link from the field to the input target field with
        // the name defined in the "Fieldname to mapping input step" column; otherwise create a "derives" link to a
        // field with the same name
        final Iterator<Vertex> inputSourceOutputFields = inputSourceVertex.getVertices(Direction.OUT, DictionaryConst.LINK_OUTPUTS).iterator();
        while (inputSourceOutputFields.hasNext()) {
            final Vertex inputSourceOutputField = inputSourceOutputFields.next();
            final String inputSourceOutputFieldName = inputSourceOutputField.getProperty(DictionaryConst.PROPERTY_NAME);
            // is there a rename for this field?
            final MappingValueRename renameMapping = inputMapping.getValueRenames().stream().filter(rename -> inputSourceOutputFieldName.equals(rename.getSourceValueName())).findAny().orElse(null);
            // if there is no rename for this field, we look for a field with the same name, otherwise we look for a field
            // that is the target of the rename mapping ( defined within the "Fieldname to mapping input step" column);
            // we look for this field within the sub-transformation and within the context of the input target step
            final String derivedFieldName = renameMapping == null ? inputSourceOutputFieldName : renameMapping.getTargetValueName();
            final Vertex derivedField = findFieldVertex(subTransMeta, inputTargetName, derivedFieldName);
            // add an "inputs" link from the field of the input source step to the input target step
            metaverseBuilder.addLink(inputSourceOutputField, DictionaryConst.LINK_INPUTS, inputTargetVertex);
            if (derivedField == null) {
                LOGGER.debug(Messages.getString("WARN.TargetInputFieldNotFound", meta.getName(), inputSourceVertex.getProperty(DictionaryConst.PROPERTY_NAME), inputSourceOutputField.getProperty(DictionaryConst.PROPERTY_NAME), subTransMeta.getName(), inputTargetName));
            } else {
                // add a "derives" link from the field of the input source step to the output field of the input target step
                metaverseBuilder.addLink(inputSourceOutputField, DictionaryConst.LINK_DERIVES, derivedField);
            }
        }
    }
    // process output mappings
    for (final MappingIODefinition outputMapping : meta.getOutputMappings()) {
        final Vertex outputSourceVertex = getOutputSourceStepVertex(subTransMeta, outputMapping);
        final List<Vertex> outputTargetVertices = getOutputTargetStepVertices(parentTransMeta, outputMapping);
        // traverse output fields of the output source step
        // for each field, if a rename exists, create a "derives" link from the field to the input target field with
        // the name defined in the "Fieldname to mapping input step" column; otherwise create a "derives" link to a
        // field with the same name
        final Iterator<Vertex> outputSourceFields = outputSourceVertex.getVertices(Direction.OUT, DictionaryConst.LINK_OUTPUTS).iterator();
        while (outputSourceFields.hasNext()) {
            final Vertex outputSourceField = outputSourceFields.next();
            final String outputSourceFieldName = outputSourceField.getProperty(DictionaryConst.PROPERTY_NAME);
            String derivedFieldName = outputSourceFieldName;
            // is there a rename mapping for this field?
            final MappingValueRename renameMapping = outputMapping.getValueRenames().stream().filter(rename -> outputSourceFieldName.equals(rename.getSourceValueName())).findAny().orElse(null);
            // otherwise the derived field name is expected to be the same
            if (renameMapping != null) {
                derivedFieldName = renameMapping.getTargetValueName();
            } else if (renameFields && fieldRenames.containsKey(outputSourceFieldName)) {
                derivedFieldName = fieldRenames.get(outputSourceFieldName);
            }
            for (final Vertex outputTargetVertex : outputTargetVertices) {
                final String outputTargetName = outputTargetVertex.getProperty(DictionaryConst.PROPERTY_NAME);
                final Vertex derivedField = findFieldVertex(parentTransMeta, outputTargetName, derivedFieldName);
                // add an "inputs" link from the field of the input source step to the input target step
                metaverseBuilder.addLink(outputSourceField, DictionaryConst.LINK_INPUTS, outputTargetVertex);
                if (derivedField == null) {
                    LOGGER.debug(Messages.getString("WARN.TargetInputFieldNotFound", meta.getName(), outputSourceVertex.getProperty(DictionaryConst.PROPERTY_NAME), outputSourceField.getProperty(DictionaryConst.PROPERTY_NAME), subTransMeta.getName(), outputTargetName));
                } else {
                    // add a "derives" link from the field of the output source step to the output field of the output target step
                    metaverseBuilder.addLink(outputSourceField, DictionaryConst.LINK_DERIVES, derivedField);
                }
                // if the output target step has any virtual input fields that themselves aren't output by any step, remove
                // them; these are the orphaned output steps of the mapping step that the base analyzer code wasn't able to
                // resolve, and since the mapping step should not have any output fields, these can be removed
                final List<Vertex> allOutputTargetInputFields = IteratorUtils.toList(outputTargetVertex.getVertices(Direction.IN, DictionaryConst.LINK_INPUTS).iterator());
                for (final Vertex outputTargetInputField : allOutputTargetInputFields) {
                    if ("true".equals(outputTargetInputField.getProperty(DictionaryConst.NODE_VIRTUAL).toString())) {
                        // check further that this field does not have a containing step that outputs it
                        final Iterator<Vertex> parentSteps = outputTargetInputField.getVertices(Direction.IN, DictionaryConst.LINK_OUTPUTS).iterator();
                        boolean hasParent = false;
                        while (parentSteps.hasNext()) {
                            hasParent = true;
                            break;
                        }
                        if (!hasParent) {
                            outputTargetInputField.remove();
                        }
                    }
                }
            }
        }
    }
}
Also used : MappingValueRename(org.pentaho.di.trans.steps.mapping.MappingValueRename) Vertex(com.tinkerpop.blueprints.Vertex) HashMap(java.util.HashMap) MappingIODefinition(org.pentaho.di.trans.steps.mapping.MappingIODefinition)

Aggregations

MappingValueRename (org.pentaho.di.trans.steps.mapping.MappingValueRename)23 RowMeta (org.pentaho.di.core.row.RowMeta)7 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)7 ArrayList (java.util.ArrayList)6 KettleException (org.pentaho.di.core.exception.KettleException)6 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)6 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)5 MappingIODefinition (org.pentaho.di.trans.steps.mapping.MappingIODefinition)5 KettleStepException (org.pentaho.di.core.exception.KettleStepException)4 HashMap (java.util.HashMap)3 TableItem (org.eclipse.swt.widgets.TableItem)3 SourceToTargetMapping (org.pentaho.di.core.SourceToTargetMapping)3 List (java.util.List)2 CTabItem (org.eclipse.swt.custom.CTabItem)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 FormAttachment (org.eclipse.swt.layout.FormAttachment)2 FormData (org.eclipse.swt.layout.FormData)2 FormLayout (org.eclipse.swt.layout.FormLayout)2 Button (org.eclipse.swt.widgets.Button)2