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;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import java.util.function.Function;
17 import org.apache.commons.csv.CSVFormat;
18 import org.apache.commons.csv.CSVParser;
19 import org.apache.commons.csv.CSVRecord;
20 import org.simantics.db.Resource;
22 public class DistrictImportUtils {
24 private DistrictImportUtils() { }
26 public static Resource importCSVAsLayer(Path csvFile) throws IOException {
28 try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
29 Map<String, Integer> header = parser.getHeaderMap();
30 System.out.println(header);
35 public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
36 return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
39 public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
41 format = format.withFirstRecordAsHeader();
42 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
43 return parser.getHeaderMap();
47 public static Map<String, Character> getSupportedCSVDelimiterFormats() {
48 Map<String, Character> delimiters = new HashMap<>();
49 delimiters.put("Comma", ',');
50 delimiters.put("Semicolon", ';');
51 delimiters.put("Tabulator", '\t');
55 public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
57 format = format.withFirstRecordAsHeader();
58 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
60 List<Map<String, String>> results = new ArrayList<>(amount);
61 Iterator<CSVRecord> iter = parser.iterator();
62 while (start < amount && iter.hasNext()) {
63 CSVRecord record = iter.next();
64 results.add(record.toMap());
71 public static List<CSVRecord> readRows(Path source, char delim, boolean firstAsHeader, int rowAmount) throws IOException {
72 List<CSVRecord> results = new ArrayList<>();
73 AtomicInteger count = new AtomicInteger(0);
74 consumeCSV(source, delim, firstAsHeader, t -> {
76 int current = count.incrementAndGet();
77 return current < rowAmount;
82 public static void consumeCSV(Path source, char delim, boolean firstAsHeader, Function<CSVRecord, Boolean> consumer) throws IOException {
83 CSVFormat format = CSVFormat.newFormat(delim);
85 format = format.withFirstRecordAsHeader();
87 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
88 Iterator<CSVRecord> records = parser.iterator();
89 while (records.hasNext()) {
90 Boolean cont = consumer.apply(records.next());
99 public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
100 Map<CSVHeader, List<String>> results = new HashMap<>();
101 CSVFormat format = CSVFormat.newFormat(delimiter);
103 format = format.withFirstRecordAsHeader();
104 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
105 Map<String, Integer> headers = parser.getHeaderMap();
106 if (headers != null && !headers.isEmpty()) {
107 for (int index = 0; index < headers.size(); index++) {
108 for (String head : headers.keySet()) {
109 results.put(new CSVHeader(head, index), new ArrayList<>());
114 Iterator<CSVRecord> records = parser.iterator();
116 while (rows < amount && records.hasNext()) {
117 CSVRecord record = records.next();
118 for (int j = 0; j < record.size(); j++) {
119 String value = record.get(j);
120 String header = Integer.toString(j);
121 CSVHeader csvHeader = new CSVHeader(header, j);
122 List<String> vals = results.get(csvHeader);
124 vals = new ArrayList<>();
125 results.put(csvHeader, vals);
135 public static class CSVHeader {
137 private final String header;
138 private final int index;
140 public CSVHeader(String header, int index) {
141 this.header = header;
145 public String getHeader() {
149 public int getIndex() {
154 public int hashCode() {
155 final int prime = 31;
157 result = prime * result + ((header == null) ? 0 : header.hashCode());
158 result = prime * result + index;
163 public boolean equals(Object obj) {
168 if (getClass() != obj.getClass())
170 CSVHeader other = (CSVHeader) obj;
171 if (header == null) {
172 if (other.header != null)
174 } else if (!header.equals(other.header))
176 if (index != other.index)
182 public static Collection<String> readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException {
183 Set<String> results = new HashSet<>();
184 CSVFormat format = CSVFormat.newFormat(delim);
185 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
186 Iterator<CSVRecord> records = parser.iterator();
187 if (records.hasNext())
189 while (records.hasNext()) {
190 CSVRecord row = records.next();
191 String value = row.get(mappingIndex);