Search in sources :

Example 6 with FixedFileInputField

use of org.pentaho.di.trans.steps.fixedinput.FixedFileInputField in project pentaho-kettle by pentaho.

the class FixedFileImportWizardPage2 method showInfo.

private void showInfo() {
    int idx = wFields.getSelectionIndex();
    if (idx >= 0) {
        int position = 0;
        for (int i = 0; i < idx; i++) {
            position += fields.get(i).getWidth();
        }
        FixedFileInputField field = fields.get(idx);
        String name = field.getName();
        int width = field.getWidth();
        int length = field.getLength();
        String type = ValueMetaFactory.getValueMetaName(field.getType());
        String format = field.getFormat();
        int precision = field.getPrecision();
        String currency = field.getCurrency();
        String decimal = field.getDecimal();
        String group = field.getGrouping();
        if (name != null) {
            wFieldname.setText(name);
        }
        wWidth.setText("" + width);
        wLength.setText("" + length);
        if (type != null) {
            wFieldtype.setText(type);
        }
        if (format != null) {
            wFormat.setText(format);
        }
        wPrecision.setText("" + precision);
        if (currency != null) {
            wCurrency.setText(currency);
        }
        if (decimal != null) {
            wDecimal.setText(decimal);
        }
        if (group != null) {
            wGroup.setText(group);
        }
        // Clear the sample list...
        wSamples.removeAll();
        String[] samples = getRowSamples(position, width);
        for (int i = 0; i < samples.length; i++) {
            wSamples.add(samples[i]);
        }
    }
}
Also used : FixedFileInputField(org.pentaho.di.trans.steps.fixedinput.FixedFileInputField)

Example 7 with FixedFileInputField

use of org.pentaho.di.trans.steps.fixedinput.FixedFileInputField in project pentaho-kettle by pentaho.

the class FixedInputDialog method getFixed.

private void getFixed() {
    final FixedInputMeta info = new FixedInputMeta();
    getInfo(info);
    Shell sh = new Shell(shell, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN);
    try {
        List<String> rows = getFirst(info, 50);
        fields = new ArrayList<FixedFileInputField>();
        fields.addAll(Arrays.asList(info.getFieldDefinition()));
        if (fields.isEmpty()) {
            FixedFileInputField field = new FixedFileInputField();
            // TODO: i18n, see also FixedTableDraw class for other references of this String
            field.setName("Field" + 1);
            // --> getNewFieldname() method
            field.setType(ValueMetaInterface.TYPE_STRING);
            field.setTrimType(ValueMetaInterface.TRIM_TYPE_NONE);
            field.setWidth(Const.toInt(info.getLineWidth(), 80));
            field.setLength(-1);
            field.setPrecision(-1);
            fields.add(field);
        } else if (info.hasChanged()) {
            // try to reuse the field mappings already set up.
            int width = 0;
            List<FixedFileInputField> reuse = new ArrayList<FixedFileInputField>();
            for (FixedFileInputField field : fields) {
                width += field.getWidth();
                // does this field fit on the line given the line width?
                if (width <= Const.toInt(info.getLineWidth(), width)) {
                    // yes, reuse it
                    reuse.add(field);
                } else {
                    // no, remove its width from the total reused width then quit looking for more
                    width -= field.getWidth();
                    continue;
                }
            }
            int lineWidth = Const.toInt(info.getLineWidth(), width);
            // set the last reused field to take up the rest of the line.
            FixedFileInputField lastField = reuse.get(reuse.size() - 1);
            // don't let the width be grater than the line width
            if (width > lineWidth) {
                width = lineWidth;
            }
            // determine width of last field : lineWidth - (totalWidthOfFieldsReused - lastFieldWidth)
            int lastWidth = Const.toInt(info.getLineWidth(), width) - (width - lastField.getWidth());
            // make the last field occupy the remaining space
            lastField.setWidth(lastWidth);
            // reuse the fields...
            fields = reuse;
        }
        final FixedFileImportWizardPage1 page1 = new FixedFileImportWizardPage1("1", props, rows, fields);
        page1.createControl(sh);
        final FixedFileImportWizardPage2 page2 = new FixedFileImportWizardPage2("2", props, rows, fields);
        page2.createControl(sh);
        Wizard wizard = new Wizard() {

            public boolean performFinish() {
                wFields.clearAll(false);
                for (int i = 0; i < fields.size(); i++) {
                    FixedFileInputField field = fields.get(i);
                    if (field.getWidth() > 0) {
                        TableItem item = new TableItem(wFields.table, SWT.NONE);
                        item.setText(1, field.getName());
                        item.setText(2, "" + ValueMetaFactory.getValueMetaName(field.getType()));
                        item.setText(3, "" + field.getFormat());
                        item.setText(4, "" + field.getWidth());
                        item.setText(5, field.getLength() < 0 ? "" : "" + field.getLength());
                        item.setText(6, field.getPrecision() < 0 ? "" : "" + field.getPrecision());
                        item.setText(7, "" + field.getCurrency());
                        item.setText(8, "" + field.getDecimal());
                        item.setText(9, "" + field.getGrouping());
                    }
                }
                int size = wFields.table.getItemCount();
                if (size == 0) {
                    new TableItem(wFields.table, SWT.NONE);
                }
                wFields.removeEmptyRows();
                wFields.setRowNums();
                wFields.optWidth(true);
                inputMeta.setChanged();
                return true;
            }
        };
        wizard.addPage(page1);
        wizard.addPage(page2);
        WizardDialog wd = new WizardDialog(shell, wizard);
        WizardDialog.setDefaultImage(GUIResource.getInstance().getImageWizard());
        wd.setMinimumPageSize(700, 375);
        wd.updateSize();
        wd.open();
    } catch (Exception e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "FixedInputDialog.ErrorShowingFixedWizard.DialogTitle"), BaseMessages.getString(PKG, "FixedInputDialog.ErrorShowingFixedWizard.DialogMessage"), e);
    }
}
Also used : TableItem(org.eclipse.swt.widgets.TableItem) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KettleValueException(org.pentaho.di.core.exception.KettleValueException) IOException(java.io.IOException) FixedFileInputField(org.pentaho.di.trans.steps.fixedinput.FixedFileInputField) Shell(org.eclipse.swt.widgets.Shell) List(java.util.List) ArrayList(java.util.ArrayList) FixedInputMeta(org.pentaho.di.trans.steps.fixedinput.FixedInputMeta) Wizard(org.eclipse.jface.wizard.Wizard) WizardDialog(org.eclipse.jface.wizard.WizardDialog)

Example 8 with FixedFileInputField

use of org.pentaho.di.trans.steps.fixedinput.FixedFileInputField in project pentaho-kettle by pentaho.

the class FixedInputDialog method getFirst.

// Grab the first x lines from the given file...
// 
private List<String> getFirst(FixedInputMeta meta, int limit) throws IOException, KettleValueException {
    List<String> lines = new ArrayList<String>();
    FixedInputMeta oneMeta = new FixedInputMeta();
    getInfo(oneMeta);
    // Add a single field with the width of the line...
    // 
    int lineWidth = Integer.parseInt(oneMeta.getLineWidth());
    if (lineWidth <= 0) {
        throw new IOException("The width of a line can not be 0 or less.");
    }
    oneMeta.allocate(1);
    FixedFileInputField field = new FixedFileInputField();
    field.setName("Field1");
    field.setType(ValueMetaInterface.TYPE_STRING);
    field.setWidth(lineWidth);
    // CHECKSTYLE:Indentation:OFF
    oneMeta.getFieldDefinition()[0] = field;
    TransMeta previewMeta = TransPreviewFactory.generatePreviewTransformation(transMeta, oneMeta, wStepname.getText());
    TransPreviewProgressDialog progressDialog = new TransPreviewProgressDialog(shell, previewMeta, new String[] { wStepname.getText() }, new int[] { limit });
    progressDialog.open();
    Trans trans = progressDialog.getTrans();
    String loggingText = progressDialog.getLoggingText();
    if (!progressDialog.isCancelled()) {
        if (trans.getResult() != null && trans.getResult().getNrErrors() > 0) {
            EnterTextDialog etd = new EnterTextDialog(shell, BaseMessages.getString(PKG, "System.Dialog.PreviewError.Title"), BaseMessages.getString(PKG, "System.Dialog.PreviewError.Message"), loggingText, true);
            etd.setReadOnly();
            etd.open();
        }
    }
    // The rows are in the transformation...
    // 
    RowMetaInterface previewRowsMeta = progressDialog.getPreviewRowsMeta(wStepname.getText());
    List<Object[]> previewRowsData = progressDialog.getPreviewRows(wStepname.getText());
    for (int i = 0; i < previewRowsData.size(); i++) {
        String line = previewRowsMeta.getString(previewRowsData.get(i), 0);
        lines.add(line);
    }
    return lines;
}
Also used : TransPreviewProgressDialog(org.pentaho.di.ui.trans.dialog.TransPreviewProgressDialog) ArrayList(java.util.ArrayList) TransMeta(org.pentaho.di.trans.TransMeta) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) IOException(java.io.IOException) FixedFileInputField(org.pentaho.di.trans.steps.fixedinput.FixedFileInputField) EnterTextDialog(org.pentaho.di.ui.core.dialog.EnterTextDialog) FixedInputMeta(org.pentaho.di.trans.steps.fixedinput.FixedInputMeta) Trans(org.pentaho.di.trans.Trans)

Example 9 with FixedFileInputField

use of org.pentaho.di.trans.steps.fixedinput.FixedFileInputField in project pentaho-kettle by pentaho.

the class FixedInputDialog method getData.

/**
 * Copy information from the meta-data input to the dialog fields.
 */
public void getData() {
    wStepname.setText(stepname);
    wFilename.setText(Const.NVL(inputMeta.getFilename(), ""));
    wLineWidth.setText(Const.NVL(inputMeta.getLineWidth(), ""));
    wLineFeedPresent.setSelection(inputMeta.isLineFeedPresent());
    wBufferSize.setText(Const.NVL(inputMeta.getBufferSize(), ""));
    wLazyConversion.setSelection(inputMeta.isLazyConversionActive());
    wHeaderPresent.setSelection(inputMeta.isHeaderPresent());
    wRunningInParallel.setSelection(inputMeta.isRunningInParallel());
    wFileType.setText(inputMeta.getFileTypeDesc());
    wEncoding.setText(Const.NVL(inputMeta.getEncoding(), ""));
    wAddResult.setSelection(inputMeta.isAddResultFile());
    for (int i = 0; i < inputMeta.getFieldDefinition().length; i++) {
        TableItem item = new TableItem(wFields.table, SWT.NONE);
        int colnr = 1;
        FixedFileInputField field = inputMeta.getFieldDefinition()[i];
        item.setText(colnr++, Const.NVL(field.getName(), ""));
        item.setText(colnr++, ValueMetaFactory.getValueMetaName(field.getType()));
        item.setText(colnr++, Const.NVL(field.getFormat(), ""));
        item.setText(colnr++, field.getWidth() >= 0 ? Integer.toString(field.getWidth()) : "");
        item.setText(colnr++, field.getLength() >= 0 ? Integer.toString(field.getLength()) : "");
        item.setText(colnr++, field.getPrecision() >= 0 ? Integer.toString(field.getPrecision()) : "");
        item.setText(colnr++, Const.NVL(field.getCurrency(), ""));
        item.setText(colnr++, Const.NVL(field.getDecimal(), ""));
        item.setText(colnr++, Const.NVL(field.getGrouping(), ""));
        item.setText(colnr++, ValueMetaString.getTrimTypeCode(field.getTrimType()));
    }
    wFields.removeEmptyRows();
    wFields.setRowNums();
    wFields.optWidth(true);
    enableFields();
    wStepname.selectAll();
    wStepname.setFocus();
}
Also used : FixedFileInputField(org.pentaho.di.trans.steps.fixedinput.FixedFileInputField) TableItem(org.eclipse.swt.widgets.TableItem)

Example 10 with FixedFileInputField

use of org.pentaho.di.trans.steps.fixedinput.FixedFileInputField in project pentaho-kettle by pentaho.

the class FixedTableDraw method getFieldOnPosition.

private FixedFileInputField getFieldOnPosition(int x) {
    int position = 0;
    for (int i = 0; i < fields.size(); i++) {
        FixedFileInputField field = fields.get(i);
        int width = field.getWidth();
        if (position <= x && position + width > x) {
            return field;
        }
        position += width;
    }
    return null;
}
Also used : FixedFileInputField(org.pentaho.di.trans.steps.fixedinput.FixedFileInputField) Point(org.eclipse.swt.graphics.Point)

Aggregations

FixedFileInputField (org.pentaho.di.trans.steps.fixedinput.FixedFileInputField)10 Point (org.eclipse.swt.graphics.Point)4 TableItem (org.eclipse.swt.widgets.TableItem)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)2 FixedInputMeta (org.pentaho.di.trans.steps.fixedinput.FixedInputMeta)2 List (java.util.List)1 Wizard (org.eclipse.jface.wizard.Wizard)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 CCombo (org.eclipse.swt.custom.CCombo)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 GC (org.eclipse.swt.graphics.GC)1 Image (org.eclipse.swt.graphics.Image)1 FormAttachment (org.eclipse.swt.layout.FormAttachment)1 FormData (org.eclipse.swt.layout.FormData)1 FormLayout (org.eclipse.swt.layout.FormLayout)1