1 package org.simantics.district.imports;
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.List;
15 import org.apache.commons.csv.CSVFormat;
16 import org.apache.commons.csv.CSVParser;
17 import org.apache.commons.csv.CSVRecord;
18 import org.simantics.db.Resource;
20 public class DistrictImportUtils {
22 private DistrictImportUtils() { }
24 public static Resource importCSVAsLayer(Path csvFile) throws IOException {
26 try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
27 Map<String, Integer> header = parser.getHeaderMap();
28 System.out.println(header);
33 public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
34 return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
37 public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
39 format = format.withFirstRecordAsHeader();
40 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
41 return parser.getHeaderMap();
45 public static Map<String, Character> getSupportedCSVDelimiterFormats() {
46 Map<String, Character> delimiters = new HashMap<>();
47 delimiters.put("Comma", ',');
48 delimiters.put("Semicolon", ';');
52 public static List<Map<String, String>> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
53 return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount);
56 public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
58 format = format.withFirstRecordAsHeader();
59 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
61 List<Map<String, String>> results = new ArrayList<>(amount);
62 Iterator<CSVRecord> iter = parser.iterator();
63 while (start < amount && iter.hasNext()) {
64 CSVRecord record = iter.next();
65 results.add(record.toMap());
72 public static List<CSVRecord> readRows(Path source, char delim, int rowAmount) throws IOException {
73 List<CSVRecord> results = new ArrayList<>();
74 CSVFormat format = CSVFormat.newFormat(delim);
75 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
76 Iterator<CSVRecord> records = parser.iterator();
78 if (rowAmount == -1) {
79 while (records.hasNext()) {
80 results.add(records.next());
84 while (rows < rowAmount && records.hasNext()) {
85 results.add(records.next());
94 public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
95 Map<CSVHeader, List<String>> results = new HashMap<>();
96 CSVFormat format = CSVFormat.newFormat(delimiter);
98 format = format.withFirstRecordAsHeader();
99 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
100 Map<String, Integer> headers = parser.getHeaderMap();
101 if (headers != null && !headers.isEmpty()) {
102 for (int index = 0; index < headers.size(); index++) {
103 for (String head : headers.keySet()) {
104 results.put(new CSVHeader(head, index), new ArrayList<>());
109 Iterator<CSVRecord> records = parser.iterator();
111 while (rows < amount && records.hasNext()) {
112 CSVRecord record = records.next();
113 for (int j = 0; j < record.size(); j++) {
114 String value = record.get(j);
115 String header = Integer.toString(j);
116 CSVHeader csvHeader = new CSVHeader(header, j);
117 List<String> vals = results.get(csvHeader);
119 vals = new ArrayList<>();
120 results.put(csvHeader, vals);
130 public static class CSVHeader {
132 private final String header;
133 private final int index;
135 public CSVHeader(String header, int index) {
136 this.header = header;
140 public String getHeader() {
144 public int getIndex() {
149 public int hashCode() {
150 final int prime = 31;
152 result = prime * result + ((header == null) ? 0 : header.hashCode());
153 result = prime * result + index;
158 public boolean equals(Object obj) {
163 if (getClass() != obj.getClass())
165 CSVHeader other = (CSVHeader) obj;
166 if (header == null) {
167 if (other.header != null)
169 } else if (!header.equals(other.header))
171 if (index != other.index)
177 public static Collection<String> readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException {
178 Set<String> results = new HashSet<>();
179 CSVFormat format = CSVFormat.newFormat(delim);
180 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
181 Iterator<CSVRecord> records = parser.iterator();
182 if (records.hasNext())
184 while (records.hasNext()) {
185 CSVRecord row = records.next();
186 String value = row.get(mappingIndex);