package fr.insee.rmes.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

public class CSVUtils {

    private static Logger logger = LogManager.getLogger(CSVUtils.class);


    private Object csvToPOJO(String csv, Object pojo) throws IOException {
        CsvMapper mapper = new CsvMapper();
        //emptyScheme is necessary
        CsvSchema bootstrapSchema = CsvSchema.emptySchema().withHeader();

        MappingIterator<Object> it = mapper.readerForUpdating(pojo).with(bootstrapSchema).readValues(csv);
        while (it.hasNextValue()) {
            pojo = it.nextValue();
        }
        return pojo;
    }

    /*
     * Create POJO with the result of a request
     */
    public Object populatePOJO(String csv, Object pojo) {
        try {
            pojo = this.csvToPOJO(csv, pojo);
        }
        catch (Exception e) {
            logger.error(e.getMessage());
        }
        return pojo;
    }

    private <TARGETCLASS> List<TARGETCLASS> csvToMultiPOJO(String csv, Class<TARGETCLASS> childClass) throws IOException {
        List<TARGETCLASS> list = new ArrayList<>();
        CsvMapper mapper = new CsvMapper();
        CsvSchema schema = CsvSchema.emptySchema().withHeader();
        MappingIterator<Map<String, String>> it = mapper.readerFor(Map.class).with(schema).readValues(csv);
        while (it.hasNext()) {
            Map<String, String> rowAsMap = it.next();
            TARGETCLASS activite = mapper.convertValue(rowAsMap, childClass);
            list.add(activite);
        }
        return list;
    }

    /**
     * Create POJOs
     * @param csv : result of the request
     * @param childClass : POJO class
     * @return
     */
    public <TARGETCLASS> List<TARGETCLASS> populateMultiPOJO(String csv, Class<TARGETCLASS> childClass) {
        List<TARGETCLASS> list = new ArrayList<>();
        try {
            list = this.csvToMultiPOJO(csv, childClass);
        }
        catch (Exception e) {
            logger.error(e.getMessage());
        }
        return list;
    }

}

@@ -1240,36 +1240,16 @@
 e %3CT
-ARGETCLASS%3E List%3CTARGETCLASS
+%3E List%3CT
 %3E cs
@@ -1276,34 +1276,24 @@
 csv, Class%3CT
-ARGETCLASS
 %3E childClass
@@ -1325,34 +1325,24 @@
       List%3CT
-ARGETCLASS
 %3E list = new
@@ -1672,26 +1672,16 @@
        T
-ARGETCLASS
  activit
@@ -1945,36 +1945,16 @@
 c %3CT
-ARGETCLASS%3E List%3CTARGETCLASS
+%3E List%3CT
 %3E po
@@ -1988,26 +1988,16 @@
  Class%3CT
-ARGETCLASS
 %3E childC
@@ -2022,18 +2022,8 @@
 st%3CT
-ARGETCLASS
 %3E li

Update CSVUtils.java