package org.easybatch.integration.opencsv;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;
import org.easybatch.core.api.Record;
import org.easybatch.core.api.RecordMapper;

import java.io.StringReader;
import java.util.List;

/**
 * A record mapper that uses <a href="http://opencsv.sourceforge.net">Open CSV</a> to map a delimited record to domain object.
 *
 * @author Mahmoud Ben Hassine (md.benhassine@gmail.com)
 */
public class OpenCsvRecordMapper<T> implements RecordMapper<T> {

    private CSVReader openCsvReader;

    private char delimiter = ',';

    private char qualifier;

    private ColumnPositionMappingStrategy<T> strategy;

    CsvToBean csvToBean;

    public OpenCsvRecordMapper(Class<? extends T> recordClass, String[] columns) {
        this.strategy = new ColumnPositionMappingStrategy<T>();
        this.strategy.setType((Class<T>) recordClass);
        this.strategy.setColumnMapping(columns);
        csvToBean = new CsvToBean();
    }

    @Override
    public T mapRecord(Record record) throws Exception {
        String recordContent = (String) record.getRawContent();
        openCsvReader = new CSVReader(new StringReader(recordContent), delimiter, qualifier);
        List list = csvToBean.parse(strategy, openCsvReader);
        return (T) list.get(0);
    }

    public void setDelimiter(final char delimiter) {
        this.delimiter = delimiter;
    }

    public void setQualifier(final char qualifier) {
        this.qualifier = qualifier;
    }

}
@@ -688,24 +688,158 @@
 qualifier;%0A%0A
+    private char escape;%0A%0A    private int line;%0A%0A    private boolean strictQualifiers;%0A%0A    private boolean ignoreLeadingWhitespace;%0A%0A
     private 
@@ -886,16 +886,24 @@
 y;%0A%0A    
+private 
 CsvToBea
@@ -1175,16 +1175,21 @@
         
+this.
 csvToBea
@@ -1389,16 +1389,33 @@
 VReader(
+%0A                
 new Stri
@@ -1442,29 +1442,182 @@
 nt),
- delimiter, qualifier
+%0A                delimiter,%0A                qualifier,%0A                escape,%0A                line,%0A                strictQualifiers,%0A                ignoreLeadingWhitespace
 );%0A 
@@ -1908,9 +1908,426 @@
 %0A    %7D%0A%0A
+    public void setEscape(char escape) %7B%0A        this.escape = escape;%0A    %7D%0A%0A    public void setLine(int line) %7B%0A        this.line = line;%0A    %7D%0A%0A    public void setStrictQualifiers(boolean strictQualifiers) %7B%0A        this.strictQualifiers = strictQualifiers;%0A    %7D%0A%0A    public void setIgnoreLeadingWhitespace(boolean ignoreLeadingWhitespace) %7B%0A        this.ignoreLeadingWhitespace = ignoreLeadingWhitespace;%0A    %7D%0A%0A
 %7D

add setters for open csv parameters