AbstractAssociationItemReader.java
package com.sintia.ffl.admin.optique.catalogue.batch.reader;
import com.sintia.ffl.admin.optique.catalogue.batch.reporter.Reporter;
import com.sintia.ffl.admin.optique.catalogue.models.AssociationCSV;
import com.sintia.ffl.admin.optique.catalogue.util.Constants;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.FileSystemResource;
import java.nio.file.Paths;
/**
* Read raw data from a csv file and convert them to {@link AssociationCSV}<br>
* As we can read an association file in the enrichment or the loading process,
* this abstract class factorize the actions that are the same.<br>
* The actuals implementation ({@see LoadCatalogAssociationItemReader} and
* {@see EnrichCatalogAssociationItemReader}) will contain the specificities
*
* @author jumazet
*/
public abstract class AbstractAssociationItemReader extends FlatFileItemReader<AssociationCSV> {
private String[] columnsName = new String[] { "maker", "provider", "glassOptoCode", "extraCode", "mandatoryExtra",
"includedExtra" };
private String fileName;
protected AbstractAssociationItemReader(String fileName, String localResourcesDirectory, String specificDirectory) {
super();
this.fileName = fileName;
String separator = System.getProperty("file.separator");
String localPath = localResourcesDirectory + separator + specificDirectory;
this.setResource(new FileSystemResource(Paths.get(localPath)
.resolve(fileName)
.toString()));
AssociationLineMapper lineMapper = new AssociationLineMapper();
lineMapper.setFieldSetMapper(new AssociationFieldSetMapper());
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames(columnsName);
tokenizer.setDelimiter(Constants.CSV_SEPARATOR);
lineMapper.setLineTokenizer(tokenizer);
// Skip the first line (ie the header line)
this.setLinesToSkip(1);
this.setEncoding("UTF-8");
this.setLineMapper(lineMapper);
}
@Override
public void doOpen()
throws Exception {
try {
super.doOpen();
} catch (Exception e) {
this.getReporter()
.addError(
String.format("Format CSV invalide - Erreur ouverture du fichier %s : %s", this.fileName, e.getMessage()));
// re-throws the exception to stop the job process
throw e;
}
}
public abstract Reporter getReporter();
}