package org.simantics.district.imports.ui; import java.io.IOException; import java.nio.file.Path; import java.util.Collections; import java.util.List; import java.util.Map; import org.apache.commons.csv.CSVRecord; import org.simantics.district.imports.DistrictImportUtils; import org.simantics.district.imports.DistrictImportUtils.CSVHeader; public class CSVImportModel { private Path source; private char delimiter; private boolean read; private Map delimiters; private Path wktFile; public CSVImportModel() { delimiters = DistrictImportUtils.getSupportedCSVDelimiterFormats(); } public void setSource(Path source) { this.source = source; } public void setDelimiter(char delimiter) { this.delimiter = delimiter; } public void setReadFirstAsHeader(boolean read) { this.read = read; } public Path getSource() { return source; } public List getRows(int amount) throws IOException { if (source != null) return DistrictImportUtils.readRows(source, delimiter, amount); else return Collections.emptyList(); } public Map> getHeaderAndRows(int amount) throws IOException { if (source != null) return DistrictImportUtils.readCSVHeaderAndRows(source, delimiter, read, amount); else return Collections.emptyMap(); } public Map getHeader() throws IOException { Map header = null; if (source != null) header = DistrictImportUtils.readCSVHeader(source, delimiter, read); if (header == null) header = Collections.emptyMap(); return header; } public boolean getReadFirstAsHeader() { return read; } public String[] getDelimiterFormats() { return delimiters.keySet().toArray(new String[delimiters.size()]); } public void setDelimiterByLabel(String item) { setDelimiter(delimiters.get(item)); } public List> readRows(int amount) throws IOException { if (source != null) return DistrictImportUtils.readRows(source, delimiter, read, amount); else return Collections.emptyList(); } public char getDelimiter() { return delimiter; } public Path getWKTFile() { return wktFile; } }