Spring Batch - How to save header information into individual row objects in FlaFileItemReader

473 views Asked by At

I am dealing with a text file delimited by a '|' having header and data followed by a trailer. This sequence can repeat in the same file. So my format is something like this

Header|Date - Employer1#|<br>
Employee ID|FirstName|LastName|<br>
Employee ID|FirstName|LastName|<br>
Trailer|2<br>
Header|Date - Employer2#|<br>
Employee ID|FirstName|LastName|<br>
Employee ID|FirstName|LastName|<br>
Employee ID|FirstName|LastName|<br>
Trailer|3<br>

My problem is that for every record, the row object that I create, must have the employer # from the header. On reading the next header, all rows that follow should have the new employer #. I am using Spring Batch with FlatFileItemReader to parse this file. I have a custom line mapper where I identify if the line is HEADER or data but I am not sure how to store the header data so that when I read the data line I have access to the previously read HEADER

@Override
    public MyDTO mapLine(String line, int lineNumber) throws Exception { 
        if(line != null && line.contains("HEADER")) {
            String[] tokenizedHeader = line.split("|");
            //Code to parse Header and save that data
            }else {
            DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer();
            delimitedLineTokenizer.setDelimiter("|");   
            delimitedLineTokenizer.setStrict(false);
            delimitedLineTokenizer.setNames(new String[] {"EMPLOYEE ID","FIRST NAME","LAST NAME"});
            setLineTokenizer(delimitedLineTokenizer);
            setFieldSetMapper(myFieldSetmapper);            
            return (MyDTO) this.fieldSetMapper.mapFieldSet(this.lineTokenizer.tokenize(line));
        }
    }

Any leads would be appreciated.
Thanks

0

There are 0 answers