]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java
ca3f4be59aef4a6c055ed1e77522d693242b7e6a
[simantics/district.git] / org.simantics.district.imports / src / org / simantics / district / imports / DistrictImportUtils.java
1 package org.simantics.district.imports;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import java.util.function.Consumer;
16 import java.util.function.Function;
17
18 import org.apache.commons.csv.CSVFormat;
19 import org.apache.commons.csv.CSVParser;
20 import org.apache.commons.csv.CSVRecord;
21 import org.simantics.db.Resource;
22
23 public class DistrictImportUtils {
24
25     private DistrictImportUtils() { }
26
27     public static Resource importCSVAsLayer(Path csvFile) throws IOException {
28         
29         try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
30             Map<String, Integer> header = parser.getHeaderMap();
31             System.out.println(header);
32         }
33         return null;
34     }
35     
36     public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
37         return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
38     }
39     
40     public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
41         if (firstAsHeader)
42             format = format.withFirstRecordAsHeader();
43         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
44             return parser.getHeaderMap();
45         }
46     }
47
48     public static Map<String, Character> getSupportedCSVDelimiterFormats() {
49         Map<String, Character> delimiters = new HashMap<>();
50         delimiters.put("Comma", ',');
51         delimiters.put("Semicolon", ';');
52         delimiters.put("Tabulator", '\t');
53         return delimiters;
54     }
55
56     public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
57         if (firstAsHeader)
58             format = format.withFirstRecordAsHeader();
59         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
60             int start = 0;
61             List<Map<String, String>> results = new ArrayList<>(amount);
62             Iterator<CSVRecord> iter = parser.iterator();
63             while (start < amount && iter.hasNext()) {
64                 CSVRecord record = iter.next();
65                 results.add(record.toMap());
66                 start++;
67             }
68             return results;
69         }
70     }
71
72     public static List<CSVRecord> readRows(Path source, char delim, boolean firstAsHeader, int rowAmount) throws IOException {
73         List<CSVRecord> results = new ArrayList<>();
74         AtomicInteger count = new AtomicInteger(0);
75         consumeCSV(source, delim, firstAsHeader, t -> {
76             results.add(t);
77             int current = count.incrementAndGet();
78             return current < rowAmount;
79         });
80         return results;
81     }
82     
83     public static void consumeCSV(Path source, char delim, boolean firstAsHeader, Function<CSVRecord, Boolean> consumer) throws IOException {
84         CSVFormat format = CSVFormat.newFormat(delim);
85         if (firstAsHeader) {
86             format = format.withFirstRecordAsHeader();
87         }
88         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
89             Iterator<CSVRecord> records = parser.iterator();
90             while (records.hasNext()) {
91                 Boolean cont = consumer.apply(records.next());
92                 if (!cont) {
93                     break;
94                 }
95             }
96         }
97     }
98
99     
100     public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
101         Map<CSVHeader, List<String>> results = new HashMap<>();
102         CSVFormat format = CSVFormat.newFormat(delimiter);
103         if (firstAsHeader)
104             format = format.withFirstRecordAsHeader();
105         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
106             Map<String, Integer> headers = parser.getHeaderMap();
107             if (headers != null && !headers.isEmpty()) {
108                 for (int index = 0; index < headers.size(); index++) {
109                     for (String head : headers.keySet()) {
110                         results.put(new CSVHeader(head, index), new ArrayList<>());
111                     }
112                 }
113             }
114             
115             Iterator<CSVRecord> records = parser.iterator();
116             int rows = 0;
117             while (rows < amount && records.hasNext()) {
118                 CSVRecord record = records.next();
119                 for (int j = 0; j < record.size(); j++) {
120                     String value = record.get(j);
121                     String header = Integer.toString(j);
122                     CSVHeader csvHeader = new CSVHeader(header, j);
123                     List<String> vals = results.get(csvHeader);
124                     if (vals == null) {
125                         vals = new ArrayList<>();
126                         results.put(csvHeader, vals);
127                     }
128                     vals.add(value);
129                 }
130                 rows++;
131             }
132         }
133         return results;
134     }
135     
136     public static class CSVHeader {
137
138         private final String header;
139         private final int index;
140         
141         public CSVHeader(String header, int index) {
142             this.header = header;
143             this.index = index;
144         }
145
146         public String getHeader() {
147             return header;
148         }
149
150         public int getIndex() {
151             return index;
152         }
153         
154         @Override
155         public int hashCode() {
156             final int prime = 31;
157             int result = 1;
158             result = prime * result + ((header == null) ? 0 : header.hashCode());
159             result = prime * result + index;
160             return result;
161         }
162
163         @Override
164         public boolean equals(Object obj) {
165             if (this == obj)
166                 return true;
167             if (obj == null)
168                 return false;
169             if (getClass() != obj.getClass())
170                 return false;
171             CSVHeader other = (CSVHeader) obj;
172             if (header == null) {
173                 if (other.header != null)
174                     return false;
175             } else if (!header.equals(other.header))
176                 return false;
177             if (index != other.index)
178                 return false;
179             return true;
180         }
181     }
182
183     public static Collection<String> readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException {
184         Set<String> results = new HashSet<>();
185         CSVFormat format = CSVFormat.newFormat(delim);
186         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
187             Iterator<CSVRecord> records = parser.iterator();
188             if (records.hasNext())
189                 records.next();
190             while (records.hasNext()) {
191                 CSVRecord row = records.next();
192                 String value = row.get(mappingIndex);
193                 results.add(value);
194             }
195         }
196         return results;
197     }
198
199 }