package yanagishima.util;


import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

public class DownloadUtil {

    public static void tsvDownload(HttpServletResponse response, String fileName, String datasource, String queryid, String encode) {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
        try (PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), encode))) {
            try(BufferedReader br = Files.newBufferedReader(PathUtil.getResultFilePath(datasource, queryid, false));
                CSVPrinter csvPrinter = new CSVPrinter(printWriter, CSVFormat.EXCEL.withDelimiter('\t').withRecordSeparator(System.getProperty("line.separator")));) {
                CSVParser parse = CSVFormat.EXCEL.withDelimiter('\t').withNullString("\\N").parse(br);
                for (CSVRecord csvRecord : parse) {
                    List<String> columnList = new ArrayList<>();
                    for (String column : csvRecord) {
                        columnList.add(column);
                    }
                    csvPrinter.printRecord(columnList);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void csvDownload(HttpServletResponse response, String fileName, String datasource, String queryid, String encode) {
        response.setContentType("text/csv; charset=Shift_JIS");
        response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
        try (PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), encode))) {
            try(BufferedReader br = Files.newBufferedReader(PathUtil.getResultFilePath(datasource, queryid, false));
                CSVPrinter csvPrinter = new CSVPrinter(printWriter, CSVFormat.EXCEL.withRecordSeparator(System.getProperty("line.separator")));) {
                CSVParser parse = CSVFormat.EXCEL.withDelimiter('\t').withNullString("\\N").parse(br);
                for (CSVRecord csvRecord : parse) {
                    List<String> columnList = new ArrayList<>();
                    for (String column : csvRecord) {
                        columnList.add(column);
                    }
                    csvPrinter.printRecord(columnList);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

@@ -253,113 +253,9 @@
 .io.
-BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.OutputStreamWriter;%0Aimport java.io.PrintWriter
+*
 ;%0Aim
@@ -640,32 +640,183 @@
 leName + %22%5C%22%22);%0A
+        try %7B%0A            writeBOM(response.getOutputStream());%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A
         try (Pri
@@ -1906,27 +1906,8 @@
 /csv
-; charset=Shift_JIS
 %22);%0A
@@ -1990,32 +1990,184 @@
 leName + %22%5C%22%22);%0A
+        try %7B%0A            writeBOM(response.getOutputStream());%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A%0A
         try (Pri
@@ -3033,32 +3033,32 @@
 meException(e);%0A
-
         %7D%0A    %7D%0A
@@ -3058,10 +3058,156 @@
 %0A    %7D%0A%0A
+    public static void writeBOM(OutputStream out) throws IOException %7B%0A        out.write(new byte%5B%5D%7B (byte)0xef,(byte)0xbb, (byte)0xbf %7D);%0A    %7D%0A%0A
 %7D%0A

Add BOM because Excel can open without Shift_JIS