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.Consumer;
16 import java.util.function.Function;
18 import org.apache.commons.csv.CSVFormat;
19 import org.apache.commons.csv.CSVParser;
20 import org.apache.commons.csv.CSVRecord;
21 import org.simantics.db.Resource;
23 public class DistrictImportUtils {
25 private DistrictImportUtils() { }
27 public static Resource importCSVAsLayer(Path csvFile) throws IOException {
29 try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
30 Map<String, Integer> header = parser.getHeaderMap();
31 System.out.println(header);
36 public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
37 return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
40 public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
42 format = format.withFirstRecordAsHeader();
43 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
44 return parser.getHeaderMap();
48 public static Map<String, Character> getSupportedCSVDelimiterFormats() {
49 Map<String, Character> delimiters = new HashMap<>();
50 delimiters.put("Comma", ',');
51 delimiters.put("Semicolon", ';');
52 delimiters.put("Tabulator", '\t');
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, boolean firstAsHeader, int rowAmount) throws IOException {
73 List<CSVRecord> results = new ArrayList<>();
74 AtomicInteger count = new AtomicInteger(0);
75 consumeCSV(source, delim, firstAsHeader, t -> {
77 int current = count.incrementAndGet();
78 return current < rowAmount;
83 public static void consumeCSV(Path source, char delim, boolean firstAsHeader, Function<CSVRecord, Boolean> consumer) throws IOException {
84 CSVFormat format = CSVFormat.newFormat(delim);
86 format = format.withFirstRecordAsHeader();
88 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
89 Iterator<CSVRecord> records = parser.iterator();
90 while (records.hasNext()) {
91 Boolean cont = consumer.apply(records.next());
100 public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
101 Map<CSVHeader, List<String>> results = new HashMap<>();
102 CSVFormat format = CSVFormat.newFormat(delimiter);
104 format = format.withFirstRecordAsHeader();
105 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
106 Map<String, Integer> headers = parser.getHeaderMap();
107 if (headers != null && !headers.isEmpty()) {
108 for (int index = 0; index < headers.size(); index++) {
109 for (String head : headers.keySet()) {
110 results.put(new CSVHeader(head, index), new ArrayList<>());
115 Iterator<CSVRecord> records = parser.iterator();
117 while (rows < amount && records.hasNext()) {
118 CSVRecord record = records.next();
119 for (int j = 0; j < record.size(); j++) {
120 String value = record.get(j);
121 String header = Integer.toString(j);
122 CSVHeader csvHeader = new CSVHeader(header, j);
123 List<String> vals = results.get(csvHeader);
125 vals = new ArrayList<>();
126 results.put(csvHeader, vals);
136 public static class CSVHeader {
138 private final String header;
139 private final int index;
141 public CSVHeader(String header, int index) {
142 this.header = header;
146 public String getHeader() {
150 public int getIndex() {
155 public int hashCode() {
156 final int prime = 31;
158 result = prime * result + ((header == null) ? 0 : header.hashCode());
159 result = prime * result + index;
164 public boolean equals(Object obj) {
169 if (getClass() != obj.getClass())
171 CSVHeader other = (CSVHeader) obj;
172 if (header == null) {
173 if (other.header != null)
175 } else if (!header.equals(other.header))
177 if (index != other.index)
183 public static Collection<String> readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException {
184 Set<String> results = new HashSet<>();
185 CSVFormat format = CSVFormat.newFormat(delim);
186 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
187 Iterator<CSVRecord> records = parser.iterator();
188 if (records.hasNext())
190 while (records.hasNext()) {
191 CSVRecord row = records.next();
192 String value = row.get(mappingIndex);