1 package org.simantics.district.imports.ui;
3 import java.io.IOException;
4 import java.nio.file.Path;
5 import java.util.Collections;
6 import java.util.HashMap;
10 import org.apache.commons.csv.CSVRecord;
11 import org.simantics.db.Resource;
12 import org.simantics.district.imports.DistrictImportUtils;
13 import org.simantics.district.imports.DistrictImportUtils.CSVHeader;
15 public class CSVImportModel {
17 // First page fills these
18 private Resource targetDiagram;
21 // Second page fills these
22 private char delimiter;
24 private Map<String, Character> delimiters;
28 private int xCoordIndex = -1;
29 private int yCoordIndex = -1;
30 private int zCoordIndex = -1;
33 private int startXCoordIndex = -1;
34 private int startYCoordIndex = -1;
35 private int startZCoordIndex = -1;
36 private int endXCoordIndex = -1;
37 private int endYCoordIndex = -1;
38 private int endZCoordIndex = -1;
41 private int componentMappingIndex = -1;
42 private Map<String, Resource> componentMappings = new HashMap<>();
43 private boolean isVertexImport;
47 public CSVImportModel() {
48 delimiters = DistrictImportUtils.getSupportedCSVDelimiterFormats();
51 public void setSource(Path source) {
55 public void setDelimiter(char delimiter) {
56 this.delimiter = delimiter;
59 public void setReadFirstAsHeader(boolean read) {
63 public Path getSource() {
67 public List<CSVRecord> getRows(int amount) throws IOException {
69 return DistrictImportUtils.readRows(source, delimiter, amount);
71 return Collections.emptyList();
74 public Map<CSVHeader, List<String>> getHeaderAndRows(int amount) throws IOException {
76 return DistrictImportUtils.readCSVHeaderAndRows(source, delimiter, read, amount);
78 return Collections.emptyMap();
81 public Map<String, Integer> getHeader() throws IOException {
82 Map<String, Integer> header = null;
84 header = DistrictImportUtils.readCSVHeader(source, delimiter, read);
86 header = Collections.emptyMap();
90 public boolean getReadFirstAsHeader() {
94 public String[] getDelimiterFormats() {
95 return delimiters.keySet().toArray(new String[delimiters.size()]);
98 public void setDelimiterByLabel(String item) {
99 setDelimiter(delimiters.get(item));
102 public List<Map<String, String>> readRows(int amount) throws IOException {
104 return DistrictImportUtils.readRows(source, delimiter, read, amount);
106 return Collections.emptyList();
109 public char getDelimiter() {
113 public Path getWKTFile() {
117 public int getXCoordIndex() {
121 public void setXCoordIndex(int xCoordIndex) {
122 this.xCoordIndex = xCoordIndex;
125 public int getYCoordIndex() {
129 public void setYCoordIndex(int yCoordIndex) {
130 this.yCoordIndex = yCoordIndex;
133 public int getZCoordIndex() {
137 public void setZCoordIndex(int zCoordIndex) {
138 this.zCoordIndex = zCoordIndex;
141 public int getComponentMappingIndex() {
142 return componentMappingIndex;
145 public void setComponentMappingIndex(int componentMappingIndex) {
146 this.componentMappingIndex = componentMappingIndex;
149 public void setParentDiagram(Resource diagram) {
150 this.targetDiagram = diagram;
153 public Resource getParentDiagram() {
154 return targetDiagram;
157 public void setWKTFile(Path wktFile) {
158 this.wktFile = wktFile;
161 public void setComponentMappings(String value, Resource resource) {
162 componentMappings.put(value, resource);
165 public Map<String, Resource> getComponentMappings() {
166 return componentMappings;
169 public boolean isVertexImport() {
170 return isVertexImport;
173 public void setVertexImport(boolean isVertexImport) {
174 this.isVertexImport = isVertexImport;
177 public void setStartXCoordIndex(int parseInt) {
178 this.startXCoordIndex = parseInt;
181 public int getStartXCoordIndex() {
182 return startXCoordIndex;
185 public void setStartYCoordIndex(int parseInt) {
186 this.startYCoordIndex = parseInt;
189 public int getStartYCoordIndex() {
190 return startYCoordIndex;
193 public void setStartZCoordIndex(int parseInt) {
194 this.startZCoordIndex = parseInt;
197 public int getStartZCoordIndex() {
198 return startZCoordIndex;
201 public void setEndXCoordIndex(int parseInt) {
202 this.endXCoordIndex = parseInt;
205 public int getEndXCoordIndex() {
206 return endXCoordIndex;
209 public void setEndYCoordIndex(int parseInt) {
210 this.endYCoordIndex = parseInt;
213 public int getEndYCoordIndex() {
214 return endYCoordIndex;
217 public void setEndZCoordIndex(int parseInt) {
218 this.endZCoordIndex = parseInt;
221 public int getEndZCoordIndex() {
222 return endZCoordIndex;