Search in sources :

Example 1 with CsvFileInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo in project data-access by pentaho.

the class CsvPhysicalStep method refreshPreview.

@Bindable
public void refreshPreview() throws Exception {
    // $NON-NLS-1$
    csvTextPreview.setValue("");
    CsvFileInfo fileInfo = datasourceModel.getModelInfo().getFileInfo();
    csvTextPreview.setValue(fileInfo.formatSampleContents());
}
Also used : CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) Bindable(org.pentaho.ui.xul.stereotype.Bindable)

Example 2 with CsvFileInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo in project data-access by pentaho.

the class CsvDatasourceServiceImpl method prepareForSerialization.

protected void prepareForSerialization(Domain domain) throws IOException {
    /*
     * This method is responsible for cleaning up legacy information when
     * changing datasource types and also manages CSV files for CSV based
     * datasources.
     */
    String relativePath = PentahoSystem.getSystemSetting("file-upload-defaults/relative-path", // $NON-NLS-1$
    String.valueOf(FileUtils.DEFAULT_RELATIVE_UPLOAD_FILE_PATH));
    String path = PentahoSystem.getApplicationContext().getSolutionPath(relativePath);
    String TMP_FILE_PATH = File.separatorChar + "system" + File.separatorChar + File.separatorChar + "tmp" + File.separatorChar;
    String sysTmpDir = PentahoSystem.getApplicationContext().getSolutionPath(TMP_FILE_PATH);
    LogicalModel logicalModel = domain.getLogicalModels().get(0);
    // $NON-NLS-1$
    String modelState = (String) logicalModel.getProperty("datasourceModel");
    if (modelState != null) {
        XStream xs = new XStream();
        DatasourceDTO datasource = (DatasourceDTO) xs.fromXML(modelState);
        CsvFileInfo csvFileInfo = datasource.getCsvModelInfo().getFileInfo();
        String tmpFileName = csvFileInfo.getTmpFilename();
        String csvFileName = csvFileInfo.getFilename();
        File tmpFile = new File(sysTmpDir + File.separatorChar + tmpFileName);
        // Move CSV temporary file to final destination.
        if (tmpFile.exists()) {
            File csvFile = new File(path + File.separatorChar + csvFileName);
            org.apache.commons.io.FileUtils.copyFile(tmpFile, csvFile);
        }
        // Cleanup logic when updating from SQL datasource to CSV
        // datasource.
        datasource.setQuery(null);
        // Update datasourceModel with the new modelState
        modelState = xs.toXML(datasource);
        logicalModel.setProperty("datasourceModel", modelState);
    }
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) XStream(com.thoughtworks.xstream.XStream) DatasourceDTO(org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO) File(java.io.File)

Example 3 with CsvFileInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo 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 CsvFileInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo 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 CsvFileInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo in project data-access by pentaho.

the class CsvDatasourceServiceImplTest method stageFile_CsvFile.

@Test
public void stageFile_CsvFile() throws Exception {
    String filename = "stageFile_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());
    } finally {
        file.delete();
    }
}
Also used : CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) Test(org.junit.Test)

Aggregations

CsvFileInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo)9 File (java.io.File)5 ModelInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)5 DatasourceDTO (org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO)3 XStream (com.thoughtworks.xstream.XStream)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 LogicalModel (org.pentaho.metadata.model.LogicalModel)2 ColumnInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ColumnInfo)2 FileTransformStats (org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats)2 StepErrorMeta (org.pentaho.di.trans.step.StepErrorMeta)1 StepMeta (org.pentaho.di.trans.step.StepMeta)1 CsvInputMeta (org.pentaho.di.trans.steps.csvinput.CsvInputMeta)1 TextFileInputField (org.pentaho.di.trans.steps.textfileinput.TextFileInputField)1 ISystemSettings (org.pentaho.platform.api.engine.ISystemSettings)1 CsvTransformGeneratorException (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)1 FileInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo)1 CsvTransformGenerator (org.pentaho.platform.dataaccess.datasource.wizard.service.agile.CsvTransformGenerator)1 Bindable (org.pentaho.ui.xul.stereotype.Bindable)1