Example usage for org.springframework.batch.item.file.transform FieldSet getNames

List of usage examples for org.springframework.batch.item.file.transform FieldSet getNames

Introduction

In this page you can find the example usage for org.springframework.batch.item.file.transform FieldSet getNames.

Prototype

String[] getNames();

Source Link

Document

Accessor for the names of the fields.

Usage

From source file:de.langmi.spring.batch.examples.readers.file.csv.CsvFlatFileItemReaderTest.java

/**
 * Test should read succesfully./*w w w .  j  a  v a  2s . com*/
 *
 * @throws Exception 
 */
@Test
public void testSuccessfulReading() throws Exception {
    // init linetokenizer
    DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
    lineTokenizer.setNames(new String[] { "id", "value" });
    // init linemapper
    DefaultLineMapper<FieldSet> lineMapper = new DefaultLineMapper<FieldSet>();
    lineMapper.setLineTokenizer(lineTokenizer);
    lineMapper.setFieldSetMapper(new PassThroughFieldSetMapper());
    // init reader
    reader.setLineMapper(lineMapper);
    reader.setResource(new FileSystemResource(INPUT_FILE));
    // open, provide "mock" ExecutionContext
    reader.open(MetaDataInstanceFactory.createStepExecution().getExecutionContext());
    // read
    try {
        int count = 0;
        FieldSet line;
        while ((line = reader.read()) != null) {
            // really test for the fieldSet names and values
            assertEquals("id", line.getNames()[0]);
            assertEquals(String.valueOf(count), line.getValues()[0]);
            assertEquals("value", line.getNames()[1]);
            // csv contains entry like '0,foo0'
            assertEquals("foo" + String.valueOf(count), line.getValues()[1]);
            count++;
        }
        assertEquals(EXPECTED_COUNT, count);
    } catch (Exception e) {
        throw e;
    } finally {
        reader.close();
    }
}

From source file:fr.acxio.tools.agia.item.database.AbstractFieldSetSqlParameterSourceProvider.java

protected MapSqlParameterSource mapFieldSet(MapSqlParameterSource sMapSqlParameterSource, FieldSet sFieldSet,
        int sRecIdx) {
    if (sFieldSet != null) {
        boolean aHasNames = sFieldSet.hasNames();
        int aFieldCount = sFieldSet.getFieldCount();
        String[] aNames = aHasNames ? sFieldSet.getNames() : null;
        String[] aValues = sFieldSet.getValues();
        for (int i = 0; i < aFieldCount; i++) {
            sMapSqlParameterSource.addValue(String.format(fieldsetNameFormat, sRecIdx,
                    (aHasNames && (aNames[i] != null) && !aNames[i].isEmpty()) ? aNames[i]
                            : String.format(unnamedColumnFormat, i)),
                    aValues[i]);//  w  w w .ja v  a  2 s  . com
        }
    }
    return sMapSqlParameterSource;
}

From source file:fr.acxio.tools.agia.transform.ListFieldSetToMapProcessor.java

protected Map<String, Object> mapFieldSet(FieldSet sFieldSet, int sRecIdx) {
    Map<String, Object> aResult = new HashMap<String, Object>();
    if (sFieldSet != null) {
        boolean aHasNames = sFieldSet.hasNames();
        int aFieldCount = sFieldSet.getFieldCount();
        String[] aNames = aHasNames ? sFieldSet.getNames() : null;
        String[] aValues = sFieldSet.getValues();
        for (int i = 0; i < aFieldCount; i++) {
            aResult.put(String.format(fieldsetNameFormat, sRecIdx,
                    (aHasNames && (aNames[i] != null) && !aNames[i].isEmpty()) ? aNames[i]
                            : String.format(unnamedColumnFormat, i)),
                    aValues[i]);//from   ww w.  j  a  v a2  s  .  c o  m
        }
    }
    return aResult;
}

From source file:org.cbioportal.annotation.pipeline.MutationFieldSetMapper.java

@Override
public MutationRecord mapFieldSet(FieldSet fs) throws BindException {
    MutationRecord record = new MutationRecord();
    Set<String> names = new HashSet(Arrays.asList(fs.getNames()));
    names.addAll(record.getHeader());//from w  w  w .  j  a va2 s. c om
    for (String field : names) {
        try {
            record.getClass().getMethod("set" + field.toUpperCase(), String.class).invoke(record,
                    fs.readRawString(field));
        } catch (Exception e) {
            if (e.getClass().equals(NoSuchMethodException.class)) {
                record.addAdditionalProperty(field, fs.readRawString(field));
            } else {
                LOG.error("Something went wrong reading field " + field);
            }
        }
    }
    return record;
}