use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.
the class CsvTransformGeneratorIT method testModifyEmptyTable_AddColumn.
public void testModifyEmptyTable_AddColumn() throws Exception {
CsvTransformGenerator gen = getCleanTransformGen();
IPentahoSession session = new StandaloneSession("test");
// create the model
ModelInfo info = gen.getModelInfo();
// generate the database table initially
gen.createOrModifyTable(session);
// now, lets update it by changing the model info slightly.. add a column
addColumnToModel(info);
gen.createOrModifyTable(session);
// make sure the table has an extra integer column in it
String tableName = gen.getTableName();
String sql = "select " + info.getColumns()[info.getColumns().length - 1].getId() + " from " + tableName + ";";
gen.execSqlStatement(sql, getDatabaseMeta(), null);
}
use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.
the class CsvTransformGeneratorIT method testGoodTransform.
public void testGoodTransform() throws Exception {
IPentahoSession session = new StandaloneSession("test");
KettleSystemListener.environmentInit(session);
String KETTLE_EMPTY_STRING_DIFFERS_FROM_NULL = System.getProperty("KETTLE_EMPTY_STRING_DIFFERS_FROM_NULL", "N");
ModelInfo info = createModel();
CsvTransformGenerator gen = new CsvTransformGenerator(info, getDatabaseMeta());
gen.preview(session);
DataRow[] rows = info.getData();
assertNotNull(rows);
assertEquals(235, rows.length);
Date testDate = new Date();
testDate.setDate(1);
testDate.setHours(0);
testDate.setMinutes(0);
testDate.setMonth(0);
testDate.setSeconds(0);
testDate.setYear(110);
// test the first row
// test the data types
DataRow row = rows[0];
assertNotNull(row);
Object[] cells = row.getCells();
assertNotNull(cells);
// assertEquals( 8, cells.length );
assertTrue(cells[0] instanceof Long);
assertTrue(cells[1] instanceof Double);
assertTrue(cells[2] instanceof Long);
assertTrue(cells[3] instanceof Date);
assertTrue(cells[4] instanceof String);
assertTrue(cells[5] instanceof Long);
assertTrue(cells[6] instanceof Double);
assertTrue(cells[7] instanceof Boolean);
// test the values
assertEquals((long) 3, cells[0]);
assertEquals(25677.96525, cells[1]);
assertEquals((long) 1231, cells[2]);
assertEquals(testDate.getYear(), ((Date) cells[3]).getYear());
assertEquals(testDate.getMonth(), ((Date) cells[3]).getMonth());
assertEquals(testDate.getDate(), ((Date) cells[3]).getDate());
assertEquals(testDate.getHours(), ((Date) cells[3]).getHours());
// assertEquals( testDate.getMinutes(), ((Date)cells[3]).getMinutes() ); this fails, a bug in the PDI date
// parsing?
assertEquals(testDate.getSeconds(), ((Date) cells[3]).getSeconds());
// assertEquals( testDate, cells[3] );
assertEquals("Afghanistan", cells[4]);
assertEquals((long) 11, cells[5]);
assertEquals(111.9090909, cells[6]);
assertEquals(false, cells[7]);
// test the second row
testDate.setDate(2);
// test the data types
row = rows[1];
assertNotNull(row);
cells = row.getCells();
assertNotNull(cells);
assertTrue(cells[0] instanceof Long);
assertTrue(cells[1] instanceof Double);
assertTrue(cells[2] instanceof Long);
assertTrue(cells[3] instanceof Date);
if ("Y".equals(KETTLE_EMPTY_STRING_DIFFERS_FROM_NULL)) {
assertTrue("".equals(cells[4]));
} else {
assertTrue(cells[4] == null);
}
assertTrue(cells[5] instanceof Long);
assertTrue(cells[6] instanceof Double);
assertTrue(cells[7] instanceof Boolean);
// test the values
assertEquals((long) 4, cells[0]);
assertEquals(24261.81026, cells[1]);
assertEquals((long) 1663, cells[2]);
assertEquals(testDate.getYear(), ((Date) cells[3]).getYear());
assertEquals(testDate.getMonth(), ((Date) cells[3]).getMonth());
assertEquals(testDate.getDate(), ((Date) cells[3]).getDate());
assertEquals(testDate.getHours(), ((Date) cells[3]).getHours());
// assertEquals( testDate.getMinutes(), ((Date)cells[3]).getMinutes() ); this fails, a bug in the PDI date
// parsing?
assertEquals(testDate.getSeconds(), ((Date) cells[3]).getSeconds());
// assertEquals( testDate, cells[3] );
if ("Y".equals(KETTLE_EMPTY_STRING_DIFFERS_FROM_NULL)) {
assertEquals("", cells[4]);
assertEquals(cells[4], "");
} else {
// IfNull value does not seem to work
assertEquals(null, cells[4]);
}
assertEquals((long) 7, cells[5]);
assertEquals(237.5714286, cells[6]);
assertEquals(true, cells[7]);
}
use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.
the class CsvTransformGeneratorIT method testCreateIndex.
public void testCreateIndex() throws Exception {
IPentahoSession session = new StandaloneSession("test");
KettleSystemListener.environmentInit(session);
// create the model
ModelInfo info = createModel();
CsvTransformGenerator gen = new CsvTransformGenerator(info, getDatabaseMeta());
String tableName = info.getStageTableName();
try {
gen.execSqlStatement(getDropTableStatement(tableName), getDatabaseMeta(), null);
} catch (CsvTransformGeneratorException e) {
// table might not be there yet, it is OK
}
// generate the database table
gen.createOrModifyTable(session);
// load the table
loadTable(gen, info, true, session);
// check the results
long rowCount = this.getRowCount(tableName);
assertEquals((long) 235, rowCount);
int indexCount = gen.createIndices(session);
assertEquals(5, indexCount);
}
use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.
the class CsvDatasourceServiceImpl method getPreviewRows.
public List<String> getPreviewRows(String filename, boolean isFirstRowHeader, int rows, String encoding) throws Exception {
checkPermissions();
List<String> previewRows = null;
if (!StringUtils.isEmpty(filename)) {
CsvUtils service = new CsvUtils();
ModelInfo mi = service.getFileContents("", filename, ",", "\"", rows, isFirstRowHeader, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
encoding);
previewRows = mi.getFileInfo().getContents();
}
return previewRows;
}
use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.
the class CsvDatasourceServiceImpl method stageFile.
public ModelInfo stageFile(String fileName, String delimiter, String enclosure, boolean isFirstRowHeader, String encoding) throws Exception {
checkPermissions();
ModelInfo modelInfo;
fileName = FilenameUtils.getName(fileName);
try {
int headerRows = isFirstRowHeader ? 1 : 0;
modelInfo = new CsvUtils().generateFields("", fileName, AgileHelper.getCsvSampleRowSize(), delimiter, enclosure, headerRows, true, true, // $NON-NLS-1$
encoding);
} catch (FileNotFoundException e) {
logger.error(e);
throw new Exception("File was not found: " + fileName);
} catch (Exception e) {
logger.error(e);
throw e;
}
return modelInfo;
}
Aggregations