Search in sources :

Example 31 with BaseFileField

use of org.pentaho.di.trans.steps.file.BaseFileField in project pentaho-kettle by pentaho.

the class TextFileInputTest method readInputWithNonEmptyNullif.

@Test
public void readInputWithNonEmptyNullif() throws Exception {
    final String virtualFile = createVirtualFile("pdi-14358.txt", "-,-\n");
    BaseFileField col2 = field("col2");
    col2.setNullString("-");
    TextFileInputMeta meta = createMetaObject(field("col1"), col2);
    TextFileInputData data = createDataObject(virtualFile, ",", "col1", "col2");
    TextFileInput input = StepMockUtil.getStep(TextFileInput.class, TextFileInputMeta.class, "test");
    List<Object[]> output = TransTestingUtil.execute(input, meta, data, 1, false);
    TransTestingUtil.assertResult(new Object[] { "-" }, output.get(0));
    deleteVfsFile(virtualFile);
}
Also used : BaseFileField(org.pentaho.di.trans.steps.file.BaseFileField) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Test(org.junit.Test)

Example 32 with BaseFileField

use of org.pentaho.di.trans.steps.file.BaseFileField in project pentaho-kettle by pentaho.

the class TextFileInputTest method readInputWithDefaultValues.

@Test
public void readInputWithDefaultValues() throws Exception {
    final String virtualFile = createVirtualFile("pdi-14832.txt", "1,\n");
    BaseFileField col2 = field("col2");
    col2.setIfNullValue("DEFAULT");
    TextFileInputMeta meta = createMetaObject(field("col1"), col2);
    TextFileInputData data = createDataObject(virtualFile, ",", "col1", "col2");
    TextFileInput input = StepMockUtil.getStep(TextFileInput.class, TextFileInputMeta.class, "test");
    List<Object[]> output = TransTestingUtil.execute(input, meta, data, 1, false);
    TransTestingUtil.assertResult(new Object[] { "1", "DEFAULT" }, output.get(0));
    deleteVfsFile(virtualFile);
}
Also used : BaseFileField(org.pentaho.di.trans.steps.file.BaseFileField) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Test(org.junit.Test)

Example 33 with BaseFileField

use of org.pentaho.di.trans.steps.file.BaseFileField in project pentaho-kettle by pentaho.

the class TextFileInputDialogTest method testMinimalWidth_PDI_14253.

@Test
public void testMinimalWidth_PDI_14253() throws Exception {
    final String virtualFile = "ram://pdi-14253.txt";
    KettleVFS.getFileObject(virtualFile).createFile();
    final String content = "r1c1,  r1c2\nr2c1  ,  r2c2  ";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(content.getBytes());
    OutputStream os = KettleVFS.getFileObject(virtualFile).getContent().getOutputStream();
    IOUtils.copy(new ByteArrayInputStream(bos.toByteArray()), os);
    os.close();
    TextFileInputMeta meta = new TextFileInputMeta();
    meta.content.lineWrapped = false;
    meta.inputFields = new BaseFileField[] { new BaseFileField("col1", -1, -1), new BaseFileField("col2", -1, -1) };
    meta.content.fileCompression = "None";
    meta.content.fileType = "CSV";
    meta.content.header = false;
    meta.content.nrHeaderLines = -1;
    meta.content.footer = false;
    meta.content.nrFooterLines = -1;
    TextFileInputData data = new TextFileInputData();
    data.files = new FileInputList();
    data.files.addFile(KettleVFS.getFileObject(virtualFile));
    data.outputRowMeta = new RowMeta();
    data.outputRowMeta.addValueMeta(new ValueMetaString("col1"));
    data.outputRowMeta.addValueMeta(new ValueMetaString("col2"));
    data.dataErrorLineHandler = mock(FileErrorHandler.class);
    data.fileFormatType = TextFileInputMeta.FILE_FORMAT_UNIX;
    data.separator = ",";
    data.filterProcessor = new TextFileFilterProcessor(new TextFileFilter[0], new Variables() {
    });
    data.filePlayList = new FilePlayListAll();
    TextFileInputDialog dialog = new TextFileInputDialog(mock(Shell.class), meta, mock(TransMeta.class), "TFIMinimalWidthTest");
    TableView tv = mock(TableView.class);
    when(tv.nrNonEmpty()).thenReturn(0);
    // click the Minimal width button
    dialog.setMinimalWidth(tv);
    RowSet output = new BlockingRowSet(5);
    TextFileInput input = StepMockUtil.getStep(TextFileInput.class, TextFileInputMeta.class, "test");
    input.setOutputRowSets(Collections.singletonList(output));
    while (input.processRow(meta, data)) {
    // wait until the step completes executing
    }
    Object[] row1 = output.getRowImmediate();
    assertRow(row1, "r1c1", "r1c2");
    Object[] row2 = output.getRowImmediate();
    assertRow(row2, "r2c1", "r2c2");
    KettleVFS.getFileObject(virtualFile).delete();
}
Also used : ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) TextFileFilterProcessor(org.pentaho.di.trans.steps.fileinput.text.TextFileFilterProcessor) RowMeta(org.pentaho.di.core.row.RowMeta) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) TextFileFilter(org.pentaho.di.trans.steps.fileinput.text.TextFileFilter) FilePlayListAll(org.pentaho.di.core.playlist.FilePlayListAll) BaseFileField(org.pentaho.di.trans.steps.file.BaseFileField) TransMeta(org.pentaho.di.trans.TransMeta) RowSet(org.pentaho.di.core.RowSet) BlockingRowSet(org.pentaho.di.core.BlockingRowSet) BlockingRowSet(org.pentaho.di.core.BlockingRowSet) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Variables(org.pentaho.di.core.variables.Variables) Shell(org.eclipse.swt.widgets.Shell) TextFileInputMeta(org.pentaho.di.trans.steps.fileinput.text.TextFileInputMeta) ByteArrayInputStream(java.io.ByteArrayInputStream) TextFileInput(org.pentaho.di.trans.steps.fileinput.text.TextFileInput) FileErrorHandler(org.pentaho.di.trans.step.errorhandling.FileErrorHandler) TextFileInputData(org.pentaho.di.trans.steps.fileinput.text.TextFileInputData) FileInputList(org.pentaho.di.core.fileinput.FileInputList) TableView(org.pentaho.di.ui.core.widget.TableView) Test(org.junit.Test)

Example 34 with BaseFileField

use of org.pentaho.di.trans.steps.file.BaseFileField in project pentaho-kettle by pentaho.

the class TextFileInputDialog method getFields.

private Vector<TextFileInputFieldInterface> getFields(TextFileInputMeta info, List<String> rows) {
    Vector<TextFileInputFieldInterface> fields = new Vector<>();
    int maxsize = 0;
    for (String row : rows) {
        int len = row.length();
        if (len > maxsize) {
            maxsize = len;
        }
    }
    int prevEnd = 0;
    int dummynr = 1;
    for (int i = 0; i < info.inputFields.length; i++) {
        BaseFileField f = info.inputFields[i];
        // See if positions are skipped, if this is the case, add dummy fields...
        if (f.getPosition() != prevEnd) {
            // gap
            BaseFileField field = new BaseFileField("Dummy" + dummynr, prevEnd, f.getPosition() - prevEnd);
            // don't include in result by default.
            field.setIgnored(true);
            fields.add(field);
            dummynr++;
        }
        BaseFileField field = new BaseFileField(f.getName(), f.getPosition(), f.getLength());
        field.setType(f.getType());
        field.setIgnored(false);
        field.setFormat(f.getFormat());
        field.setPrecision(f.getPrecision());
        field.setTrimType(f.getTrimType());
        field.setDecimalSymbol(f.getDecimalSymbol());
        field.setGroupSymbol(f.getGroupSymbol());
        field.setCurrencySymbol(f.getCurrencySymbol());
        field.setRepeated(f.isRepeated());
        field.setNullString(f.getNullString());
        fields.add(field);
        prevEnd = field.getPosition() + field.getLength();
    }
    if (info.inputFields.length == 0) {
        BaseFileField field = new BaseFileField("Field1", 0, maxsize);
        fields.add(field);
    } else {
        // Take the last field and see if it reached until the maximum...
        BaseFileField f = info.inputFields[info.inputFields.length - 1];
        int pos = f.getPosition();
        int len = f.getLength();
        if (pos + len < maxsize) {
            // If not, add an extra trailing field!
            BaseFileField field = new BaseFileField("Dummy" + dummynr, pos + len, maxsize - pos - len);
            // don't include in result by default.
            field.setIgnored(true);
            fields.add(field);
        }
    }
    Collections.sort(fields);
    return fields;
}
Also used : TextFileInputFieldInterface(org.pentaho.di.core.gui.TextFileInputFieldInterface) BaseFileField(org.pentaho.di.trans.steps.file.BaseFileField) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Vector(java.util.Vector)

Example 35 with BaseFileField

use of org.pentaho.di.trans.steps.file.BaseFileField in project pentaho-kettle by pentaho.

the class TextFileInputDialog method getFieldsData.

private void getFieldsData(TextFileInputMeta in, boolean insertAtTop) {
    for (int i = 0; i < in.inputFields.length; i++) {
        BaseFileField field = in.inputFields[i];
        TableItem item;
        if (insertAtTop) {
            item = new TableItem(wFields.table, SWT.NONE, i);
        } else {
            if (i >= wFields.table.getItemCount()) {
                item = wFields.table.getItem(i);
            } else {
                item = new TableItem(wFields.table, SWT.NONE);
            }
        }
        item.setText(1, Const.NVL(field.getName(), ""));
        String type = field.getTypeDesc();
        String format = field.getFormat();
        String position = "" + field.getPosition();
        String length = "" + field.getLength();
        String prec = "" + field.getPrecision();
        String curr = field.getCurrencySymbol();
        String group = field.getGroupSymbol();
        String decim = field.getDecimalSymbol();
        String def = field.getNullString();
        String ifNull = field.getIfNullValue();
        String trim = field.getTrimTypeDesc();
        String rep = field.isRepeated() ? BaseMessages.getString(PKG, "System.Combo.Yes") : BaseMessages.getString(PKG, "System.Combo.No");
        if (type != null) {
            item.setText(2, type);
        }
        if (format != null) {
            item.setText(3, format);
        }
        if (position != null && !"-1".equals(position)) {
            item.setText(4, position);
        }
        if (length != null && !"-1".equals(length)) {
            item.setText(5, length);
        }
        if (prec != null && !"-1".equals(prec)) {
            item.setText(6, prec);
        }
        if (curr != null) {
            item.setText(7, curr);
        }
        if (decim != null) {
            item.setText(8, decim);
        }
        if (group != null) {
            item.setText(9, group);
        }
        if (def != null) {
            item.setText(10, def);
        }
        if (ifNull != null) {
            item.setText(11, ifNull);
        }
        if (trim != null) {
            item.setText(12, trim);
        }
        if (rep != null) {
            item.setText(13, rep);
        }
    }
}
Also used : TableItem(org.eclipse.swt.widgets.TableItem) BaseFileField(org.pentaho.di.trans.steps.file.BaseFileField) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString)

Aggregations

BaseFileField (org.pentaho.di.trans.steps.file.BaseFileField)36 Test (org.junit.Test)19 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)12 KettleException (org.pentaho.di.core.exception.KettleException)9 KettleFileException (org.pentaho.di.core.exception.KettleFileException)7 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)5 ArrayList (java.util.ArrayList)4 KettleStepException (org.pentaho.di.core.exception.KettleStepException)4 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)4 TableItem (org.eclipse.swt.widgets.TableItem)3 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)3 Shell (org.eclipse.swt.widgets.Shell)2 TextFileInputFieldInterface (org.pentaho.di.core.gui.TextFileInputFieldInterface)2 RowMeta (org.pentaho.di.core.row.RowMeta)2 Variables (org.pentaho.di.core.variables.Variables)2 TextFileInputMeta (org.pentaho.di.trans.steps.fileinput.text.TextFileInputMeta)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1