I have a need to be able to access the name of the flat file that the current line being read is from. We are porting an application and some processes that are currently in place require this information.
Here is my current configuration:
private FlatFileItemReader<Invoice> invoiceFlatFileItemReader() throws Exception {
FlatFileItemReader<Invoice> reader = new FlatFileItemReader<>();
reader.setLinesToSkip(1); // header row
reader.setLineMapper(new InvoiceLineMapper());
reader.afterPropertiesSet();
return reader;
}
InvoiceLineMapper:
public class InvoiceLineMapper implements LineMapper<Invoice> {
@Override
public Invoice mapLine(String line, int lineNumber) throws Exception {
String[] fields = line.split(",");
ArrayList<String> fieldList = new ArrayList<>();
for (int i = 0; i < fields.length; i++) {
fieldList.add(fields[i].replaceAll("\"", "");
}
Invoice invoice = new Invoice();
invoice.setCustomerNumber(Integer.parseInt(fieldList.get(0));
invoice.setCustomerName(fieldList.get(1));
// set other things and stuff...
//Need to be able to set the file name on the model here
invoice.setFileName(theFileName);
}
}
I am delegating to the invoiceFlatFileItemReader()
via a MultiResourceItemReader
:
@Bean
public ItemReader<Invoice> invoiceItemReader() throws Exception {
ResourcePatternResolver resolver = new PathMatchingResourcePatterResolver(this.getClass().getClassLoader());
// currently being loaded from the classpath, eventually will be read from S3...that comes later
Resource[] resources = resolver.getResources("classpath*:**/InvoiceList*.csv*");
MultiResourceItemReader<Invoice> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(invoiceFlatFileItemReader());
return reader;
}
How can I get the name of the resource passed down the chain to where I can access it in the InvoiceLineMapper
class and add it to the model (which later gets persisted with a RepositoryItemWriter
)?