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.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; 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("SEMICOMMA", ';'); return delimiters; } public static List> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException { return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount); } 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 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; } } }