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", ';');
49 delimiters.put("Tabulator", '\t');
53 public static List<Map<String, String>> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
54 return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount);
57 public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
59 format = format.withFirstRecordAsHeader();
60 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
62 List<Map<String, String>> results = new ArrayList<>(amount);
63 Iterator<CSVRecord> iter = parser.iterator();
64 while (start < amount && iter.hasNext()) {
65 CSVRecord record = iter.next();
66 results.add(record.toMap());
73 public static List<CSVRecord> readRows(Path source, char delim, int rowAmount) throws IOException {
74 List<CSVRecord> results = new ArrayList<>();
75 CSVFormat format = CSVFormat.newFormat(delim);
76 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
77 Iterator<CSVRecord> records = parser.iterator();
79 if (rowAmount == -1) {
80 while (records.hasNext()) {
81 results.add(records.next());
85 while (rows < rowAmount && records.hasNext()) {
86 results.add(records.next());
95 public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
96 Map<CSVHeader, List<String>> results = new HashMap<>();
97 CSVFormat format = CSVFormat.newFormat(delimiter);
99 format = format.withFirstRecordAsHeader();
100 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
101 Map<String, Integer> headers = parser.getHeaderMap();
102 if (headers != null && !headers.isEmpty()) {
103 for (int index = 0; index < headers.size(); index++) {
104 for (String head : headers.keySet()) {
105 results.put(new CSVHeader(head, index), new ArrayList<>());
110 Iterator<CSVRecord> records = parser.iterator();
112 while (rows < amount && records.hasNext()) {
113 CSVRecord record = records.next();
114 for (int j = 0; j < record.size(); j++) {
115 String value = record.get(j);
116 String header = Integer.toString(j);
117 CSVHeader csvHeader = new CSVHeader(header, j);
118 List<String> vals = results.get(csvHeader);
120 vals = new ArrayList<>();
121 results.put(csvHeader, vals);
131 public static class CSVHeader {
133 private final String header;
134 private final int index;
136 public CSVHeader(String header, int index) {
137 this.header = header;
141 public String getHeader() {
145 public int getIndex() {
150 public int hashCode() {
151 final int prime = 31;
153 result = prime * result + ((header == null) ? 0 : header.hashCode());
154 result = prime * result + index;
159 public boolean equals(Object obj) {
164 if (getClass() != obj.getClass())
166 CSVHeader other = (CSVHeader) obj;
167 if (header == null) {
168 if (other.header != null)
170 } else if (!header.equals(other.header))
172 if (index != other.index)
178 public static Collection<String> readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException {
179 Set<String> results = new HashSet<>();
180 CSVFormat format = CSVFormat.newFormat(delim);
181 try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
182 Iterator<CSVRecord> records = parser.iterator();
183 if (records.hasNext())
185 while (records.hasNext()) {
186 CSVRecord row = records.next();
187 String value = row.get(mappingIndex);