Search in sources :

Example 1 with FixedInputMeta

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

the class FixedInputDialog method preview.

// Preview the data
private void preview() {
    // execute a complete preview transformation in the background.
    // This is how we do it...
    // 
    FixedInputMeta oneMeta = new FixedInputMeta();
    getInfo(oneMeta);
    TransMeta previewMeta = TransPreviewFactory.generatePreviewTransformation(transMeta, oneMeta, wStepname.getText());
    EnterNumberDialog numberDialog = new EnterNumberDialog(shell, props.getDefaultPreviewSize(), BaseMessages.getString(PKG, "FixedInputDialog.PreviewSize.DialogTitle"), BaseMessages.getString(PKG, "FixedInputDialog.PreviewSize.DialogMessage"));
    int previewSize = numberDialog.open();
    if (previewSize > 0) {
        TransPreviewProgressDialog progressDialog = new TransPreviewProgressDialog(shell, previewMeta, new String[] { wStepname.getText() }, new int[] { previewSize });
        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();
            }
        }
        PreviewRowsDialog prd = new PreviewRowsDialog(shell, transMeta, SWT.NONE, wStepname.getText(), progressDialog.getPreviewRowsMeta(wStepname.getText()), progressDialog.getPreviewRows(wStepname.getText()), loggingText);
        prd.open();
    }
}
Also used : TransPreviewProgressDialog(org.pentaho.di.ui.trans.dialog.TransPreviewProgressDialog) TransMeta(org.pentaho.di.trans.TransMeta) EnterTextDialog(org.pentaho.di.ui.core.dialog.EnterTextDialog) FixedInputMeta(org.pentaho.di.trans.steps.fixedinput.FixedInputMeta) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) PreviewRowsDialog(org.pentaho.di.ui.core.dialog.PreviewRowsDialog) EnterNumberDialog(org.pentaho.di.ui.core.dialog.EnterNumberDialog) Trans(org.pentaho.di.trans.Trans)

Example 2 with FixedInputMeta

use of org.pentaho.di.trans.steps.fixedinput.FixedInputMeta 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 3 with FixedInputMeta

use of org.pentaho.di.trans.steps.fixedinput.FixedInputMeta 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)

Aggregations

ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)3 FixedInputMeta (org.pentaho.di.trans.steps.fixedinput.FixedInputMeta)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Trans (org.pentaho.di.trans.Trans)2 TransMeta (org.pentaho.di.trans.TransMeta)2 FixedFileInputField (org.pentaho.di.trans.steps.fixedinput.FixedFileInputField)2 EnterTextDialog (org.pentaho.di.ui.core.dialog.EnterTextDialog)2 TransPreviewProgressDialog (org.pentaho.di.ui.trans.dialog.TransPreviewProgressDialog)2 List (java.util.List)1 Wizard (org.eclipse.jface.wizard.Wizard)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 Shell (org.eclipse.swt.widgets.Shell)1 TableItem (org.eclipse.swt.widgets.TableItem)1 KettleValueException (org.pentaho.di.core.exception.KettleValueException)1 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)1 EnterNumberDialog (org.pentaho.di.ui.core.dialog.EnterNumberDialog)1 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)1 PreviewRowsDialog (org.pentaho.di.ui.core.dialog.PreviewRowsDialog)1