]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java
Enhancements to district functionalities and code
[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
15 import org.apache.commons.csv.CSVFormat;
16 import org.apache.commons.csv.CSVParser;
17 import org.apache.commons.csv.CSVRecord;
18 import org.simantics.db.Resource;
19
20 public class DistrictImportUtils {
21
22     private DistrictImportUtils() { }
23
24     public static Resource importCSVAsLayer(Path csvFile) throws IOException {
25         
26         try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
27             Map<String, Integer> header = parser.getHeaderMap();
28             System.out.println(header);
29         }
30         return null;
31     }
32     
33     public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
34         return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
35     }
36     
37     public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
38         if (firstAsHeader)
39             format = format.withFirstRecordAsHeader();
40         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
41             return parser.getHeaderMap();
42         }
43     }
44
45     public static Map<String, Character> getSupportedCSVDelimiterFormats() {
46         Map<String, Character> delimiters = new HashMap<>();
47         delimiters.put("COMMA", ',');
48         delimiters.put("SEMICOMMA", ';');
49         return delimiters;
50     }
51
52     public static List<Map<String, String>> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
53         return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount);
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, int rowAmount) throws IOException {
73         List<CSVRecord> results = new ArrayList<>();
74         CSVFormat format = CSVFormat.newFormat(delim);
75         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
76             Iterator<CSVRecord> records = parser.iterator();
77             int rows = 0;
78             if (rowAmount == -1) {
79                 while (records.hasNext()) {
80                     results.add(records.next());
81                     rows++;
82                 }
83             } else {
84                 while (rows < rowAmount && records.hasNext()) {
85                     results.add(records.next());
86                     rows++;
87                 }
88             }
89         }
90         return results;
91     }
92
93     
94     public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
95         Map<CSVHeader, List<String>> results = new HashMap<>();
96         CSVFormat format = CSVFormat.newFormat(delimiter);
97         if (firstAsHeader)
98             format = format.withFirstRecordAsHeader();
99         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
100             Map<String, Integer> headers = parser.getHeaderMap();
101             if (headers != null && !headers.isEmpty()) {
102                 for (int index = 0; index < headers.size(); index++) {
103                     for (String head : headers.keySet()) {
104                         results.put(new CSVHeader(head, index), new ArrayList<>());
105                     }
106                 }
107             }
108             
109             Iterator<CSVRecord> records = parser.iterator();
110             int rows = 0;
111             while (rows < amount && records.hasNext()) {
112                 CSVRecord record = records.next();
113                 for (int j = 0; j < record.size(); j++) {
114                     String value = record.get(j);
115                     String header = Integer.toString(j);
116                     CSVHeader csvHeader = new CSVHeader(header, j);
117                     List<String> vals = results.get(csvHeader);
118                     if (vals == null) {
119                         vals = new ArrayList<>();
120                         results.put(csvHeader, vals);
121                     }
122                     vals.add(value);
123                 }
124                 rows++;
125             }
126         }
127         return results;
128     }
129     
130     public static class CSVHeader {
131
132         private final String header;
133         private final int index;
134         
135         public CSVHeader(String header, int index) {
136             this.header = header;
137             this.index = index;
138         }
139
140         public String getHeader() {
141             return header;
142         }
143
144         public int getIndex() {
145             return index;
146         }
147         
148         @Override
149         public int hashCode() {
150             final int prime = 31;
151             int result = 1;
152             result = prime * result + ((header == null) ? 0 : header.hashCode());
153             result = prime * result + index;
154             return result;
155         }
156
157         @Override
158         public boolean equals(Object obj) {
159             if (this == obj)
160                 return true;
161             if (obj == null)
162                 return false;
163             if (getClass() != obj.getClass())
164                 return false;
165             CSVHeader other = (CSVHeader) obj;
166             if (header == null) {
167                 if (other.header != null)
168                     return false;
169             } else if (!header.equals(other.header))
170                 return false;
171             if (index != other.index)
172                 return false;
173             return true;
174         }
175     }
176
177     public static Collection<String> readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException {
178         Set<String> results = new HashSet<>();
179         CSVFormat format = CSVFormat.newFormat(delim);
180         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
181             Iterator<CSVRecord> records = parser.iterator();
182             if (records.hasNext())
183                 records.next();
184             while (records.hasNext()) {
185                 CSVRecord row = records.next();
186                 String value = row.get(mappingIndex);
187                 results.add(value);
188             }
189         }
190         return results;
191     }
192
193 }