Announcement Announcement Module
No announcement yet.
Reading a File if field is empty setting the default value Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading a File if field is empty setting the default value

    Hi all,

    I am very new to spring batch. I have requirement in which i have to read a file having a header(Field Names) record and data records

    i have to validate 1st record (check the field names matching against set of predefined names)- note that this record need to be skipped- i mean should not be part of items in processor)

    read and store rest of the field values to a POJO

    if the field 'date' is empty , i need to set the default value as 'xxxx-yy-zz'

    i am unable to 1st and 3rd requirement with batch here is the sample reader XML. please help

    <bean id="reader" class="org.springframework.batch.item.file.FlatFil eItemReader">
    <property name="resource" value="classpath:input/import" />
    <property name="encoding" value="UTF-8" />
    <property name="linesToSkip" value="1" />
    <property name="lineMapper" ref="line.mapper"/>

    <bean id="line.mapper" class="org.springframework.batch.item.file.mapping .DefaultLineMapper">
    <property name="lineTokenizer" ref="line.tokenizer"/>
    <property name="fieldSetMapper" ref="fieldSet.enity.mapper"/>

    <bean id="line.tokenizer" class="org.springframework.batch.item.file.transfo rm.DelimitedLineTokenizer">
    <property name="delimiter">
    <util:constant static-field="org.springframework.batch.item.file.transfo rm.DelimitedLineTokenizer.DELIMITER_TAB"/>
    <property name="names" value="id,date,age " />
    <property name="strict" value="false"/>

    <bean id="fieldSet.enity.mapper" class="org.springframework.batch.item.file.mapping .BeanWrapperFieldSetMapper">
    <property name="targetType" value="a.b.myPOJO"/>
    <property name="customEditors">
    <entry key="java.util.Date">
    <bean class="org.springframework.beans.propertyeditors.C ustomDateEditor">
    <bean class="java.text.SimpleDateFormat">
    <constructor-arg value="yyyy-mm-dd" />
    <constructor-arg value="true" />
    Last edited by binoj_viswam; Mar 15th, 2013, 01:04 AM.

  • #2
    For requirement 1, I would read the one line in it's own step. Read it, validate it then continue on if it's valid and don't if it's not. Then on the second step, skip it (this works with a couple lines at the top of a file...if it's a large number of lines we'd want to explore other options).
    For requirement 3, implement your own FieldSetMapper.