Subversion Repositories SmartDukaan

Rev

Rev 34769 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
34769 vikas.jang 1
package com.spice.profitmandi.common.util;
2
 
3
import org.springframework.beans.factory.annotation.Value;
4
 
5
import java.io.File;
34835 vikas 6
import java.nio.charset.StandardCharsets;
34769 vikas.jang 7
import java.nio.file.*;
8
import java.sql.*;
34835 vikas 9
import java.util.List;
10
import java.util.Map;
11
import java.util.Set;
34769 vikas.jang 12
 
13
public class CsvUtil {
14
 
15
    private CsvUtil() {
16
    }
34835 vikas 17
    private static final String connURL = "jdbc:mysql://192.168.142.141:3306/auth";
34769 vikas.jang 18
    private static final String username = "root";
19
    private static final String password = "shop2020";
20
 
34835 vikas 21
    public static File generateCsvFromQuery(String query, String headers) throws Exception {
22
        Connection conn = DriverManager.getConnection(connURL, username, password);
34769 vikas.jang 23
        PreparedStatement stmt = conn.prepareStatement(query);
24
        ResultSet rs = stmt.executeQuery();
25
 
26
        ResultSetMetaData metaData = rs.getMetaData();
27
        int columnCount = metaData.getColumnCount();
28
 
29
        StringBuilder csvBuilder = new StringBuilder();
30
 
31
        // Headers
32
        if (headers != null && !headers.trim().isEmpty()) {
33
            csvBuilder.append(headers.trim()).append("\n");
34
        } else {
35
            for (int i = 1; i <= columnCount; i++) {
36
                csvBuilder.append(metaData.getColumnLabel(i));
37
                if (i < columnCount) csvBuilder.append(",");
38
            }
39
            csvBuilder.append("\n");
40
        }
41
 
42
        // Rows
43
        while (rs.next()) {
44
            for (int i = 1; i <= columnCount; i++) {
45
                String value = rs.getString(i);
46
                csvBuilder.append(value != null ? value.replaceAll(",", " ") : "");
47
                if (i < columnCount) csvBuilder.append(",");
48
            }
49
            csvBuilder.append("\n");
50
        }
51
 
52
        rs.close();
53
        stmt.close();
54
        conn.close();
55
 
56
        Path tempFile = Files.createTempFile("query-result-", ".csv");
57
        Files.write(tempFile, csvBuilder.toString().getBytes());
58
        return tempFile.toFile();
59
    }
34835 vikas 60
 
61
    public static File generateCsvFromListMap(List<Map<String, Object>> data, String headers) throws Exception {
62
        StringBuilder csvBuilder = new StringBuilder();
63
 
64
        if (headers != null && !headers.trim().isEmpty()) {
65
            csvBuilder.append(headers.trim()).append("\n");
66
        } else if (!data.isEmpty()) {
67
            Set<String> keys = data.get(0).keySet();
68
            csvBuilder.append(String.join(",", keys)).append("\n");
69
        }
70
 
71
        for (Map<String, Object> row : data) {
72
            int i = 0;
73
            for (Object value : row.values()) {
74
                String cell = value != null ? value.toString().replaceAll(",", " ") : "";
75
                csvBuilder.append(cell);
76
                if (++i < row.size()) csvBuilder.append(",");
77
            }
78
            csvBuilder.append("\n");
79
        }
80
 
81
        Path tempFile = Files.createTempFile("query-result-", ".csv");
82
        Files.write(tempFile, csvBuilder.toString().getBytes(StandardCharsets.UTF_8));
83
        return tempFile.toFile();
84
    }
34769 vikas.jang 85
}
86