package org.simantics.district.imports; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.function.Function; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import org.simantics.db.Resource; public class DistrictImportUtils { private DistrictImportUtils() { } public static Resource importCSVAsLayer(Path csvFile) throws IOException { try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) { Map header = parser.getHeaderMap(); System.out.println(header); } return null; } public static Map readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException { return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader); } public static Map readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException { if (firstAsHeader) format = format.withFirstRecordAsHeader(); try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { return parser.getHeaderMap(); } } public static Map getSupportedCSVDelimiterFormats() { Map delimiters = new HashMap<>(); delimiters.put("Comma", ','); delimiters.put("Semicolon", ';'); delimiters.put("Tabulator", '\t'); return delimiters; } public static List> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException { if (firstAsHeader) format = format.withFirstRecordAsHeader(); try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { int start = 0; List> results = new ArrayList<>(amount); Iterator iter = parser.iterator(); while (start < amount && iter.hasNext()) { CSVRecord record = iter.next(); results.add(record.toMap()); start++; } return results; } } public static List readRows(Path source, char delim, boolean firstAsHeader, int rowAmount) throws IOException { List results = new ArrayList<>(); AtomicInteger count = new AtomicInteger(0); consumeCSV(source, delim, firstAsHeader, t -> { results.add(t); int current = count.incrementAndGet(); return current < rowAmount; }); return results; } public static void consumeCSV(Path source, char delim, boolean firstAsHeader, Function consumer) throws IOException { CSVFormat format = CSVFormat.newFormat(delim); if (firstAsHeader) { format = format.withFirstRecordAsHeader(); } try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { Iterator records = parser.iterator(); while (records.hasNext()) { Boolean cont = consumer.apply(records.next()); if (!cont) { break; } } } } public static Map> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException { Map> results = new HashMap<>(); CSVFormat format = CSVFormat.newFormat(delimiter); if (firstAsHeader) format = format.withFirstRecordAsHeader(); try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { Map headers = parser.getHeaderMap(); if (headers != null && !headers.isEmpty()) { for (int index = 0; index < headers.size(); index++) { for (String head : headers.keySet()) { results.put(new CSVHeader(head, index), new ArrayList<>()); } } } Iterator records = parser.iterator(); int rows = 0; while (rows < amount && records.hasNext()) { CSVRecord record = records.next(); for (int j = 0; j < record.size(); j++) { String value = record.get(j); String header = Integer.toString(j); CSVHeader csvHeader = new CSVHeader(header, j); List vals = results.get(csvHeader); if (vals == null) { vals = new ArrayList<>(); results.put(csvHeader, vals); } vals.add(value); } rows++; } } return results; } public static class CSVHeader { private final String header; private final int index; public CSVHeader(String header, int index) { this.header = header; this.index = index; } public String getHeader() { return header; } public int getIndex() { return index; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((header == null) ? 0 : header.hashCode()); result = prime * result + index; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; CSVHeader other = (CSVHeader) obj; if (header == null) { if (other.header != null) return false; } else if (!header.equals(other.header)) return false; if (index != other.index) return false; return true; } } public static Collection readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException { Set results = new HashSet<>(); CSVFormat format = CSVFormat.newFormat(delim); try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { Iterator records = parser.iterator(); if (records.hasNext()) records.next(); while (records.hasNext()) { CSVRecord row = records.next(); String value = row.get(mappingIndex); results.add(value); } } return results; } }