Search in sources :

Example 1 with FileTransformStats

use of org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats in project data-access by pentaho.

the class CsvTransformGeneratorIT method loadTable.

private int loadTable(CsvTransformGenerator gen, ModelInfo info, boolean truncate, IPentahoSession session) throws InterruptedException, CsvTransformGeneratorException {
    gen.loadTable(truncate, session, false);
    FileTransformStats stats = gen.getTransformStats();
    // wait until it it done
    while (!stats.isRowsFinished()) {
        Thread.sleep(100);
    }
    return 1;
}
Also used : FileTransformStats(org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats)

Example 2 with FileTransformStats

use of org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats in project data-access by pentaho.

the class TableInputTransformGenerator method createInputStep.

protected StepMeta createInputStep(TransMeta transMeta) {
    TableInputMeta inputMeta = new TableInputMeta();
    inputMeta.setDatabaseMeta(sourceDatabaseMeta);
    inputMeta.setExecuteEachInputRow(false);
    inputMeta.setRowLimit(Integer.toString(rowLimit));
    inputMeta.setSQL(sql);
    inputMeta.setVariableReplacementActive(false);
    inputMeta.setLazyConversionActive(false);
    // inputMeta.setTargetSteps(null);
    StepMeta inputStepMeta = new StepMeta(TABLE_INPUT, TABLE_INPUT, inputMeta);
    inputStepMeta.setStepErrorMeta(new StepErrorMeta(transMeta, inputStepMeta));
    transMeta.addStep(inputStepMeta);
    final FileTransformStats stats = getTransformStats();
    StepErrorMeta inputErrorMeta = new StepErrorMeta(transMeta, inputStepMeta) {

        public void addErrorRowData(Object[] row, int startIndex, long nrErrors, String errorDescriptions, String fieldNames, String errorCodes) {
            StringBuffer sb = new StringBuffer();
            sb.append("Rejected Row: ");
            for (Object rowData : row) {
                sb.append(rowData);
                sb.append(", ");
            }
            sb.append("\r\n");
            stats.getErrors().add(sb.toString() + errorDescriptions);
            super.addErrorRowData(row, startIndex, nrErrors, errorDescriptions, fieldNames, errorCodes);
        }
    };
    StepMeta outputDummyStepMeta = addDummyStep(transMeta, "InputErrorDummy");
    inputErrorMeta.setTargetStep(outputDummyStepMeta);
    inputErrorMeta.setEnabled(true);
    inputStepMeta.setStepErrorMeta(inputErrorMeta);
    return inputStepMeta;
}
Also used : StepErrorMeta(org.pentaho.di.trans.step.StepErrorMeta) StepMeta(org.pentaho.di.trans.step.StepMeta) TableInputMeta(org.pentaho.di.trans.steps.tableinput.TableInputMeta) FileTransformStats(org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats)

Example 3 with FileTransformStats

use of org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats in project data-access by pentaho.

the class CsvTransformGenerator method createInputStep.

protected StepMeta createInputStep(TransMeta transMeta) {
    CsvInputMeta csvInputMeta = new CsvInputMeta();
    CsvFileInfo fileInfo = getModelInfo().getFileInfo();
    String fileName = fileInfo.getTmpFilename();
    String path;
    if (fileName.endsWith(".tmp")) {
        // $NON-NLS-1$
        path = PentahoSystem.getApplicationContext().getSolutionPath(TMP_FILE_PATH);
    } else {
        String relativePath = PentahoSystem.getSystemSetting("file-upload-defaults/relative-path", // $NON-NLS-1$
        String.valueOf(DEFAULT_RELATIVE_UPLOAD_FILE_PATH));
        path = PentahoSystem.getApplicationContext().getSolutionPath(relativePath);
    }
    File file = new File(path + fileInfo.getTmpFilename());
    String filename = file.getAbsolutePath();
    ColumnInfo[] columns = getModelInfo().getColumns();
    TextFileInputField[] inputFields = new TextFileInputField[columns.length];
    int idx = 0;
    for (ColumnInfo column : columns) {
        TextFileInputField field = new TextFileInputField();
        field.setCurrencySymbol(fileInfo.getCurrencySymbol());
        field.setDecimalSymbol(fileInfo.getCurrencySymbol());
        field.setFormat(column.getFormat());
        field.setGroupSymbol(fileInfo.getGroupSymbol());
        field.setIfNullValue(fileInfo.getIfNull());
        field.setIgnored(column.isIgnore());
        field.setLength(column.getLength());
        field.setName(column.getId());
        field.setNullString(fileInfo.getNullStr());
        // field.setPosition(position);
        field.setPrecision(column.getPrecision());
        field.setRepeated(false);
        field.setSamples(null);
        field.setTrimType(ValueMeta.TRIM_TYPE_BOTH);
        field.setType(convertDataType(column));
        inputFields[idx] = field;
        idx++;
    }
    csvInputMeta.setAddResultFile(false);
    // $NON-NLS-1$
    csvInputMeta.setBufferSize("5000");
    csvInputMeta.setDelimiter(fileInfo.getDelimiter());
    csvInputMeta.setEnclosure(fileInfo.getEnclosure());
    csvInputMeta.setEncoding(fileInfo.getEncoding());
    csvInputMeta.setFilename(filename);
    csvInputMeta.setFilenameField(null);
    // TODO strip off more than one row if present...
    csvInputMeta.setHeaderPresent(fileInfo.getHeaderRows() > 0);
    // inputMeta.get.setID(1);
    csvInputMeta.setIncludingFilename(false);
    csvInputMeta.setInputFields(inputFields);
    csvInputMeta.setLazyConversionActive(true);
    // $NON-NLS-1$
    csvInputMeta.setRowNumField("");
    csvInputMeta.setRunningInParallel(false);
    // inputMeta.setTargetSteps(null);
    StepMeta csvInputStepMeta = new StepMeta(CSV_INPUT, CSV_INPUT, csvInputMeta);
    csvInputStepMeta.setStepErrorMeta(new StepErrorMeta(transMeta, csvInputStepMeta));
    transMeta.addStep(csvInputStepMeta);
    csvErrorRowCount = 0;
    final FileTransformStats stats = getTransformStats();
    StepErrorMeta csvInputErrorMeta = new StepErrorMeta(transMeta, csvInputStepMeta) {

        public void addErrorRowData(Object[] row, int startIndex, long nrErrors, String errorDescriptions, String fieldNames, String errorCodes) {
            if (csvErrorRowCount < maxErrorRows) {
                StringBuffer sb = new StringBuffer();
                sb.append("Rejected Row: ");
                for (Object rowData : row) {
                    sb.append(rowData);
                    sb.append(", ");
                }
                sb.append("\r\n");
                stats.getErrors().add(sb.toString() + errorDescriptions);
            }
            csvErrorRowCount++;
            stats.setErrorCount(csvErrorRowCount);
            super.addErrorRowData(row, startIndex, nrErrors, errorDescriptions, fieldNames, errorCodes);
        }
    };
    StepMeta outputDummyStepMeta = addDummyStep(transMeta, "CSVInputErrorDummy");
    csvInputErrorMeta.setTargetStep(outputDummyStepMeta);
    csvInputErrorMeta.setEnabled(true);
    csvInputStepMeta.setStepErrorMeta(csvInputErrorMeta);
    return csvInputStepMeta;
}
Also used : CsvInputMeta(org.pentaho.di.trans.steps.csvinput.CsvInputMeta) TextFileInputField(org.pentaho.di.trans.steps.textfileinput.TextFileInputField) StepErrorMeta(org.pentaho.di.trans.step.StepErrorMeta) ColumnInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ColumnInfo) StepMeta(org.pentaho.di.trans.step.StepMeta) CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) File(java.io.File) FileTransformStats(org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats)

Example 4 with FileTransformStats

use of org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats in project data-access by pentaho.

the class CsvDatasourceServiceImplTest method testHasPermissions.

@Test
public void testHasPermissions() throws Exception {
    hasPermissions = true;
    final ISystemSettings systemSettings = mock(ISystemSettings.class);
    when(systemSettings.getSystemSetting("data-access-override", "false")).thenReturn("false");
    PentahoSystem.setSystemSettingsService(systemSettings);
    String filename = "anotherStageFile_CsvFile.csv";
    File file = createTmpCsvFile(filename);
    file.deleteOnExit();
    try {
        ModelInfo modelInfo = service.stageFile(filename, ",", "\n", true, "utf-8");
        CsvFileInfo fileInfo = modelInfo.getFileInfo();
        assertEquals("One header row", 1, fileInfo.getHeaderRows());
        assertEquals("Header + content row", 2, fileInfo.getContents().size());
        assertEquals(filename, fileInfo.getTmpFilename());
        final FileInfo[] stagedFiles = service.getStagedFiles();
        assertNotNull(stagedFiles);
        boolean present = false;
        for (FileInfo info : stagedFiles) {
            if (filename.equals(info.getName())) {
                present = true;
                break;
            }
        }
        assertTrue(present);
        final String encoding = service.getEncoding(filename);
        assertNotNull(encoding);
        final List<String> previewRows = service.getPreviewRows(filename, true, 1, "utf-8");
        assertNotNull(previewRows);
        assertEquals(1, previewRows.size());
        assertEquals("col1,col2", previewRows.get(0));
        final DatasourceDTO datasourceDto = mock(DatasourceDTO.class);
        when(datasourceDto.getCsvModelInfo()).thenReturn(modelInfo);
        try {
            final FileTransformStats fileTransformStats = service.generateDomain(datasourceDto);
        } catch (Exception e) {
        // Testing this logic is not a purpose of this junit
        }
        // Passed permissions check
        verify(datasourceDto, times(1)).getCsvModelInfo();
    } finally {
        file.delete();
    }
}
Also used : CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) FileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo) ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings) Matchers.anyString(org.mockito.Matchers.anyString) DatasourceDTO(org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO) File(java.io.File) FileTransformStats(org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats) Test(org.junit.Test)

Example 5 with FileTransformStats

use of org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats in project data-access by pentaho.

the class CsvDatasourceServiceImpl method generateDomain.

public FileTransformStats generateDomain(DatasourceDTO datasourceDto) throws Exception {
    checkPermissions();
    synchronized (lock) {
        ModelInfo modelInfo = datasourceDto.getCsvModelInfo();
        IPentahoSession pentahoSession = null;
        try {
            pentahoSession = PentahoSessionHolder.getSession();
            KettleSystemListener.environmentInit(pentahoSession);
            String statsKey = // $NON-NLS-1$
            FileTransformStats.class.getSimpleName() + "_" + modelInfo.getFileInfo().getTmpFilename();
            FileTransformStats stats = new FileTransformStats();
            pentahoSession.setAttribute(statsKey, stats);
            CsvTransformGenerator csvTransformGenerator = new CsvTransformGenerator(modelInfo, AgileHelper.getDatabaseMeta());
            csvTransformGenerator.setTransformStats(stats);
            try {
                csvTransformGenerator.dropTable(modelInfo.getStageTableName());
            } catch (CsvTransformGeneratorException e) {
                // this is ok, the table may not have existed.
                // $NON-NLS-1$
                logger.info("Could not drop table before staging");
            }
            csvTransformGenerator.createOrModifyTable(pentahoSession);
            // no longer need to truncate the table since we dropped it a few lines up, so just pass false
            csvTransformGenerator.loadTable(false, pentahoSession, true);
            ArrayList<String> combinedErrors = new ArrayList<String>(modelInfo.getCsvInputErrors());
            combinedErrors.addAll(modelInfo.getTableOutputErrors());
            if (stats.getErrors() != null && stats.getErrors().size() > 0) {
                stats.getErrors().addAll(combinedErrors);
            } else {
                stats.setErrors(combinedErrors);
            }
            // wait until it it done
            while (!stats.isRowsFinished()) {
                Thread.sleep(200);
            }
            modelerWorkspace.setDomain(modelerService.generateCSVDomain(modelInfo));
            modelerWorkspace.getWorkspaceHelper().autoModelFlat(modelerWorkspace);
            modelerWorkspace.getWorkspaceHelper().autoModelRelationalFlat(modelerWorkspace);
            modelerWorkspace.setModelName(modelInfo.getDatasourceName());
            modelerWorkspace.getWorkspaceHelper().populateDomain(modelerWorkspace);
            Domain workspaceDomain = modelerWorkspace.getDomain();
            XStream xstream = new XStream();
            String serializedDto = xstream.toXML(datasourceDto);
            workspaceDomain.getLogicalModels().get(0).setProperty("datasourceModel", serializedDto);
            workspaceDomain.getLogicalModels().get(0).setProperty("DatasourceType", "CSV");
            prepareForSerialization(workspaceDomain);
            modelerService.serializeModels(workspaceDomain, modelerWorkspace.getModelName());
            stats.setDomain(modelerWorkspace.getDomain());
            return stats;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw e;
        } finally {
            if (pentahoSession != null) {
                pentahoSession.destroy();
            }
        }
    }
}
Also used : ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) XStream(com.thoughtworks.xstream.XStream) CsvTransformGenerator(org.pentaho.platform.dataaccess.datasource.wizard.service.agile.CsvTransformGenerator) ArrayList(java.util.ArrayList) Domain(org.pentaho.metadata.model.Domain) 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) FileTransformStats(org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats)

Aggregations

FileTransformStats (org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats)5 File (java.io.File)2 StepErrorMeta (org.pentaho.di.trans.step.StepErrorMeta)2 StepMeta (org.pentaho.di.trans.step.StepMeta)2 CsvFileInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo)2 ModelInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)2 XStream (com.thoughtworks.xstream.XStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 CsvInputMeta (org.pentaho.di.trans.steps.csvinput.CsvInputMeta)1 TableInputMeta (org.pentaho.di.trans.steps.tableinput.TableInputMeta)1 TextFileInputField (org.pentaho.di.trans.steps.textfileinput.TextFileInputField)1 Domain (org.pentaho.metadata.model.Domain)1 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)1 ISystemSettings (org.pentaho.platform.api.engine.ISystemSettings)1 ColumnInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ColumnInfo)1 CsvTransformGeneratorException (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)1