/*
 * The MIT License
 *
 *  Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud@benhassine.fr)
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

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 (mahmoud@benhassine.fr)
 */
public class OpenCsvRecordMapper<T> implements RecordMapper<T> {

    private CSVReader openCsvReader;

    private char delimiter = ',';

    private char qualifier;

    private char escape;

    private int line;

    private boolean strictQualifiers;

    private boolean ignoreLeadingWhitespace;

    private ColumnPositionMappingStrategy<T> strategy;

    private 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);
        this.csvToBean = new CsvToBean();
    }

    @Override
    public T mapRecord(Record record) throws Exception {
        String payload = (String) record.getPayload();
        openCsvReader = new CSVReader(
                new StringReader(payload),
                delimiter,
                qualifier,
                escape,
                line,
                strictQualifiers,
                ignoreLeadingWhitespace);
        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;
    }

    public void setEscape(char escape) {
        this.escape = escape;
    }

    public void setLine(int line) {
        this.line = line;
    }

    public void setStrictQualifiers(boolean strictQualifiers) {
        this.strictQualifiers = strictQualifiers;
    }

    public void setIgnoreLeadingWhitespace(boolean ignoreLeadingWhitespace) {
        this.ignoreLeadingWhitespace = ignoreLeadingWhitespace;
    }

}
@@ -1894,127 +1894,32 @@
 ate 
-char escape;%0A%0A    private int line;%0A%0A    private boolean strictQualifiers;%0A%0A    private boolean ignoreLeadingWhitespace
+boolean strictQualifiers
 ;%0A%0A 
@@ -2589,111 +2589,24 @@
     
-escape,%0A                line,%0A                strictQualifiers,%0A                ignoreLeadingWhitespace
+strictQualifiers
 );%0A 
@@ -2920,397 +2920,101 @@
  set
-Escape(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
+StrictQualifiers(boolean strictQualifiers) %7B%0A        this.strictQualifiers = strictQualifiers
 ;%0A  

remove unused parameters