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());
}
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();
}
}
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();
}
}
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"));
}
Aggregations