]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java
Updates to Simantics district CSV import
[simantics/district.git] / org.simantics.district.imports.ui / src / org / simantics / district / imports / ui / CSVImportModel.java
1 package org.simantics.district.imports.ui;
2
3 import java.io.IOException;
4 import java.nio.file.Path;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
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;
14
15 public class CSVImportModel {
16
17     // First page fills these
18     private Resource targetDiagram;
19     private Path source;
20     
21     // Second page fills these
22     private char delimiter;
23     private boolean readFirstAsHeader = true;
24     private Map<String, Character> delimiters;
25 //    private Path wktFile;
26     
27     // Vertex import
28     private int xCoordIndex = -1;
29     private int yCoordIndex = -1;
30     private int zCoordIndex = -1;
31     
32     // Edge import
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;
39     
40     // common
41     private int componentMappingIndex = -1;
42     private Map<String, Resource> componentMappings = new HashMap<>();
43     private boolean isVertexImport;
44     private String sourceCRS;
45     private int tempIndex;
46     private int pressureIndex;
47     private int diameterIndex;
48     private int outerDiamterIndex;
49     private int nominalMassFlowIndex;
50     
51     // Third page
52
53     public CSVImportModel() {
54         delimiters = DistrictImportUtils.getSupportedCSVDelimiterFormats();
55     }
56     
57     public void setSource(Path source) {
58         this.source = source;
59     }
60
61     public void setDelimiter(char delimiter) {
62         this.delimiter = delimiter;
63     }
64
65     public void setReadFirstAsHeader(boolean read) {
66         this.readFirstAsHeader = read;
67     }
68
69     public Path getSource() {
70         return source;
71     }
72
73     public List<CSVRecord> getRows(int amount) throws IOException {
74         if (source != null)
75             return DistrictImportUtils.readRows(source, delimiter, amount);
76         else
77             return Collections.emptyList();
78     }
79     
80     public Map<CSVHeader, List<String>> getHeaderAndRows(int amount) throws IOException {
81         if (source != null)
82             return DistrictImportUtils.readCSVHeaderAndRows(source, delimiter, readFirstAsHeader, amount);
83         else
84             return Collections.emptyMap();
85     }
86     
87     public Map<String, Integer> getHeader() throws IOException {
88         Map<String, Integer> header = null;
89         if (source != null)
90             header = DistrictImportUtils.readCSVHeader(source, delimiter, readFirstAsHeader);
91         if (header == null)
92             header = Collections.emptyMap();
93         return header;
94     }
95
96     public boolean getReadFirstAsHeader() {
97         return readFirstAsHeader;
98     }
99
100     public String[] getDelimiterFormats() {
101         return delimiters.keySet().toArray(new String[delimiters.size()]);
102     }
103
104     public void setDelimiterByLabel(String item) {
105         setDelimiter(delimiters.get(item));
106     }
107
108     public List<Map<String, String>> readRows(int amount) throws IOException {
109         if (source != null)
110             return DistrictImportUtils.readRows(source, delimiter, readFirstAsHeader, amount);
111         else
112             return Collections.emptyList();
113     }
114
115     public char getDelimiter() {
116         return delimiter;
117     }
118
119 //    public Path getWKTFile() {
120 //        return wktFile;
121 //    }
122
123     public int getXCoordIndex() {
124         return xCoordIndex;
125     }
126
127     public void setXCoordIndex(int xCoordIndex) {
128         this.xCoordIndex = xCoordIndex;
129     }
130
131     public int getYCoordIndex() {
132         return yCoordIndex;
133     }
134
135     public void setYCoordIndex(int yCoordIndex) {
136         this.yCoordIndex = yCoordIndex;
137     }
138
139     public int getZCoordIndex() {
140         return zCoordIndex;
141     }
142
143     public void setZCoordIndex(int zCoordIndex) {
144         this.zCoordIndex = zCoordIndex;
145     }
146
147     public int getComponentMappingIndex() {
148         return componentMappingIndex;
149     }
150     
151     public void setComponentMappingIndex(int componentMappingIndex) {
152         this.componentMappingIndex = componentMappingIndex;
153     }
154
155     public void setParentDiagram(Resource diagram) {
156         this.targetDiagram = diagram;
157     }
158
159     public Resource getParentDiagram() {
160         return targetDiagram;
161     }
162
163 //    public void setWKTFile(Path wktFile) {
164 //        this.wktFile = wktFile;
165 //    }
166
167     public void setComponentMappings(String value, Resource resource) {
168         componentMappings.put(value, resource);
169     }
170
171     public Map<String, Resource> getComponentMappings() {
172         return componentMappings;
173     }
174
175     public boolean isVertexImport() {
176         return isVertexImport;
177     }
178     
179     public void setVertexImport(boolean isVertexImport) {
180         this.isVertexImport = isVertexImport;
181     }
182
183     public void setStartXCoordIndex(int parseInt) {
184         this.startXCoordIndex = parseInt;
185     }
186     
187     public int getStartXCoordIndex() {
188         return startXCoordIndex;
189     }
190
191     public void setStartYCoordIndex(int parseInt) {
192         this.startYCoordIndex = parseInt;
193     }
194     
195     public int getStartYCoordIndex() {
196         return startYCoordIndex;
197     }
198
199     public void setStartZCoordIndex(int parseInt) {
200         this.startZCoordIndex = parseInt;
201     }
202     
203     public int getStartZCoordIndex() {
204         return startZCoordIndex;
205     }
206
207     public void setEndXCoordIndex(int parseInt) {
208         this.endXCoordIndex = parseInt;
209     }
210     
211     public int getEndXCoordIndex() {
212         return endXCoordIndex;
213     }
214
215     public void setEndYCoordIndex(int parseInt) {
216         this.endYCoordIndex = parseInt;
217     }
218     
219     public int getEndYCoordIndex() {
220         return endYCoordIndex;
221     }
222
223     public void setEndZCoordIndex(int parseInt) {
224         this.endZCoordIndex = parseInt;
225     }
226     
227     public int getEndZCoordIndex() {
228         return endZCoordIndex;
229     }
230
231     public void setSourceCRS(String crs) {
232         this.sourceCRS = crs;
233     }
234
235     public String getSourceCRS() {
236         return sourceCRS;
237     }
238
239     public void setTempIndex(int tempIndex) {
240         this.tempIndex = tempIndex;
241     }
242     
243     public int getTempIndex() {
244         return tempIndex;
245     }
246
247     public void setPressureIndex(int pressureIndex) {
248         this.pressureIndex = pressureIndex;
249     }
250
251     public int getPressureIndex() {
252         return pressureIndex;
253     }
254
255     public void setDiameterIndex(int parseInt) {
256         this.diameterIndex = parseInt;
257     }
258     
259     public int getDiameterIndex() {
260         return diameterIndex;
261     }
262
263     public void setOuterDiameterIndex(int parseInt) {
264         this.outerDiamterIndex = parseInt;
265     }
266     
267     public int getOuterDiamterIndex() {
268         return outerDiamterIndex;
269     }
270
271     public void setNominalMassFlowIndex(int parseInt) {
272         this.nominalMassFlowIndex = parseInt;
273     }
274     
275     public int getNominalMassFlowIndex() {
276         return nominalMassFlowIndex;
277     }
278
279 }