Search in sources :

Example 6 with ModelInfo

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);
}
Also used : ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Example 7 with ModelInfo

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]);
}
Also used : ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) DataRow(org.pentaho.platform.dataaccess.datasource.wizard.models.DataRow) Date(java.util.Date)

Example 8 with ModelInfo

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);
}
Also used : ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Example 9 with ModelInfo

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;
}
Also used : CsvUtils(org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils) ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)

Example 10 with ModelInfo

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;
}
Also used : CsvUtils(org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils) ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) FileNotFoundException(java.io.FileNotFoundException) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)

Aggregations

ModelInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)23 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)12 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)11 CsvTransformGeneratorException (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)10 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)6 CsvFileInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo)6 Test (org.junit.Test)4 File (java.io.File)3 ColumnInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ColumnInfo)3 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)3 XStream (com.thoughtworks.xstream.XStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Date (java.util.Date)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Database (org.pentaho.di.core.database.Database)2 CsvUtils (org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils)2 DataRow (org.pentaho.platform.dataaccess.datasource.wizard.models.DataRow)2 DatasourceDTO (org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO)2 FileTransformStats (org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats)2