Search in sources :

Example 6 with ExcelInputMeta

use of org.pentaho.di.trans.steps.excelinput.ExcelInputMeta in project pentaho-kettle by pentaho.

the class ExcelInputDialogTest method getFieldsTest.

@Test
public /**
 * http://jira.pentaho.com/browse/PDI-13930
 */
void getFieldsTest() throws Exception {
    ExcelInputDialog dialog = Mockito.mock(ExcelInputDialog.class);
    RowMeta fields = new RowMeta();
    ExcelInputMeta info = Mockito.mock(ExcelInputMeta.class);
    Mockito.doReturn(true).when(info).readAllSheets();
    int[] startColumn = { 0 };
    Mockito.doReturn(startColumn).when(info).getStartColumn();
    int[] startRow = { 0 };
    Mockito.doReturn(startRow).when(info).getStartRow();
    KWorkbook workbook = Mockito.mock(KWorkbook.class);
    Mockito.doReturn(1).when(workbook).getNumberOfSheets();
    KSheet sheet = Mockito.mock(KSheet.class);
    Mockito.doReturn(sheet).when(workbook).getSheet(0);
    KCell cell = Mockito.mock(KCell.class);
    int fieldCount = 400;
    for (int i = 0; i <= fieldCount - 1; i++) {
        Mockito.doReturn(cell).when(sheet).getCell(i, 0);
        Mockito.doReturn(cell).when(sheet).getCell(i, 1);
    }
    Mockito.doReturn("testValue").when(cell).getContents();
    Mockito.doReturn(KCellType.NUMBER).when(cell).getType();
    PluginRegistry pluginRegistry = Mockito.mock(PluginRegistry.class);
    PluginInterface stringPlugin = Mockito.mock(PluginInterface.class);
    Mockito.doReturn(stringPlugin).when(pluginRegistry).getPlugin(ValueMetaPluginType.class, "1");
    Mockito.doReturn(Mockito.mock(ValueMetaInterface.class)).when(pluginRegistry).loadClass(stringPlugin, ValueMetaInterface.class);
    ValueMetaFactory.pluginRegistry = pluginRegistry;
    Method processingWorkbookMethod = ExcelInputDialog.class.getDeclaredMethod("processingWorkbook", RowMetaInterface.class, ExcelInputMeta.class, KWorkbook.class);
    processingWorkbookMethod.setAccessible(true);
    processingWorkbookMethod.invoke(dialog, fields, info, workbook);
    Assert.assertEquals(fieldCount, fields.size());
}
Also used : ExcelInputMeta(org.pentaho.di.trans.steps.excelinput.ExcelInputMeta) RowMeta(org.pentaho.di.core.row.RowMeta) KWorkbook(org.pentaho.di.core.spreadsheet.KWorkbook) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) PluginInterface(org.pentaho.di.core.plugins.PluginInterface) KSheet(org.pentaho.di.core.spreadsheet.KSheet) Method(java.lang.reflect.Method) KCell(org.pentaho.di.core.spreadsheet.KCell) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Test(org.junit.Test)

Example 7 with ExcelInputMeta

use of org.pentaho.di.trans.steps.excelinput.ExcelInputMeta in project pentaho-kettle by pentaho.

the class ExcelInputDialog method getSheets.

/**
 * Get the names of the sheets from the Excel workbooks and let the user select some or all of them.
 */
public void getSheets() {
    List<String> sheetnames = new ArrayList<String>();
    ExcelInputMeta info = new ExcelInputMeta();
    getInfo(info);
    FileInputList fileList = info.getFileList(transMeta);
    for (FileObject fileObject : fileList.getFiles()) {
        try {
            KWorkbook workbook = WorkbookFactory.getWorkbook(info.getSpreadSheetType(), KettleVFS.getFilename(fileObject), info.getEncoding());
            int nrSheets = workbook.getNumberOfSheets();
            for (int j = 0; j < nrSheets; j++) {
                KSheet sheet = workbook.getSheet(j);
                String sheetname = sheet.getName();
                if (Const.indexOfString(sheetname, sheetnames) < 0) {
                    sheetnames.add(sheetname);
                }
            }
            workbook.close();
        } catch (Exception e) {
            new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"), BaseMessages.getString(PKG, "ExcelInputDialog.ErrorReadingFile.DialogMessage", KettleVFS.getFilename(fileObject)), e);
        }
    }
    // Put it in an array:
    String[] lst = sheetnames.toArray(new String[sheetnames.size()]);
    // Let the user select the sheet-names...
    EnterListDialog esd = new EnterListDialog(shell, SWT.NONE, lst);
    String[] selection = esd.open();
    if (selection != null) {
        for (int j = 0; j < selection.length; j++) {
            wSheetnameList.add(new String[] { selection[j], "" });
        }
        wSheetnameList.removeEmptyRows();
        wSheetnameList.setRowNums();
        wSheetnameList.optWidth(true);
        checkAlerts();
    }
}
Also used : KWorkbook(org.pentaho.di.core.spreadsheet.KWorkbook) ArrayList(java.util.ArrayList) KSheet(org.pentaho.di.core.spreadsheet.KSheet) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) EnterListDialog(org.pentaho.di.ui.core.dialog.EnterListDialog) KettleStepException(org.pentaho.di.core.exception.KettleStepException) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) ExcelInputMeta(org.pentaho.di.trans.steps.excelinput.ExcelInputMeta) FileObject(org.apache.commons.vfs2.FileObject) FileInputList(org.pentaho.di.core.fileinput.FileInputList)

Example 8 with ExcelInputMeta

use of org.pentaho.di.trans.steps.excelinput.ExcelInputMeta in project pentaho-kettle by pentaho.

the class ExcelInputDialog method preview.

/**
 * Preview the data generated by this step. This generates a transformation using this step & a dummy and previews it.
 */
private void preview() {
    // Create the excel reader step...
    ExcelInputMeta oneMeta = new ExcelInputMeta();
    getInfo(oneMeta);
    if (oneMeta.isAcceptingFilenames()) {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
        // Nothing
        mb.setMessage(BaseMessages.getString(PKG, "ExcelInputDialog.Dialog.SpecifyASampleFile.Message"));
        // found
        // that
        // matches
        // your
        // criteria
        // Sorry!
        mb.setText(BaseMessages.getString(PKG, "ExcelInputDialog.Dialog.SpecifyASampleFile.Title"));
        mb.open();
        return;
    }
    TransMeta previewMeta = TransPreviewFactory.generatePreviewTransformation(transMeta, oneMeta, wStepname.getText());
    EnterNumberDialog numberDialog = new EnterNumberDialog(shell, props.getDefaultPreviewSize(), BaseMessages.getString(PKG, "ExcelInputDialog.PreviewSize.DialogTitle"), BaseMessages.getString(PKG, "ExcelInputDialog.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 : ExcelInputMeta(org.pentaho.di.trans.steps.excelinput.ExcelInputMeta) TransPreviewProgressDialog(org.pentaho.di.ui.trans.dialog.TransPreviewProgressDialog) TransMeta(org.pentaho.di.trans.TransMeta) EnterTextDialog(org.pentaho.di.ui.core.dialog.EnterTextDialog) 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) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 9 with ExcelInputMeta

use of org.pentaho.di.trans.steps.excelinput.ExcelInputMeta in project pentaho-kettle by pentaho.

the class ExcelWriterStepIntIT method testPDI11374.

@Test
public void testPDI11374() throws KettleException, IOException {
    String stepName = "Excel Writer";
    ExcelWriterStepMeta meta = new ExcelWriterStepMeta();
    meta.setDefault();
    File tempOutputFile = File.createTempFile("testPDI11374", ".xlsx");
    tempOutputFile.deleteOnExit();
    meta.setFileName(tempOutputFile.getAbsolutePath().replace(".xlsx", ""));
    meta.setExtension("xlsx");
    meta.setSheetname("Sheet10");
    meta.setOutputFields(new ExcelWriterStepField[] {});
    meta.setHeaderEnabled(true);
    TransMeta transMeta = TransTestFactory.generateTestTransformation(null, meta, stepName);
    List<RowMetaAndData> inputList = getPDI11374RowMetaAndData();
    TransTestFactory.executeTestTransformation(transMeta, TransTestFactory.INJECTOR_STEPNAME, stepName, TransTestFactory.DUMMY_STEPNAME, inputList);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException ignore) {
    // Wait a second to ensure that the output file is properly closed
    }
    // Now, check the result
    String checkStepName = "Excel Input";
    ExcelInputMeta excelInput = new ExcelInputMeta();
    excelInput.setFileName(new String[] { tempOutputFile.getAbsolutePath() });
    excelInput.setFileMask(new String[] { "" });
    excelInput.setExcludeFileMask(new String[] { "" });
    excelInput.setFileRequired(new String[] { "N" });
    excelInput.setIncludeSubFolders(new String[] { "N" });
    excelInput.setSpreadSheetType(SpreadSheetType.POI);
    excelInput.setSheetName(new String[] { "Sheet10" });
    excelInput.setStartColumn(new int[] { 0 });
    excelInput.setStartRow(new int[] { 0 });
    // Ensures that we can check the header names
    excelInput.setStartsWithHeader(false);
    ExcelInputField[] fields = new ExcelInputField[5];
    for (int i = 0; i < 5; i++) {
        fields[i] = new ExcelInputField();
        fields[i].setName("field" + (i + 1));
    }
    excelInput.setField(fields);
    transMeta = TransTestFactory.generateTestTransformation(null, excelInput, checkStepName);
    // Remove the Injector hop, as it's not needed for this transformation
    TransHopMeta injectHop = transMeta.findTransHop(transMeta.findStep(TransTestFactory.INJECTOR_STEPNAME), transMeta.findStep(stepName));
    transMeta.removeTransHop(transMeta.indexOfTransHop(injectHop));
    inputList = new ArrayList<RowMetaAndData>();
    List<RowMetaAndData> result = TransTestFactory.executeTestTransformation(transMeta, TransTestFactory.INJECTOR_STEPNAME, stepName, TransTestFactory.DUMMY_STEPNAME, inputList);
    assertNotNull(result);
    assertEquals(2, result.size());
    assertEquals(5, result.get(0).getRowMeta().size());
    assertEquals(ValueMetaInterface.TYPE_STRING, result.get(0).getValueMeta(0).getType());
    assertEquals(ValueMetaInterface.TYPE_STRING, result.get(0).getValueMeta(1).getType());
    assertEquals(ValueMetaInterface.TYPE_STRING, result.get(0).getValueMeta(2).getType());
    assertEquals(ValueMetaInterface.TYPE_STRING, result.get(0).getValueMeta(3).getType());
    assertEquals(ValueMetaInterface.TYPE_STRING, result.get(0).getValueMeta(4).getType());
    assertEquals("col1", result.get(0).getString(0, "default-value"));
    assertEquals("col2", result.get(0).getString(1, "default-value"));
    assertEquals("col3", result.get(0).getString(2, "default-value"));
    assertEquals("col4", result.get(0).getString(3, "default-value"));
    assertEquals("col5", result.get(0).getString(4, "default-value"));
    assertEquals("data1", result.get(1).getString(0, "default-value"));
    assertEquals("data2", result.get(1).getString(1, "default-value"));
    assertEquals("data3", result.get(1).getString(2, "default-value"));
    assertEquals("data4", result.get(1).getString(3, "default-value"));
    assertEquals("data5", result.get(1).getString(4, "default-value"));
}
Also used : TransMeta(org.pentaho.di.trans.TransMeta) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ExcelInputField(org.pentaho.di.trans.steps.excelinput.ExcelInputField) ExcelInputMeta(org.pentaho.di.trans.steps.excelinput.ExcelInputMeta) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) TransHopMeta(org.pentaho.di.trans.TransHopMeta) File(java.io.File) Test(org.junit.Test)

Aggregations

ExcelInputMeta (org.pentaho.di.trans.steps.excelinput.ExcelInputMeta)9 Test (org.junit.Test)4 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)4 FileObject (org.apache.commons.vfs2.FileObject)3 MessageBox (org.eclipse.swt.widgets.MessageBox)3 KWorkbook (org.pentaho.di.core.spreadsheet.KWorkbook)3 TransMeta (org.pentaho.di.trans.TransMeta)3 KettlePluginException (org.pentaho.di.core.exception.KettlePluginException)2 KettleStepException (org.pentaho.di.core.exception.KettleStepException)2 FileInputList (org.pentaho.di.core.fileinput.FileInputList)2 RowMeta (org.pentaho.di.core.row.RowMeta)2 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)2 KSheet (org.pentaho.di.core.spreadsheet.KSheet)2 BaseStepMeta (org.pentaho.di.trans.step.BaseStepMeta)2 StepMeta (org.pentaho.di.trans.step.StepMeta)2 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)2 IExternalResourceInfo (org.pentaho.metaverse.api.model.IExternalResourceInfo)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Method (java.lang.reflect.Method)1