package com.commerzinfo.input.csv;

import au.com.bytecode.opencsv.bean.CsvToBean;
import au.com.bytecode.opencsv.bean.HeaderColumnNameTranslateMappingStrategy;
import com.commerzinfo.DataRow;
import com.commerzinfo.util.CompressionUtil;
import com.commerzinfo.util.DateUtil;
import com.commerzinfo.util.DecimalFormatUtil;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CSVParser {
    private static Logger logger = LoggerFactory.getLogger(CSVParser.class);

    public static List<DataRow> handleCSV(File file) throws IOException {
        List<DataRow> dataRows = Lists.newArrayList();

        au.com.bytecode.opencsv.CSVReader csvReader = new au.com.bytecode.opencsv.CSVReader(new InputStreamReader(CompressionUtil.getCorrectInputStream(file), "UTF-8"), ';', '"');
        HeaderColumnNameTranslateMappingStrategy<CSVBean> strat = new HeaderColumnNameTranslateMappingStrategy<CSVBean>();
        strat.setType(CSVBean.class);
        Map<String, String> map = new HashMap<String, String>();
        map.put("Buchungstag", "buchungstag");
        map.put("Wertstellung", "wertstellung");
        map.put("Buchungstext", "buchungstext");
        map.put("Betrag", "betrag");
        map.put("Währung", "waehrung");
        strat.setColumnMapping(map);

        CsvToBean<CSVBean> csv = new CsvToBean<CSVBean>();
        List<CSVBean> csvBeanList = csv.parse(strat, csvReader);

        for (CSVBean csvBean : csvBeanList) {
            try {
                DataRow row = new DataRow();
                row.setBookingDate(DateUtil.parse(csvBean.getBuchungstag()));
                row.setValueDate(DateUtil.parse(csvBean.getWertstellung()));
                row.setBookingText(csvBean.getBuchungstext());
                row.setValue((java.math.BigDecimal) DecimalFormatUtil.parse(csvBean.getBetrag(), DecimalFormatUtil.Mode.CSV));
                dataRows.add(row);
            } catch (ParseException e) {
                logger.error("problem with datarow mapping", e);
            }
        }
        logger.info("{} has {} parsed rows", file.getAbsolutePath(), dataRows.size());
        return dataRows;
    }
}

@@ -29,16 +29,58 @@
 t.csv;%0A%0A
+import au.com.bytecode.opencsv.CSVReader;%0A
 import a
@@ -196,16 +196,69 @@
 rategy;%0A
+import au.com.bytecode.opencsv.bean.MappingStrategy;%0A
 import c
@@ -745,376 +745,84 @@
 tic 
-Logger logger = LoggerFactory.getLogger(CSVParser.class);%0A%0A    public static List%3CDataRow%3E handleCSV(File file) throws IOException %7B%0A        List%3CDataRow%3E dataRows = Lists.newArrayList();%0A%0A        au.com.bytecode.opencsv.CSVReader
+final CsvToBean%3CCSVBean%3E
  csv
-Reader
+ToBean
  = new 
-au.com.bytecode.opencsv.CSVReader(new InputStreamReader(CompressionUtil.getCorrectInputStream(file), %22UTF-8%22), ';', '%22');
+CsvToBean%3CCSVBean%3E();%0A%0A    static %7B
 %0A   
@@ -826,16 +826,22 @@
         
+final 
 HeaderCo
@@ -882,21 +882,40 @@
 SVBean%3E 
-s
+translateMappingS
 trat
+egy
  = new H
@@ -970,29 +970,48 @@
 ();%0A        
-s
+translateMappingS
 trat
+egy
 .setType(CSV
@@ -1318,21 +1318,40 @@
         
-s
+translateMappingS
 trat
+egy
 .setColu
@@ -1370,65 +1370,514 @@
 p);%0A
+        mappingStrategy = translateMappingStrategy;
 %0A    
+%7D%0A
     
-CsvToBean%3CCSVBean%3E csv = new CsvToBean%3CCSVBean%3E(
+private static Logger logger = LoggerFactory.getLogger(CSVParser.class);%0A    private static MappingStrategy%3CCSVBean%3E mappingStrategy = null;%0A%0A    public static List%3CDataRow%3E handleCSV(File file) throws IOException %7B%0A        List%3CDataRow%3E dataRows = Lists.newArrayList();%0A        CSVReader csvReader = null;%0A        try %7B%0A            csvReader = new CSVReader(new InputStreamReader(CompressionUtil.getCorrectInputStream(file), %22UTF-8%22), ';', '%22'
 );%0A 
@@ -1879,24 +1879,28 @@
 ');%0A        
+    
 List%3CCSVBean
@@ -1922,20 +1922,36 @@
  csv
+ToBean
 .parse(
-s
+mappingS
 trat
+egy
 , cs
@@ -1957,24 +1957,28 @@
 svReader);%0A%0A
+    
         for 
@@ -2015,38 +2015,46 @@
 ) %7B%0A            
+    
 try %7B%0A
+    
                 
@@ -2090,32 +2090,36 @@
                 
+    
 row.setBookingDa
@@ -2172,32 +2172,36 @@
                 
+    
 row.setValueDate
@@ -2253,32 +2253,36 @@
                 
+    
 row.setBookingTe
@@ -2308,24 +2308,28 @@
 ngstext());%0A
+    
             
@@ -2459,16 +2459,20 @@
         
+    
 dataRows
@@ -2482,16 +2482,20 @@
 d(row);%0A
+    
         
@@ -2539,24 +2539,28 @@
             
+    
 logger.error
@@ -2596,16 +2596,140 @@
 g%22, e);%0A
+                %7D%0A            %7D%0A        %7D finally %7B%0A            if (csvReader != null) %7B%0A                csvReader.close();%0A
         

make common things static