Search in sources :

Example 16 with SourceToTargetMapping

use of org.pentaho.di.core.SourceToTargetMapping in project pentaho-kettle by pentaho.

the class Spoon method generateFieldMapping.

/**
 * Create a new SelectValues step in between this step and the previous. If the previous fields are not there, no
 * mapping can be made, same with the required fields.
 *
 * @param stepMeta
 *          The target step to map against.
 */
// retry of required fields acquisition
public void generateFieldMapping(TransMeta transMeta, StepMeta stepMeta) {
    try {
        if (stepMeta != null) {
            StepMetaInterface smi = stepMeta.getStepMetaInterface();
            RowMetaInterface targetFields = smi.getRequiredFields(transMeta);
            RowMetaInterface sourceFields = transMeta.getPrevStepFields(stepMeta);
            // Build the mapping: let the user decide!!
            String[] source = sourceFields.getFieldNames();
            for (int i = 0; i < source.length; i++) {
                ValueMetaInterface v = sourceFields.getValueMeta(i);
                source[i] += EnterMappingDialog.STRING_ORIGIN_SEPARATOR + v.getOrigin() + ")";
            }
            String[] target = targetFields.getFieldNames();
            EnterMappingDialog dialog = new EnterMappingDialog(shell, source, target);
            List<SourceToTargetMapping> mappings = dialog.open();
            if (mappings != null) {
                // OK, so we now know which field maps where.
                // This allows us to generate the mapping using a
                // SelectValues Step...
                SelectValuesMeta svm = new SelectValuesMeta();
                svm.allocate(mappings.size(), 0, 0);
                // CHECKSTYLE:Indentation:OFF
                for (int i = 0; i < mappings.size(); i++) {
                    SourceToTargetMapping mapping = mappings.get(i);
                    svm.getSelectFields()[i].setName(sourceFields.getValueMeta(mapping.getSourcePosition()).getName());
                    svm.getSelectFields()[i].setRename(target[mapping.getTargetPosition()]);
                    svm.getSelectFields()[i].setLength(-1);
                    svm.getSelectFields()[i].setPrecision(-1);
                }
                // a new comment. Sincerely yours CO ;)
                // Now that we have the meta-data, create a new step info object
                String stepName = stepMeta.getName() + " Mapping";
                // if
                stepName = transMeta.getAlternativeStepname(stepName);
                // it's already there, rename it.
                StepMeta newStep = new StepMeta("SelectValues", stepName, svm);
                newStep.setLocation(stepMeta.getLocation().x + 20, stepMeta.getLocation().y + 20);
                newStep.setDraw(true);
                transMeta.addStep(newStep);
                addUndoNew(transMeta, new StepMeta[] { newStep }, new int[] { transMeta.indexOfStep(newStep) });
                // Redraw stuff...
                refreshTree();
                refreshGraph();
            }
        } else {
            throw new KettleException("There is no target to do a field mapping against!");
        }
    } catch (KettleException e) {
        new ErrorDialog(shell, "Error creating mapping", "There was an error when Kettle tried to generate a field mapping against the target step", e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) EnterMappingDialog(org.pentaho.di.ui.core.dialog.EnterMappingDialog) StepMetaInterface(org.pentaho.di.trans.step.StepMetaInterface) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) StepMeta(org.pentaho.di.trans.step.StepMeta) Point(org.pentaho.di.core.gui.Point) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) SelectValuesMeta(org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta) SourceToTargetMapping(org.pentaho.di.core.SourceToTargetMapping)

Example 17 with SourceToTargetMapping

use of org.pentaho.di.core.SourceToTargetMapping in project pentaho-kettle by pentaho.

the class EnterMappingDialog method refreshMappings.

private void refreshMappings() {
    // Refresh the results...
    wResult.removeAll();
    for (int i = 0; i < mappings.size(); i++) {
        SourceToTargetMapping mapping = mappings.get(i);
        String mappingString = sourceList[mapping.getSourcePosition()] + " --> " + targetList[mapping.getTargetPosition()];
        wResult.add(mappingString);
    }
    wSource.removeAll();
    // Refresh the sources
    for (int a = 0; a < sourceList.length; a++) {
        boolean found = false;
        if (wSourceHide.getSelection()) {
            for (int b = 0; b < mappings.size() && !found; b++) {
                SourceToTargetMapping mapping = mappings.get(b);
                if (mapping.getSourcePosition() == Const.indexOfString(sourceList[a], sourceList)) {
                    found = true;
                }
            }
        }
        if (!found) {
            wSource.add(sourceList[a]);
        }
    }
    wTarget.removeAll();
    // Refresh the targets
    for (int a = 0; a < targetList.length; a++) {
        boolean found = false;
        if (wTargetHide.getSelection()) {
            for (int b = 0; b < mappings.size() && !found; b++) {
                SourceToTargetMapping mapping = mappings.get(b);
                if (mapping.getTargetPosition() == Const.indexOfString(targetList[a], targetList)) {
                    found = true;
                }
            }
        }
        if (!found) {
            wTarget.add(targetList[a]);
        }
    }
}
Also used : SourceToTargetMapping(org.pentaho.di.core.SourceToTargetMapping)

Example 18 with SourceToTargetMapping

use of org.pentaho.di.core.SourceToTargetMapping in project pentaho-kettle by pentaho.

the class EnterMappingDialog method guess.

private void guess() {
    // Guess the target for all the sources...
    String[] sortedSourceList = Arrays.copyOf(sourceList, sourceList.length);
    // Sort Longest to Shortest string - makes matching better
    Arrays.sort(sortedSourceList, new Comparator<String>() {

        @Override
        public int compare(String s1, String s2) {
            return s2.length() - s1.length();
        }
    });
    // Look for matches using longest field name to shortest
    ArrayList<GuessPair> pList = new ArrayList<GuessPair>();
    for (int i = 0; i < sourceList.length; i++) {
        int idx = Const.indexOfString(sortedSourceList[i], wSource.getItems());
        if (idx >= 0) {
            pList.add(findTargetPair(idx));
        }
    }
    // Now add them in order or source field list
    Collections.sort(pList, new Comparator<GuessPair>() {

        @Override
        public int compare(GuessPair s1, GuessPair s2) {
            return s1.getSrcIndex() - s2.getSrcIndex();
        }
    });
    for (GuessPair p : pList) {
        if (p.getFound()) {
            SourceToTargetMapping mapping = new SourceToTargetMapping(p.getSrcIndex(), p.getTargetIndex());
            mappings.add(mapping);
        }
    }
    refreshMappings();
}
Also used : ArrayList(java.util.ArrayList) SourceToTargetMapping(org.pentaho.di.core.SourceToTargetMapping)

Example 19 with SourceToTargetMapping

use of org.pentaho.di.core.SourceToTargetMapping in project pentaho-kettle by pentaho.

the class BaseStepXulDialog method generateFieldMapping.

/**
 * Create a new field mapping between source and target steps.
 *
 * @param shell
 *          the shell of the parent window
 * @param sourceFields
 *          the source fields
 * @param targetFields
 *          the target fields
 * @param fieldMapping
 *          the list of source to target mappings to default to (can be empty but not null)
 * @throws KettleException
 *           in case something goes wrong during the field mapping
 */
public static final void generateFieldMapping(Shell shell, RowMetaInterface sourceFields, RowMetaInterface targetFields, java.util.List<SourceToTargetMapping> fieldMapping) throws KettleException {
    // Build the mapping: let the user decide!!
    String[] source = sourceFields.getFieldNames();
    for (int i = 0; i < source.length; i++) {
        ValueMetaInterface v = sourceFields.getValueMeta(i);
        source[i] += EnterMappingDialog.STRING_ORIGIN_SEPARATOR + v.getOrigin() + ")";
    }
    String[] target = targetFields.getFieldNames();
    EnterMappingDialog dialog = new EnterMappingDialog(shell, source, target, fieldMapping);
    java.util.List<SourceToTargetMapping> newMapping = dialog.open();
    if (newMapping != null) {
        fieldMapping.clear();
        fieldMapping.addAll(newMapping);
    }
}
Also used : EnterMappingDialog(org.pentaho.di.ui.core.dialog.EnterMappingDialog) SourceToTargetMapping(org.pentaho.di.core.SourceToTargetMapping) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 20 with SourceToTargetMapping

use of org.pentaho.di.core.SourceToTargetMapping in project pentaho-kettle by pentaho.

the class LucidDBStreamingLoaderDialog method generateMappings.

/**
 * Reads in the fields from the previous steps and from the ONE next step and opens an EnterMappingDialog with this
 * information. After the user did the mapping, those information is put into the Select/Rename table.
 */
private void generateMappings(String tabName) {
    TableView myTb;
    boolean flag = false;
    if (tabName.equals(BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.KeyTab.TabTitle"))) {
        myTb = wKeysTb;
    } else if (tabName.equals(BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.FieldsTab.TabTitle"))) {
        myTb = wFieldsTb;
        // Hidden Update Field when select operation INSERT
        if (BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.Operation.CCombo.Item2").equalsIgnoreCase(wOperation.getItem(wOperation.getSelectionIndex()))) {
            flag = true;
        }
    } else {
        return;
    }
    // Determine the source and target fields...
    // 
    RowMetaInterface sourceFields;
    RowMetaInterface targetFields;
    try {
        sourceFields = transMeta.getPrevStepFields(stepMeta);
    } catch (KettleException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.DoMapping.UnableToFindSourceFields.Title"), BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.DoMapping.UnableToFindSourceFields.Message"), e);
        return;
    }
    // refresh data
    input.setDatabaseMeta(transMeta.findDatabase(wConnection.getText()));
    input.setTableName(transMeta.environmentSubstitute(wTable.getText()));
    StepMetaInterface stepMetaInterface = stepMeta.getStepMetaInterface();
    try {
        targetFields = stepMetaInterface.getRequiredFields(transMeta);
    } catch (KettleException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.DoMapping.UnableToFindTargetFields.Title"), BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.DoMapping.UnableToFindTargetFields.Message"), e);
        return;
    }
    String[] inputNames = new String[sourceFields.size()];
    for (int i = 0; i < sourceFields.size(); i++) {
        ValueMetaInterface value = sourceFields.getValueMeta(i);
        inputNames[i] = value.getName() + EnterMappingDialog.STRING_ORIGIN_SEPARATOR + value.getOrigin() + ")";
    }
    // Create the existing mapping list...
    // 
    List<SourceToTargetMapping> mappings = new ArrayList<SourceToTargetMapping>();
    StringBuffer missingSourceFields = new StringBuffer();
    StringBuffer missingTargetFields = new StringBuffer();
    // 
    if (missingSourceFields.length() > 0 || missingTargetFields.length() > 0) {
        String message = "";
        if (missingSourceFields.length() > 0) {
            message += BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.DoMapping.SomeSourceFieldsNotFound", missingSourceFields.toString()) + Const.CR;
        }
        if (missingTargetFields.length() > 0) {
            message += BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.DoMapping.SomeTargetFieldsNotFound", missingSourceFields.toString()) + Const.CR;
        }
        message += Const.CR;
        message += BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.DoMapping.SomeFieldsNotFoundContinue") + Const.CR;
        MessageDialog.setDefaultImage(GUIResource.getInstance().getImageSpoon());
        boolean goOn = MessageDialog.openConfirm(shell, BaseMessages.getString(PKG, "LucidDBStreamingLoaderDialog.DoMapping.SomeFieldsNotFoundTitle"), message);
        if (!goOn) {
            return;
        }
    }
    EnterMappingDialog d = new EnterMappingDialog(LucidDBStreamingLoaderDialog.this.shell, sourceFields.getFieldNames(), targetFields.getFieldNames(), mappings);
    mappings = d.open();
    // 
    if (mappings != null) {
        // Clear and re-populate!
        // 
        myTb.table.removeAll();
        myTb.table.setItemCount(mappings.size());
        for (int i = 0; i < mappings.size(); i++) {
            SourceToTargetMapping mapping = mappings.get(i);
            TableItem item = myTb.table.getItem(i);
            item.setText(2, sourceFields.getValueMeta(mapping.getSourcePosition()).getName());
            item.setText(1, targetFields.getValueMeta(mapping.getTargetPosition()).getName());
        }
        myTb.setRowNums();
        myTb.optWidth(true);
        // Hidden Update Field when select INSERT.
        int width = myTb.table.getColumn(3).getWidth();
        if (flag) {
            myTb.table.getColumn(3).setWidth(0);
        } else {
            myTb.table.getColumn(3).setWidth(width);
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) EnterMappingDialog(org.pentaho.di.ui.core.dialog.EnterMappingDialog) TableItem(org.eclipse.swt.widgets.TableItem) StepMetaInterface(org.pentaho.di.trans.step.StepMetaInterface) ArrayList(java.util.ArrayList) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) SourceToTargetMapping(org.pentaho.di.core.SourceToTargetMapping) TableView(org.pentaho.di.ui.core.widget.TableView)

Aggregations

SourceToTargetMapping (org.pentaho.di.core.SourceToTargetMapping)27 EnterMappingDialog (org.pentaho.di.ui.core.dialog.EnterMappingDialog)24 ArrayList (java.util.ArrayList)22 TableItem (org.eclipse.swt.widgets.TableItem)21 KettleException (org.pentaho.di.core.exception.KettleException)21 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)21 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)21 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)20 StepMetaInterface (org.pentaho.di.trans.step.StepMetaInterface)14 RowMeta (org.pentaho.di.core.row.RowMeta)5 ValueMetaNone (org.pentaho.di.core.row.value.ValueMetaNone)3 TableView (org.pentaho.di.ui.core.widget.TableView)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